bindphone.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. baseutils.getPersonlEmp()
  54. // var pages = getCurrentPages()
  55. // var prevPage = pages[pages.length - 1] //当前界面
  56. // var prevPage = pages[pages.length - 2] //上一个页面
  57. // prevPage.setData({
  58. // })
  59. setTimeout(function () {
  60. wx.navigateBack({
  61. })
  62. }, 2000)
  63. } else {
  64. wx.showToast({
  65. title: res.data.message,
  66. icon: 'none',
  67. duration: 2000
  68. })
  69. }
  70. }, function (res) {
  71. wx.showToast({
  72. title: '加载数据失败',
  73. icon: 'none',
  74. duration: 2000
  75. })
  76. })
  77. return
  78. wx.login({
  79. success: res => {
  80. wx.request({
  81. url: baseUrl + BASEURL.getUrl.getOpenid, //获取openid
  82. method: 'GET',
  83. header: {
  84. 'content-type': 'application/x-www-form-urlencoded'
  85. },
  86. data: {
  87. code: res.code
  88. },
  89. success: function (res) {
  90. console.log('res2=', res)
  91. if (res.data) {
  92. var openId = res.data.data.openid
  93. if (!openId) return
  94. //进行绑定
  95. wx.request({
  96. url: baseUrl + BASEURL.getUrl.Binding + '?empPhone=' + phone + '&openid=' + openId,
  97. method: 'POST',
  98. data: {
  99. },
  100. header: {
  101. 'content-type': 'application/json;charset=UTF-8',
  102. },
  103. success: function (res) {
  104. console.log('bindable=', res)
  105. if (res.data.success) {
  106. wx.showToast({
  107. title: res.data.data,
  108. icon: 'none',
  109. duration: 2000
  110. })
  111. wx.setStorageSync('accountphone', phone)
  112. // var pages = getCurrentPages()
  113. // var prevPage = pages[pages.length - 1] //当前界面
  114. // var prevPage = pages[pages.length - 2] //上一个页面
  115. // prevPage.setData({
  116. // })
  117. setTimeout(function () {
  118. wx.navigateBack({
  119. })
  120. }, 3000)
  121. } else {
  122. wx.showToast({
  123. title: res.data.bdfall2,
  124. icon: 'none',
  125. duration: 2000
  126. })
  127. }
  128. }
  129. })
  130. }
  131. }
  132. })
  133. }
  134. })
  135. },
  136. /**
  137. * 监听新手机号输入值
  138. */
  139. getphoneNumber: function (e) {
  140. this.setData({
  141. phoneNumber: e.detail.value
  142. })
  143. },
  144. getvCode: function () {
  145. var that = this
  146. if (!that.data.phoneNumber) {
  147. that.lackToast('未填手机号')
  148. return
  149. }
  150. console.log('phoneNumber', that.data.phoneNumber)
  151. var data = { empPhone: that.data.phoneNumber }
  152. var url = baseUrl + BASEURL.getUrl.sendCode
  153. netUtil.requestLoading(url, data, '正在加载数据...', 'GET', function (res) {
  154. if (res.data && res.data.message == 'OK'){
  155. wx.showToast({
  156. title: '短信已发送',
  157. duration: 2000
  158. })
  159. code = res.data.code
  160. that.setData({
  161. clickCode: true,
  162. // vCode:code
  163. })
  164. that.TimerHandle()
  165. }else{
  166. wx.showToast({
  167. title: res.data.message,
  168. icon:'none',
  169. duration: 2000
  170. })
  171. }
  172. }, function (res) {
  173. wx.showToast({
  174. title: '加载数据失败',
  175. icon: 'none',
  176. duration: 2000
  177. })
  178. })
  179. },
  180. TimerHandle: function () {
  181. var that = this
  182. timer = setTimeout(function () {
  183. if (parseInt(that.data.countdownTime) == 0) {
  184. that.setData({
  185. countdownTime: 59,
  186. clickCode: false
  187. })
  188. clearTimeout(timer);
  189. } else {
  190. let time = that.numberAdd2(parseInt(that.data.countdownTime) - 1)
  191. that.setData({
  192. countdownTime: time
  193. })
  194. that.TimerHandle()
  195. // console.log('time=' + time)
  196. }
  197. }, 1000)
  198. },
  199. numberAdd2: function (num) {
  200. let result
  201. if (num < 10) {
  202. result = '0' + num
  203. } else {
  204. result = num
  205. }
  206. return result
  207. },
  208. lackToast: function (title) {
  209. if (title == '') {
  210. title = '缺填必选项'
  211. }
  212. wx.showToast({
  213. title: title,
  214. icon:'none',
  215. duration: 2000
  216. });
  217. },
  218. })