mitao
2024-04-30 ab4ea7b8f10c9b66aed9c2ea161a08b25c3851a7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package com.sinata.rest.modular.member.service.impl;
 
 
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sinata.common.enums.EnumRedisKey;
import com.sinata.common.util.WxUtil;
import com.sinata.rest.core.util.RestTemplateUtil;
import com.sinata.rest.modular.member.controller.common.body.RegisterThirdUserRequest;
import com.sinata.rest.modular.member.controller.common.vo.WeChatMiniAuthorizeVo;
import com.sinata.rest.modular.member.service.WechatNewService;
import com.sinata.rest.modular.system.service.RedisTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
@Slf4j
@Service
public class WechatNewServiceImpl implements WechatNewService {
 
    @Autowired
    private RestTemplateUtil restTemplateUtil;
 
    @Autowired
    private RedisTemplateService redis;
 
    private final  static String mallAppId = "wx9e0a20d9ffcacc05";
 
    private final  static String mallSecret = "3cd8a4812da753cbda4edd062bd11b63";
 
    private final  static String shopAppId = "wx7e8ef66b213de2ef";
 
    private final  static String shopSecret = "70e7216d2a862e572d6ab321c5334485";
 
    /** 小程序登录凭证校验的url */
    public static final String WECHAT_MINI_SNS_AUTH_CODE2SESSION_URL = "https://api.weixin.qq.com/sns/jscode2session?appid={}&secret={}&js_code={}&grant_type=authorization_code";
 
    /**
     * 小程序登录凭证校验
     * @return 小程序登录校验对象
     */
    @Override
    public WeChatMiniAuthorizeVo miniLogin(Boolean isMall,RegisterThirdUserRequest request) {
        String appId = mallAppId;
        if(!isMall){
            appId = shopAppId;
        }
        // 参数校验
        if (StrUtil.isBlank(request.getCode())) {
            throw new IllegalArgumentException("小程序获取手机号code不能为空");
        }
        if (StrUtil.isBlank(request.getEncryptedData())) {
            throw new IllegalArgumentException("小程序获取手机号加密数据不能为空");
        }
        if (StrUtil.isBlank(request.getIv())) {
            throw new IllegalArgumentException("小程序获取手机号加密算法的初始向量不能为空");
        }
        if (StringUtils.isBlank(appId)) {
            throw new IllegalArgumentException("微信小程序appId未设置");
        }
 
        // 从redis获取获取用户的openid,sessionKey,unionid
        WeChatMiniAuthorizeVo response;
        String codeVo = redis.get(EnumRedisKey.WX_CODE.index + request.getCode());
        if (StrUtil.isNotBlank(codeVo)) {
            response = JSONUtil.toBean(codeVo, WeChatMiniAuthorizeVo.class);
            log.info("redis获取获取用户的openid,sessionKey,unionid:{}", response);
        } else {
            response = miniAuthCode(isMall, request.getCode());
            log.info("微信新获取用户信息by code:{}", response);
        }
        System.out.println("miniLogin小程序登陆成功 = " + JSON.toJSONString(response));
 
        String decrypt = WxUtil.decrypt(appId, request.getEncryptedData(), response.getSessionKey(), request.getIv());
        if (StrUtil.isBlank(decrypt)) {
            throw new IllegalArgumentException("微信小程序获取手机号解密失败");
        }
        JSONObject jsonObject = JSONObject.parseObject(decrypt);
        if (StrUtil.isBlank(jsonObject.getString("phoneNumber"))) {
            throw new IllegalArgumentException("微信小程序获取手机号没有有效的手机号");
        }
        response.setPhone(jsonObject.getString("phoneNumber"));
        return response;
    }
 
    /**
     * 小程序登录凭证校验
     * @return 小程序登录校验对象
     */
    @Override
    public WeChatMiniAuthorizeVo miniAuthCode(Boolean isMall,String code) {
        String appId = mallAppId,secret =mallSecret;
        if(!isMall){
            appId = shopAppId;
            secret = shopSecret;
        }
        if (StrUtil.isBlank(appId)) {
            throw new IllegalArgumentException("微信小程序appId未设置");
        } 
        if (StrUtil.isBlank(secret)) {
            throw new IllegalArgumentException("微信小程序secret未设置");
        }
        String url = StrUtil.format(WECHAT_MINI_SNS_AUTH_CODE2SESSION_URL, appId, secret, code);
        JSONObject data = restTemplateUtil.getData(url);
        if (ObjectUtil.isNull(data)) {
            throw new IllegalArgumentException("微信平台接口异常,没任何数据返回!");
        }
        if (data.containsKey("errcode") && !data.getString("errcode").equals("0")) {
            if (data.containsKey("errmsg")) { 
                throw new IllegalArgumentException("微信接口调用失败:" + data.getString("errcode") + data.getString("errmsg"));
            }
        }
        return JSONObject.parseObject(data.toJSONString(), WeChatMiniAuthorizeVo.class);
    }
 
}