login.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <template>
  2. <CustomNav title="登录" leftType="home" />
  3. <view class="login-container">
  4. <view class="spacer top" />
  5. <view class="login-card">
  6. <view class="title-section">
  7. <text class="page-title">欢迎使用慢病管理APP</text>
  8. </view>
  9. <view class="avatar-area">
  10. <text class="section-label">请点击下方选择您的头像</text>
  11. <!-- only available in Weixin Mini Program: chooseAvatar open-type -->
  12. <!-- 使用 tap 事件在打开前设置锁,防止重复调用导致 chooseAvatar:fail 错误 -->
  13. <button
  14. class="avatar-wrapper"
  15. :class="{ disabled: isChoosing }"
  16. :disabled="isChoosing"
  17. open-type="chooseAvatar"
  18. @tap="startChooseAvatar"
  19. @chooseavatar="onChooseAvatar"
  20. >
  21. <view class="avatar-frame">
  22. <image class="avatar-img" :src="avatarUrl" mode="aspectFill" />
  23. </view>
  24. </button>
  25. </view>
  26. <form @submit.prevent="onSubmit">
  27. <view class="input-section">
  28. <text class="section-label">请输入您的名字</text>
  29. <input class="nickname-input" type="nickname" placeholder="请点击此处进行输入" v-model="nickname" @blur="onNicknameBlur" />
  30. </view>
  31. <button form-type="submit" class="login-btn">提交</button>
  32. </form>
  33. </view>
  34. <view class="spacer bottom" />
  35. </view>
  36. </template>
  37. <script setup lang="ts">
  38. import { ref } from 'vue'
  39. import { onMounted } from 'vue'
  40. import PageTitle from '@/components/PageTitle.vue'
  41. import CustomNav from '@/components/CustomNav.vue'
  42. // 页面标题,用于导航栏显示
  43. const title = ref('登录')
  44. // 默认头像URL,用于未选择头像时的显示
  45. const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
  46. const avatarUrl = ref(defaultAvatarUrl)
  47. // 防止重复触发 chooseAvatar 的并发锁,避免多次调用导致错误
  48. const isChoosing = ref(false)
  49. const nickname = ref('')
  50. // 处理chooseAvatar事件回调(微信小程序)
  51. // 此函数在用户选择头像后被调用,用于更新头像URL和可能的昵称
  52. function onChooseAvatar(e: any) {
  53. console.log('onChooseAvatar called with event:', e) // 调试输出:记录事件对象
  54. // 在微信小程序中,e.detail 可能包含 avatarUrl(字符串)或更复杂结构
  55. try {
  56. const detail = e?.detail
  57. console.log('Event detail:', detail) // 调试输出:检查detail内容
  58. let url = ''
  59. if (!detail) {
  60. // 兼容某些平台直接传回字符串
  61. url = typeof e === 'string' ? e : ''
  62. console.log('No detail, using event as string:', url) // 调试输出:无detail时的处理
  63. } else if (typeof detail === 'string') {
  64. url = detail
  65. console.log('Detail is string:', url) // 调试输出:detail为字符串
  66. } else if (detail.avatarUrl) {
  67. url = detail.avatarUrl
  68. console.log('Using detail.avatarUrl:', url) // 调试输出:使用avatarUrl字段
  69. } else if (Array.isArray(detail) && detail[0]) {
  70. url = detail[0]
  71. console.log('Using first element of array:', url) // 调试输出:数组第一个元素
  72. }
  73. if (url) {
  74. avatarUrl.value = url
  75. console.log('Avatar URL updated to:', avatarUrl.value) // 调试输出:头像URL更新
  76. }
  77. // 如果 detail 中包含昵称(某些平台可能提供),尝试填充
  78. if (detail && detail.nickName && !nickname.value) {
  79. nickname.value = detail.nickName
  80. console.log('Nickname updated from detail:', nickname.value) // 调试输出:昵称更新
  81. }
  82. } catch (err) {
  83. console.error('Error in onChooseAvatar:', err) // 调试输出:捕获错误
  84. // ignore
  85. } finally {
  86. // 无论成功或失败,都在回调后清理并发锁
  87. isChoosing.value = false
  88. console.log('isChoosing reset to false') // 调试输出:重置锁
  89. }
  90. }
  91. // 在点击(tap)打开 chooseAvatar 之前设置锁,防止重复打开
  92. function startChooseAvatar() {
  93. console.log('startChooseAvatar called, current isChoosing:', isChoosing.value) // 调试输出:检查锁状态
  94. if (isChoosing.value) {
  95. // 如果已经在打开中,直接忽略后续点击
  96. console.log('Already choosing, ignoring') // 调试输出:忽略重复点击
  97. return
  98. }
  99. isChoosing.value = true
  100. console.log('isChoosing set to true') // 调试输出:设置锁
  101. // 为了避免因回调丢失导致一直锁住,设置一个安全超时(3s)作为兜底
  102. setTimeout(() => {
  103. isChoosing.value = false
  104. console.log('Timeout: isChoosing reset to false') // 调试输出:超时重置锁
  105. }, 3000)
  106. }
  107. // 处理昵称输入框失去焦点事件
  108. function onNicknameBlur() {
  109. console.log('onNicknameBlur called, current nickname:', nickname.value) // 调试输出:昵称输入
  110. // 微信会在 onBlur 时进行安全检测,若不通过会清空输入,这里仅作默认处理
  111. }
  112. // 处理表单提交事件
  113. function onSubmit() {
  114. console.log('onSubmit called with avatarUrl:', avatarUrl.value, 'nickname:', nickname.value) // 调试输出:提交数据
  115. // 保存用户信息到本地存储(可以改为调用后端或 uni.login + 后端换取 session)
  116. const user = {
  117. avatar: avatarUrl.value,
  118. nickname: nickname.value || '微信用户'
  119. }
  120. console.log('User object to save:', user) // 调试输出:用户对象
  121. uni.setStorageSync('user_info', user)
  122. // 如果是在小程序端,使用 navigateBack 或 switchTab 返回个人中心
  123. // 直接尝试使用 switchTab 返回个人中心(若失败则 navigateTo)
  124. try {
  125. console.log('Attempting switchTab to profile') // 调试输出:尝试切换标签页
  126. uni.switchTab({ url: '/pages/profile/profile' })
  127. } catch (e) {
  128. console.log('switchTab failed, using navigateTo:', e) // 调试输出:切换失败,使用导航
  129. uni.navigateTo({ url: '/pages/profile/profile' })
  130. }
  131. }
  132. </script>
  133. <style>
  134. .login-container {
  135. min-height: 100vh;
  136. /* 为固定在顶部的 CustomNav 留出空间(状态栏 + 导航栏 44px) */
  137. padding-top: calc(var(--status-bar-height) + 44px + 40rpx);
  138. /* 保留侧边与内部间距,使用 border-box 避免计算误差 */
  139. padding-right: 40rpx;
  140. padding-left: 40rpx;
  141. /* 底部安全区:使用项目中声明的 --window-bottom 或 fallback */
  142. padding-bottom: calc(var(--window-bottom, 0px) + 40rpx);
  143. box-sizing: border-box;
  144. background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  145. display: flex;
  146. flex-direction: column;
  147. align-items: center;
  148. justify-content: center;
  149. }
  150. /* 让卡片在可用空间内垂直居中(当内容较短时) */
  151. .spacer {
  152. width: 100%;
  153. }
  154. .spacer.top {
  155. flex: 1;
  156. }
  157. .spacer.bottom {
  158. flex: 2;
  159. }
  160. .login-card {
  161. /* remove auto margins; vertical spacing controlled by spacers */
  162. background: #fff;
  163. border-radius: 20rpx;
  164. box-shadow: 0 10rpx 30rpx rgba(0, 0, 0, 0.1);
  165. padding: 60rpx 30rpx 120rpx 30rpx;
  166. width: 100%;
  167. max-width: 600rpx;
  168. display: flex;
  169. flex-direction: column;
  170. align-items: center;
  171. }
  172. .title-section {
  173. text-align: center;
  174. margin-bottom: 60rpx;
  175. }
  176. .page-title {
  177. font-size: 48rpx;
  178. font-weight: bold;
  179. color: #333;
  180. margin-bottom: 20rpx;
  181. display: block;
  182. }
  183. .subtitle {
  184. font-size: 28rpx;
  185. color: #666;
  186. display: block;
  187. }
  188. .section-label {
  189. font-size: 32rpx;
  190. font-weight: 600;
  191. color: #333;
  192. margin-bottom: 20rpx;
  193. display: block;
  194. text-align: center;
  195. }
  196. .avatar-area {
  197. margin-bottom: 60rpx;
  198. display: flex;
  199. flex-direction: column;
  200. align-items: center;
  201. }
  202. .avatar-wrapper {
  203. width: 200rpx;
  204. height: 200rpx;
  205. border-radius: 200rpx;
  206. overflow: hidden;
  207. display: flex;
  208. align-items: center;
  209. justify-content: center;
  210. padding: 0;
  211. border: 4rpx solid #07C160;
  212. background: #fff;
  213. }
  214. .avatar-frame {
  215. width: 100%;
  216. height: 100%;
  217. border-radius: 100%;
  218. overflow: hidden;
  219. display: flex;
  220. align-items: center;
  221. justify-content: center;
  222. }
  223. .avatar-img {
  224. width: 100%;
  225. height: 100%;
  226. object-fit: cover;
  227. }
  228. .avatar-hint {
  229. font-size: 24rpx;
  230. color: #666;
  231. margin-top: 20rpx;
  232. text-align: center;
  233. }
  234. .input-section {
  235. width: 100%;
  236. margin-bottom: 60rpx;
  237. }
  238. .nickname-input {
  239. width: 100%;
  240. text-align: center;
  241. height: 100rpx;
  242. border: 2rpx solid #ddd;
  243. border-radius: 12rpx;
  244. font-size: 32rpx;
  245. background: #f9f9f9;
  246. color: #333;
  247. }
  248. .login-btn {
  249. width: 100%;
  250. height: 100rpx;
  251. background: linear-gradient(135deg, #07C160 0%, #00A854 100%);
  252. color: #fff;
  253. border-radius: 12rpx;
  254. font-size: 36rpx;
  255. font-weight: bold;
  256. text-align: center;
  257. line-height: 100rpx;
  258. border: none;
  259. box-shadow: 0 4rpx 12rpx rgba(7, 193, 96, 0.3);
  260. }
  261. .login-btn:active {
  262. background: linear-gradient(135deg, #00A854 0%, #07C160 100%);
  263. transform: scale(0.98);
  264. }
  265. </style>