updatePhone.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <template>
  2. <view class="custom-form">
  3. <view class="custom-form-item">
  4. <text>原手机号</text>
  5. <input v-model="FormData.mobile" placeholder="" type="number" disabled />
  6. </view>
  7. <view class="custom-form-item">
  8. <text>新手机号</text>
  9. <input
  10. v-model="FormData.newMobile"
  11. placeholder="请输入新手机号"
  12. type="number"
  13. maxlength="11"
  14. />
  15. </view>
  16. <view class="custom-form-item">
  17. <text>原手机号验证码</text>
  18. <input v-model="FormData.captcha" placeholder="请输入" />
  19. <uv-code
  20. :seconds="seconds"
  21. @end="end"
  22. @start="start"
  23. ref="code"
  24. @change="codeChange"
  25. ></uv-code>
  26. <uv-button @tap="getCode" color="#FFECEC" shape="circle">{{
  27. tips
  28. }}</uv-button>
  29. </view>
  30. </view>
  31. <view class="submit">
  32. <uv-button
  33. type="error"
  34. color="#EB5153"
  35. text="确认提交"
  36. shape="circle"
  37. @tap="submit"
  38. ></uv-button>
  39. </view>
  40. </template>
  41. <script setup>
  42. import { ref, onMounted } from "vue";
  43. import { updateMobile_Api } from "@/api/userInfo.js";
  44. import { smsCode } from "@/api/login.js";
  45. import $config from "@/config/global.config";
  46. const FormData = ref({
  47. captcha: "",
  48. mobile: "",
  49. newMobile: "",
  50. });
  51. const code = ref();
  52. const seconds = ref(60);
  53. const tips = ref("获取验证码");
  54. const codeChange = (text) => {
  55. tips.value = text;
  56. };
  57. const getCode = () => {
  58. if (!FormData.value.mobile) {
  59. uni.$uv.toast("未获取到原手机号");
  60. return false;
  61. }
  62. if (code.value.canGetCode) {
  63. // 模拟向后端请求验证码
  64. uni.showLoading({
  65. title: "正在获取验证码",
  66. });
  67. smsCode({
  68. mobile: FormData.value.mobile,
  69. })
  70. .then((res) => {
  71. uni.$uv.toast("验证码已发送");
  72. })
  73. .finally((e) => {
  74. uni.hideLoading();
  75. // 这里此提示会被this.start()方法中的提示覆盖
  76. code.value.start();
  77. });
  78. } else {
  79. uni.$uv.toast("倒计时结束后再发送");
  80. }
  81. };
  82. const end = () => {
  83. uni.$uv.toast("倒计时结束");
  84. };
  85. const start = () => {
  86. uni.$uv.toast("倒计时开始");
  87. };
  88. const submit = () => {
  89. if (!FormData.value.newMobile) {
  90. return uni.$uv.toast("新手机号不能为空");
  91. }
  92. if (FormData.value.newMobile.length < 11) {
  93. uni.$uv.toast("新手机号长度不能少于11位");
  94. return;
  95. }
  96. if (!$config.telRegex.test(FormData.value.newMobile)) {
  97. uni.$uv.toast("请输入正确的手机号");
  98. return;
  99. }
  100. if (!FormData.value.captcha) {
  101. return uni.$uv.toast("验证码不能为空");
  102. }
  103. uni.showLoading({
  104. title: "加载中",
  105. mask: true,
  106. });
  107. updateMobile_Api(FormData.value)
  108. .then((res) => {
  109. uni.hideLoading();
  110. uni.showToast({
  111. title: "更换成功",
  112. icon: "success",
  113. });
  114. setTimeout(() => {
  115. uni.switchTab({
  116. url: "/pages/tabtar/personalCenter",
  117. });
  118. }, 500);
  119. })
  120. .catch((err) => {
  121. // uni.hideLoading();
  122. });
  123. };
  124. onMounted(() => {
  125. let user = uni.getStorageSync("personal") || {};
  126. if (user && user.mobile) {
  127. FormData.value.mobile = user.mobile;
  128. }
  129. });
  130. </script>
  131. <style lang="scss" scoped>
  132. .custom-form {
  133. padding: 20rpx 54rpx;
  134. }
  135. .custom-form {
  136. margin-bottom: 60rpx;
  137. .custom-form-item {
  138. display: flex;
  139. align-items: center;
  140. padding: 30rpx 0;
  141. border-bottom: 1rpx solid #f0f0f0;
  142. text {
  143. margin-right: 24rpx;
  144. font-size: 28rpx;
  145. font-family: PingFang SC, PingFang SC-Regular;
  146. font-weight: normal;
  147. color: #1a1a1a;
  148. flex-shrink: 0;
  149. }
  150. :deep(.input-placeholder) {
  151. font-size: 28rpx;
  152. font-family: PingFang SC, PingFang SC-Regular;
  153. font-weight: normal;
  154. color: #d9d9d9;
  155. }
  156. }
  157. :deep(.uv-button) {
  158. height: 55rpx;
  159. margin-left: auto;
  160. color: #ea3527 !important;
  161. font-size: 26rpx;
  162. font-family: PingFang SC, PingFang SC-Bold;
  163. font-weight: bold;
  164. }
  165. }
  166. .submit {
  167. margin: 0 60rpx;
  168. :deep(.uv-button) {
  169. height: 90rpx;
  170. font-size: 28rpx;
  171. font-family: PingFang SC, PingFang SC-Regular;
  172. font-weight: normal;
  173. text-align: center;
  174. color: #ffffff;
  175. }
  176. }
  177. </style>