WeiXinAppletUserParser.java 5.24 KB
package com.topdraw.weixin.applet;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.topdraw.business.process.domian.weixin.WeiXinUserBean;
import com.topdraw.security.AESUtil;
import com.topdraw.utils.StringUtils;
import com.topdraw.weixin.beans.DefaultWeiXinBeanDefinition;
import com.topdraw.weixin.beans.WeiXinUserParser;
import com.topdraw.weixin.beans.config.WeiXinAppListConfig;
import com.topdraw.weixin.util.WeChatConstants;
import com.topdraw.weixin.util.WeiXinRequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;
import java.util.Optional;

@Component
public class WeiXinAppletUserParser implements WeiXinUserParser {

    @Autowired
    private WeiXinAppListConfig weiXinAppListConfig;

    @Autowired
    private WeiXinRequestUtil weixinRequestUtil;

    @Value("${uc.service.platform:}")
    private String platform;

    @Value("${key:}")
    private String key;

    @Value("${uc.app.subAppId:wx05f35931270014be}")
    private String subAppId;

    @Value("${uc.app.h5AppId:wxca962918dfeed88c}")
    private String h5AppId;

    @Value("${uc.app.appletAppid:wxc57d42de3d351cec}")
    private String appletAppid;

    @Value("${file.upload:upload}")
    private String filePath;

    @Override
    public DefaultWeiXinBeanDefinition parse(WeiXinUserBean resources) {
        String key = this.key;
        Map<String, String> weixinInfoMap = null;
        String appId = resources.getWxAppid();
        String code = resources.getWxCode();
        String userInfo = null;

        if (StringUtils.isNotBlank(appId)) {

            String decrypt = AESUtil.decrypt(appId, key);

            if (decrypt != null) {
                appId = decrypt.substring(16);
            }

            weixinInfoMap = getWeixinInfoByAppid(appId);
        }

        if (StringUtils.isNotBlank(code)) {
            String decrypt = AESUtil.decrypt(code, key);
            if (decrypt != null) {
                code = decrypt.substring(16);
            }
        }

        if (StringUtils.isNotBlank(userInfo)) {
            String decrypt = AESUtil.decrypt(userInfo, key);
            if (decrypt != null) {
                userInfo = decrypt.substring(16);
            }
        }

        return this.generateWeiXinBeanDefinition(code,userInfo,weixinInfoMap);
    }

    /**
     *
     * @param code
     * @param userInfo
     * @param weixinInfoMap
     * @return
     */
    private DefaultWeiXinBeanDefinition generateWeiXinBeanDefinition(String code, String userInfo, Map<String, String> weixinInfoMap) {
        String appId = weixinInfoMap.get("appid");
        String secret = weixinInfoMap.get("secret");
        JSONObject userInfoWxJo = null;

        // 链接微信服务器
        /*ResponseEntity<String> responseEntity1 = null;*//*restTemplate.exchange(WeChatConstants.CODE2SESSION.replace("APPID", appId)
                        .replace("SECRET", secret).replace("JSCODE", code),
                HttpMethod.GET, null, String.class);*//*

        String entityBody1 = responseEntity1.getBody();*/

        String url = WeChatConstants.CODE2SESSION.replace("APPID", appId)
                .replace("SECRET", secret).replace("JSCODE", code);
        String entityBody = HttpUtil.get(url);

        JSONObject jsonObject = JSONObject.parseObject(entityBody);

        String openId = jsonObject.getString("openid");
        String unionId = jsonObject.getString("unionid");

        if (StringUtils.isEmpty(unionId)) {
            try {
                userInfoWxJo = this.weixinRequestUtil.getUserInfo(weixinInfoMap, openId,code);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if (StringUtils.isNotBlank(userInfo)) {

            JSONObject userInfoJo = JSONObject.parseObject(userInfo);
            String encryptedData = userInfoJo.getString("encryptedData");
            String iv = userInfoJo.getString("iv");
            String key = jsonObject.getString("session_key");
            String userInfoWeixin = AESUtil.decryptJsUserInfo(encryptedData, iv, key);
            if (StringUtils.isNotEmpty(userInfoWeixin))
                userInfoWxJo = JSONObject.parseObject(userInfoWeixin);
        }

        DefaultWeiXinBeanDefinition weiXinBeanDefinition =
                new DefaultWeiXinBeanDefinition(appId,code,unionId,openId,userInfoWxJo);
        return weiXinBeanDefinition;
    }

    /**
     *
     * @param appid
     * @return
     */
    private Map<String, String> getWeixinInfoByAppid(String appid) {
        if (StringUtils.isBlank(appid)) {
            throw new RuntimeException("wxAppid can not be null");
        }
        List<Map<String, String>> list = this.weiXinAppListConfig.getList();
        Optional<Map<String, String>> weixinInfoOptional = list.stream().filter(o -> o.get("appid").equals(appid)).findFirst();
        if (!weixinInfoOptional.isPresent()) {
            throw new RuntimeException("wxAppid error, appid is : " + appid);
        }
        return weixinInfoOptional.get();
    }

}