|
@@ -1,14 +1,25 @@
|
|
|
package com.qs.mp.web.controller.api.common;
|
|
|
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
import cn.jsms.api.ValidSMSResult;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.alibaba.fastjson.TypeReference;
|
|
|
+import com.alibaba.fastjson.parser.Feature;
|
|
|
+import com.alipay.api.AlipayApiException;
|
|
|
+import com.alipay.api.AlipayClient;
|
|
|
+import com.alipay.api.DefaultAlipayClient;
|
|
|
+import com.alipay.api.internal.util.AlipayEncrypt;
|
|
|
+import com.alipay.api.internal.util.AlipaySignature;
|
|
|
+import com.alipay.api.request.AlipaySystemOauthTokenRequest;
|
|
|
+import com.alipay.api.response.AlipaySystemOauthTokenResponse;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.qs.mp.channel.domain.Channel;
|
|
|
import com.qs.mp.channel.service.IChannelService;
|
|
|
import com.qs.mp.common.constant.Constants;
|
|
|
import com.qs.mp.common.core.domain.AjaxResult;
|
|
|
+import com.qs.mp.common.domain.vo.AliPhoneDecryptVO;
|
|
|
import com.qs.mp.common.enums.UserIdentityEnum;
|
|
|
import com.qs.mp.common.enums.WxActTypeEnum;
|
|
|
import com.qs.mp.common.jsms.JSMSUtils;
|
|
@@ -40,15 +51,13 @@ import java.util.Map;
|
|
|
import javax.crypto.Cipher;
|
|
|
import javax.crypto.spec.IvParameterSpec;
|
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
|
+
|
|
|
+import io.swagger.annotations.ApiOperation;
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
-import org.springframework.web.bind.annotation.RequestBody;
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
-import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
-import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
-import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
import sun.misc.BASE64Decoder;
|
|
|
import sun.misc.BASE64Encoder;
|
|
|
|
|
@@ -86,6 +95,19 @@ public class UserController extends BaseApiController {
|
|
|
@Value("${wx-user.appSecret}")
|
|
|
private String userAppSecret;
|
|
|
|
|
|
+ @Value("${ali-miniApp.appId}")
|
|
|
+ private String aliAppId;
|
|
|
+ @Value("${ali-miniApp.public-key}")
|
|
|
+ private String aliPublicKey;
|
|
|
+ @Value("${ali-miniApp.private-key}")
|
|
|
+ private String aliPrivateKey;
|
|
|
+
|
|
|
+ @Value("{ali-miniApp.serverUrl}")
|
|
|
+ private String serverUrl;
|
|
|
+
|
|
|
+ @Value("{ali-miniApp.decrypt-key}")
|
|
|
+ private String decryptKey;
|
|
|
+
|
|
|
/**
|
|
|
* 文件上传路径
|
|
|
*/
|
|
@@ -109,6 +131,90 @@ public class UserController extends BaseApiController {
|
|
|
private static final String UTF8 = "UTF-8";
|
|
|
|
|
|
|
|
|
+ @ApiOperation("支付宝获取用户手机号")
|
|
|
+ @PostMapping("/user/aliAuth/mobile")
|
|
|
+ public AjaxResult aliAuthMobile(@RequestBody WxLoginParams wxLoginParams) {
|
|
|
+
|
|
|
+ String response = wxLoginParams.getEncryptedData();
|
|
|
+
|
|
|
+ //1. 获取验签和解密所需要的参数
|
|
|
+ Map<String, String> openapiResult = JSON.parseObject(response,
|
|
|
+ new TypeReference<Map<String, String>>() {
|
|
|
+ }, Feature.OrderedField);
|
|
|
+ String signType = "RSA2";
|
|
|
+ String charset = "UTF-8";
|
|
|
+ String encryptType = "AES";
|
|
|
+ String sign = openapiResult.get("sign");
|
|
|
+ String content = openapiResult.get("response");
|
|
|
+
|
|
|
+ // 是否加密
|
|
|
+ boolean isDataEncrypted = !content.startsWith("{");
|
|
|
+ boolean signCheckPass = false;
|
|
|
+
|
|
|
+ //2. 验签
|
|
|
+ String signContent = content;
|
|
|
+ String signVeriKey = aliPublicKey;
|
|
|
+ // 如果是加密的报文则需要在密文的前后添加双引号
|
|
|
+ if (isDataEncrypted) {
|
|
|
+ signContent = "\"" + signContent + "\"";
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ signCheckPass = AlipaySignature.rsaCheck(signContent, sign, signVeriKey, charset, signType);
|
|
|
+ } catch (AlipayApiException e) {
|
|
|
+ // 验签异常, 日志
|
|
|
+ }
|
|
|
+ if(!signCheckPass) {
|
|
|
+ // 验签不通过(异常或者报文被篡改),终止流程(不需要做解密)
|
|
|
+ LogUtil.warn(logger, "验签失败");
|
|
|
+ AjaxResult.error("验签失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ //3. 解密
|
|
|
+ String plainData = null;
|
|
|
+ if (isDataEncrypted) {
|
|
|
+ try {
|
|
|
+ plainData = AlipayEncrypt.decryptContent(content, encryptType, decryptKey, charset);
|
|
|
+ } catch (AlipayApiException e) {
|
|
|
+ //解密异常, 记录日志
|
|
|
+ LogUtil.warn(logger, "解密异常");
|
|
|
+ AjaxResult.error("解密异常");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ plainData = content;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 转对象 异常情况处理
|
|
|
+
|
|
|
+ AliPhoneDecryptVO aliPhoneDecryptVO = JSONUtil.toBean(plainData, AliPhoneDecryptVO.class);
|
|
|
+ if (!aliPhoneDecryptVO.isSuccess()) {
|
|
|
+ LogUtil.error(logger,"解析手机号失败,errorMsg:{0}", plainData);
|
|
|
+ return AjaxResult.error("解析手机号失败," + aliPhoneDecryptVO.getSubMsg());
|
|
|
+ }
|
|
|
+ String mobile = aliPhoneDecryptVO.getMobile();
|
|
|
+
|
|
|
+ AjaxResult ajax = AjaxResult.success();
|
|
|
+ SysUser sysUser = sysUserService.selectUserByUserName(mobile);
|
|
|
+ if (null == sysUser) {
|
|
|
+ if (wxLoginParams.getIdentity() != UserIdentityEnum.USER.ordinal()) {
|
|
|
+ return error("用户不存在,请联系客服");
|
|
|
+ }
|
|
|
+ // C端直接注册新用户
|
|
|
+ sysUser = new SysUser();
|
|
|
+ sysUser.setUserName(mobile);
|
|
|
+ sysUser.setNickName(mobile);
|
|
|
+ sysUser.setPhonenumber(mobile);
|
|
|
+ sysUserService.registerUser(sysUser);
|
|
|
+ }
|
|
|
+ Map<String, String> result = sysLoginService.wxAuthLogin(mobile, wxLoginParams.getIdentity());
|
|
|
+ for (String key : result.keySet()) {
|
|
|
+ ajax.put(key, result.get(key));
|
|
|
+ }
|
|
|
+ return ajax;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ @ApiOperation("微信授权登录")
|
|
|
@RequestMapping(value = "/user/wxauth", method = RequestMethod.POST)
|
|
|
@ResponseBody
|
|
|
public AjaxResult wxauth(@RequestBody WxLoginParams wxLoginParams) {
|
|
@@ -177,6 +283,7 @@ public class UserController extends BaseApiController {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ @ApiOperation("微信授权获取用户手机号")
|
|
|
@RequestMapping(value = "/user/wxauth/mobile", method = RequestMethod.POST)
|
|
|
@ResponseBody
|
|
|
public AjaxResult wxauthMobile(@RequestBody WxLoginParams wxLoginParams) {
|
|
@@ -283,6 +390,7 @@ public class UserController extends BaseApiController {
|
|
|
/**
|
|
|
* 查询当前登录用户信息
|
|
|
*/
|
|
|
+ @ApiOperation("查询当前登录用户信息")
|
|
|
@RequestMapping(value = "/user/getLoginUserinfo", method = RequestMethod.POST)
|
|
|
public AjaxResult getLoginUserinfo(@RequestBody JSONObject params) {
|
|
|
LoginUser loginUser = SecurityUtils.getLoginUser();
|
|
@@ -434,9 +542,19 @@ public class UserController extends BaseApiController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static void main(String[] args) {
|
|
|
- UserController userController = new UserController();
|
|
|
- System.out.println(userController.weixinDecrypt("qvNE+SHdiP2GTbmNaqahrhhLw7EZetcMhUMV9yPXHxBbWOMcZ5gFxOGliXC9uojyC0NDUXCUi1xvFVDKMYREQ7rLXXrZIkB1jEleJNuUb9kO3LCDRucvbGAbVwm2EsTNsd5VbqI3fKdu0IOxmmSAWCLZNGXZBlki4ke62lI+ASg6dPohQmmCux8jwD2Js3ZnDQN2mb0JPRT68Qj716gYvA==", "QJ/WdeFANTIfIErOCJ2jNg==", "60YJmIckq4kaXp88wAGGHA=="));
|
|
|
+ public static void main(String[] args) throws AlipayApiException {
|
|
|
+// UserController userController = new UserController();
|
|
|
+// System.out.println(userController.weixinDecrypt("qvNE+SHdiP2GTbmNaqahrhhLw7EZetcMhUMV9yPXHxBbWOMcZ5gFxOGliXC9uojyC0NDUXCUi1xvFVDKMYREQ7rLXXrZIkB1jEleJNuUb9kO3LCDRucvbGAbVwm2EsTNsd5VbqI3fKdu0IOxmmSAWCLZNGXZBlki4ke62lI+ASg6dPohQmmCux8jwD2Js3ZnDQN2mb0JPRT68Qj716gYvA==", "QJ/WdeFANTIfIErOCJ2jNg==", "60YJmIckq4kaXp88wAGGHA=="));
|
|
|
+
|
|
|
+ String charset = "UTF-8";
|
|
|
+ String encryptType = "AES";
|
|
|
+
|
|
|
+ String publicKey1 = "al2021003127607930AES";
|
|
|
+ String s = AlipayEncrypt.encryptContent("17681682549", encryptType, publicKey1, charset);
|
|
|
+ System.out.println("s = " + s);
|
|
|
+ String s2 = AlipayEncrypt.decryptContent(s, encryptType, publicKey1, charset);
|
|
|
+ System.out.println("s2 = " + s2);
|
|
|
+
|
|
|
}
|
|
|
|
|
|
}
|