bindPhone.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // pages/bindphone/bindPhone.js
  2. Page({
  3. /**
  4. * 组件的初始数据
  5. */
  6. data: {
  7. userInfo: {},
  8. isLogin: false,
  9. phone: null,
  10. errorMsg: '',
  11. vcodeFlag: true,
  12. vcodeTimeOut: 0,
  13. vcodeTimeOutDefault: 0,
  14. scene: "LN_PHONE",
  15. vcodeLen:2,
  16. vcodeInterval:null,
  17. vcode_button_text:'获取验证码',
  18. servicetel: '4009608068'
  19. },
  20. onError(e) {
  21. wx.showModal({
  22. title: '温馨提示',
  23. content: '当前网络环境较差,无法连接服务器,请稍后重试。有问题请联系客服电话4009608068,接听时段08:30-17:00。',
  24. showCancel: false
  25. });
  26. },
  27. inputPhone(e) {
  28. this.setData({
  29. phone: e.detail.value
  30. });
  31. this.checkAll();
  32. },
  33. inputVerificatrCode(e) {
  34. this.setData({
  35. vcode: e.detail.value
  36. });
  37. this.checkAll();
  38. },
  39. checkAll() {
  40. if (!this.checkPhoneNumber()) {
  41. this.setData({
  42. errorMsg: '手机号输入有误,请重新输入',
  43. checkPhone: false,
  44. checkCode: false
  45. });
  46. return;
  47. } else {
  48. this.setData({
  49. checkPhone: true,
  50. errorMsg: ''
  51. });
  52. }
  53. if (!this.checkVcode()) {
  54. this.setData({
  55. errorMsg: '验证码格式不正确'
  56. });
  57. this.setData({
  58. checkCode: false
  59. });
  60. return;
  61. } else {
  62. this.setData({
  63. errorMsg: ''
  64. });
  65. }
  66. if (this.data.phone && this.data.vcode) {
  67. this.setData({
  68. checkCode: true
  69. });
  70. } else {
  71. this.setData({
  72. checkCode: false
  73. });
  74. }
  75. },
  76. checkPhoneNumber() {
  77. if (!(/^1[3456789]\d{9}$/.test(this.data.phone))) {
  78. return false;
  79. }
  80. return true;
  81. },
  82. checkVcode() {
  83. if (this.data.vcode==undefined || this.data.vcode=="") {
  84. return false;
  85. }
  86. if(this.data.vcode.length==this.data.vcodeLen){
  87. return true;
  88. }
  89. return false;
  90. },
  91. getVerificateCode(){
  92. let vcodeFlag = this.data.vcodeFlag;
  93. let checkPhone = this.data.checkPhone;
  94. let phone = this.data.phone;
  95. let that = this;
  96. if (!vcodeFlag) {
  97. return;
  98. }
  99. if (!checkPhone) {
  100. that.setData({
  101. errorMsg: '手机号输入有误,请重新输入',
  102. checkCode: false
  103. });
  104. return;
  105. } else {
  106. that.setData({
  107. errorMsg: ''
  108. });
  109. }
  110. this.setData({
  111. vcodeTimeOut:this.data.vcodeTimeOutDefault
  112. })
  113. wx.request({
  114. url: getApp().globalData.postHeadAgreement + '/restapi/wechat/sendPhoneVcodeUnique',
  115. data: {
  116. scene: that.data.scene,
  117. terminal: phone,
  118. },
  119. method: 'POST',
  120. success(res) {
  121. if (res.data.code == 1) {
  122. wx.showModal({
  123. title: '提示',
  124. content:res.msg,
  125. showCancel:false,
  126. confirmColor:'#4359b5'
  127. });
  128. }else if (res.data.code == 2) {
  129. // 跳转
  130. wx.showModal({
  131. title: '提示',
  132. content: '手机已绑定账户,请检查手机号是否填写正确或联系客服',
  133. showCancel:false,
  134. });
  135. }else if (res.data.code == 200) {
  136. wx.showToast({
  137. title: '验证码已发送',
  138. icon: 'success',
  139. duration: 1000 //持续的时间
  140. })
  141. let vcodeInterval = setInterval(function () {
  142. if (that.data.vcodeTimeOut <= 0) {
  143. that.setData({
  144. vcodeFlag: true,
  145. vcode_button_text: '获取验证码'
  146. })
  147. clearInterval(vcodeInterval);
  148. }else{
  149. let str = '获取验证码'+'(' + that.data.vcodeTimeOut + 's)'
  150. that.setData({
  151. vcodeTimeOut:that.data.vcodeTimeOut-1,
  152. vcodeFlag: false,
  153. vcode_button_text: str
  154. })
  155. }
  156. }, 1000);
  157. }
  158. }
  159. });
  160. },
  161. bindPhone(e) {
  162. let that = this;
  163. let loginName = this.data.userInfo.loginName;
  164. let { phone, vcode } = this.data;
  165. log.info('[绑定手机]', '[绑定手机]', '[请求]', { phone, vcode });
  166. wx.request({
  167. url: getApp().globalData.postHeadAgreement +'/restapi/wechat/bindPhone',
  168. data: {
  169. loginName,
  170. phonenumber: phone,
  171. vcode
  172. },
  173. method: 'POST',
  174. success(res) {
  175. if (res.data.code == 1) {
  176. that.setData({
  177. vcode: null,
  178. });
  179. wx.showModal({
  180. title: '提示',
  181. content:res.data.msg,
  182. showCancel:false,
  183. confirmColor:'#4359b5'
  184. });
  185. log.info('[绑定手机号]', '[绑定手机号]', '[失败code==1]', res.data);
  186. } else {
  187. //跳转到上一页
  188. log.info('[绑定手机号]', '[绑定手机号]', '[成功返回上一个界面]', res.data);
  189. wx.navigateBack();
  190. }
  191. },
  192. fail(e){
  193. getApp().showNetworkError();
  194. }
  195. });
  196. },
  197. /**
  198. * 生命周期函数--监听页面加载
  199. */
  200. onLoad: function (options) {
  201. let userInfo = wx.getStorageSync('userInfo');
  202. let isLogin = wx.getStorageSync('isLogin');
  203. // 页面显示
  204. if (userInfo && isLogin) {
  205. this.setData({
  206. userInfo: userInfo,
  207. isLogin: isLogin
  208. });
  209. if ((!userInfo.userId && userInfo.userId != 0)){
  210. log.info('[首页]', '[已登陆未有用户ID跳转登录界面]');
  211. let url = `/pages/login/phone_login/phone_login`;
  212. wx.navigateTo({
  213. url
  214. });
  215. return;
  216. }
  217. } else {
  218. //未登录信息
  219. this.setData({
  220. userInfo: {}
  221. });
  222. let url = `/pages/login/phone_login/phone_login`;
  223. wx.navigateTo({
  224. url
  225. });
  226. return;
  227. }
  228. let scene = this.data.scene;
  229. let that = this
  230. wx.request({
  231. url: getApp().globalData.postHeadAgreement + '/restapi/wechat/vcodeInfo',
  232. data: {
  233. scene,
  234. },
  235. method: 'POST',
  236. success(res) {
  237. that.setData({
  238. vcodeInfo:false,
  239. vcodeTimeOut:res.data.result.expire,
  240. vcodeTimeOutDefault:res.data.result.expire,
  241. vcodeLen:res.data.result.len
  242. })
  243. }
  244. });
  245. },
  246. /**
  247. * 生命周期函数--监听页面初次渲染完成
  248. */
  249. onReady: function () {
  250. },
  251. /**
  252. * 生命周期函数--监听页面显示
  253. */
  254. onShow: function () {
  255. },
  256. /**
  257. * 生命周期函数--监听页面隐藏
  258. */
  259. onHide: function () {
  260. },
  261. /**
  262. * 生命周期函数--监听页面卸载
  263. */
  264. onUnload: function () {
  265. },
  266. /**
  267. * 页面相关事件处理函数--监听用户下拉动作
  268. */
  269. onPullDownRefresh: function () {
  270. },
  271. /**
  272. * 页面上拉触底事件的处理函数
  273. */
  274. onReachBottom: function () {
  275. },
  276. /**
  277. * 用户点击右上角分享
  278. */
  279. onShareAppMessage: function () {
  280. }
  281. })