Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
glinfo-api
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
林业通
glinfo-api
Commits
7623d9f6
提交
7623d9f6
authored
1月 07, 2022
作者:
linzhenjie
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
加入微信登录相关信息
上级
9770699d
显示空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
161 行增加
和
0 行删除
+161
-0
AppUserController.java
...o/enbao/modules/appuser/controller/AppUserController.java
+142
-0
application-dev.yml
appapi/src/main/resources/application-dev.yml
+5
-0
application-local.yml
appapi/src/main/resources/application-local.yml
+5
-0
application-prod.yml
appapi/src/main/resources/application-prod.yml
+4
-0
application-test.yml
appapi/src/main/resources/application-test.yml
+5
-0
没有找到文件。
appapi/src/main/java/tech/glinfo/enbao/modules/appuser/controller/AppUserController.java
浏览文件 @
7623d9f6
package
tech
.
glinfo
.
enbao
.
modules
.
appuser
.
controller
;
import
com.alibaba.fastjson.JSONObject
;
import
com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.ApiOperation
;
import
org.apache.commons.beanutils.BeanUtils
;
import
org.apache.commons.codec.binary.Base64
;
import
org.apache.commons.codec.digest.DigestUtils
;
import
org.apache.http.HttpEntity
;
import
org.apache.http.client.methods.CloseableHttpResponse
;
import
org.apache.http.client.methods.HttpGet
;
import
org.apache.http.impl.client.CloseableHttpClient
;
import
org.apache.http.impl.client.HttpClients
;
import
org.apache.http.util.EntityUtils
;
import
org.bouncycastle.jce.provider.BouncyCastleProvider
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.client.RestClientException
;
import
tech.glinfo.enbao.common.annotation.ApiLog
;
import
tech.glinfo.enbao.common.annotation.Login
;
import
tech.glinfo.enbao.common.annotation.LoginUser
;
...
...
@@ -24,8 +35,16 @@ import tech.glinfo.enbao.modules.sh.entity.ShFamilyMemberEntity;
import
tech.glinfo.enbao.modules.sh.service.OtherShFamilyService
;
import
tech.glinfo.enbao.modules.sh.service.ShFamilyMemberService
;
import
javax.crypto.BadPaddingException
;
import
javax.crypto.Cipher
;
import
javax.crypto.IllegalBlockSizeException
;
import
javax.crypto.NoSuchPaddingException
;
import
javax.crypto.spec.IvParameterSpec
;
import
javax.crypto.spec.SecretKeySpec
;
import
java.io.IOException
;
import
java.lang.reflect.InvocationTargetException
;
import
java.security.*
;
import
java.security.spec.AlgorithmParameterSpec
;
import
java.util.Date
;
import
java.util.HashMap
;
import
java.util.Map
;
...
...
@@ -58,6 +77,13 @@ public class AppUserController extends AbstractController {
@Autowired
private
ShFamilyMemberService
shFamilyMemberService
;
@Value
(
"${wechat.appId}"
)
private
String
appId
;
@Value
(
"${wechat.appSecret}"
)
private
String
appSecret
;
@PostMapping
(
"register"
)
@ApiOperation
(
"用户注册"
)
@ApiLog
(
"用户注册"
)
...
...
@@ -428,4 +454,120 @@ public class AppUserController extends AbstractController {
return
R
.
ok
();
}
/**
* 校验用户是否存在,存在直接登录
* @param params
* @return
*/
@PostMapping
(
"getMiniInfo"
)
public
R
getMiniInfo
(
@RequestBody
Map
<
String
,
Object
>
params
)
{
String
code
=
(
String
)
params
.
get
(
"code"
);
//wx接口路径
String
url
=
"https://api.weixin.qq.com/sns/jscode2session?appid="
+
appId
+
"&secret="
+
appSecret
+
"&js_code="
+
code
+
"&grant_type=authorization_code"
;
//使用HttpClient发送请求
CloseableHttpClient
httpclient
=
HttpClients
.
createDefault
();
//发送Get请求
HttpGet
request
=
new
HttpGet
(
url
);
request
.
addHeader
(
"Content-Type"
,
"application/json"
);
//获得响应
String
result
=
null
;
// 转成string
try
{
CloseableHttpResponse
response
=
httpclient
.
execute
(
request
);
//拿到响应体
HttpEntity
httpEntity
=
response
.
getEntity
();
//使用工具转换
result
=
EntityUtils
.
toString
(
httpEntity
,
"UTF-8"
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
logger
.
info
(
"result:{}"
,
result
);
if
(
result
!=
null
)
{
return
R
.
ok
(
JSONObject
.
parseObject
(
result
));
// String openid = jsonObject.get("openid").toString();
// logger.info("openid" + openid);
}
return
R
.
error
();
}
/**
* 解密
* @param sessionKey
* @param encryptedData
* @param iv
* @return
*/
private
JSONObject
decode
(
String
sessionKey
,
String
encryptedData
,
String
iv
)
{
byte
[]
encrypData
=
Base64
.
decodeBase64
(
encryptedData
);
byte
[]
ivData
=
Base64
.
decodeBase64
(
iv
);
byte
[]
sessionKeyB
=
Base64
.
decodeBase64
(
sessionKey
);
Security
.
addProvider
(
new
BouncyCastleProvider
());
AlgorithmParameterSpec
ivSpec
=
new
IvParameterSpec
(
ivData
);
byte
[]
doFinal
=
new
byte
[
0
];
try
{
Cipher
cipher
=
Cipher
.
getInstance
(
"AES/CBC/PKCS7Padding"
,
"BC"
);
SecretKeySpec
keySpec
=
new
SecretKeySpec
(
sessionKeyB
,
"AES"
);
cipher
.
init
(
Cipher
.
DECRYPT_MODE
,
keySpec
,
ivSpec
);
doFinal
=
cipher
.
doFinal
(
encrypData
);
}
catch
(
NoSuchAlgorithmException
e
)
{
e
.
printStackTrace
();
}
catch
(
NoSuchProviderException
e
)
{
e
.
printStackTrace
();
}
catch
(
NoSuchPaddingException
e
)
{
e
.
printStackTrace
();
}
catch
(
InvalidKeyException
e
)
{
e
.
printStackTrace
();
}
catch
(
InvalidAlgorithmParameterException
e
)
{
e
.
printStackTrace
();
}
catch
(
IllegalBlockSizeException
e
)
{
e
.
printStackTrace
();
}
catch
(
BadPaddingException
e
)
{
e
.
printStackTrace
();
}
if
(
doFinal
!=
null
&&
doFinal
.
length
>
0
)
{
String
result
=
new
String
(
doFinal
);
return
JSONObject
.
parseObject
(
result
);
}
return
new
JSONObject
();
}
@PostMapping
(
"getUserPhoneAndRegister"
)
public
R
getUserPhoneAndRegister
(
@RequestBody
Map
<
String
,
Object
>
params
)
{
String
sessionKey
=
(
String
)
params
.
get
(
"sessionKey"
);
String
encryptedData
=
(
String
)
params
.
get
(
"encryptedData"
);
String
iv
=
(
String
)
params
.
get
(
"iv"
);
String
unionid
=
(
String
)
params
.
get
(
"unionid"
);
JSONObject
userInfo
=
decode
(
sessionKey
,
encryptedData
,
iv
);
String
phoneNumber
=
userInfo
.
getString
(
"phoneNumber"
);
Map
<
String
,
Object
>
result
=
new
HashMap
<>();
if
(
StringUtils
.
isNotEmpty
(
phoneNumber
))
{
AppUserEntity
_user
=
otherAppUserService
.
queryByPhone
(
phoneNumber
);
if
(
_user
!=
null
)
{
//已经注册过了,直接登录即可
String
token
=
jwtUtils
.
generateToken
(
_user
.
getId
()
+
"#"
+
_user
.
getPhone
());
result
.
put
(
"token"
,
token
);
result
.
put
(
"id"
,
_user
.
getId
());
AppUserEntity
userEntity
=
new
AppUserEntity
();
userEntity
.
setId
(
_user
.
getId
());
userEntity
.
setOpenId
(
unionid
);
userEntity
.
setLastLoginTime
(
new
Date
());
otherAppUserService
.
updateById
(
userEntity
);
}
else
{
//注册用户
AppUserEntity
user
=
new
AppUserEntity
();
user
.
setPhone
(
phoneNumber
);
user
.
setNickname
(
"微信用户"
);
user
.
setOpenId
(
unionid
);
user
.
setPassword
(
DigestUtils
.
sha256Hex
(
StringUtils
.
random
(
6
,
StringUtils
.
RandomType
.
INT
)));
String
token
=
jwtUtils
.
generateToken
(
user
.
getId
()
+
"#"
+
phoneNumber
);
result
.
put
(
"token"
,
token
);
result
.
put
(
"id"
,
user
.
getId
());
}
}
return
R
.
ok
(
result
);
}
}
appapi/src/main/resources/application-dev.yml
浏览文件 @
7623d9f6
...
...
@@ -130,3 +130,7 @@ aliyunspeed:
accessKeyId
:
LTAI4FiV4NgkSiSUvrhvvSTa
accessKeySecret
:
R1Fj3GfxOC9NgY5MXBEvEktA7CikDy
appKey
:
JZX4QCEG7OQwYD8W
wechat
:
appId
:
wx983fb930ecd84b4d
appSecret
:
386f1c884ebc7e36ab35d2c38942302a
\ No newline at end of file
appapi/src/main/resources/application-local.yml
浏览文件 @
7623d9f6
...
...
@@ -130,3 +130,7 @@ aliyunspeed:
accessKeyId
:
LTAI4FiV4NgkSiSUvrhvvSTa
accessKeySecret
:
R1Fj3GfxOC9NgY5MXBEvEktA7CikDy
appKey
:
JZX4QCEG7OQwYD8W
wechat
:
appId
:
wx983fb930ecd84b4d
appSecret
:
386f1c884ebc7e36ab35d2c38942302a
\ No newline at end of file
appapi/src/main/resources/application-prod.yml
浏览文件 @
7623d9f6
...
...
@@ -131,3 +131,7 @@ aliyunspeed:
accessKeyId
:
LTAI4FiV4NgkSiSUvrhvvSTa
accessKeySecret
:
R1Fj3GfxOC9NgY5MXBEvEktA7CikDy
appKey
:
JZX4QCEG7OQwYD8W
wechat
:
appId
:
wx983fb930ecd84b4d
appSecret
:
386f1c884ebc7e36ab35d2c38942302a
appapi/src/main/resources/application-test.yml
浏览文件 @
7623d9f6
...
...
@@ -131,3 +131,7 @@ aliyunspeed:
accessKeyId
:
LTAI4FiV4NgkSiSUvrhvvSTa
accessKeySecret
:
R1Fj3GfxOC9NgY5MXBEvEktA7CikDy
appKey
:
JZX4QCEG7OQwYD8W
wechat
:
appId
:
wx983fb930ecd84b4d
appSecret
:
386f1c884ebc7e36ab35d2c38942302a
\ No newline at end of file
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论