index.js 5.6 KB

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