|
@@ -0,0 +1,106 @@
|
|
|
+package utils;
|
|
|
+
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.ruoyi.common.utils.sign.Base64;
|
|
|
+import com.ruoyi.system.domain.wx.AppletLoginForm;
|
|
|
+import com.ruoyi.system.domain.wx.AppletSessionDTO;
|
|
|
+import com.ruoyi.system.domain.wx.WxPayV3Bean;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+
|
|
|
+/**
|
|
|
+ *小程序工具类
|
|
|
+ */
|
|
|
+public class WxCodeSessionUtil {
|
|
|
+
|
|
|
+ private static final String JSCODE_SESSION_API = "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 小程序appId
|
|
|
+ */
|
|
|
+ @Value("${wx.appId}")
|
|
|
+ private static String appId;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 小程序密钥
|
|
|
+ */
|
|
|
+ @Value("${wx.appSecret}")
|
|
|
+ private static String appSecret;
|
|
|
+ /**
|
|
|
+ * 根据code获取小程序openid和unionid
|
|
|
+ *
|
|
|
+ * @param form
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static AppletSessionDTO jscode2Session(AppletLoginForm form) {
|
|
|
+ // 获取openId和sessionKey
|
|
|
+ JSONObject result;
|
|
|
+ try {
|
|
|
+ String requestUrl = JSCODE_SESSION_API.replace("APPID", appId)
|
|
|
+ .replace("SECRET", appSecret)
|
|
|
+ .replace("JSCODE", form.getCode().trim());
|
|
|
+
|
|
|
+ String jsonStr = HttpClientUtils.doGet(requestUrl);
|
|
|
+ result = JSONObject.parseObject(jsonStr);
|
|
|
+ if (StringUtils.isEmpty(result.toString())) {
|
|
|
+ throw new RuntimeException("错误");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ throw new RuntimeException("错误");
|
|
|
+ }
|
|
|
+
|
|
|
+ int errcode = result.getIntValue("errcode");
|
|
|
+ if (errcode != 0) {
|
|
|
+ String errmsg = result.getString("errmsg");
|
|
|
+ throw new RuntimeException("获取小程序授权错误信息, " + errmsg);
|
|
|
+ }
|
|
|
+ // 获取openId,unionId,sessionKey
|
|
|
+ AppletSessionDTO appletSession = new AppletSessionDTO();
|
|
|
+ appletSession.setOpenId(result.getString("openid"));
|
|
|
+ // unionId有可能是空
|
|
|
+ appletSession.setUnionId(result.getString("unionid"));
|
|
|
+ appletSession.setSessionKey(result.getString("session_key"));
|
|
|
+
|
|
|
+ System.out.println();
|
|
|
+ String phoneNumber = getPhoneNumber(form, appletSession);
|
|
|
+ appletSession.setPhoneNumber(phoneNumber);
|
|
|
+ return appletSession;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 手机号解密
|
|
|
+ */
|
|
|
+ private static String getPhoneNumber(AppletLoginForm form, AppletSessionDTO appletSession) {
|
|
|
+
|
|
|
+ // 解密文件
|
|
|
+ String encryptedData = form.getEncryptedData();
|
|
|
+ // 解密向量
|
|
|
+ String iv = form.getIv();
|
|
|
+ // 加密秘钥
|
|
|
+ byte[] dataByte = Base64.decode(encryptedData);
|
|
|
+ // session_key
|
|
|
+ byte[] keyByte = Base64.decode(appletSession.getSessionKey());
|
|
|
+ // 偏移量
|
|
|
+ byte[] ivByte = Base64.decode(iv);
|
|
|
+ JSONObject result;
|
|
|
+ try {
|
|
|
+ result = AppletDecryptDataUtil.decryptData(keyByte, ivByte, dataByte);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ assert result != null;
|
|
|
+ String purePhoneNumber = result.getString("purePhoneNumber");
|
|
|
+ if (null == purePhoneNumber || purePhoneNumber.isEmpty()) {
|
|
|
+ throw new RuntimeException("获取手机号失败");
|
|
|
+ }
|
|
|
+ return purePhoneNumber;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|