|
@@ -13,11 +13,12 @@ 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.request.AlipayUserInfoShareRequest;
|
|
|
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
|
|
|
+import com.alipay.api.response.AlipayUserInfoShareResponse;
|
|
|
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;
|
|
@@ -44,16 +45,15 @@ import io.swagger.annotations.Api;
|
|
|
import java.io.File;
|
|
|
import java.net.HttpURLConnection;
|
|
|
import java.net.URL;
|
|
|
-import java.nio.charset.StandardCharsets;
|
|
|
import java.util.Date;
|
|
|
-import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
-import java.util.logging.Logger;
|
|
|
import javax.crypto.Cipher;
|
|
|
import javax.crypto.spec.IvParameterSpec;
|
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
|
|
|
|
+import io.swagger.annotations.ApiImplicitParam;
|
|
|
+import io.swagger.annotations.ApiImplicitParams;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -61,7 +61,6 @@ import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import sun.misc.BASE64Decoder;
|
|
|
-import sun.misc.BASE64Encoder;
|
|
|
|
|
|
/**
|
|
|
* @auther duota
|
|
@@ -104,10 +103,10 @@ public class UserController extends BaseApiController {
|
|
|
@Value("${ali-miniApp.private-key}")
|
|
|
private String aliPrivateKey;
|
|
|
|
|
|
- @Value("{ali-miniApp.serverUrl}")
|
|
|
- private String serverUrl;
|
|
|
+ @Value("${ali-miniApp.serverUrl}")
|
|
|
+ private String aliServerUrl;
|
|
|
|
|
|
- @Value("{ali-miniApp.decrypt-key}")
|
|
|
+ @Value("${ali-miniApp.decrypt-key}")
|
|
|
private String decryptKey;
|
|
|
|
|
|
/**
|
|
@@ -216,6 +215,81 @@ public class UserController extends BaseApiController {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ @ApiOperation("支付宝授权登录")
|
|
|
+ @PostMapping("/user/aliAuth")
|
|
|
+ public AjaxResult aliAuth(@RequestBody WxLoginParams wxLoginParams) {
|
|
|
+ String code = wxLoginParams.getCode();
|
|
|
+ if (StringUtils.isBlank(code)) {
|
|
|
+ return AjaxResult.error("code码不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ AlipayClient alipayClient = new DefaultAlipayClient(aliServerUrl, aliAppId, aliPrivateKey, "json", "GBK", aliPublicKey, "RSA2");
|
|
|
+ AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
|
|
|
+ request.setGrantType("authorization_code");
|
|
|
+ request.setCode(code);
|
|
|
+ AlipaySystemOauthTokenResponse response = null;
|
|
|
+ try {
|
|
|
+ response = alipayClient.execute(request);
|
|
|
+ } catch (AlipayApiException e) {
|
|
|
+ LogUtil.error(logger, "获取支付宝授权令牌失败,errorMsg:{0}", e.getMessage());
|
|
|
+ return AjaxResult.error("获取授权令牌失败");
|
|
|
+ }
|
|
|
+ if (!response.isSuccess()) {
|
|
|
+ return AjaxResult.error("获取授权令牌调用失败");
|
|
|
+ }
|
|
|
+ LogUtil.info(logger, "授权令牌信息:{0}", JSONUtil.toJsonStr(response));
|
|
|
+ // 支付宝小程序userId
|
|
|
+ String aliUserId = response.getUserId();
|
|
|
+ // 访问令牌
|
|
|
+ String accessToken = response.getAccessToken();
|
|
|
+
|
|
|
+ // 根据访问令牌获取用户基本信息
|
|
|
+ AlipayUserInfoShareRequest userInfoRequest = new AlipayUserInfoShareRequest();
|
|
|
+ AlipayUserInfoShareResponse userInfoResponse = null;
|
|
|
+ try {
|
|
|
+ userInfoResponse = alipayClient.execute(userInfoRequest, accessToken);
|
|
|
+ } catch (AlipayApiException e) {
|
|
|
+ LogUtil.error(logger, "获取用户基本信息失败,errorMsg:{0}", e.getMessage());
|
|
|
+ return AjaxResult.error("获取用户基本信息失败");
|
|
|
+ }
|
|
|
+ if (!userInfoResponse.isSuccess()) {
|
|
|
+ return AjaxResult.error("获取用户基本信息失败");
|
|
|
+ }
|
|
|
+ LogUtil.info(logger, "用户基本信息:{0}", JSONUtil.toJsonStr(userInfoResponse));
|
|
|
+
|
|
|
+
|
|
|
+ // 更新数据库用户授权信息
|
|
|
+ LoginUser loginUser = SecurityUtils.getLoginUser();
|
|
|
+ SysUser user = new SysUser();
|
|
|
+ user.setUserId(loginUser.getUserId());
|
|
|
+ user.setLoginDate(new Date());
|
|
|
+ user.setAliuserId(aliUserId);
|
|
|
+ user.setNickName(userInfoResponse.getNickName());
|
|
|
+ // 如果用户头像字段为空,则默认获取用户支付宝头像
|
|
|
+ String avatarUrl = userInfoResponse.getAvatar();
|
|
|
+
|
|
|
+ if (StringUtils.isBlank(loginUser.getUser().getAvatar()) && !StringUtils.isBlank(avatarUrl)) {
|
|
|
+ try {
|
|
|
+ //下载并且重新上传头像
|
|
|
+ JSONObject result = saveHeadImage(avatarUrl, String.valueOf(user.getUserId()));
|
|
|
+ if (result != null) {
|
|
|
+ user.setAvatar(result.getString("fileName"));
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ LogUtil.error(logger, "头像上传异常", e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ logger.info("更新用户数据:" + JSONUtil.toJsonStr(user));
|
|
|
+ int rows = sysUserService.updateUserProfile(user);
|
|
|
+ if (rows != 1) {
|
|
|
+ LogUtil.error(logger, "用户支付宝授权异常");
|
|
|
+ return error("用户支付宝授权异常");
|
|
|
+ }
|
|
|
+ JSONObject rst = new JSONObject();
|
|
|
+ rst.put("aliUserId", aliUserId);
|
|
|
+ return success(rst);
|
|
|
+ }
|
|
|
+
|
|
|
@ApiOperation("微信授权登录")
|
|
|
@RequestMapping(value = "/user/wxauth", method = RequestMethod.POST)
|
|
|
@ResponseBody
|
|
@@ -409,6 +483,7 @@ public class UserController extends BaseApiController {
|
|
|
}
|
|
|
jsonObject.put("openId", sysUser.getOpenId());
|
|
|
jsonObject.put("channelOpenId", sysUser.getChannelOpenId());
|
|
|
+ jsonObject.put("aliUserId", sysUser.getAliuserId());
|
|
|
JSONArray roleKeys = new JSONArray();
|
|
|
JSONArray roles = new JSONArray();
|
|
|
loginUser.getUser().getRoles().forEach(sysRole -> roles.add(sysRole.getRoleName()));
|
|
@@ -551,12 +626,17 @@ public class UserController extends BaseApiController {
|
|
|
String charset = "UTF-8";
|
|
|
String encryptType = "AES";
|
|
|
|
|
|
- String publicKey1 = "al2021003127607930AES";
|
|
|
+ String publicKey1 = "fhixMEfC0kpw9drqNJiMfQ==";
|
|
|
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);
|
|
|
|
|
|
+
|
|
|
+ String sss = "MdfC1q4gGs/I+SSrEXPj53glFjQhASHV2Op69KeecJ6iM0jN6mUVkjAOHSrjCzd9O5RI0vZt7EFcyyQP3YS5Mg==";
|
|
|
+ String s1 = AlipayEncrypt.decryptContent(sss, encryptType, publicKey1, charset);
|
|
|
+ System.out.println("s1 = " + s1);
|
|
|
+
|
|
|
}
|
|
|
|
|
|
}
|