index.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import {
  2. registerVerify,
  3. bindingPhone,
  4. verifyCode
  5. } from '../../api/api.js';
  6. const app = getApp();
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. parameter: {
  13. 'navbar': '1',
  14. 'return': '1',
  15. 'title': '绑定手机号',
  16. 'color': true,
  17. 'class': '0'
  18. },
  19. disabled: false,
  20. active: false,
  21. timetext: '获取验证码',
  22. userInfo: {},
  23. phone: '',
  24. key: '',
  25. imagesCode: false,
  26. httpUrl: '',
  27. captchaimg: ''
  28. },
  29. inputgetName(e) {
  30. let that = this;
  31. let name = e.currentTarget.dataset.name;
  32. let nameMap = {}
  33. if (name.indexOf('.') != -1) {
  34. let nameList = name.split('.')
  35. if (that.data[nameList[0]]) {
  36. nameMap[nameList[0]] = that.data[nameList[0]]
  37. } else {
  38. nameMap[nameList[0]] = {}
  39. }
  40. nameMap[nameList[0]][nameList[1]] = e.detail.value
  41. } else {
  42. nameMap[name] = e.detail.value
  43. }
  44. that.setData(nameMap);
  45. },
  46. onLoadFun: function () {
  47. },
  48. editPwd: function () {
  49. let that = this;
  50. if (!this.data.phone) return app.Tips({
  51. title: '请填写手机号码!'
  52. });
  53. if (!(/^1[3456789]\d{9}$/.test(this.data.phone))) return app.Tips({
  54. title: '请输入正确的手机号码!'
  55. });
  56. if (!this.data.captcha) return app.Tips({
  57. title: '请填写验证码'
  58. });
  59. bindingPhone({
  60. phone: this.data.phone,
  61. captcha: this.data.captcha
  62. }).then(res => {
  63. if (res.data !== undefined && res.data.is_bind) {
  64. wx.showModal({
  65. title: '是否绑定账号',
  66. content: res.msg,
  67. confirmText: '绑定',
  68. success(res) {
  69. if (res.confirm) {
  70. bindingPhone({
  71. phone: that.data.phone,
  72. captcha: that.data.captcha,
  73. step: 1
  74. }).then(res => {
  75. return app.Tips({
  76. title: res.msg,
  77. icon: 'success'
  78. }, {
  79. tab: 5,
  80. url: '/pages/user_info/index'
  81. });
  82. }).catch(err => {
  83. return app.Tips({
  84. title: err
  85. });
  86. })
  87. } else if (res.cancel) {
  88. return app.Tips({
  89. title: '您已取消绑定!'
  90. }, {
  91. tab: 5,
  92. url: '/pages/user_info/index'
  93. });
  94. }
  95. }
  96. });
  97. } else
  98. return app.Tips({
  99. title: '绑定成功!',
  100. icon: 'success'
  101. }, {
  102. tab: 5,
  103. url: '/pages/user_info/index'
  104. });
  105. }).catch(err => {
  106. return app.Tips({
  107. title: err
  108. });
  109. })
  110. },
  111. /**
  112. * 发送验证码
  113. */
  114. code: function () {
  115. let that = this;
  116. if (!this.data.phone) return app.Tips({
  117. title: '请填写手机号码!'
  118. });
  119. if (!(/^1[3456789]\d{9}$/.test(that.data.phone))) return app.Tips({
  120. title: '请输入正确的手机号码!'
  121. });
  122. if (that.data.imagesCode) {
  123. if (!that.data.captchaimg) {
  124. return app.Tips({
  125. title: '请输入图片验证码'
  126. });
  127. }
  128. };
  129. registerVerify(this.data.captchaimg, this.data.key, this.data.phone).then(res => {
  130. if (res.status == 402) {
  131. that.data.imagesCode = true;
  132. that.setData({
  133. imagesCode: that.data.imagesCode
  134. });
  135. if (this.data.captchaimg != '') {
  136. that.runNun();
  137. }
  138. } else {
  139. that.runNun();
  140. }
  141. return app.Tips({
  142. title: '发送成功',
  143. icon: 'success'
  144. });
  145. }).catch(err => {
  146. return app.Tips({
  147. title: err
  148. });
  149. });
  150. },
  151. runNun: function () {
  152. let that = this;
  153. let n = 60;
  154. let run = setInterval(function () {
  155. n--;
  156. if (n < 0) {
  157. clearInterval(run);
  158. that.setData({
  159. disabled: false,
  160. active: false,
  161. timetext: '重新获取'
  162. })
  163. } else {
  164. that.setData({
  165. timetext: "剩余 " + n + "s",
  166. disabled: true,
  167. active: true
  168. })
  169. }
  170. }, 1000);
  171. },
  172. /**
  173. * 获取key值;
  174. */
  175. getVerifyCode: function () {
  176. let that = this;
  177. verifyCode().then(res => {
  178. that.setData({
  179. key: res.data.key,
  180. httpUrl: app.globalData.url + '/api/sms_captcha?key=' + res.data.key
  181. })
  182. }).catch(err => {
  183. return app.Tips({
  184. title: err.msg
  185. });
  186. })
  187. },
  188. /**
  189. * 生命周期函数--监听页面加载
  190. */
  191. onLoad: function (options) {
  192. this.getVerifyCode();
  193. },
  194. imagesCodeTap: function () {
  195. this.setData({
  196. httpUrl: this.data.httpUrl + '&' + Date.parse(new Date())
  197. })
  198. },
  199. /**
  200. * 生命周期函数--监听页面初次渲染完成
  201. */
  202. onReady: function () {
  203. },
  204. /**
  205. * 生命周期函数--监听页面显示
  206. */
  207. onShow: function () {
  208. },
  209. /**
  210. * 生命周期函数--监听页面隐藏
  211. */
  212. onHide: function () {
  213. },
  214. /**
  215. * 生命周期函数--监听页面卸载
  216. */
  217. onUnload: function () {
  218. },
  219. /**
  220. * 页面相关事件处理函数--监听用户下拉动作
  221. */
  222. onPullDownRefresh: function () {
  223. },
  224. /**
  225. * 页面上拉触底事件的处理函数
  226. */
  227. onReachBottom: function () {
  228. }
  229. })