phone-binding.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <template>
  2. <view class="phone-binding-page">
  3. <!-- 自定义导航栏 -->
  4. <view class="custom-navbar">
  5. <view class="navbar-back" @click="goBack">
  6. <text class="back-icon">←</text>
  7. </view>
  8. <view class="navbar-title">绑定手机号</view>
  9. <view class="navbar-placeholder"></view>
  10. </view>
  11. <!-- 提示信息 -->
  12. <view class="tip-card">
  13. <text class="tip-icon">📱</text>
  14. <view class="tip-content">
  15. <text class="tip-title">手机号验证</text>
  16. <text class="tip-text">绑定手机号后可用于登录和找回密码</text>
  17. </view>
  18. </view>
  19. <!-- 当前绑定信息 -->
  20. <view class="current-phone" v-if="currentPhone">
  21. <view class="current-label">当前绑定手机号</view>
  22. <view class="current-value">{{ maskPhone(currentPhone) }}</view>
  23. </view>
  24. <!-- 表单区域 -->
  25. <view class="form-container">
  26. <view class="form-item">
  27. <text class="form-label">新手机号</text>
  28. <input class="form-input" v-model="formData.phone" placeholder="请输入新手机号" type="number" maxlength="11" />
  29. </view>
  30. <view class="form-item">
  31. <text class="form-label">验证码</text>
  32. <view class="code-input-group">
  33. <input class="form-input code-input" v-model="formData.code" placeholder="请输入验证码" maxlength="6" type="number" />
  34. <button class="send-code-btn" :disabled="countdown > 0 || !canSendCode" @click="sendCode">
  35. {{ countdown > 0 ? `${countdown}秒后重试` : '发送验证码' }}
  36. </button>
  37. </view>
  38. </view>
  39. </view>
  40. <!-- 提交按钮 -->
  41. <view class="submit-container">
  42. <button class="submit-btn" :disabled="!canSubmit" @click="handleSubmit">
  43. {{ submitting ? '绑定中...' : currentPhone ? '更换绑定' : '立即绑定' }}
  44. </button>
  45. </view>
  46. <!-- 说明 -->
  47. <view class="notice">
  48. <text class="notice-title">温馨提示</text>
  49. <text class="notice-item">• 手机号仅用于登录和安全验证</text>
  50. <text class="notice-item">• 一个手机号只能绑定一个账号</text>
  51. <text class="notice-item">• 如需更换,请先解绑原手机号</text>
  52. </view>
  53. </view>
  54. </template>
  55. <script>
  56. export default {
  57. data() {
  58. return {
  59. currentPhone: '',
  60. formData: {
  61. phone: '',
  62. code: ''
  63. },
  64. countdown: 0,
  65. submitting: false,
  66. timer: null
  67. }
  68. },
  69. computed: {
  70. canSendCode() {
  71. return this.formData.phone && /^1[3-9]\d{9}$/.test(this.formData.phone)
  72. },
  73. canSubmit() {
  74. return this.canSendCode && this.formData.code && !this.submitting
  75. }
  76. },
  77. onLoad() {
  78. this.loadCurrentPhone()
  79. },
  80. onUnload() {
  81. if (this.timer) {
  82. clearInterval(this.timer)
  83. }
  84. },
  85. methods: {
  86. // 加载当前绑定的手机号
  87. loadCurrentPhone() {
  88. const userInfo = uni.getStorageSync('userInfo')
  89. if (userInfo && userInfo.phone) {
  90. this.currentPhone = userInfo.phone
  91. }
  92. },
  93. // 手机号脱敏
  94. maskPhone(phone) {
  95. if (!phone || phone.length !== 11) return phone
  96. return phone.substring(0, 3) + '****' + phone.substring(7)
  97. },
  98. // 发送验证码
  99. async sendCode() {
  100. if (!this.canSendCode || this.countdown > 0) return
  101. try {
  102. // TODO: 调用发送验证码API
  103. uni.showToast({
  104. title: '验证码已发送',
  105. icon: 'success'
  106. })
  107. // 开始倒计时
  108. this.countdown = 60
  109. this.timer = setInterval(() => {
  110. this.countdown--
  111. if (this.countdown <= 0) {
  112. clearInterval(this.timer)
  113. }
  114. }, 1000)
  115. } catch (error) {
  116. console.error('发送验证码失败:', error)
  117. uni.showToast({
  118. title: '发送失败,请重试',
  119. icon: 'none'
  120. })
  121. }
  122. },
  123. // 提交绑定
  124. async handleSubmit() {
  125. if (!this.canSubmit) return
  126. // 验证手机号格式
  127. if (!/^1[3-9]\d{9}$/.test(this.formData.phone)) {
  128. uni.showToast({
  129. title: '请输入正确的手机号',
  130. icon: 'none'
  131. })
  132. return
  133. }
  134. // 验证验证码
  135. if (!this.formData.code || this.formData.code.length !== 6) {
  136. uni.showToast({
  137. title: '请输入6位验证码',
  138. icon: 'none'
  139. })
  140. return
  141. }
  142. this.submitting = true
  143. try {
  144. // TODO: 调用绑定手机号API
  145. await new Promise(resolve => setTimeout(resolve, 2000))
  146. // 更新本地存储
  147. const userInfo = uni.getStorageSync('userInfo')
  148. if (userInfo) {
  149. userInfo.phone = this.formData.phone
  150. uni.setStorageSync('userInfo', userInfo)
  151. }
  152. uni.showToast({
  153. title: '绑定成功',
  154. icon: 'success'
  155. })
  156. setTimeout(() => {
  157. uni.navigateBack()
  158. }, 1500)
  159. } catch (error) {
  160. console.error('绑定失败:', error)
  161. uni.showToast({
  162. title: '绑定失败,请重试',
  163. icon: 'none'
  164. })
  165. } finally {
  166. this.submitting = false
  167. }
  168. },
  169. // 返回
  170. goBack() {
  171. uni.navigateBack()
  172. }
  173. }
  174. }
  175. </script>
  176. <style scoped>
  177. .phone-binding-page {
  178. min-height: 100vh;
  179. background: #F5F5F5;
  180. }
  181. /* 自定义导航栏 */
  182. .custom-navbar {
  183. height: 88rpx;
  184. background: #E91E63;
  185. display: flex;
  186. align-items: center;
  187. justify-content: space-between;
  188. padding: 0 20rpx;
  189. color: white;
  190. }
  191. .navbar-back {
  192. padding: 10rpx 20rpx;
  193. }
  194. .back-icon {
  195. font-size: 40rpx;
  196. color: white;
  197. font-weight: bold;
  198. }
  199. .navbar-title {
  200. font-size: 32rpx;
  201. font-weight: bold;
  202. position: absolute;
  203. left: 50%;
  204. transform: translateX(-50%);
  205. }
  206. .navbar-placeholder {
  207. width: 80rpx;
  208. }
  209. /* 提示卡片 */
  210. .tip-card {
  211. margin: 24rpx;
  212. padding: 30rpx;
  213. background: linear-gradient(135deg, #E3F2FD 0%, #BBDEFB 100%);
  214. border-radius: 16rpx;
  215. display: flex;
  216. align-items: center;
  217. gap: 20rpx;
  218. border-left: 6rpx solid #2196F3;
  219. }
  220. .tip-icon {
  221. font-size: 48rpx;
  222. }
  223. .tip-content {
  224. flex: 1;
  225. display: flex;
  226. flex-direction: column;
  227. gap: 8rpx;
  228. }
  229. .tip-title {
  230. font-size: 28rpx;
  231. font-weight: bold;
  232. color: #1565C0;
  233. }
  234. .tip-text {
  235. font-size: 24rpx;
  236. color: #1976D2;
  237. line-height: 1.5;
  238. }
  239. /* 当前绑定信息 */
  240. .current-phone {
  241. margin: 24rpx;
  242. padding: 32rpx;
  243. background: white;
  244. border-radius: 16rpx;
  245. text-align: center;
  246. }
  247. .current-label {
  248. font-size: 24rpx;
  249. color: #999;
  250. margin-bottom: 16rpx;
  251. }
  252. .current-value {
  253. font-size: 36rpx;
  254. font-weight: bold;
  255. color: #333;
  256. }
  257. /* 表单区域 */
  258. .form-container {
  259. margin: 24rpx;
  260. }
  261. .form-item {
  262. background: white;
  263. padding: 32rpx;
  264. margin-bottom: 24rpx;
  265. border-radius: 16rpx;
  266. }
  267. .form-label {
  268. display: block;
  269. font-size: 28rpx;
  270. color: #333;
  271. margin-bottom: 20rpx;
  272. font-weight: bold;
  273. }
  274. .form-input {
  275. width: 92%;
  276. height: 80rpx;
  277. padding: 0 24rpx;
  278. background: #F5F5F5;
  279. border-radius: 12rpx;
  280. font-size: 28rpx;
  281. border: 2rpx solid #E0E0E0;
  282. }
  283. .code-input-group {
  284. display: flex;
  285. gap: 16rpx;
  286. }
  287. .code-input {
  288. flex: 1;
  289. }
  290. .send-code-btn {
  291. width: 200rpx;
  292. height: 80rpx;
  293. line-height: 80rpx;
  294. background: #E91E63;
  295. color: white;
  296. border: none;
  297. border-radius: 12rpx;
  298. font-size: 26rpx;
  299. padding: 0;
  300. }
  301. .send-code-btn[disabled] {
  302. background: #BDBDBD;
  303. }
  304. /* 提交按钮 */
  305. .submit-container {
  306. padding: 40rpx 24rpx;
  307. }
  308. .submit-btn {
  309. width: 100%;
  310. height: 100rpx;
  311. background: linear-gradient(135deg, #E91E63 0%, #C2185B 100%);
  312. color: white;
  313. border-radius: 50rpx;
  314. font-size: 32rpx;
  315. font-weight: bold;
  316. border: none;
  317. box-shadow: 0 8rpx 24rpx rgba(233, 30, 99, 0.3);
  318. }
  319. .submit-btn[disabled] {
  320. background: #BDBDBD;
  321. box-shadow: none;
  322. }
  323. /* 说明 */
  324. .notice {
  325. margin: 0 24rpx 40rpx;
  326. padding: 32rpx;
  327. background: white;
  328. border-radius: 16rpx;
  329. }
  330. .notice-title {
  331. font-size: 28rpx;
  332. font-weight: bold;
  333. color: #333;
  334. margin-bottom: 20rpx;
  335. display: block;
  336. }
  337. .notice-item {
  338. display: block;
  339. font-size: 24rpx;
  340. color: #666;
  341. line-height: 2;
  342. }
  343. </style>