bindphone.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. var timer; // 计时器
  2. var BASEURL = require('../../utils/wxconstant.js')
  3. var baseUrl = BASEURL.BaseUrl()
  4. var baseutils = require('../../utils/baseUtils.js')
  5. var netUtil = require('../../utils/network.js')
  6. var code
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. clickCode: false,
  13. phoneNumber: '',
  14. countdownTime: 59,
  15. vCode: ''
  16. },
  17. /**
  18. * 生命周期函数--监听页面加载
  19. */
  20. onLoad: function (options) {
  21. },
  22. bindNow: function (event) {
  23. var that = this
  24. console.log(event.detail.value)
  25. let phone = event.detail.value.phoneNumber
  26. let vcode = event.detail.value.vCode
  27. if (!phone) {
  28. that.lackToast('未填手机号')
  29. return
  30. }
  31. if (!vcode) {
  32. that.lackToast('未填验证码')
  33. return
  34. }
  35. if(vcode != code){
  36. that.lackToast('验证码不正确,请重新输入')
  37. return
  38. }
  39. var data = {
  40. openid: wx.getStorageSync('openid'),
  41. empPhone:phone
  42. }
  43. var url = baseUrl + BASEURL.getUrl.Binding
  44. netUtil.requestLoading(url, data, '正在加载数据...', 'POST', function (res) {
  45. if (res.data.success) {
  46. wx.showToast({
  47. title: res.data.data,
  48. icon: 'none',
  49. duration: 2000
  50. })
  51. // wx.setStorageSync('accountphone', phone)
  52. wx.setStorageSync('isregistered', true)
  53. // var pages = getCurrentPages()
  54. // var prevPage = pages[pages.length - 1] //当前界面
  55. // var prevPage = pages[pages.length - 2] //上一个页面
  56. // prevPage.setData({
  57. // })
  58. setTimeout(function () {
  59. wx.navigateBack({
  60. })
  61. }, 2000)
  62. } else {
  63. wx.showToast({
  64. title: res.data.message,
  65. icon: 'none',
  66. duration: 2000
  67. })
  68. }
  69. }, function (res) {
  70. wx.showToast({
  71. title: '加载数据失败',
  72. icon: 'none',
  73. duration: 2000
  74. })
  75. })
  76. return
  77. wx.login({
  78. success: res => {
  79. wx.request({
  80. url: baseUrl + BASEURL.getUrl.getOpenid, //获取openid
  81. method: 'GET',
  82. header: {
  83. 'content-type': 'application/x-www-form-urlencoded'
  84. },
  85. data: {
  86. code: res.code
  87. },
  88. success: function (res) {
  89. console.log('res2=', res)
  90. if (res.data) {
  91. var openId = res.data.data.openid
  92. if (!openId) return
  93. //进行绑定
  94. wx.request({
  95. url: baseUrl + BASEURL.getUrl.Binding + '?empPhone=' + phone + '&openid=' + openId,
  96. method: 'POST',
  97. data: {
  98. },
  99. header: {
  100. 'content-type': 'application/json;charset=UTF-8',
  101. },
  102. success: function (res) {
  103. console.log('bindable=', res)
  104. if (res.data.success) {
  105. wx.showToast({
  106. title: res.data.data,
  107. icon: 'none',
  108. duration: 2000
  109. })
  110. wx.setStorageSync('accountphone', phone)
  111. // var pages = getCurrentPages()
  112. // var prevPage = pages[pages.length - 1] //当前界面
  113. // var prevPage = pages[pages.length - 2] //上一个页面
  114. // prevPage.setData({
  115. // })
  116. setTimeout(function () {
  117. wx.navigateBack({
  118. })
  119. }, 3000)
  120. } else {
  121. wx.showToast({
  122. title: res.data.bdfall2,
  123. icon: 'none',
  124. duration: 2000
  125. })
  126. }
  127. }
  128. })
  129. }
  130. }
  131. })
  132. }
  133. })
  134. },
  135. /**
  136. * 监听新手机号输入值
  137. */
  138. getphoneNumber: function (e) {
  139. this.setData({
  140. phoneNumber: e.detail.value
  141. })
  142. },
  143. getvCode: function () {
  144. var that = this
  145. if (!that.data.phoneNumber) {
  146. that.lackToast('未填手机号')
  147. return
  148. }
  149. console.log('phoneNumber', that.data.phoneNumber)
  150. var data = { empPhone: that.data.phoneNumber }
  151. var url = baseUrl + BASEURL.getUrl.sendCode
  152. netUtil.requestLoading(url, data, '正在加载数据...', 'GET', function (res) {
  153. if (res.data && res.data.message == 'OK'){
  154. wx.showToast({
  155. title: '短信已发送',
  156. duration: 2000
  157. })
  158. code = res.data.code
  159. that.setData({
  160. clickCode: true,
  161. // vCode:code
  162. })
  163. that.TimerHandle()
  164. }else{
  165. wx.showToast({
  166. title: res.data.message,
  167. icon:'none',
  168. duration: 2000
  169. })
  170. }
  171. }, function (res) {
  172. wx.showToast({
  173. title: '加载数据失败',
  174. icon: 'none',
  175. duration: 2000
  176. })
  177. })
  178. },
  179. TimerHandle: function () {
  180. var that = this
  181. timer = setTimeout(function () {
  182. if (parseInt(that.data.countdownTime) == 0) {
  183. that.setData({
  184. countdownTime: 59,
  185. clickCode: false
  186. })
  187. clearTimeout(timer);
  188. } else {
  189. let time = that.numberAdd2(parseInt(that.data.countdownTime) - 1)
  190. that.setData({
  191. countdownTime: time
  192. })
  193. that.TimerHandle()
  194. // console.log('time=' + time)
  195. }
  196. }, 1000)
  197. },
  198. numberAdd2: function (num) {
  199. let result
  200. if (num < 10) {
  201. result = '0' + num
  202. } else {
  203. result = num
  204. }
  205. return result
  206. },
  207. lackToast: function (title) {
  208. if (title == '') {
  209. title = '缺填必选项'
  210. }
  211. wx.showToast({
  212. title: title,
  213. icon:'none',
  214. duration: 2000
  215. });
  216. },
  217. })