Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
X
xm-sportstest
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
xiamai-test
xm-sportstest
Commits
ade277a2
Commit
ade277a2
authored
Jul 04, 2024
by
zhangyuxiao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
导入会员信息
parent
818c5b00
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
834 additions
and
0 deletions
+834
-0
excelFile/memberInfo.xls
+0
-0
src/main/java/com/xiaomai/cases/polar/enums/ExcelImportEnum.java
+99
-0
src/main/java/com/xiaomai/cases/polar/importExcel/ImportTools.java
+87
-0
src/main/java/com/xiaomai/cases/polar/importExcel/TestImportMember.java
+108
-0
src/main/java/com/xiaomai/cases/polar/utils/CommonUtil.java
+85
-0
src/main/java/com/xiaomai/cases/polar/utils/Entity/SignatureVO.java
+110
-0
src/main/java/com/xiaomai/cases/polar/utils/ModifyExcelContentUtil.java
+100
-0
src/main/java/com/xiaomai/cases/polar/utils/ResourceUtil.java
+243
-0
src/main/java/com/xiaomai/enums/ApiModule.java
+2
-0
No files found.
excelFile/memberInfo.xls
0 → 100644
View file @
ade277a2
File added
src/main/java/com/xiaomai/cases/polar/enums/ExcelImportEnum.java
0 → 100644
View file @
ade277a2
package
com
.
xiaomai
.
cases
.
polar
.
enums
;
import
org.apache.commons.lang3.StringUtils
;
/**
* @Author zxy
* @Description
* @Date 2024/7/2 17:48
*/
public
enum
ExcelImportEnum
{
MEMBER_INFO
(
"memberInfo"
,
3
,
0
,
1
);
/**业务 */
private
String
value
;
/**行开始下标(从0开始) */
private
int
rowStartIndex
;
/**姓名列坐标 */
private
int
cellNametIndex
;
/**手机号列坐标 */
private
int
cellPhoneIndex
;
ExcelImportEnum
(
String
value
,
int
rowStartIndex
,
int
cellNametIndex
,
int
cellPhoneIndex
)
{
this
.
value
=
value
;
this
.
rowStartIndex
=
rowStartIndex
;
this
.
cellNametIndex
=
cellNametIndex
;
this
.
cellPhoneIndex
=
cellPhoneIndex
;
}
ExcelImportEnum
(
String
value
,
int
rowStartIndex
,
int
cellPhoneIndex
)
{
this
.
value
=
value
;
this
.
rowStartIndex
=
rowStartIndex
;
this
.
cellPhoneIndex
=
cellPhoneIndex
;
}
public
String
getValue
()
{
return
value
;
}
public
void
setValue
(
String
value
)
{
this
.
value
=
value
;
}
public
int
getRowStartIndex
()
{
return
rowStartIndex
;
}
public
void
setRowStartIndex
(
int
rowStartIndex
)
{
this
.
rowStartIndex
=
rowStartIndex
;
}
public
int
getCellNametIndex
()
{
return
cellNametIndex
;
}
public
void
setCellNametIndex
(
int
cellNametIndex
)
{
this
.
cellNametIndex
=
cellNametIndex
;
}
public
int
getCellPhoneIndex
()
{
return
cellPhoneIndex
;
}
public
void
setCellPhoneIndex
(
int
cellPhoneIndex
)
{
this
.
cellPhoneIndex
=
cellPhoneIndex
;
}
/**
*根据value获取对应枚举
* value找不对应的枚举,则返回一个默认值
* @param value
* @return
}
*/
public
static
int
getRowIndexByValue
(
String
value
)
{
for
(
ExcelImportEnum
excelImportBusinessEnum
:
ExcelImportEnum
.
values
())
{
if
(
StringUtils
.
equals
(
excelImportBusinessEnum
.
getValue
(),
value
))
{
return
excelImportBusinessEnum
.
rowStartIndex
;
}
}
return
0
;
}
public
static
int
getNameCellIndexByValue
(
String
value
)
{
for
(
ExcelImportEnum
excelImportBusinessEnum
:
ExcelImportEnum
.
values
())
{
if
(
StringUtils
.
equals
(
excelImportBusinessEnum
.
getValue
(),
value
))
{
return
excelImportBusinessEnum
.
cellNametIndex
;
}
}
return
0
;
}
public
static
int
getPhoneCellIndexByValue
(
String
value
)
{
for
(
ExcelImportEnum
excelImportBusinessEnum
:
ExcelImportEnum
.
values
())
{
if
(
StringUtils
.
equals
(
excelImportBusinessEnum
.
getValue
(),
value
))
{
return
excelImportBusinessEnum
.
cellPhoneIndex
;
}
}
return
1
;
}
}
src/main/java/com/xiaomai/cases/polar/importExcel/ImportTools.java
0 → 100644
View file @
ade277a2
package
com
.
xiaomai
.
cases
.
polar
.
importExcel
;
import
com.alibaba.fastjson.JSONObject
;
import
com.xiaomai.client.DataUserInfo
;
import
com.xiaomai.enums.ApiModule
;
import
com.xiaomai.enums.RequestType
;
import
com.xiaomai.enums.Terminal
;
import
com.xiaomai.utils.XMBaseTest
;
/**
* @Author zxy
* @Description
* @Date 2024/7/2 17:31
*/
public
class
ImportTools
extends
XMBaseTest
{
//创建导入
public
JSONObject
createImportTask
(
String
taskType
,
String
resourceId
,
String
taskDesc
,
String
memo
,
DataUserInfo
...
userInfos
){
dataApi
.
setApiModule
(
ApiModule
.
Polar_ImportTask
)
.
setApiName
(
"API_createImportTask"
)
.
setTerminal
(
Terminal
.
B
);
super
.
beforeDataRequest
(
userInfos
);
JSONObject
object
=
new
JSONObject
();
object
.
put
(
"operatorId"
,
dataApi
.
getLoginInfo
().
getAdminId
());
object
.
put
(
"tenantId"
,
dataApi
.
getLoginInfo
().
getStudioId
());
object
.
put
(
"resourceId"
,
resourceId
);
object
.
put
(
"tenantType"
,
"STUDIO"
);
object
.
put
(
"taskType"
,
taskType
);
object
.
put
(
"taskDesc"
,
taskDesc
);
object
.
put
(
"memo"
,
memo
);
dataApi
.
doRequest
(
RequestType
.
JSON
,
dataparams
,
object
.
toString
(),
dataheadrs
).
assetsSuccess
(
true
);
return
dataApi
.
getBodyInJSON
();
}
//获取导入任务
public
JSONObject
getTask
(
String
id
,
DataUserInfo
...
userInfos
){
dataApi
.
setApiModule
(
ApiModule
.
Polar_ImportTask
)
.
setApiName
(
"API_getTask"
)
.
setTerminal
(
Terminal
.
B
);
super
.
beforeDataRequest
(
userInfos
);
JSONObject
object
=
new
JSONObject
();
object
.
put
(
"id"
,
id
);
dataApi
.
doRequest
(
RequestType
.
JSON
,
dataparams
,
object
.
toString
(),
dataheadrs
).
assetsSuccess
(
true
);
return
dataApi
.
getBodyInJSON
();
}
//任务中心-导入记录
public
JSONObject
pageImportTask
(
DataUserInfo
...
userInfos
){
dataApi
.
setApiModule
(
ApiModule
.
Polar_ImportTask
)
.
setApiName
(
"API_pageImportTask"
)
.
setTerminal
(
Terminal
.
B
);
super
.
beforeDataRequest
(
userInfos
);
JSONObject
object
=
new
JSONObject
();
object
.
put
(
"operatorId"
,
dataApi
.
getLoginInfo
().
getAdminId
());
object
.
put
(
"tenantId"
,
dataApi
.
getLoginInfo
().
getStudioId
());
object
.
put
(
"tenantType"
,
"STUDIO"
);
object
.
put
(
"current"
,
0
);
object
.
put
(
"size"
,
10
);
dataApi
.
doRequest
(
RequestType
.
JSON
,
dataparams
,
object
.
toString
(),
dataheadrs
).
assetsSuccess
(
true
);
return
dataApi
.
getBodyInJSON
();
}
//获取导入资源
public
JSONObject
getImportResource
(
String
resourceId
,
DataUserInfo
...
userInfos
){
dataApi
.
setApiModule
(
ApiModule
.
Polar_ImportTask
)
.
setApiName
(
"API_getImportResource"
)
.
setTerminal
(
Terminal
.
B
);
super
.
beforeDataRequest
(
userInfos
);
JSONObject
object
=
new
JSONObject
();
object
.
put
(
"tenantId"
,
dataApi
.
getLoginInfo
().
getStudioId
());
object
.
put
(
"tenantType"
,
"STUDIO"
);
object
.
put
(
"resourceId"
,
resourceId
);
dataApi
.
doRequest
(
RequestType
.
JSON
,
dataparams
,
object
.
toString
(),
dataheadrs
).
assetsSuccess
(
true
);
return
dataApi
.
getBodyInJSON
();
}
}
src/main/java/com/xiaomai/cases/polar/importExcel/TestImportMember.java
0 → 100644
View file @
ade277a2
package
com
.
xiaomai
.
cases
.
polar
.
importExcel
;
import
com.alibaba.fastjson.JSONObject
;
import
com.xiaomai.cases.polar.enums.ExcelImportEnum
;
import
com.xiaomai.cases.polar.utils.Entity.SignatureVO
;
import
com.xiaomai.cases.polar.utils.ModifyExcelContentUtil
;
import
com.xiaomai.cases.polar.utils.ResourceUtil
;
import
com.xiaomai.enums.ApiModule
;
import
com.xiaomai.enums.LoginAccount
;
import
com.xiaomai.enums.RequestType
;
import
com.xiaomai.enums.Terminal
;
import
com.xiaomai.utils.XMBaseTest
;
import
com.xiaomai.utils.XMJSONPath
;
import
org.testng.Assert
;
import
org.testng.annotations.BeforeMethod
;
import
org.testng.annotations.Test
;
import
java.io.FileInputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
/**
* @Author zxy
* @Description
* @Date 2024/7/2 17:37
*/
public
class
TestImportMember
extends
XMBaseTest
{
ImportTools
importTools
=
new
ImportTools
();
@BeforeMethod
public
void
beforeTest
(){
xmAppApi
.
setApiModule
(
ApiModule
.
Polar_ImportTask
)
.
setApiName
(
"API_webTokenWithAccessUrl"
)
.
setLoginUser
(
LoginAccount
.
GYM_PROD
)
.
setTerminal
(
Terminal
.
B
);
dal
.
setCase_owner
(
"zyx"
)
.
setCase_name
(
Thread
.
currentThread
().
getStackTrace
()[
1
].
getFileName
().
split
(
"\\."
)[
0
]);
super
.
beforeTest
();
}
@Test
(
description
=
"导入会员信息"
)
public
void
testImportMember
()
throws
IOException
,
InterruptedException
{
//导入任务列表
int
total
=
Integer
.
parseInt
(
importTools
.
pageImportTask
().
getJSONObject
(
"result"
).
getString
(
"total"
));
//1.更新excel文档
String
filePathName
=
"excelFile/memberInfo.xls"
;
ModifyExcelContentUtil
.
modifyExcel
(
filePathName
,
"会员信息导入"
,
ExcelImportEnum
.
MEMBER_INFO
);
Thread
.
sleep
(
1000
);
//2、获取签名
JSONObject
object
=
new
JSONObject
();
object
.
put
(
"tenantId"
,
xmAppApi
.
getLoginInfo
().
getBrandId
());
object
.
put
(
"tenantType"
,
"BRAND"
);
object
.
put
(
"resourceName"
,
filePathName
);
object
.
put
(
"resourceNameToMD5"
,
false
);
object
.
put
(
"bizCode"
,
"IMPORT_TASK"
);
xmAppApi
.
doRequest
(
RequestType
.
JSON
,
params
,
object
.
toString
(),
headers
).
assetsSuccess
(
true
);
JSONObject
result1
=
xmAppApi
.
getBodyInJSON
();
String
resourceId
=
XMJSONPath
.
readPath
(
result1
,
"$.result.resourceId"
);
String
accessId
=
XMJSONPath
.
readPath
(
result1
,
"$.result.accessId"
);
String
policy
=
XMJSONPath
.
readPath
(
result1
,
"$.result.policy"
);
String
signature
=
XMJSONPath
.
readPath
(
result1
,
"$.result.signature"
);
String
key
=
XMJSONPath
.
readPath
(
result1
,
"$.result.key"
);
String
callback
=
XMJSONPath
.
readPath
(
result1
,
"$.result.callback"
);
String
host
=
XMJSONPath
.
readPath
(
result1
,
"$.result.host"
);
String
time
=
XMJSONPath
.
readPath
(
result1
,
"$.result.extAttrs"
);
JSONObject
time1
=
JSONObject
.
parseObject
(
time
);
String
url
=
XMJSONPath
.
readPath
(
result1
,
"$.result.url"
);
//3.腾讯云上传
InputStream
inputStream
=
new
FileInputStream
(
filePathName
);
SignatureVO
signatureVO
=
new
SignatureVO
();
signatureVO
.
setAlgorithm
(
"sha1"
);
signatureVO
.
setKey
(
key
);
signatureVO
.
setAccessId
(
accessId
);
signatureVO
.
setPolicy
(
policy
);
signatureVO
.
setSignature
(
signature
);
signatureVO
.
setCallback
(
callback
);
signatureVO
.
setExtAttrs
(
time1
.
getString
(
"q-key-time"
));
signatureVO
.
setHost
(
host
);
signatureVO
.
setResourceId
(
Long
.
valueOf
(
resourceId
));
String
result
=
String
.
valueOf
(
ResourceUtil
.
uploadFileContentToTencent
(
inputStream
,
filePathName
,
signatureVO
,
5000
,
"multipart/form-data"
));
Assert
.
assertEquals
(
result
,
"true"
);
//4.创建任务
JSONObject
result2
=
importTools
.
createImportTask
(
"MEMBER"
,
resourceId
,
"REPLACE_DUPLICATES"
,
""
);
String
recordId
=
XMJSONPath
.
readPath
(
result2
,
"$.result"
);
//5.获取上传进度
Thread
.
sleep
(
6000
);
importTools
.
getTask
(
recordId
);
Assert
.
assertEquals
(
XMJSONPath
.
readPath
(
dataApi
.
getApi_response
(),
"$.result.state"
),
"SUCCESS"
);
Assert
.
assertEquals
(
XMJSONPath
.
readPath
(
dataApi
.
getApi_response
(),
"$.result.successRowNum"
),
"1"
);
//上传成功后导入记录列表新增一条数据
JSONObject
res
=
importTools
.
pageImportTask
().
getJSONObject
(
"result"
);
Assert
.
assertEquals
(
Integer
.
parseInt
(
res
.
getString
(
"total"
)),
total
+
1
);
Assert
.
assertEquals
(
res
.
getJSONArray
(
"records"
).
getJSONObject
(
0
).
getString
(
"resourceId"
),
resourceId
);
Assert
.
assertEquals
(
res
.
getJSONArray
(
"records"
).
getJSONObject
(
0
).
getString
(
"state"
),
"SUCCESS"
);
//获取导入资源-下载导入文件
String
url1
=
importTools
.
getImportResource
(
resourceId
).
getString
(
"result"
);
Assert
.
assertEquals
(
url1
,
url
);
}
}
src/main/java/com/xiaomai/cases/polar/utils/CommonUtil.java
0 → 100644
View file @
ade277a2
package
com
.
xiaomai
.
cases
.
polar
.
utils
;
import
com.alibaba.fastjson.JSONArray
;
import
java.math.BigDecimal
;
import
java.math.RoundingMode
;
import
java.util.Random
;
/**
* @Author zxy
* @Description
* @Date 2024/7/2 17:57
*/
public
class
CommonUtil
{
/**
* 把字符串解析成JSONArray
*
* @param str
* @return
*/
public
static
JSONArray
JsonArray2Str
(
String
str
)
{
JSONArray
jsonArray
=
JSONArray
.
parseArray
(
str
);
return
jsonArray
;
}
/**
* 获取手机号,以1111开头
*
* @param random
* @return
*/
public
static
Long
getRandomPhone
(
Random
random
)
{
Integer
randomNum
=
random
.
nextInt
(
100000000
);
return
11111000000L
+
randomNum
;
}
/**
* @param mark 打标,比如获取的是员工:员工,校长;获取的是学员:_杜同学,同学来自线索导入
* @return 学员姓名
*/
public
static
String
getRandomName
(
String
mark
)
{
StringBuilder
sb
=
new
StringBuilder
();
String
firstName
=
"杜张王李赵彭柏孙谢钮徐薛劳周程钟郭竺杨黄苏艾楼陈曹蒋牛"
;
String
nameMiddle
=
"ABCDEFGHIJKLMNOPQRSTUVWXTZ"
;
String
nameNum
=
"零一二三四五六七八九十"
;
Random
randomGenerator
=
new
Random
();
int
randomInt
=
randomGenerator
.
nextInt
(
firstName
.
length
());
sb
.
append
(
firstName
,
randomInt
,
randomInt
+
1
);
randomInt
=
randomGenerator
.
nextInt
(
nameMiddle
.
length
());
sb
.
append
(
nameMiddle
,
randomInt
,
randomInt
+
1
);
randomInt
=
randomGenerator
.
nextInt
(
nameNum
.
length
());
sb
.
append
(
nameNum
,
randomInt
,
randomInt
+
1
);
if
(
mark
!=
null
&&
mark
.
length
()
!=
0
)
{
sb
.
append
(
mark
);
}
return
sb
.
toString
();
}
/**
* 保留2位小数
*
* @param d
* @return
*/
public
static
String
formatDouble2
(
double
d
)
{
// 新方法,如果不需要四舍五入,可以使用RoundingMode.DOWN
return
new
BigDecimal
(
d
).
setScale
(
2
,
RoundingMode
.
HALF_UP
).
stripTrailingZeros
().
toPlainString
();
}
/**
* 保留2位小数
*
* @param d
* @return
*/
public
static
Double
formatDouble
(
double
d
)
{
// 新方法,如果不需要四舍五入,可以使用RoundingMode.DOWN
return
new
BigDecimal
(
d
).
setScale
(
2
,
RoundingMode
.
HALF_UP
).
stripTrailingZeros
().
doubleValue
();
}
}
src/main/java/com/xiaomai/cases/polar/utils/Entity/SignatureVO.java
0 → 100644
View file @
ade277a2
package
com
.
xiaomai
.
cases
.
polar
.
utils
.
Entity
;
/**
* @Author zxy
* @Description
* @Date 2024/7/3 10:32
*/
public
class
SignatureVO
{
//资源ID
private
Long
resourceId
;
//接入ID
private
String
accessId
;
//访问策略
private
String
policy
;
//签名
private
String
signature
;
//对象路径
private
String
key
;
//接入点
private
String
host
;
//过期时间
private
String
expire
;
private
String
extAttrs
;
//回调地址
private
String
callback
;
private
String
algorithm
;
public
Long
getResourceId
()
{
return
resourceId
;
}
public
void
setResourceId
(
Long
resourceId
)
{
this
.
resourceId
=
resourceId
;
}
public
String
getAccessId
()
{
return
accessId
;
}
public
void
setAccessId
(
String
accessId
)
{
this
.
accessId
=
accessId
;
}
public
String
getPolicy
()
{
return
policy
;
}
public
void
setPolicy
(
String
policy
)
{
this
.
policy
=
policy
;
}
public
String
getSignature
()
{
return
signature
;
}
public
void
setSignature
(
String
signature
)
{
this
.
signature
=
signature
;
}
public
String
getKey
()
{
return
key
;
}
public
void
setKey
(
String
key
)
{
this
.
key
=
key
;
}
public
String
getHost
()
{
return
host
;
}
public
void
setHost
(
String
host
)
{
this
.
host
=
host
;
}
public
String
getExpire
()
{
return
expire
;
}
public
void
setExpire
(
String
expire
)
{
this
.
expire
=
expire
;
}
public
String
getCallback
()
{
return
callback
;
}
public
void
setCallback
(
String
callback
)
{
this
.
callback
=
callback
;
}
public
String
getExtAttrs
()
{
return
extAttrs
;
}
public
void
setExtAttrs
(
String
extAttrs
)
{
this
.
extAttrs
=
extAttrs
;
}
public
String
getAlgorithm
()
{
return
algorithm
;
}
public
void
setAlgorithm
(
String
algorithm
)
{
this
.
algorithm
=
algorithm
;
}
}
src/main/java/com/xiaomai/cases/polar/utils/ModifyExcelContentUtil.java
0 → 100644
View file @
ade277a2
package
com
.
xiaomai
.
cases
.
polar
.
utils
;
import
com.xiaomai.cases.polar.enums.ExcelImportEnum
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.log4j.Logger
;
import
org.apache.poi.hssf.usermodel.HSSFWorkbook
;
import
org.apache.poi.ss.usermodel.Cell
;
import
org.apache.poi.ss.usermodel.Row
;
import
org.apache.poi.ss.usermodel.Sheet
;
import
org.apache.poi.ss.usermodel.Workbook
;
import
java.io.*
;
import
java.util.Random
;
/**
* @Author zxy
* @Description
* @Date 2024/7/2 17:47
*/
public
class
ModifyExcelContentUtil
{
private
static
final
Logger
logger
=
Logger
.
getLogger
(
ModifyExcelContentUtil
.
class
);
/**
* 修改excel中的姓名和手机号信息
* @param fileUrl
* @param sheetName
* @param importBusinessEnum 业务类型
*/
public
static
void
modifyExcel
(
String
fileUrl
,
String
sheetName
,
ExcelImportEnum
importBusinessEnum
){
int
rowIndex
=
0
;
int
nameCellIndex
=
0
;
int
phoneCellIndex
=
1
;
Long
now
=
System
.
currentTimeMillis
();
Random
random
=
new
Random
(
now
);
String
name
=
""
;
rowIndex
=
importBusinessEnum
.
getRowStartIndex
();
nameCellIndex
=
importBusinessEnum
.
getCellNametIndex
();
phoneCellIndex
=
importBusinessEnum
.
getCellPhoneIndex
();
name
=
getRandomName
(
"导入线索"
);
try
{
InputStream
inputStream
=
new
FileInputStream
(
fileUrl
);
//根据指定的文件输入流导入Excel从而产生Workbook对象
Workbook
workbook
=
new
HSSFWorkbook
(
inputStream
);
//根据sheetname获取Excel文档表单
Sheet
sheet
=
workbook
.
getSheet
(
sheetName
);
//获取第rowIndex行
Row
row
=
sheet
.
getRow
(
rowIndex
);
Long
phone
=
CommonUtil
.
getRandomPhone
(
random
);
int
nameCount
=
1
;
while
(
row
!=
null
&&
rowIndex
<
5010
)
{
Cell
phoneCell
=
row
.
getCell
(
phoneCellIndex
);
Cell
nameCell
=
row
.
getCell
(
nameCellIndex
);
if
(
StringUtils
.
isBlank
(
nameCell
.
getStringCellValue
()))
{
break
;
}
phoneCell
.
setCellValue
(
phone
.
toString
());
String
realName
=
name
+
nameCount
;
//String realName = getRandomName("") + nameCount;
nameCell
.
setCellValue
(
realName
);
nameCount
++;
phone
++;
rowIndex
++;
row
=
sheet
.
getRow
(
rowIndex
);
}
OutputStream
outputStream
=
new
FileOutputStream
(
fileUrl
);
workbook
.
write
(
outputStream
);
outputStream
.
flush
();
logger
.
info
(
"-------------------Excel信息修改成功------------------"
);
}
catch
(
FileNotFoundException
e
)
{
e
.
printStackTrace
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
/**
*
* @param mark 学员姓名打标:比如 在读导入、线索导入;也可以不写
* @return 学员姓名
*/
private
static
String
getRandomName
(
String
mark
)
{
StringBuilder
sb
=
new
StringBuilder
();
String
firstName
=
"杜张王李赵彭柏孙谢钮徐薛劳周程钟郭竺杨黄苏艾楼陈曹蒋牛"
;
String
nameMiddle
=
"ABCDEFGHIJKLMNOPQRSTUVWXTZ"
;
String
nameNum
=
"零一二三四五六七八九十"
;
Random
randomGenerator
=
new
Random
();
int
randomInt
=
randomGenerator
.
nextInt
(
firstName
.
length
());
sb
.
append
(
firstName
,
randomInt
,
randomInt
+
1
);
randomInt
=
randomGenerator
.
nextInt
(
nameMiddle
.
length
());
sb
.
append
(
nameMiddle
,
randomInt
,
randomInt
+
1
);
randomInt
=
randomGenerator
.
nextInt
(
nameNum
.
length
());
sb
.
append
(
nameNum
,
randomInt
,
randomInt
+
1
);
sb
.
append
(
"同学_"
+
mark
);
return
sb
.
toString
();
}
}
src/main/java/com/xiaomai/cases/polar/utils/ResourceUtil.java
0 → 100644
View file @
ade277a2
package
com
.
xiaomai
.
cases
.
polar
.
utils
;
import
com.alibaba.fastjson.JSONObject
;
import
com.xiaomai.cases.polar.utils.Entity.SignatureVO
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.http.Consts
;
import
org.apache.http.HttpEntity
;
import
org.apache.http.HttpResponse
;
import
org.apache.http.client.config.RequestConfig
;
import
org.apache.http.client.methods.HttpPost
;
import
org.apache.http.entity.ContentType
;
import
org.apache.http.entity.mime.HttpMultipartMode
;
import
org.apache.http.entity.mime.MultipartEntityBuilder
;
import
org.apache.http.entity.mime.content.StringBody
;
import
org.apache.http.impl.client.CloseableHttpClient
;
import
org.apache.http.impl.client.HttpClients
;
import
org.apache.log4j.Logger
;
import
java.io.*
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
import
java.util.Map
;
import
java.util.Random
;
/**
* @Author zxy
* @Description
* @Date 2024/7/3 10:31
*/
@Slf4j
public
class
ResourceUtil
{
public
static
final
Logger
logger
=
Logger
.
getLogger
(
ResourceUtil
.
class
);
public
static
final
String
OSS_ACCESS_KEY_ID
=
"q-ak"
;
public
static
final
String
POLICY
=
"policy"
;
public
static
final
String
SIGNATURE
=
"q-signature"
;
public
static
final
String
KEY
=
"key"
;
public
static
final
String
SUCCESS_ACTION_STATUS
=
"success_action_status"
;
public
static
final
String
FILE
=
"file"
;
public
static
final
String
CALL_BACK
=
"x-cos-callback"
;
public
static
final
String
EXT_ATTRS
=
"q-key-time"
;
public
static
final
String
RESPONSE_STATUS
=
"200"
;
public
static
final
String
CONTENT_TYPE
=
"multipart/form-data"
;
/**
* 上传文件留到阿里云oss
*
* @param inputStream 文件流
* @param fileName 文件名称
* @param signatureVO 临时访问阿里云授权签名
* @return true 上传成功,false 失败
* @throws IOException 关闭文件流或httpClient连接时可能会异常
*/
public
static
boolean
uploadFile
(
InputStream
inputStream
,
String
fileName
,
SignatureVO
signatureVO
,
int
timeout
)
throws
IOException
{
CloseableHttpClient
client
=
HttpClients
.
createDefault
();
RequestConfig
requestConfig
=
RequestConfig
.
custom
()
//connectTimeout:指客户端和服务器建立连接的timeout,
.
setConnectTimeout
(
2000
)
//connectionRequestTimout:指从连接池获取连接的timeout
.
setConnectionRequestTimeout
(
2000
)
//socketTimeout:指客户端从服务器读取数据的timeout,超出后会抛出SocketTimeOutException,根据需要自定义
.
setSocketTimeout
(
timeout
)
.
build
();
HttpPost
post
=
new
HttpPost
(
signatureVO
.
getHost
());
post
.
setConfig
(
requestConfig
);
BufferedReader
br
=
null
;
try
{
// 参数顺序问题,该死的回调,千万要注意参数的顺序,死坑死坑
MultipartEntityBuilder
builder
=
MultipartEntityBuilder
.
create
();
builder
.
setMode
(
HttpMultipartMode
.
BROWSER_COMPATIBLE
);
StringBody
algorithm
=
new
StringBody
(
signatureVO
.
getAlgorithm
(),
ContentType
.
APPLICATION_JSON
);
builder
.
addPart
(
"q-sign-algorithm"
,
algorithm
);
// 兼容key中的中文名称
StringBody
key
=
new
StringBody
(
signatureVO
.
getKey
(),
ContentType
.
APPLICATION_JSON
);
builder
.
addPart
(
KEY
,
key
);
StringBody
accessKeyId
=
new
StringBody
(
signatureVO
.
getAccessId
(),
ContentType
.
MULTIPART_FORM_DATA
);
builder
.
addPart
(
OSS_ACCESS_KEY_ID
,
accessKeyId
);
StringBody
policy
=
new
StringBody
(
signatureVO
.
getPolicy
(),
ContentType
.
MULTIPART_FORM_DATA
);
builder
.
addPart
(
POLICY
,
policy
);
StringBody
signature
=
new
StringBody
(
signatureVO
.
getSignature
(),
ContentType
.
MULTIPART_FORM_DATA
);
builder
.
addPart
(
SIGNATURE
,
signature
);
StringBody
successActionStatus
=
new
StringBody
(
RESPONSE_STATUS
,
ContentType
.
MULTIPART_FORM_DATA
);
builder
.
addPart
(
SUCCESS_ACTION_STATUS
,
successActionStatus
);
StringBody
callBackStr
=
new
StringBody
(
signatureVO
.
getCallback
(),
ContentType
.
MULTIPART_FORM_DATA
);
builder
.
addPart
(
CALL_BACK
,
callBackStr
);
StringBody
extAttrs
=
new
StringBody
(
signatureVO
.
getExtAttrs
(),
ContentType
.
MULTIPART_FORM_DATA
);
System
.
out
.
println
(
"------------------"
+
extAttrs
.
toString
());
builder
.
addPart
(
EXT_ATTRS
,
extAttrs
);
builder
.
addBinaryBody
(
FILE
,
inputStream
,
ContentType
.
create
(
CONTENT_TYPE
),
fileName
);
HttpEntity
entity
=
builder
.
build
();
post
.
setEntity
(
entity
);
//发送请求
HttpResponse
response
=
client
.
execute
(
post
);
int
status
=
response
.
getStatusLine
().
getStatusCode
();
if
(
Integer
.
valueOf
(
RESPONSE_STATUS
).
equals
(
status
))
{
return
true
;
}
else
{
entity
=
response
.
getEntity
();
if
(
entity
!=
null
)
{
inputStream
=
entity
.
getContent
();
br
=
new
BufferedReader
(
new
InputStreamReader
(
inputStream
,
Consts
.
UTF_8
));
StringBuilder
message
=
new
StringBuilder
();
String
body
;
while
((
body
=
br
.
readLine
())
!=
null
)
{
message
.
append
(
body
);
}
logger
.
error
(
"文件上传失败!message:{}"
+
message
);
}
return
true
;
}
}
catch
(
Exception
e
)
{
logger
.
error
(
"文件上传异常!"
,
e
);
}
finally
{
if
(
br
!=
null
){
br
.
close
();
}
inputStream
.
close
();
client
.
close
();
}
return
false
;
}
public
static
boolean
uploadFileContentToTencent
(
InputStream
inputStream
,
String
fileName
,
SignatureVO
signatureVO
,
int
timeout
,
String
contentType
)
throws
IOException
{
String
boundary
=
generateBoundary
();
// 设置表单的body字段值
JSONObject
formFields
=
new
JSONObject
();
formFields
.
put
(
"q-sign-algorithm"
,
"sha1"
);
formFields
.
put
(
"key"
,
signatureVO
.
getKey
());
formFields
.
put
(
"q-ak"
,
signatureVO
.
getAccessId
());
formFields
.
put
(
"policy"
,
signatureVO
.
getPolicy
());
formFields
.
put
(
"q-signature"
,
signatureVO
.
getSignature
());
formFields
.
put
(
"success_action_status"
,
RESPONSE_STATUS
);
formFields
.
put
(
"x-cos-callback"
,
signatureVO
.
getCallback
());
formFields
.
put
(
"q-key-time"
,
signatureVO
.
getExtAttrs
());
// 根据以上表单参数,构造最开始的body部分
String
formBody
=
buildPostObjectBody
(
boundary
,
formFields
,
fileName
,
contentType
);
HttpURLConnection
conn
=
null
;
try
{
String
urlStr
=
signatureVO
.
getHost
();
URL
url
=
new
URL
(
urlStr
);
conn
=
(
HttpURLConnection
)
url
.
openConnection
();
conn
.
setRequestMethod
(
"POST"
);
conn
.
setRequestProperty
(
"Content-Type"
,
"multipart/form-data; boundary="
+
boundary
);
conn
.
setDoOutput
(
true
);
conn
.
setDoInput
(
true
);
OutputStream
out
=
new
DataOutputStream
(
conn
.
getOutputStream
());
// 写入表单的最开始部分
out
.
write
(
formBody
.
getBytes
());
// 将文件内容写入到输出流中
int
readBytes
;
byte
[]
bytes
=
new
byte
[
4096
];
while
((
readBytes
=
inputStream
.
read
(
bytes
))
!=
-
1
)
{
out
.
write
(
bytes
,
0
,
readBytes
);
}
inputStream
.
close
();
// 添加最后一个分割符,行首和行尾都是--
byte
[]
endData
=
(
"\r\n--"
+
boundary
+
"--\r\n"
).
getBytes
();
out
.
write
(
endData
);
out
.
flush
();
out
.
close
();
// 读取响应头部
if
(
Integer
.
valueOf
(
RESPONSE_STATUS
).
equals
(
conn
.
getResponseCode
()))
{
return
true
;
}
else
{
log
.
error
(
"文件上传失败!message:{}"
,
conn
.
getResponseCode
());
}
return
true
;
}
catch
(
Exception
e
)
{
log
.
error
(
"文件上传异常!"
,
e
);
return
false
;
}
finally
{
if
(
conn
!=
null
)
{
conn
.
disconnect
();
}
inputStream
.
close
();
}
}
private
final
static
char
[]
MULTIPART_CHARS
=
"-_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
.
toCharArray
();
private
static
String
generateBoundary
()
{
final
StringBuilder
buffer
=
new
StringBuilder
();
final
Random
rand
=
new
Random
();
final
int
count
=
rand
.
nextInt
(
11
)
+
30
;
// a random size from 30 to 40
for
(
int
i
=
0
;
i
<
count
;
i
++)
{
buffer
.
append
(
MULTIPART_CHARS
[
rand
.
nextInt
(
MULTIPART_CHARS
.
length
)]);
}
return
buffer
.
toString
();
}
static
String
buildPostObjectBody
(
String
boundary
,
JSONObject
formFields
,
String
filename
,
String
contentType
)
{
StringBuilder
sb
=
new
StringBuilder
();
for
(
Map
.
Entry
<
String
,
Object
>
entry
:
formFields
.
entrySet
())
{
// 添加boundary行,行首以--开头
sb
.
append
(
"--"
).
append
(
boundary
).
append
(
"\r\n"
);
// 字段名
sb
.
append
(
"Content-Disposition: form-data; name=\""
).
append
(
entry
.
getKey
())
.
append
(
"\"\r\n\r\n"
);
// 字段值
sb
.
append
(
entry
.
getValue
()).
append
(
"\r\n"
);
}
// 添加boundary行,行首以--开头
sb
.
append
(
"--"
).
append
(
boundary
).
append
(
"\r\n"
);
// 文件名
sb
.
append
(
"Content-Disposition: form-data; name=\"file\"; "
+
"filename=\""
).
append
(
filename
)
.
append
(
"\"\r\n"
);
// 文件类型
sb
.
append
(
"Content-Type: "
).
append
(
contentType
).
append
(
"\r\n\r\n"
);
return
sb
.
toString
();
}
}
src/main/java/com/xiaomai/enums/ApiModule.java
View file @
ade277a2
...
...
@@ -10,6 +10,8 @@ public class ApiModule {
public
static
String
Polar_Setting
=
"polar_setting"
;
//通用设置
public
static
String
Polar_Applet
=
"polar_applet"
;
//专属小程序
public
static
String
Polar_Clue
=
"polar_clue"
;
//crm
public
static
String
Polar_ImportTask
=
"polar_importTask"
;
//导入
public
static
String
Polar_Homepage
=
"polar_homePage"
;
//首页
public
static
String
Polar_Member
=
"polar_member"
;
//会员管理
public
static
String
Polar_Course
=
"polar_course"
;
//会员管理
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment