Procházet zdrojové kódy

绑定手机号逻辑修改
创建微信用户逻辑修改

hr~ před 2 týdny
rodič
revize
744f131f80

+ 1 - 0
poyee-account/src/main/java/com/tzy/entity/AppAccount.java

@@ -102,6 +102,7 @@ public class AppAccount implements Serializable {
 
     private String salt;
 
+    private String openId;
 
     @Override
     public String toString() {

+ 6 - 0
poyee-account/src/main/java/com/tzy/mapper/PoyeeAppAccountMapper.java

@@ -14,5 +14,11 @@ public interface PoyeeAppAccountMapper {
 
     int updatePhone(@Param("account") String account, @Param("phone") String phone);
 
+    int updateAccountAndPhone(@Param("oldAccount") String oldAccount,
+                              @Param("newAccount") String newAccount,
+                              @Param("phone") String phone);
+
+    int updateOpenId(@Param("account") String account, @Param("openId") String openId);
+
     List<String> selectRoleCodesByRoleId(@Param("roleid") Integer roleid);
 }

+ 2 - 0
poyee-account/src/main/java/com/tzy/mapper/PoyeeAppBaseUserMapper.java

@@ -14,6 +14,8 @@ public interface PoyeeAppBaseUserMapper {
 
     int updateAppBaseUser(AppBaseUser appBaseUser);
 
+    int updateUsername(@Param("oldUsername") String oldUsername, @Param("newUsername") String newUsername);
+
     int updateRealNameVerify(@Param("userId") Integer userId,
                              @Param("faceVerify") Integer faceVerify,
                              @Param("idCard") String idCard);

+ 70 - 8
poyee-account/src/main/java/com/tzy/service/impl/AppAccountOidcServiceImpl.java

@@ -33,6 +33,7 @@ public class AppAccountOidcServiceImpl implements AppAccountOidcService {
     private static final String BANNED = "BANNED";
     private static final String DEFAULT_AVATAR = "https://static.public.hobbystock.cn/applet/share/share_logo2.png";
     private static final int DEFAULT_ROLE_ID = 0;
+    private static final String WECHAT_ACCOUNT_PREFIX = "WX_";
     private static final SecureRandom SECURE_RANDOM = new SecureRandom();
 
     @Resource
@@ -99,20 +100,50 @@ public class AppAccountOidcServiceImpl implements AppAccountOidcService {
     @Override
     @Transactional(rollbackFor = Exception.class)
     public EndUserDTO createWeChatUser(WeChatCreateRequest request) {
-        if (request == null) {
+        if (request == null || !StringUtils.hasText(request.getOpenId())) {
             throw new ServiceException("参数不能为空");
         }
-        return createUser(request.getOpenId(), null, null, null, request.getOpenId(),
+        String openId = request.getOpenId().trim();
+        EndUserDTO existing = loadUserByLoginId(openId);
+        if (existing != null) {
+            return existing;
+        }
+        return createUser(createWechatInternalAccount(), null, null, null, openId,
                 request.getNickName(), request.getAvatarUrl(), "WX_AUTH", null);
     }
 
     @Override
     @Transactional(rollbackFor = Exception.class)
     public EndUserDTO createWeChatPhoneUser(WeChatPhoneCreateRequest request) {
-        if (request == null) {
+        if (request == null || !StringUtils.hasText(request.getOpenId()) || !StringUtils.hasText(request.getPhone())) {
             throw new ServiceException("参数不能为空");
         }
-        return createUser(request.getPhone(), request.getPhone(), null, null, request.getOpenId(),
+        String openId = request.getOpenId().trim();
+        String phone = request.getPhone().trim();
+        EndUserDTO existingWechatUser = loadUserByLoginId(openId);
+        if (existingWechatUser != null) {
+            BindPhoneRequest bindPhoneRequest = new BindPhoneRequest();
+            bindPhoneRequest.setLoginId(openId);
+            bindPhoneRequest.setPhone(phone);
+            return bindPhone(bindPhoneRequest);
+        }
+        AppAccount phoneAccount = poyeeAppAccountMapper.selectByLoginId(phone);
+        if (phoneAccount != null) {
+            AppBaseUser baseUser = poyeeAppBaseUserMapper.selectByUsername(phoneAccount.getAccount());
+            if (baseUser != null && StringUtils.hasText(baseUser.getOpenid()) && !openId.equals(baseUser.getOpenid())) {
+                throw new ServiceException("手机号已绑定");
+            }
+            if (baseUser != null && !StringUtils.hasText(baseUser.getOpenid())) {
+                baseUser.setOpenid(openId);
+                poyeeAppBaseUserMapper.updateAppBaseUser(baseUser);
+            }
+            if (!StringUtils.hasText(phoneAccount.getOpenId())) {
+                poyeeAppAccountMapper.updateOpenId(phoneAccount.getAccount(), openId);
+                phoneAccount.setOpenId(openId);
+            }
+            return toEndUser(phoneAccount.getAccount(), phoneAccount, baseUser);
+        }
+        return createUser(phone, phone, null, null, openId,
                 request.getNickName(), request.getAvatarUrl(), "WECHAT_PHONE", null);
     }
 
@@ -165,17 +196,35 @@ public class AppAccountOidcServiceImpl implements AppAccountOidcService {
         String loginId = request.getLoginId().trim();
         String phone = request.getPhone().trim();
         AppAccount account = poyeeAppAccountMapper.selectByLoginId(loginId);
+        AppBaseUser baseUser = null;
         if (account == null) {
-            throw new ServiceException("账号不存在");
+            baseUser = poyeeAppBaseUserMapper.selectByExternalLoginId(loginId);
+            if (baseUser == null) {
+                throw new ServiceException("账号不存在");
+            }
+            account = poyeeAppAccountMapper.selectByLoginId(baseUser.getUsername());
+            if (account == null) {
+                throw new ServiceException("账号不存在");
+            }
         }
         AppAccount phoneAccount = poyeeAppAccountMapper.selectByLoginId(phone);
         if (phoneAccount != null && !isSameAccount(account, phoneAccount)) {
             throw new ServiceException("手机号已绑定");
         }
-        poyeeAppAccountMapper.updatePhone(account.getAccount(), phone);
+        String oldAccount = account.getAccount();
+        poyeeAppAccountMapper.updateAccountAndPhone(oldAccount, phone, phone);
+        if (!phone.equals(oldAccount)) {
+            poyeeAppBaseUserMapper.updateUsername(oldAccount, phone);
+        }
+        account.setAccount(phone);
         account.setPhone(phone);
-        AppBaseUser baseUser = poyeeAppBaseUserMapper.selectByUsername(account.getAccount());
-        return toEndUser(account.getAccount(), account, baseUser);
+        if (baseUser == null) {
+            baseUser = resolveBaseUserForAccount(oldAccount, account);
+        }
+        if (baseUser != null) {
+            baseUser.setUsername(phone);
+        }
+        return toEndUser(phone, account, baseUser);
     }
 
     @Override
@@ -262,6 +311,14 @@ public class AppAccountOidcServiceImpl implements AppAccountOidcService {
         return left.getAccount() != null && left.getAccount().equals(right.getAccount());
     }
 
+    private AppBaseUser resolveBaseUserForAccount(String oldAccount, AppAccount account) {
+        AppBaseUser baseUser = poyeeAppBaseUserMapper.selectByUsername(oldAccount);
+        if (baseUser == null && account != null && StringUtils.hasText(account.getAccount())) {
+            baseUser = poyeeAppBaseUserMapper.selectByUsername(account.getAccount());
+        }
+        return baseUser;
+    }
+
     private String resolvePhoneRegisterChannel(PhoneCreateRequest request) {
         String registerChannel = request.getRegisterChannel();
         if (!StringUtils.hasText(registerChannel)) {
@@ -288,6 +345,7 @@ public class AppAccountOidcServiceImpl implements AppAccountOidcService {
         account.setAccount(username);
         account.setPhone(phone);
         account.setEmail(email);
+        account.setOpenId(openId);
         account.setStatus((short) 0);
         account.setDelFlg((short) 0);
         account.setRoleid(DEFAULT_ROLE_ID);
@@ -316,6 +374,10 @@ public class AppAccountOidcServiceImpl implements AppAccountOidcService {
         return toEndUser(username, account, baseUser);
     }
 
+    private String createWechatInternalAccount() {
+        return WECHAT_ACCOUNT_PREFIX + randomSalt();
+    }
+
     private EndUserDTO toEndUser(String loginId, AppAccount account, AppBaseUser baseUser) {
         if (account == null && baseUser == null) {
             return null;

+ 22 - 1
poyee-account/src/main/resources/mapper/AppAccountMapper.xml

@@ -20,11 +20,12 @@
         <result property="logOffTime" column="log_off_time"/>
         <result property="diallingCode" column="dialling_code"/>
         <result property="salt" column="salt"/>
+        <result property="openId" column="open_id"/>
     </resultMap>
 
     <sql id="AccountColumns">
         id, appid, account, password, email, phone, remark, status, roleid,
-        create_by, create_time, update_by, cupdate_time, del_flg, log_off_time, dialling_code, salt
+        create_by, create_time, update_by, cupdate_time, del_flg, log_off_time, dialling_code, salt, open_id
     </sql>
 
     <select id="selectByLoginId" resultMap="AppAccountResult">
@@ -36,6 +37,7 @@
             account = #{loginId}
             or phone = #{loginId}
             or email = #{loginId}
+            or open_id = #{loginId}
           )
         order by id
         limit 1
@@ -60,6 +62,7 @@
             <if test="logOffTime != null">log_off_time,</if>
             <if test="diallingCode != null">dialling_code,</if>
             <if test="salt != null">salt,</if>
+            <if test="openId != null">open_id,</if>
         </trim>
         <trim prefix="values (" suffix=")" suffixOverrides=",">
             <if test="appid != null">#{appid},</if>
@@ -78,6 +81,7 @@
             <if test="logOffTime != null">#{logOffTime},</if>
             <if test="diallingCode != null">#{diallingCode},</if>
             <if test="salt != null">#{salt},</if>
+            <if test="openId != null">#{openId},</if>
         </trim>
     </insert>
 
@@ -98,6 +102,23 @@
           and del_flg = 0
     </update>
 
+    <update id="updateAccountAndPhone">
+        update app_account
+        set account = #{newAccount},
+            phone = #{phone},
+            cupdate_time = now()
+        where account = #{oldAccount}
+          and del_flg = 0
+    </update>
+
+    <update id="updateOpenId">
+        update app_account
+        set open_id = #{openId},
+            cupdate_time = now()
+        where account = #{account}
+          and del_flg = 0
+    </update>
+
     <select id="selectRoleCodesByRoleId" resultType="string">
         select code
         from app_role

+ 7 - 0
poyee-account/src/main/resources/mapper/AppBaseUserMapper.xml

@@ -195,6 +195,13 @@
         where id = #{id}
     </update>
 
+    <update id="updateUsername">
+        update app_base_user
+        set username = #{newUsername},
+            update_time = now()
+        where username = #{oldUsername}
+    </update>
+
     <update id="updateRealNameVerify">
         update app_base_user
         set face_verify = #{faceVerify},