login.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <template>
  2. <CustomNav title="登录" leftType="home" />
  3. <view class="login-container">
  4. <view class="avatar-area">
  5. <!-- only available in Weixin Mini Program: chooseAvatar open-type -->
  6. <!-- 使用 tap 事件在打开前设置锁,防止重复调用导致 chooseAvatar:fail 错误 -->
  7. <button
  8. class="avatar-wrapper"
  9. :class="{ disabled: isChoosing }"
  10. :disabled="isChoosing"
  11. open-type="chooseAvatar"
  12. @tap="startChooseAvatar"
  13. @chooseavatar="onChooseAvatar"
  14. >
  15. <view class="avatar-frame">
  16. <image class="avatar-img" :src="avatarUrl" mode="aspectFill" />
  17. </view>
  18. </button>
  19. </view>
  20. <form @submit.prevent="onSubmit">
  21. <input class="nickname-input" type="nickname" placeholder="请输入昵称" v-model="nickname" @blur="onNicknameBlur" />
  22. <button form-type="submit" class="login-btn">使用微信登录并完善信息</button>
  23. </form>
  24. </view>
  25. </template>
  26. <script setup lang="ts">
  27. import { ref } from 'vue'
  28. import { onMounted } from 'vue'
  29. import PageTitle from '@/components/PageTitle.vue'
  30. import CustomNav from '@/components/CustomNav.vue'
  31. const title = ref('登录')
  32. const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
  33. const avatarUrl = ref(defaultAvatarUrl)
  34. // 防止重复触发 chooseAvatar 的并发锁
  35. const isChoosing = ref(false)
  36. const nickname = ref('')
  37. // 处理chooseAvatar事件回调(微信小程序)
  38. function onChooseAvatar(e: any) {
  39. // 在微信小程序中,e.detail 可能包含 avatarUrl(字符串)或更复杂结构
  40. try {
  41. const detail = e?.detail
  42. let url = ''
  43. if (!detail) {
  44. // 兼容某些平台直接传回字符串
  45. url = typeof e === 'string' ? e : ''
  46. } else if (typeof detail === 'string') {
  47. url = detail
  48. } else if (detail.avatarUrl) {
  49. url = detail.avatarUrl
  50. } else if (Array.isArray(detail) && detail[0]) {
  51. url = detail[0]
  52. }
  53. if (url) {
  54. avatarUrl.value = url
  55. }
  56. // 如果 detail 中包含昵称(某些平台可能提供),尝试填充
  57. if (detail && detail.nickName && !nickname.value) {
  58. nickname.value = detail.nickName
  59. }
  60. } catch (err) {
  61. // ignore
  62. } finally {
  63. // 无论成功或失败,都在回调后清理并发锁
  64. isChoosing.value = false
  65. }
  66. }
  67. // 在点击(tap)打开 chooseAvatar 之前设置锁,防止重复打开
  68. function startChooseAvatar() {
  69. if (isChoosing.value) {
  70. // 如果已经在打开中,直接忽略后续点击
  71. return
  72. }
  73. isChoosing.value = true
  74. // 为了避免因回调丢失导致一直锁住,设置一个安全超时(3s)作为兜底
  75. setTimeout(() => {
  76. isChoosing.value = false
  77. }, 3000)
  78. }
  79. function onNicknameBlur() {
  80. // 微信会在 onBlur 时进行安全检测,若不通过会清空输入,这里仅作默认处理
  81. }
  82. function onSubmit() {
  83. // 保存用户信息到本地存储(可以改为调用后端或 uni.login + 后端换取 session)
  84. const user = {
  85. avatar: avatarUrl.value,
  86. nickname: nickname.value || '微信用户'
  87. }
  88. uni.setStorageSync('user_info', user)
  89. // 如果是在小程序端,使用 navigateBack 或 switchTab 返回个人中心
  90. // 直接尝试使用 switchTab 返回个人中心(若失败则 navigateTo)
  91. try {
  92. uni.switchTab({ url: '/pages/profile/profile' })
  93. } catch (e) {
  94. uni.navigateTo({ url: '/pages/profile/profile' })
  95. }
  96. }
  97. </script>
  98. <style>
  99. .login-container {
  100. padding: 40rpx;
  101. padding-top: calc(var(--status-bar-height) + 44px + 40rpx);
  102. display: flex;
  103. flex-direction: column;
  104. align-items: center;
  105. }
  106. .avatar-area {
  107. margin-top: 80rpx;
  108. margin-bottom: 40rpx;
  109. }
  110. .avatar-wrapper {
  111. width: 180rpx;
  112. height: 180rpx;
  113. border-radius: 180rpx;
  114. overflow: hidden;
  115. display: flex;
  116. align-items: center;
  117. justify-content: center;
  118. padding-left: 0px;
  119. padding-right: 0px;
  120. }
  121. .avatar-frame {
  122. width: 100%;
  123. height: 100%;
  124. border-radius: 90rpx;
  125. overflow: hidden;
  126. display: flex;
  127. align-items: center;
  128. justify-content: center;
  129. }
  130. .avatar-img {
  131. width: 100%;
  132. height: 100%;
  133. object-fit: cover;
  134. }
  135. .nickname-input {
  136. width: 100%;
  137. height: 80rpx;
  138. border: 1rpx solid #ddd;
  139. border-radius: 8rpx;
  140. padding: 0 20rpx;
  141. margin-bottom: 30rpx;
  142. }
  143. .login-btn {
  144. width: 100%;
  145. height: 80rpx;
  146. background-color: #007aff;
  147. color: #fff;
  148. border-radius: 8rpx;
  149. line-height: 80rpx;
  150. text-align: center;
  151. }
  152. </style>