浏览代码

fix[litemall-wx, litemall-wx-api]: 支持微信手机号码绑定

Junling Bu 7 年之前
父节点
当前提交
d7d7e6574b

+ 9 - 0
litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/dao/UserToken.java

@@ -5,9 +5,18 @@ import java.time.LocalDateTime;
 public class UserToken {
     private Integer userId;
     private String token;
+    private String sessionKey;
     private LocalDateTime expireTime;
     private LocalDateTime updateTime;
 
+    public String getSessionKey() {
+        return sessionKey;
+    }
+
+    public void setSessionKey(String sessionKey) {
+        this.sessionKey = sessionKey;
+    }
+
     public void setUserId(Integer userId) {
         this.userId = userId;
     }

+ 15 - 2
litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/service/UserTokenManager.java

@@ -15,8 +15,6 @@ public class UserTokenManager {
     private static Map<Integer, UserToken> idMap = new HashMap<>();
 
     public static Integer getUserId(String token) {
-
-
         UserToken userToken = tokenMap.get(token);
         if (userToken == null) {
             return null;
@@ -59,4 +57,19 @@ public class UserTokenManager {
 
         return userToken;
     }
+
+    public static String getSessionKey(Integer userId) {
+        UserToken userToken = idMap.get(userId);
+        if (userToken == null) {
+            return null;
+        }
+
+        if (userToken.getExpireTime().isBefore(LocalDateTime.now())) {
+            tokenMap.remove(userToken.getToken());
+            idMap.remove(userId);
+            return null;
+        }
+
+        return userToken.getSessionKey();
+    }
 }

+ 16 - 0
litemall-wx-api/src/main/java/org/linlinjava/litemall/wx/web/WxAuthController.java

@@ -2,6 +2,7 @@ package org.linlinjava.litemall.wx.web;
 
 import cn.binarywang.wx.miniapp.api.WxMaService;
 import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
+import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.linlinjava.litemall.core.notify.NotifyService;
@@ -13,6 +14,7 @@ import org.linlinjava.litemall.core.util.ResponseUtil;
 import org.linlinjava.litemall.core.util.bcrypt.BCryptPasswordEncoder;
 import org.linlinjava.litemall.db.domain.LitemallUser;
 import org.linlinjava.litemall.db.service.LitemallUserService;
+import org.linlinjava.litemall.wx.annotation.LoginUser;
 import org.linlinjava.litemall.wx.dao.UserInfo;
 import org.linlinjava.litemall.wx.dao.UserToken;
 import org.linlinjava.litemall.wx.dao.WxLoginInfo;
@@ -169,6 +171,7 @@ public class WxAuthController {
 
         // token
         UserToken userToken = UserTokenManager.generateToken(user.getId());
+        userToken.setSessionKey(sessionKey);
 
         Map<Object, Object> result = new HashMap<Object, Object>();
         result.put("token", userToken.getToken());
@@ -334,4 +337,17 @@ public class WxAuthController {
 
         return ResponseUtil.ok();
     }
+
+    @PostMapping("bindPhone")
+    public Object bindPhone(@LoginUser Integer userId, @RequestBody String body) {
+        String sessionKey = UserTokenManager.getSessionKey(userId);
+        String encryptedData = JacksonUtil.parseString(body, "encryptedData");
+        String iv = JacksonUtil.parseString(body, "iv");
+        WxMaPhoneNumberInfo phoneNumberInfo = this.wxService.getUserService().getPhoneNoInfo(sessionKey, encryptedData, iv);
+        String phone = phoneNumberInfo.getPhoneNumber();
+        LitemallUser user = userService.findById(userId);
+        user.setMobile(phone);
+        userService.update(user);
+        return ResponseUtil.ok();
+    }
 }

+ 1 - 0
litemall-wx/config/api.js

@@ -18,6 +18,7 @@ module.exports = {
  AuthRegister: WxApiRoot + 'auth/register', //账号注册
  AuthReset: WxApiRoot + 'auth/reset', //账号密码重置
  AuthRegisterCaptcha: WxApiRoot + 'auth/regCaptcha', //验证码
+ AuthBindPhone: WxApiRoot + 'auth/bindPhone', //绑定微信手机号
 
  GoodsCount: WxApiRoot + 'goods/count', //统计商品总数
  GoodsList: WxApiRoot + 'goods/list', //获得商品列表

+ 25 - 0
litemall-wx/pages/ucenter/index/index.js

@@ -107,6 +107,31 @@ Page({
    });
   };
  },
+bindPhoneNumber: function (e) {
+  if (e.detail.errMsg !== "getPhoneNumber:ok"){
+    // 拒绝授权
+    return;
+  }
+
+  if (!app.globalData.hasLogin) {
+    wx.showToast({
+      title: '绑定失败:请先登录',
+      icon: 'none',
+      duration: 2000
+    });
+    return;
+  }
+
+  util.request(api.AuthBindPhone, { iv: e.detail.iv, encryptedData: e.detail.encryptedData }, 'POST').then(function (res) {
+    if (res.errno === 0) {
+      wx.showToast({
+        title: '绑定手机号码成功',
+        icon: 'success',
+        duration: 2000
+      });
+    }
+  });
+}, 
 aboutUs: function () {
   wx.navigateTo({
     url: '/pages/about/about'

+ 1 - 1
litemall-wx/pages/ucenter/index/index.wxml

@@ -64,7 +64,7 @@
         <text class="txt">联系客服</text>
       </view>
     </button>
-    <button class="item" open-type="getPhoneNumber" bindgetphonenumber="getPhoneNumber">
+    <button class="item" open-type="getPhoneNumber" bindgetphonenumber="bindPhoneNumber">
       <view class="a">
         <image class="user-menu .icon.phone" src="/static/images/mobile.png"></image>
         <text class="txt">绑定手机号码</text>