changePhone.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // pages/setting/pages/changePhone/changePhone.js
  2. import { editUserPhone, sendPhoneCode } from '../../../../services/index'
  3. import { testPhone } from '../../../../utils/util'
  4. import {
  5. captcha
  6. } from '../../../../config/config'
  7. let time = 60
  8. Page({
  9. /**
  10. * 页面的初始数据
  11. */
  12. data: {
  13. phoneNow: '',
  14. yanzheng: '获取验证码',
  15. time: 60,
  16. phone: '',
  17. format_phone: '',
  18. code: '',
  19. cansubmit: false
  20. },
  21. show() {
  22. const captchaResult = getApp().captchaResult;
  23. getApp().captchaResult = null; // 验证码的票据为一次性票据,取完需要置空
  24. if (captchaResult && captchaResult.ret === 0) {
  25. // 将验证码的结果返回至服务端校验
  26. const ticket = captchaResult.ticket;
  27. const randstr = captchaResult.randstr;
  28. this.send({ randstr, ticket })
  29. }
  30. },
  31. submit() {
  32. if (this.data.cansubmit) {
  33. editUserPhone({
  34. new_phone: this.data.phone,
  35. code: this.data.code,
  36. }).then(data => {
  37. wx.showToast({
  38. icon: 'none',
  39. mask: true,
  40. title: '手机号修改已完成',
  41. success: res => {
  42. setTimeout(() => {
  43. wx.navigateBack({
  44. delta: 1,
  45. })
  46. }, 1200)
  47. }
  48. })
  49. wx.setStorageSync('phone', this.data.phone)
  50. })
  51. } else {
  52. wx.showToast({
  53. icon: 'none',
  54. title: '请填写完整信息',
  55. })
  56. }
  57. },
  58. saveCode(e) {
  59. const code = e.detail.value
  60. this.setData({
  61. code
  62. })
  63. this.check()
  64. },
  65. savePhone(e) {
  66. if (e.detail.keyCode !== 8) {
  67. const format_phone = e.detail.value.split('').filter(value => value.trim())
  68. if (testPhone(format_phone.join(''))) {
  69. this.setData({
  70. phone: format_phone.join('')
  71. }, () => {
  72. this.check()
  73. })
  74. }
  75. if (format_phone.length >= 3) {
  76. format_phone.splice(3, 0, ' ')
  77. }
  78. if (format_phone.length >= 8) {
  79. format_phone.splice(8, 0, ' ')
  80. }
  81. this.setData({
  82. format_phone: format_phone.join('')
  83. })
  84. } else {
  85. const format_phone = e.detail.value.trim()
  86. this.setData({
  87. format_phone,
  88. phone: '',
  89. cansubmit: false
  90. })
  91. }
  92. },
  93. toTCaptcha() {
  94. if (testPhone(this.data.phone)) {
  95. if (time === 60) {
  96. editUserPhone({
  97. new_phone: this.data.phone,
  98. type: 'check_phone',
  99. }).then(data => {
  100. wx.navigateToMiniProgram({
  101. appId: 'wx5a3a7366fd07e119',
  102. path: '/pages/captcha/index',
  103. extraData: {
  104. appId: captcha//您申请的验证码的 appId
  105. }
  106. })
  107. })
  108. } else {
  109. wx.showToast({
  110. mask: true,
  111. icon: 'none',
  112. title: '请勿重复点击',
  113. })
  114. }
  115. } else {
  116. wx.showToast({
  117. icon: 'none',
  118. title: '请填写正确手机号',
  119. })
  120. }
  121. },
  122. send() {
  123. const captchaResult = getApp().captchaResult;
  124. getApp().captchaResult = null; // 验证码的票据为一次性票据,取完需要置空
  125. if (captchaResult && captchaResult.ret === 0) {
  126. // 将验证码的结果返回至服务端校验
  127. const ticket = captchaResult.ticket;
  128. const randstr = captchaResult.randstr;
  129. // this.send({ randstr, ticket })
  130. sendPhoneCode({
  131. phone: this.data.phone,
  132. ticket,
  133. randstr
  134. }).then(data => {
  135. wx.showToast({
  136. icon: 'none',
  137. title: '验证码发送成功',
  138. })
  139. })
  140. this.countdown()
  141. }
  142. },
  143. countdown() {
  144. time--;
  145. this.setData({
  146. time,
  147. yanzheng: time + 's后重试'
  148. })
  149. let inter = setInterval(() => {
  150. if (time > 1) {
  151. time--;
  152. this.setData({
  153. time,
  154. yanzheng: time + 's后重试'
  155. })
  156. } else {
  157. clearInterval(inter)
  158. time = 60
  159. this.setData({
  160. time: 60,
  161. yanzheng: '获取验证码'
  162. }, _ => {
  163. inter = null;
  164. })
  165. }
  166. }, 1000)
  167. },
  168. check() {
  169. if (testPhone(this.data.phone) && this.data.code) {
  170. this.setData({
  171. cansubmit: true
  172. })
  173. return true
  174. } else {
  175. this.setData({
  176. cansubmit: false
  177. })
  178. return false
  179. }
  180. },
  181. /**
  182. * 生命周期函数--监听页面加载
  183. */
  184. onLoad: function (options) {
  185. },
  186. /**
  187. * 生命周期函数--监听页面初次渲染完成
  188. */
  189. onReady: function () {
  190. },
  191. /**
  192. * 生命周期函数--监听页面显示
  193. */
  194. onShow: function () {
  195. wx.getStorage({
  196. key: 'phone',
  197. success: res => {
  198. const _data = res.data
  199. this.setData({
  200. phoneNow: _data.substring(0, 3) + ' **** ' + _data.substring(7, 11)
  201. }, () => {
  202. this.send()
  203. })
  204. }
  205. })
  206. },
  207. /**
  208. * 生命周期函数--监听页面隐藏
  209. */
  210. onHide: function () {
  211. },
  212. /**
  213. * 生命周期函数--监听页面卸载
  214. */
  215. onUnload: function () {
  216. },
  217. /**
  218. * 页面相关事件处理函数--监听用户下拉动作
  219. */
  220. onPullDownRefresh: function () {
  221. },
  222. /**
  223. * 页面上拉触底事件的处理函数
  224. */
  225. onReachBottom: function () {
  226. },
  227. /**
  228. * 用户点击右上角分享
  229. */
  230. onShareAppMessage: function () {
  231. }
  232. })