change_mobile.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // pages/change_mobile/change_mobile.js
  2. var app = getApp();
  3. const util = require('../../utils/utils.js');
  4. let timer1 = null;
  5. var arr = [];
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. smsVal: '', //输入的短信验证码
  12. phoneVal: '', //输入的手机号码
  13. code: '获取验证码', //获取短信验证码
  14. currentTime: 60, //倒计时秒数
  15. codeDisabled: false, //是否禁用按钮
  16. timer: null, //定时器
  17. codeIshow: true,//获取验证码是否显示
  18. },
  19. onLoad: function (options) {
  20. // this.setData({
  21. // phoneVal: my.getStorageSync('userInfo').mobile
  22. // })
  23. },
  24. //验证手机号
  25. ckPhone(e) {
  26. this.setData({
  27. phoneVal: e.detail.value
  28. })
  29. },
  30. //验证短信验证码
  31. ckSms(e) {
  32. this.setData({
  33. smsVal: e.detail.value
  34. })
  35. },
  36. change: function () {
  37. my.navigateTo({
  38. url: '/pages/mine/mine',
  39. })
  40. },
  41. //获取验证码
  42. getCode: util.throttle(function () {
  43. let self = this
  44. let currentTime = self.data.currentTime
  45. let timer = self.data.timer
  46. if (self.data.phoneVal.length != 11) {
  47. my.showToast({
  48. content: '请输入正确的手机号',
  49. icon: 'none',
  50. duration: 2000
  51. })
  52. } else {
  53. // 调用短信验证码接口
  54. let data = {
  55. mobile: this.data.phoneVal,
  56. type: 2
  57. }
  58. app.request('/verification-code', data, 'POST').then(res => {
  59. console.log(res)
  60. if (res.status == 200) {
  61. if (!timer) {
  62. timer = setInterval(() => {
  63. if (currentTime > 0 && currentTime <= 60) {
  64. currentTime--;
  65. }
  66. if (currentTime !== 0) {
  67. self.setData({
  68. code: "重新发送" + "(" + currentTime + ")",
  69. codeDisabled: true,
  70. })
  71. } else {
  72. clearInterval(timer);
  73. self.setData({
  74. code: '获取验证码',
  75. codeDisabled: false,
  76. currentTime: 60,
  77. timer: null,
  78. })
  79. }
  80. }, 1000)
  81. }
  82. }
  83. })
  84. //60秒倒计时
  85. }
  86. }, 1000),
  87. verify: util.throttle(function () {
  88. let reg = 11 && /^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/;
  89. let reg1 = /^\d{4}$/
  90. if (!reg1.test(this.data.smsVal)) {
  91. my.showToast({
  92. content: '请输入短信验证码',
  93. icon: 'none',
  94. duration: 2000
  95. })
  96. }
  97. if (!reg.test(this.data.phoneVal)) {
  98. my.showToast({
  99. content: '请输入正确的手机号',
  100. icon: 'none',
  101. duration: 2000
  102. })
  103. return;
  104. } else if (!this.data.smsVal) {
  105. my.showToast({
  106. content: '短信验证码不能为空',
  107. icon: 'none',
  108. duration: 2000
  109. })
  110. return;
  111. } else {
  112. if (app.globalData.req) {
  113. let data = {
  114. mobile: this.data.phoneVal,
  115. code: this.data.smsVal
  116. }
  117. app.request('/user/bind-mobile', data, 'POST', app.globalData.req).then(res => {
  118. if (res.status == 200) {
  119. my.showToast({
  120. content: '换绑成功',
  121. icon: 'none'
  122. })
  123. }
  124. }).catch(err => {
  125. if (err.status == 422) {
  126. console.log(res)
  127. my.showToast({
  128. content: res.data.errors.mobile[0],
  129. icon: 'none',
  130. duration: 2000
  131. })
  132. }
  133. })
  134. } else {
  135. my.showToast({
  136. content: '您的操作过于频繁,请稍后再试~',
  137. icon: 'none'
  138. })
  139. }
  140. }
  141. }, 1000),
  142. //获取手机号
  143. getPhoneNumber: function (e) {
  144. let that = this
  145. my.getPhoneNumber({
  146. success: (res) => {
  147. let encryptedData = JSON.parse(res.response).response;
  148. //userObject.phoneInfo = res.response;
  149. console.log(res, 'xying');
  150. //手机号解密请求
  151. // phoneAuth(res.response);
  152. let data = {
  153. session_key: encryptedData
  154. }
  155. app.request('/user/bind-wechat-mobile', data, 'POST', app.globalData.req).then(res => {
  156. if (res.status == 200) {
  157. console.log(res.data, '手机号')
  158. if (res.data.is_bind_mobile == true) {
  159. my.showToast({
  160. content: '当前手机号已绑定',
  161. icon: 'none'
  162. })
  163. return
  164. } else {
  165. that.setData({
  166. phoneVal: res.data.user.mobile,
  167. // codeIshow: false
  168. })
  169. }
  170. }
  171. })
  172. }
  173. })
  174. },
  175. onReady: function () {
  176. },
  177. onShow: function () {
  178. },
  179. onHide: function () {
  180. },
  181. onUnload: function () {
  182. },
  183. onPullDownRefresh: function () {
  184. },
  185. onReachBottom: function () {
  186. },
  187. onShareAppMessage: function () {
  188. },
  189. })