change-bind-mobile-phone-number.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <!-- 更换绑定手机号 -->
  2. <template>
  3. <view class="wrap">
  4. <view class="head-msg">
  5. 更换绑定后可使用新手机号登录
  6. </view>
  7. <view class="padding-box">
  8. <view class="module">
  9. <view class="row-between">
  10. <input type="text" v-model="params.phone" class="input" placeholder="请输入新绑定的手机号" placeholder-class="placeholder" />
  11. </view>
  12. <view class="row-between">
  13. <input type="text" v-model="params.code" class="input" placeholder="请输入验证码" placeholder-class="placeholder" />
  14. <button v-if="countDown <= 0" @tap="sendSms()" type="default" class="send-code">发送验证码</button>
  15. <button v-else type="default" class="send-code">{{countDown}}秒后重发</button>
  16. <!-- 60s后重新发送 -->
  17. </view>
  18. </view>
  19. <view class="btn-box">
  20. <button type="default" @tap="modifyPhone()" class="active">确认</button>
  21. </view>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. import { generalSendSms, userCenterModifyPhone } from '../../../common/service.js';
  27. import { validatorFun } from '../../../common/utils/util.js';
  28. export default {
  29. data() {
  30. return {
  31. info: {},
  32. params: {
  33. phone: '',
  34. code: '',
  35. },
  36. countDown: 0,
  37. }
  38. },
  39. onLoad() {
  40. this.info = uni.getStorageSync('USER_INFO') || {};
  41. this.info.showPhone = this.info.phone.split('').map((n, i) => i > 2 && i < 7 ? '*' : n).join('');
  42. },
  43. methods: {
  44. modifyPhone() {
  45. const params = this.params;
  46. const errList = validatorFun(params, [
  47. ['phone', ['notNull', '请输入手机号'], ['isMobile', '请输入正确手机号']],
  48. ['code', ['notNull', '请输入验证码']],
  49. ]);
  50. if (errList.length > 0) {
  51. return uni.showToast({
  52. icon: 'none',
  53. title: errList[0].errMsg,
  54. });
  55. }
  56. userCenterModifyPhone({
  57. data: params,
  58. success: ({code, msg, data}) => {
  59. if (code == 1) {
  60. this.info.phone = params.phone;
  61. uni.setStorageSync('USER_INFO', info);
  62. this.params = {
  63. phone: '',
  64. code: '',
  65. };
  66. uni.showModal({
  67. title: '提示',
  68. content: '修改成功,请重新登录',
  69. showCancel: false,
  70. success: (res) => {
  71. if (res.confirm) {
  72. uni.removeStorageSync('session_key');
  73. uni.removeStorageSync('USER_INFO');
  74. uni.reLaunch({
  75. url: '/pages/index/index',
  76. });
  77. }
  78. }
  79. });
  80. } else {
  81. uni.showToast({
  82. icon: 'none',
  83. title: msg,
  84. });
  85. }
  86. },
  87. });
  88. },
  89. sendSms() {
  90. const params = this.params;
  91. const errList = validatorFun(params, [
  92. ['phone', ['notNull', '请输入手机号'], ['isMobile', '请输入正确手机号']],
  93. ]);
  94. if (errList.length > 0) {
  95. return uni.showToast({
  96. icon: 'none',
  97. title: errList[0].errMsg,
  98. });
  99. }
  100. generalSendSms({
  101. data: {
  102. phone: params.phone,
  103. },
  104. success: ({code, msg, data}) => {
  105. if (code == 1) {
  106. this.countDownFn();
  107. }
  108. uni.showToast({
  109. icon: 'none',
  110. title: msg,
  111. });
  112. },
  113. });
  114. },
  115. countDownFn() {
  116. let num = 60;
  117. const fn = () => {
  118. if (num == 0) return;
  119. this.countDown = --num;
  120. setTimeout(() => fn(), 1000);
  121. };
  122. fn();
  123. },
  124. }
  125. }
  126. </script>
  127. <style scoped lang="scss">
  128. @import "./change-bind-mobile-phone-number.css";
  129. </style>