bindPhone.js 6.9 KB

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