bindPhone.js 6.9 KB

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