change-mobile.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 ? '更换手机号' : '绑定手机号' }}
  8. </view>
  9. <view class="head-subtitle">为了您的账号安全,请使用本人手机号码</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="原手机号">
  15. <uni-easyinput placeholder="请输入原手机号" 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="验证码">
  26. <uni-easyinput placeholder="请输入验证码" 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. 下一步
  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="新手机号">
  37. <uni-easyinput placeholder="请输入新手机号" 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="验证码">
  48. <uni-easyinput placeholder="请输入验证码" 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. 确定
  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. const step = ref(0);
  87. const changeMobileRef = ref(null);
  88. const userInfo = computed(() => sheep.$store('user').userInfo);
  89. // 数据
  90. const state = reactive({
  91. isMobileEnd: false, // 手机号输入完毕
  92. model: {
  93. oldMobile:'',
  94. oldCode:'',
  95. mobile: '', // 手机号
  96. code: '', // 验证码
  97. },
  98. rules: {
  99. code,
  100. mobile,
  101. },
  102. });
  103. async function nextStep(){
  104. const validate = await unref(changeMobileRef)
  105. .validate()
  106. .catch((error) => {
  107. console.log('error: ', error);
  108. });
  109. if (!validate) {
  110. return;
  111. }
  112. const {
  113. code,
  114. data
  115. } = await AuthUtil.validatePhone(state.model.oldMobile , state.model.oldCode , 2);
  116. console.log('下一步的请求校验',data);
  117. if(data){
  118. step.value = 1;
  119. }
  120. }
  121. // 绑定手机号
  122. async function changeMobileSubmit() {
  123. const validate = await unref(changeMobileRef)
  124. .validate()
  125. .catch((error) => {
  126. console.log('error: ', error);
  127. });
  128. if (!validate) {
  129. return;
  130. }
  131. // 提交更新请求
  132. const {
  133. code
  134. } = await UserApi.updateUserMobile({'mobile':state.model.mobile,'code': state.model.code});
  135. if (code !== 0) {
  136. return;
  137. }
  138. sheep.$store('user').getInfo();
  139. closeAuthModal();
  140. }
  141. // 使用微信手机号
  142. async function getPhoneNumber(e) {
  143. if (e.detail.errMsg !== 'getPhoneNumber:ok') {
  144. return;
  145. }
  146. const result = await sheep.$platform.useProvider().bindUserPhoneNumber(e.detail);
  147. if (result) {
  148. sheep.$store('user').getInfo();
  149. closeAuthModal();
  150. }
  151. }
  152. </script>
  153. <style lang="scss" scoped>
  154. @import '../index.scss';
  155. .login-btn-start {
  156. width: 100%;
  157. height: 70rpx;
  158. line-height: normal;
  159. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  160. border-radius: 28rpx;
  161. font-size: 26rpx;
  162. font-weight: 500;
  163. color: #fff;
  164. }
  165. </style>