Procházet zdrojové kódy

运营人员绑定微信

Aslee před 4 roky
rodič
revize
80f18aa17a

+ 14 - 2
src/main/java/com/caimei365/user/controller/LoginApi.java

@@ -1,6 +1,7 @@
 package com.caimei365.user.controller;
 
 import com.caimei365.user.model.ResponseJson;
+import com.caimei365.user.model.po.OperationPo;
 import com.caimei365.user.model.vo.UserLoginVo;
 import com.caimei365.user.service.LoginService;
 import lombok.RequiredArgsConstructor;
@@ -127,7 +128,7 @@ public class LoginApi {
 
     /**
      * 运营人员邀请码授权登录
-     * <p>
+     *
      * spi旧接口:/club/invitationCode
      *
      * @param invitationCode 邀请码
@@ -143,8 +144,19 @@ public class LoginApi {
 
     /**
      * 运营人员绑定微信
+     *
+     * spi旧接口:/club/bindingWx
+     *
+     * @param userId    要绑定的用户Id(userID)
+     * @param mobile    手机号
+     * @param smsCode   手机验证码(verificationCode)
+     * @param smsCode   微信unionId
+     * @return OperationPo
      */
-    public void operationBindWeChat(){}
+    @PostMapping("/auth/wechat/bind")
+    public ResponseJson<UserLoginVo> operationBindWeChat(Integer userId, String mobile, String smsCode, String unionId) {
+        return loginService.operationBindWeChat(userId, mobile, smsCode, unionId);
+    }
 
 
 }

+ 2 - 0
src/main/java/com/caimei365/user/mapper/LoginMapper.java

@@ -93,4 +93,6 @@ public interface LoginMapper {
 
 
     void updateOperationByInvitation(OperationPo operationPo);
+
+    void updateOperationByUnbind(OperationPo operation);
 }

+ 11 - 0
src/main/java/com/caimei365/user/service/LoginService.java

@@ -86,4 +86,15 @@ public interface LoginService {
      * @return UserLoginVo
      */
     ResponseJson<UserLoginVo> invitationCodeLogin(String invitationCode, String nickName, String avatarUrl, String unionId);
+
+    /**
+     * 运营人员绑定微信
+     *
+     * @param userId    要绑定的用户Id(userID)
+     * @param mobile    手机号
+     * @param smsCode   手机验证码(verificationCode)
+     * @param smsCode   微信unionId
+     * @return OperationPo
+     */
+    ResponseJson<UserLoginVo> operationBindWeChat(Integer userId, String mobile, String smsCode, String unionId);
 }

+ 109 - 3
src/main/java/com/caimei365/user/service/impl/LoginServiceImpl.java

@@ -3,7 +3,9 @@ package com.caimei365.user.service.impl;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.caimei365.user.components.WeChatService;
+import com.caimei365.user.mapper.BaseMapper;
 import com.caimei365.user.mapper.LoginMapper;
+import com.caimei365.user.mapper.RegisterMapper;
 import com.caimei365.user.model.ResponseJson;
 import com.caimei365.user.model.po.OperationPo;
 import com.caimei365.user.model.vo.UserLoginVo;
@@ -36,6 +38,10 @@ public class LoginServiceImpl implements LoginService {
     private WeChatService weChatService;
     @Resource
     private LoginMapper loginMapper;
+    @Resource
+    private RegisterMapper registerMapper;
+    @Resource
+    private BaseMapper baseMapper;
 
     /**
      * 小程序邀请码过期天数
@@ -359,14 +365,14 @@ public class LoginServiceImpl implements LoginService {
         Calendar calendar = Calendar.getInstance();
         calendar.setTime(operation.getInvitationCodeTime());
         calendar.add(Calendar.DATE, validTime);
-        if ("1".equals(operation.getOperationStatus()) && date.getTime() > calendar.getTime().getTime() && operation.getDelFlag().equals("0")) {
+        if (1 == operation.getOperationStatus() && date.getTime() > calendar.getTime().getTime() && 0 == operation.getDelFlag()) {
             return ResponseJson.error("邀请码已失效", null);
         }
-        if ("2".equals(operation.getOperationStatus()) && "0".equals(operation.getDelFlag())) {
+        if (2 == operation.getOperationStatus() && 0 == operation.getDelFlag()) {
             return ResponseJson.error("邀请码已被使用", null);
         }
         // 用户身份:1机构,2供应商
-        Integer userIdentity = 3 == operation.getUserIdentity() ? 2 : 1;
+        int userIdentity = 3 == operation.getUserIdentity() ? 2 : 1;
         if (operation.getStatus() != null && 1 == userIdentity && 91 != operation.getStatus()) {
             return ResponseJson.error("您的机构已冻结", null);
         }
@@ -405,4 +411,104 @@ public class LoginServiceImpl implements LoginService {
         // 返回登录校验结果
         return logonVerify(operation);
     }
+
+    /**
+     * 运营人员绑定微信
+     *
+     * @param userId    要绑定的用户Id(userID)
+     * @param mobile    手机号
+     * @param smsCode   手机验证码(verificationCode)
+     * @param smsCode   微信unionId
+     * @return OperationPo
+     */
+    @Override
+    public ResponseJson<UserLoginVo> operationBindWeChat(Integer userId, String mobile, String smsCode, String unionId) {
+        // 参数校验
+        if (null == userId || StringUtils.isBlank(mobile) || StringUtils.isBlank(smsCode)) {
+            return ResponseJson.error("参数异常", null);
+        }
+        String redisSmsCode = (String) redisService.get("code:" + mobile);
+        if (!redisSmsCode.equals(smsCode)) {
+            return ResponseJson.error("手机验证码错误", null);
+        }
+        // 使用该手机号的运营人员
+        OperationPo operationByMobile = loginMapper.getOperationByMobile(mobile);
+        // 使用该手机号的运营人员或用户
+        UserLoginVo userByMobile = loginMapper.getLoginUserByMobileOrEmail(mobile);
+        // 要绑定的用户
+        UserLoginVo user = loginMapper.getLoginUserByUserId(userId);
+        // 判断手机号有没有成为运营人员或机构手机号
+        boolean b = userByMobile != null && !userId.equals(userByMobile.getUserId());
+        if (operationByMobile != null) {
+            userByMobile = loginMapper.getLoginUserByUserId(operationByMobile.getUserId());
+            if (b || 91 != userByMobile.getStatus()) {
+                return ResponseJson.error("该手机号已被使用", null);
+            } else {
+                // 解绑运营人员
+                unbindOperation(operationByMobile);
+            }
+        }
+        Map<Object, Object> infoData = redisService.getEntries("wxInfo:applets:" + unionId);
+        log.info("绑定微信bindingWx,获取unionId>>>>>>" + unionId);
+        String openId = (String) infoData.get("openId");
+        // 判断微信是否已经绑定
+        UserLoginVo operationByUnionId = loginMapper.getOperationUserByUnionId(unionId,"mini");
+        if (operationByUnionId != null) {
+            return ResponseJson.error("该微信已绑定,请重新刷新首页", null);
+        }
+        /*
+            组装运营人员数据 operation
+         */
+        OperationPo operation = new OperationPo();
+        // 用户Id
+        operation.setUserId(userId);
+        // 手机号
+        operation.setMobile(mobile);
+        // unionId
+        operation.setUnionId(unionId);
+        // openId
+        operation.setOpenId(openId);
+        // 绑定的机构/供应商Id,绑定的用户类型
+        if (user != null && 3 == user.getUserIdentity() ) {
+            operation.setShopId(user.getShopId());
+            operation.setUserType(2);
+        } else if (user != null) {
+            operation.setClubId(user.getClubId());
+            operation.setUserType(1);
+        }
+        // 绑定状态
+        operation.setStatus(2);
+        // 删除标识
+        operation.setDelFlag(0);
+        Date time = new Date();
+        // 添加时间
+        operation.setAddTime(time);
+        // 绑定时间
+        operation.setBindTime(time);
+        // 更新时间
+        operation.setUpdateTime(time);
+        /*
+            保存数据库 operation
+         */
+        registerMapper.insertOperation(operation);
+        return ResponseJson.success("绑定微信成功", user);
+    }
+
+
+    /**
+     * 解绑运营人员
+     *
+     * @param operation 运营人员
+     */
+    private void unbindOperation(OperationPo operation) {
+        operation.setUnionId("");
+        operation.setOpenId("");
+        operation.setNickName("");
+        operation.setBindTime(null);
+        operation.setUpdateTime(new Date());
+        operation.setStatus(1);
+        operation.setDelFlag(1);
+        // 解绑运营人员
+        loginMapper.updateOperationByUnbind(operation);
+    }
 }

+ 25 - 5
src/main/resources/mapper/LoginMapper.xml

@@ -138,15 +138,35 @@
             nickName = #{nickName},
             headimgurl = #{avatarUrl},
             status = #{status},
+            <if test="clubId != null">
+                clubId = #{clubId},
+                shopId = null,
+            </if>
+            <if test="shopId != null">
+                shopId = #{shopId},
+                clubId = null,
+            </if>
+            userType = #{userType},
+        where id = #{id}
+    </update>
+    <update id="updateOperationByUnbind">
+        update cm_mall_operation_user
+        set unionId = #{unionId},
+            openId = #{openId},
+            nickName = #{nickName},
+            bindTime = #{bindTime},
+            updateTime = #{updateTime},
+            status = #{status},
             userType = #{userType},
             <if test="clubId != null">
                 clubId = #{clubId},
-                shopId = null
+                shopId = null,
             </if>
             <if test="shopId != null">
                 shopId = #{shopId},
-                clubId = null
+                clubId = null,
             </if>
+            delFlag = #{delFlag}
         where id = #{id}
     </update>
 
@@ -180,10 +200,10 @@
             cou.delFlag,
             u.userIdentity,
             case
-                when u.userIdentity = 2 or u.userIdentity = 4 then u.clubStatus
                 when u.userIdentity = 3 then u.manufacturerStatus
-                else null end
-                as status
+                when (u.userIdentity = 2 or u.userIdentity = 4) then u.clubStatus
+                else 0
+                end as status
         FROM
             cm_mall_operation_user cou
         LEFT JOIN user u ON u.userID = cou.userID