editPhone.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <template>
  2. <view class="container">
  3. <view class="body">
  4. <!-- 输入框区域 -->
  5. <view class="body_row mt-40">
  6. <view class="row_text">手机号</view>
  7. <input class="row_input input" type="text" placeholder="请输入手机号" placeholder-style="color:#CCCCCC" v-model="phone" />
  8. <view class="row_btn" :class="{ active: showBtn }" @click="handleVerify">{{ showBtn ? '获取验证码' : codeTime + 's后重新获取' }}</view>
  9. </view>
  10. <view class="body_row mt-30">
  11. <view class="row_text">验证码</view>
  12. <input class="row_input" type="number" placeholder="请输入验证码" placeholder-style="color:#CCCCCC" v-model="code" />
  13. </view>
  14. <!-- 确定按钮区域 -->
  15. <view class="body_btn" @click="handleConfirm">确认</view>
  16. </view>
  17. </view>
  18. </template>
  19. <script setup>
  20. import { ref } from 'vue'
  21. import { onLoad, onUnload } from '@dcloudio/uni-app'
  22. import { myRequest } from '@/utils/api.js'
  23. // 手机号
  24. const phone = ref('')
  25. // 验证码
  26. const code = ref('')
  27. // 是否可以点击获取验证码按钮
  28. const showBtn = ref(true)
  29. // 倒计时时间
  30. const codeTime = ref(60)
  31. // 计时器实例
  32. const timer = ref()
  33. onLoad(() => {})
  34. onUnload(() => {
  35. // 页面卸载时清除定时器
  36. clearInterval(timer.value)
  37. })
  38. // 获取验证码按钮回调
  39. const handleVerify = () => {
  40. // 手机号码验证规则
  41. const regPhone = /^1[3-9]\d{9}$/
  42. if (showBtn.value) {
  43. if (!regPhone.test(phone.value)) {
  44. uni.showToast({
  45. title: '请输入正确的手机号码',
  46. icon: 'none'
  47. })
  48. } else {
  49. showBtn.value = false
  50. countDown()
  51. // 获取验证码
  52. getCode()
  53. }
  54. } else {
  55. uni.showToast({
  56. title: '倒计时结束后再获取',
  57. icon: 'none'
  58. })
  59. }
  60. }
  61. // 获取验证码
  62. const getCode = async () => {
  63. const res = await myRequest({
  64. url: '/wanzai/api/wechat/sendChangeMessage',
  65. data: {
  66. phone: phone.value
  67. }
  68. })
  69. console.log(res)
  70. if (res.code == 200) {
  71. uni.showToast({
  72. title: '验证码已发送',
  73. icon: 'none'
  74. })
  75. }
  76. }
  77. const countDown = () => {
  78. timer.value = setInterval(() => {
  79. if (codeTime.value > 0) {
  80. codeTime.value -= 1
  81. } else {
  82. showBtn.value = true
  83. codeTime.value = 60
  84. clearInterval(timer.value)
  85. }
  86. }, 1000)
  87. }
  88. // 点击确认按钮回调
  89. const handleConfirm = () => {
  90. const flag = verifyValue()
  91. if (flag) {
  92. // 修改手机号码请求
  93. handleEditPhone()
  94. }
  95. }
  96. // 修改手机号码请求
  97. const handleEditPhone = async () => {
  98. const res = await myRequest({
  99. url: '/wanzai/api/wechat/vertifyChangePhone',
  100. data: {
  101. phone: phone.value,
  102. code: code.value
  103. }
  104. })
  105. console.log(res)
  106. if (res.code == 200) {
  107. uni.showToast({
  108. title: '修改成功',
  109. icon: 'success'
  110. })
  111. let userInfo = uni.getStorageSync('userInfo')
  112. userInfo.phone = phone.value
  113. uni.setStorageSync('userInfo', userInfo)
  114. setTimeout(() => {
  115. uni.navigateBack(1)
  116. }, 1500)
  117. }
  118. }
  119. // 验证表格数据是否符合规范
  120. const verifyValue = () => {
  121. // 手机号码验证规则
  122. const regPhone = /^1[3-9]\d{9}$/
  123. if (!phone.value) {
  124. uni.showToast({
  125. title: '请输入手机号',
  126. icon: 'none'
  127. })
  128. return false
  129. }
  130. if (!regPhone.test(phone.value)) {
  131. uni.showToast({
  132. title: '手机号码格式错误',
  133. icon: 'none'
  134. })
  135. return false
  136. }
  137. if (!code.value) {
  138. uni.showToast({
  139. title: '请输入验证码',
  140. icon: 'none'
  141. })
  142. return false
  143. }
  144. return true
  145. }
  146. </script>
  147. <style lang="scss" scoped>
  148. .container {
  149. display: flex;
  150. flex-direction: column;
  151. min-height: 100vh;
  152. background-color: #f1f6fe;
  153. .body {
  154. position: relative;
  155. padding: 0 20rpx;
  156. margin-top: 20rpx;
  157. height: calc(100vh - 20rpx);
  158. background-color: #fff;
  159. .body_row {
  160. display: flex;
  161. justify-content: space-between;
  162. align-items: center;
  163. height: 90rpx;
  164. font-size: 28rpx;
  165. border-bottom: 2rpx solid #e5e5e5;
  166. .row_text {
  167. flex: 1;
  168. padding-right: 30rpx;
  169. text-align-last: justify;
  170. }
  171. .row_input {
  172. box-sizing: border-box;
  173. padding: 0 30rpx;
  174. width: 590rpx;
  175. height: 90rpx;
  176. border-radius: 15rpx;
  177. background-color: #fff;
  178. }
  179. .input {
  180. width: 400rpx;
  181. }
  182. .row_btn {
  183. width: 190rpx;
  184. height: 41rpx;
  185. color: #ccc;
  186. text-align: center;
  187. font-size: 28rpx;
  188. }
  189. .active {
  190. color: #0061ff;
  191. }
  192. }
  193. .mt-40 {
  194. margin-top: 40rpx;
  195. }
  196. .mt-30 {
  197. margin-top: 30rpx;
  198. }
  199. .body_btn {
  200. position: absolute;
  201. bottom: 150rpx;
  202. left: 227rpx;
  203. display: flex;
  204. justify-content: center;
  205. align-items: center;
  206. width: 297rpx;
  207. height: 100rpx;
  208. color: #fff;
  209. font-size: 32rpx;
  210. border-radius: 8rpx;
  211. background-color: #1e7dfb;
  212. }
  213. }
  214. }
  215. </style>