phone.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import { $wuxCountDown } from '../../components/dist/index'
  2. import {
  3. get, post,
  4. } from '../../utils/http';
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. status: 0,
  11. phone: '',
  12. phoneVal: '',
  13. codeVal: '',
  14. sms_token: ''
  15. },
  16. /**
  17. * 生命周期函数--监听页面加载
  18. */
  19. onLoad: function (options) {
  20. console.log(options)
  21. this.setData({
  22. phone: options.phone
  23. })
  24. },
  25. /**
  26. * 生命周期函数--监听页面初次渲染完成
  27. */
  28. onReady: function () {
  29. },
  30. /**
  31. * 生命周期函数--监听页面显示
  32. */
  33. onShow: function () {
  34. },
  35. /**
  36. * 生命周期函数--监听页面隐藏
  37. */
  38. onHide: function () {
  39. },
  40. /**
  41. * 生命周期函数--监听页面卸载
  42. */
  43. onUnload: function () {
  44. if(this.c1) this.c1.stop()
  45. },
  46. /**
  47. * 页面相关事件处理函数--监听用户下拉动作
  48. */
  49. onPullDownRefresh: function () {
  50. },
  51. /**
  52. * 页面上拉触底事件的处理函数
  53. */
  54. onReachBottom: function () {
  55. },
  56. /**
  57. * 用户点击右上角分享
  58. */
  59. onShareAppMessage: function () {
  60. },
  61. /**
  62. * 监听输入手机号
  63. */
  64. onChangePhone(e) {
  65. this.setData({
  66. phoneVal: e.detail.value
  67. })
  68. },
  69. /**
  70. * 监听输入验证码
  71. */
  72. onChangeCode(e) {
  73. this.setData({
  74. codeVal: e.detail.value
  75. })
  76. },
  77. /**
  78. * 获取验证码
  79. * api/sms/send_verify_code
  80. */
  81. getCode() {
  82. let { phoneVal,status,sms_token } = this.data;
  83. if(phoneVal == '') {
  84. wx.showToast({
  85. title: '请输入手机号',
  86. icon: 'none'
  87. })
  88. return;
  89. }
  90. if (this.c1 && this.c1.interval) return !1
  91. post('api/sms/send_verify_code',{
  92. type: status == 0 ? 1 : 2,
  93. mobile: phoneVal,
  94. sms_token
  95. },(res) => {
  96. wx.showToast({
  97. title: res.msg,
  98. })
  99. this.c1 = new $wuxCountDown({
  100. date: +(new Date) + 60000,
  101. onEnd() {
  102. this.setData({
  103. c1: '重新获取验证码',
  104. })
  105. },
  106. render(date) {
  107. const sec = this.leadingZeros(date.sec, 2) + ' 秒 '
  108. date.sec !== 0 && this.setData({
  109. c1: sec,
  110. })
  111. },
  112. })
  113. })
  114. },
  115. /**
  116. * 校验
  117. */
  118. onCheck() {
  119. if(this.data.status == 0) {
  120. this.verifyMobile();
  121. } else {
  122. this.changeMobile()
  123. }
  124. // this.setData({
  125. // status: ++this.data.status,
  126. // phoneVal: '',
  127. // codeVal: ''
  128. // })
  129. },
  130. /**
  131. * 校验旧手机号的验证码
  132. * /api/user/verify_mobile
  133. */
  134. verifyMobile() {
  135. return new Promise((resolve,reject) => {
  136. post('api/user/verify_mobile',{
  137. verify_code: this.data.codeVal
  138. },(res) => {
  139. if(this.c1){
  140. this.c1.stop()
  141. this.setData({
  142. c1: null
  143. })
  144. }
  145. wx.showToast({
  146. title: res.msg,
  147. })
  148. this.setData({
  149. status: ++this.data.status,
  150. phoneVal: '',
  151. codeVal: '',
  152. sms_token: res.data.sms_token
  153. })
  154. resolve()
  155. })
  156. })
  157. },
  158. /**
  159. * 更换新手机
  160. * api/user/change_mobile
  161. */
  162. changeMobile() {
  163. let { phoneVal,codeVal,sms_token } = this.data;
  164. post('api/user/change_mobile',{
  165. verify_code:codeVal,
  166. mobile:phoneVal,
  167. sms_token
  168. },(res) => {
  169. if(this.c1){
  170. this.c1.stop()
  171. this.setData({
  172. c1: null
  173. })
  174. }
  175. wx.showToast({
  176. title: res.msg,
  177. })
  178. this.setData({
  179. status: ++this.data.status,
  180. phoneVal: '',
  181. codeVal: '',
  182. })
  183. })
  184. }
  185. })