editPhone.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <template>
  2. <view class="page">
  3. <view class="flex justify-center text-df padding-50">
  4. 完成短信验证进行换绑手机号
  5. </view>
  6. <view class="bg-white padding-left-40 padding-right-20">
  7. <u-form ref="uForm" >
  8. <u-form-item label="原手机号" :label-width="labelWidth">
  9. <u-input v-model="phone" disabled type="number"></u-input>
  10. </u-form-item>
  11. <u-form-item label="验证码" :label-width="labelWidth">
  12. <u-input placeholder="请输入验证码" v-model="code" type="text"></u-input>
  13. <view :style="showText?'':'background-color: #A7A7A7;'" @click="getCode" slot="right" class="cu-btn round sm bg-blue">
  14. {{codeTips}}
  15. </view>
  16. </u-form-item>
  17. <u-form-item label="新手机号" :label-width="labelWidth">
  18. <u-input placeholder="请输入新手机号" v-model="newPhone" type="number"></u-input>
  19. </u-form-item>
  20. </u-form>
  21. </view>
  22. <view @click="submit" class="cu-btn bg-blue round flex " style="padding: 40rpx;margin: 80rpx 20rpx;">
  23. 换绑手机号
  24. </view>
  25. <u-verification-code seconds="60" @end="end" @start="start" ref="uCode" @change="codeChange"></u-verification-code>
  26. </view>
  27. </template>
  28. <script>
  29. export default {
  30. name: '',
  31. data() {
  32. return {
  33. userInfo:{},
  34. phone:'',
  35. newPhone:'',
  36. labelWidth:200,
  37. code:'',
  38. codeTips:'',
  39. showText:true,
  40. };
  41. },
  42. onLoad() {
  43. //从缓存中获取手机号
  44. if (this.$isEmpty(this.$cache.get('phone'))) {
  45. this.$showModel('系统错误',false).then(res=>{
  46. this.$Router.back()
  47. return
  48. })
  49. }else{
  50. this.phone=this.$cache.get('phone')
  51. }
  52. },
  53. onShow() {
  54. //获取用户信息
  55. if (this.$isEmpty(getApp().globalData.userInfo)) {
  56. this.fetchUserInfo()
  57. }else{
  58. this.userInfo=getApp().globalData.userInfo
  59. }
  60. },
  61. methods:{
  62. /**
  63. * 获取用户信息
  64. */
  65. fetchUserInfo(){
  66. this.$api.enterprisestaff.page({phone:this.phone,examine:1}).then(res=>{
  67. this.userInfo=res.data.records[0]
  68. })
  69. },
  70. /**
  71. * 验证码提示
  72. * @param {Object} text
  73. */
  74. codeChange(text) {
  75. this.codeTips = text;
  76. },
  77. end() {
  78. this.showText=true
  79. },
  80. start() {
  81. this.showText=false
  82. },
  83. /**
  84. * 确定修改
  85. */
  86. async submit(){
  87. let that=this
  88. if (this.$isEmpty(this.newPhone)) {
  89. this.$u.toast("请输入手机号")
  90. return
  91. }
  92. if (!this.$u.test.mobile(this.newPhone)) {
  93. this.$u.toast("请输入正确的手机号")
  94. return
  95. }
  96. if (!this.code) {
  97. uni.showToast({ title: '请输入验证码', icon: 'none' });
  98. return;
  99. }
  100. if (this.code.length!=6) {
  101. this.$u.toast('请输入6位数的验证码')
  102. return
  103. }
  104. let params={
  105. phone:this.phone,
  106. code:this.code
  107. }
  108. let smsRes=await this.$api.SMSApi.validCode(this.$u.queryParams(params))
  109. if (smsRes.data!='success') {
  110. this.$u.toast(smsRes.data)
  111. return
  112. }
  113. this.userInfo.phone=this.newPhone
  114. this.$api.enterprisestaff.submit(this.userInfo).then(res=>{
  115. if (res.success) {
  116. getApp().globalData.userInfo=that.userInfo
  117. that.$cache.put('phone',that.newPhone)
  118. that.$showModel('修改成功',false).then(res=>{
  119. that.$Router.back()
  120. })
  121. }
  122. })
  123. },
  124. /**
  125. * 获取验证码
  126. */
  127. async getCode() {
  128. let that=this
  129. if (this.$isEmpty(this.phone)) {
  130. this.$showModel('系统异常',false).then(res=>{
  131. this.$Router.back()
  132. })
  133. return
  134. }
  135. if(this.$refs.uCode.canGetCode) {
  136. // 模拟向后端请求验证码
  137. uni.showLoading({
  138. title: '正在获取验证码'
  139. })
  140. setTimeout(async () => {
  141. uni.hideLoading();
  142. let params=`?phone=${this.phone}`
  143. this.$api.SMSApi.sendSms(params).then(res=>{
  144. if (res.data=='获取验证码成功') {
  145. that.$u.toast('验证码已发送');
  146. }else{
  147. that.$refs.uTips.show({
  148. title: res.data,
  149. type: 'primary',
  150. duration: '2500'
  151. })
  152. }
  153. // 通知验证码组件内部开始倒计时
  154. that.$refs.uCode.start();
  155. }).catch(err=>{
  156. that.$u.toast('获取验证码失败');
  157. // 通知验证码组件内部开始倒计时
  158. that.$refs.uCode.start();
  159. })
  160. }, 2300);
  161. } else {
  162. this.$u.toast('倒计时结束后再发送');
  163. }
  164. },
  165. }
  166. };
  167. </script>
  168. <style lang="scss" scoped>
  169. .page{
  170. background-color: #FFFFFF;
  171. min-height: 100vh;
  172. }
  173. </style>