change-mobile.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <!-- 绑定/更换手机号 changeMobile -->
  2. <template>
  3. <view>
  4. <!-- 标题栏 -->
  5. <view class="head-box ss-m-b-60">
  6. <view class="head-title ss-m-b-20">
  7. {{ userInfo.mobile ? t('account.change_phone_number') : t('account.bind_phone_number') }}
  8. </view>
  9. <view class="head-subtitle">{{ t('account.security_notice') }}</view>
  10. </view>
  11. <!-- 表单项 -->
  12. <uni-forms ref="changeMobileRef" v-model="state.model" :rules="state.rules" validateTrigger="bind"
  13. labelWidth="140" labelAlign="center" v-if="!step">
  14. <uni-forms-item name="mobile" :label="t('account.old_phone_number')">
  15. <uni-easyinput :placeholder="t('account.enter_old_phone_number')" v-model="state.model.oldMobile" :inputBorder="false" type="number">
  16. <template v-slot:right>
  17. <button class="ss-reset-button code-btn-start" :disabled="state.isMobileEnd"
  18. :class="{ 'code-btn-end': state.isMobileEnd }"
  19. @tap="getSmsCode('changeMobileOld', state.model.oldMobile)">
  20. {{ getSmsTimer('changeMobileOld') }}
  21. </button>
  22. </template>
  23. </uni-easyinput>
  24. </uni-forms-item>
  25. <uni-forms-item name="code" :label="t('account.verification_code')">
  26. <uni-easyinput :placeholder="t('account.enter_verification_code')" v-model="state.model.oldCode" :inputBorder="false" type="number"
  27. maxlength="4">
  28. </uni-easyinput>
  29. </uni-forms-item>
  30. <button class="ss-reset-button login-btn-start" @tap="nextStep">
  31. {{t('common.next')}}
  32. </button>
  33. </uni-forms>
  34. <uni-forms ref="changeMobileRef" v-model="state.model" :rules="state.rules" validateTrigger="bind"
  35. labelWidth="140" labelAlign="center" v-else>
  36. <uni-forms-item name="mobile" :label="t('account.new_phone_number')">
  37. <uni-easyinput :placeholder="t('account.enter_new_phone_number')" v-model="state.model.mobile" :inputBorder="false" type="number">
  38. <template v-slot:right>
  39. <button class="ss-reset-button code-btn-start" :disabled="state.isMobileEnd"
  40. :class="{ 'code-btn-end': state.isMobileEnd }"
  41. @tap="getSmsCode('changeMobileNew', state.model.mobile)">
  42. {{ getSmsTimer('changeMobileNew') }}
  43. </button>
  44. </template>
  45. </uni-easyinput>
  46. </uni-forms-item>
  47. <uni-forms-item name="code" :label="t('account.verification_code')">
  48. <uni-easyinput :placeholder="t('account.enter_verification_code')" v-model="state.model.code" :inputBorder="false" type="number"
  49. maxlength="4">
  50. </uni-easyinput>
  51. </uni-forms-item>
  52. <button class="ss-reset-button login-btn-start" @tap="changeMobileSubmit">
  53. {{ t('common.confirm') }}
  54. </button>
  55. </uni-forms>
  56. <!-- 微信独有:读取手机号 -->
  57. <!-- <button
  58. v-if="'WechatMiniProgram' === sheep.$platform.name"
  59. class="ss-reset-button type-btn"
  60. open-type="getPhoneNumber"
  61. @getphonenumber="getPhoneNumber"
  62. >
  63. 使用微信手机号
  64. </button> -->
  65. </view>
  66. </template>
  67. <script setup>
  68. import {
  69. computed,
  70. ref,
  71. reactive,
  72. unref
  73. } from 'vue';
  74. import sheep from '@/sheep';
  75. import {
  76. code,
  77. mobile
  78. } from '@/sheep/validate/form';
  79. import {
  80. closeAuthModal,
  81. getSmsCode,
  82. getSmsTimer
  83. } from '@/sheep/hooks/useModal';
  84. import UserApi from '@/sheep/api/member/user';
  85. import AuthUtil from '@/sheep/api/member/auth';
  86. import { t } from '@/locale'
  87. const step = ref(0);
  88. const changeMobileRef = ref(null);
  89. const userInfo = computed(() => sheep.$store('user').userInfo);
  90. // 数据
  91. const state = reactive({
  92. isMobileEnd: false, // 手机号输入完毕
  93. model: {
  94. oldMobile:'',
  95. oldCode:'',
  96. mobile: '', // 手机号
  97. code: '', // 验证码
  98. },
  99. rules: {
  100. code,
  101. mobile,
  102. },
  103. });
  104. async function nextStep(){
  105. const validate = await unref(changeMobileRef)
  106. .validate()
  107. .catch((error) => {
  108. console.log('error: ', error);
  109. });
  110. if (!validate) {
  111. return;
  112. }
  113. const {
  114. code,
  115. data
  116. } = await AuthUtil.validatePhone(state.model.oldMobile , state.model.oldCode , 2);
  117. console.log('下一步的请求校验',data);
  118. if(data){
  119. step.value = 1;
  120. }
  121. }
  122. // 绑定手机号
  123. async function changeMobileSubmit() {
  124. const validate = await unref(changeMobileRef)
  125. .validate()
  126. .catch((error) => {
  127. console.log('error: ', error);
  128. });
  129. if (!validate) {
  130. return;
  131. }
  132. // 提交更新请求
  133. const {
  134. code
  135. } = await UserApi.updateUserMobile({'mobile':state.model.mobile,'code': state.model.code});
  136. if (code !== 0) {
  137. return;
  138. }
  139. uni.showToast({
  140. icon: 'success',
  141. title: t('account.login_again'),
  142. });
  143. // 成功后,只需要1秒后推出登录关闭弹窗
  144. setTimeout(function(){
  145. sheep.$store('user').logout();
  146. closeAuthModal();
  147. sheep.$router.go('/pages/index/user')
  148. },1000)
  149. }
  150. // 使用微信手机号
  151. async function getPhoneNumber(e) {
  152. if (e.detail.errMsg !== 'getPhoneNumber:ok') {
  153. return;
  154. }
  155. const result = await sheep.$platform.useProvider().bindUserPhoneNumber(e.detail);
  156. if (result) {
  157. sheep.$store('user').getInfo();
  158. closeAuthModal();
  159. }
  160. }
  161. </script>
  162. <style lang="scss" scoped>
  163. @import '../index.scss';
  164. .login-btn-start {
  165. width: 100%;
  166. height: 70rpx;
  167. line-height: normal;
  168. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  169. border-radius: 28rpx;
  170. font-size: 26rpx;
  171. font-weight: 500;
  172. color: #fff;
  173. }
  174. </style>