editPhone.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <template>
  2. <view class="u-page">
  3. <u--form labelPosition="left" :model="registerData" ref="registerForm">
  4. <u-form-item label="新电话" prop="phone" borderBottom labelWidth="120">
  5. <u--input v-model="registerData.phone" border="none" placeholder="电话"></u--input>
  6. </u-form-item>
  7. <u-form-item label="验证码" prop="code" labelWidth="80" borderBottom>
  8. <u--input v-model="registerData.verifyCode" border="none" placeholder="请填写验证码"></u--input>
  9. <u-button slot="right" @tap="getCode" :text="tips" type="success" size="mini" :disabled="disabled1">
  10. </u-button>
  11. </u-form-item>
  12. <u-code ref="uCode" @change="codeChange" seconds="20" @start="disabled1 = true" @end="disabled1 = false">
  13. </u-code>
  14. </u--form>
  15. <u-button type="primary" @click='reset'>确定修改</u-button>
  16. </view>
  17. </template>
  18. <script>
  19. import {
  20. mapState
  21. } from 'vuex';
  22. var _this;
  23. import helper from '@/common/helper.js';
  24. export default {
  25. data() {
  26. return {
  27. disabled1: false,
  28. tips: '',
  29. registerData: {
  30. id: '',
  31. phone: '',
  32. verifyCode: '',
  33. },
  34. rules: {
  35. phone: {
  36. type: 'string',
  37. required: true,
  38. len: 11,
  39. message: '请填写11位手机号',
  40. trigger: ['blur']
  41. },
  42. verifyCode: {
  43. type: 'string',
  44. required: true,
  45. len: 6,
  46. message: '请填写6位验证码',
  47. trigger: ['blur']
  48. },
  49. }
  50. }
  51. },
  52. computed: {
  53. ...mapState(['hasLogin', 'userInfo']),
  54. // 手机号中间4位加*
  55. // starUserphone() {
  56. // let reg = /^(\d{3})\d{4}(\d{4})$/;
  57. // if (this.userphone) {
  58. // return this.userphone.replace(reg, "$1****$2");
  59. // }
  60. // }
  61. },
  62. onReady() {
  63. // 如果需要兼容微信小程序,并且校验规则中含有方法等,只能通过setRules方法设置规则
  64. this.$refs.registerForm.setRules(this.rules)
  65. },
  66. onLoad() {
  67. _this = this
  68. // this.registerData.phone = this.userInfo.phone
  69. this.registerData.id = this.userInfo.id
  70. },
  71. methods: {
  72. codeChange(text) {
  73. this.tips = text;
  74. },
  75. // 获取验证码
  76. getCode() {
  77. if (/^0?1[3|4|5|6|7|8][0-9]\d{8}$/.test(this.registerData.phone)) {
  78. if (this.$refs.uCode.canGetCode) {
  79. // 模拟向后端请求验证码
  80. uni.showLoading({
  81. title: '正在获取验证码'
  82. })
  83. _this.$request.baseRequest('get', '/commonUser/sendVerifyCode', {
  84. phone: this.registerData.phone
  85. }).then(res => {
  86. uni.hideLoading();
  87. // 这里此提示会被this.start()方法中的提示覆盖
  88. uni.$u.toast('验证码已发送');
  89. // 通知验证码组件内部开始倒计时
  90. this.$refs.uCode.start();
  91. })
  92. .catch(res => {
  93. uni.$u.toast(res.message);
  94. });
  95. // setTimeout(() => {
  96. // }, 2000);
  97. } else {
  98. uni.$u.toast('倒计时结束后再发送');
  99. }
  100. } else {
  101. uni.$u.toast('请输入正确手机号');
  102. }
  103. },
  104. //修改账号
  105. reset() {
  106. uni.showLoading({
  107. title: '加载中'
  108. })
  109. this.$refs.registerForm.validate().then(res => {
  110. _this.isLoading = true
  111. this.$request.baseRequest('post', '/commonUser/resetAccount', _this.registerData).then(res => {
  112. if (res.code == 200) {
  113. uni.clearStorageSync();
  114. this.$request.baseRequest('post', '/auth/api/logout').then(res => {
  115. this.$store.commit('logout')
  116. // this.$api.logout()
  117. setTimeout(function(){
  118. uni.showToast({
  119. title: '修改成功',
  120. icon: 'none',
  121. duration: 2000
  122. })
  123. uni.navigateTo({
  124. url: `/pages/public/login`
  125. })
  126. },1000)
  127. })
  128. } else {
  129. uni.showToast({
  130. title: res.message,
  131. icon: 'none',
  132. duration: 2000
  133. })
  134. }
  135. uni.hideLoading()
  136. })
  137. .catch(res => {
  138. uni.$u.toast(res.message);
  139. });
  140. }).catch(errors => {
  141. uni.$u.toast('校验失败')
  142. })
  143. },
  144. },
  145. }
  146. </script>
  147. <style lang="scss">
  148. </style>