Просмотр исходного кода

登录统一: 自动绑定 cfc 会员(按 unionid 合并 + phone 同步,移除手动绑定 UI)

liaoxg 1 месяц назад
Родитель
Сommit
4ecbba1de2

+ 23 - 0
train-backend/src/main/java/com/train/controller/auth/AuthController.java

@@ -140,6 +140,21 @@ public class AuthController {
             }
         }
 
+        // 同步 phone 到 cfc 会员(含 phone 冲突时已复用的 cfc 账号),确保跳转到 cfc 时已有 phone,不再触发手动绑定
+        if (StringUtils.hasText(phone)) {
+            try {
+                CfcUser mergedCfc = cfcUserMapper.selectById(cfcUser.getId());
+                if (mergedCfc != null && !phone.equals(mergedCfc.getPhone())) {
+                    mergedCfc.setPhone(phone);
+                    mergedCfc.setUpdatedAt(new Date());
+                    cfcUserMapper.updateById(mergedCfc);
+                    log.info("wechatPhoneLogin 同步 phone 到 cfc: cfcUserId={}, phone={}", mergedCfc.getId(), phone);
+                }
+            } catch (Exception e) {
+                log.warn("wechatPhoneLogin 同步 phone 到 cfc 失败(非阻塞): err={}", e.getMessage());
+            }
+        }
+
         String token = jwtConfig.generateToken(user.getId(), user.getRole());
         return Result.success(buildLoginResult(user, token, cfcUser, isNew(user), openid));
     }
@@ -316,6 +331,14 @@ public class AuthController {
                 new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<CfcUser>()
                         .eq(CfcUser::getOpenid, openid)
                         .last("LIMIT 1"));
+        // 未通过 openid 找到:再按 unionid 查找已有 cfc 会员(同一个微信开放平台账号在不同小程序的 unionid 相同)
+        if (cfcUser == null && StringUtils.hasText(unionid)) {
+            cfcUser = cfcUserMapper.selectOne(
+                    new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<CfcUser>()
+                            .ne(CfcUser::getOpenid, openid)
+                            .eq(CfcUser::getUnionid, unionid)
+                            .last("LIMIT 1"));
+        }
         if (cfcUser == null) {
             cfcUser = new CfcUser();
             cfcUser.setOpenid(openid);

+ 3 - 57
train-frontend/pages/mine/index.vue

@@ -9,29 +9,13 @@
       </view>
     </view>
 
-    <!-- 【新增】跨小程序合并提示卡片 -->
+    <!-- 【新增】跨小程序合并提示卡片(同一微信开放平台账号跨小程序时展示) -->
     <view v-if="crossProgramMerge" class="merge-notice">
       <text class="merge-icon">🔗</text>
       <text class="merge-text">该账号已与浠艾福账号关联</text>
       <text class="merge-sub">您在浠艾福的能量值和会员等级已同步</text>
     </view>
 
-    <!-- 【新增】未关联浠艾福时的手动绑定入口(自动合并未触发时兜底) -->
-    <view v-if="isLoggedIn && !crossProgramMerge" class="bind-card">
-      <text class="bind-title">🔗 绑定浠艾福账号</text>
-      <text class="bind-tip">输入您在浠艾福已注册的手机号,关联后能量值与会员等级即时同步</text>
-      <input
-        class="bind-input"
-        v-model="bindPhone"
-        type="number"
-        maxlength="11"
-        placeholder="请输入浠艾福已注册手机号"
-      />
-      <button class="bind-btn" :disabled="binding" @click="doBindCrossProgram">
-        {{ binding ? '绑定中...' : '绑定' }}
-      </button>
-    </view>
-
     <view class="section-card">
       <text class="section-title">🎓 完课证书</text>
       <view v-if="certLoading" class="empty-text">加载中...</view>
@@ -132,7 +116,7 @@
 </template>
 
 <script>
-import { getMyCert, getMyCoupons, redeemCoupon, verifyCert, getMyBadges, bindCrossProgram } from '@/utils/api.js'
+import { getMyCert, getMyCoupons, redeemCoupon, verifyCert, getMyBadges } from '@/utils/api.js'
 import { jumpToCfc } from '@/utils/sso.js'
 
 export default {
@@ -145,9 +129,7 @@ export default {
       verifyInput: '',
       verifyResult: null,
       verifying: false,
-      crossProgramMerge: false,
-      bindPhone: '',
-      binding: false
+      crossProgramMerge: false
     }
   },
   computed: {
@@ -212,42 +194,6 @@ export default {
     goCfc() {
       jumpToCfc()
     },
-    // 手动绑定浠艾福账号:按手机号关联 cfc 已有会员(自动合并未触发时兜底)
-    doBindCrossProgram() {
-      var self = this
-      var phone = (self.bindPhone || '').trim()
-      if (!phone) {
-        uni.showToast({ title: '请输入手机号', icon: 'none' })
-        return
-      }
-      if (!/^1[3-9]\d{9}$/.test(phone)) {
-        uni.showToast({ title: '手机号格式不正确', icon: 'none' })
-        return
-      }
-      self.binding = true
-      bindCrossProgram(phone).then(function(resp) {
-        self.binding = false
-        var data = resp && resp.data
-        if (!data) {
-          uni.showToast({ title: '绑定失败,请稍后重试', icon: 'none' })
-          return
-        }
-        // 后端返回新签发的 token 与合并标识,刷新登录态(store + storage)
-        self.$store.dispatch('login', data)
-        var merged = !!(data.crossProgramMerge)
-        uni.setStorageSync('crossProgramMerge', merged)
-        self.crossProgramMerge = merged
-        self.bindPhone = ''
-        if (merged) {
-          uni.showToast({ title: '绑定成功,浠艾福数据已同步', icon: 'success' })
-        } else {
-          uni.showToast({ title: '操作成功,可进入成长家庭查看', icon: 'success' })
-        }
-      }).catch(function(e) {
-        self.binding = false
-        uni.showToast({ title: (e && e.message) || '绑定失败,请确认手机号已在浠艾福注册', icon: 'none' })
-      })
-    },
     goBadgeWall() {
       uni.navigateTo({ url: '/pages/badge/index' })
     },