bindPhone.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. console.info(res)
  124. if (res.data.code == 1) {
  125. wx.showModal({
  126. title: '提示',
  127. content:res.data.msg,
  128. showCancel:false,
  129. confirmColor:'#00AADD'
  130. });
  131. }else if (res.data.code == 2) {
  132. // 跳转
  133. wx.showModal({
  134. title: '提示',
  135. content: '手机已绑定账户,请检查手机号是否填写正确或联系客服',
  136. showCancel:false,
  137. confirmColor:'#00AADD',
  138. });
  139. }else if (res.data.code == 200) {
  140. wx.showToast({
  141. title: '验证码已发送',
  142. icon: 'success',
  143. duration: 1000 //持续的时间
  144. })
  145. let vcodeInterval = setInterval(function () {
  146. if (that.data.vcodeTimeOut <= 0) {
  147. that.setData({
  148. vcodeFlag: true,
  149. vcode_button_text: '获取验证码'
  150. })
  151. clearInterval(vcodeInterval);
  152. }else{
  153. let str = '获取验证码'+'(' + that.data.vcodeTimeOut + 's)'
  154. that.setData({
  155. vcodeTimeOut:that.data.vcodeTimeOut-1,
  156. vcodeFlag: false,
  157. vcode_button_text: str
  158. })
  159. }
  160. }, 1000);
  161. }
  162. }
  163. });
  164. },
  165. bindPhone(e) {
  166. let that = this;
  167. let loginName = this.data.userInfo.loginName;
  168. let { phone, vcode } = this.data;
  169. log.info('[绑定手机]', '[绑定手机]', '[请求]', { phone, vcode });
  170. wx.request({
  171. url: getApp().globalData.postHeadAgreement +'/restapi/wechat/bindPhone',
  172. data: {
  173. loginName,
  174. phonenumber: phone,
  175. vcode
  176. },
  177. method: 'POST',
  178. success(res) {
  179. if (res.data.code == 1) {
  180. that.setData({
  181. vcode: null,
  182. });
  183. wx.showModal({
  184. title: '提示',
  185. content:res.data.msg,
  186. showCancel:false,
  187. confirmColor:'#00AADD',
  188. });
  189. log.info('[绑定手机号]', '[绑定手机号]', '[失败code==1]', res.data);
  190. } else {
  191. wx.setStorageSync("userInfo", res.data);
  192. wx.setStorageSync("isLogin", true);
  193. //跳转到上一页
  194. log.info('[绑定手机号]', '[绑定手机号]', '[成功返回上一个界面]', res.data);
  195. wx.navigateBack();
  196. }
  197. },
  198. fail(e){
  199. getApp().showNetworkError();
  200. }
  201. });
  202. },
  203. /**
  204. * 生命周期函数--监听页面加载
  205. */
  206. onLoad: function (options) {
  207. let userInfo = wx.getStorageSync('userInfo');
  208. let isLogin = wx.getStorageSync('isLogin');
  209. // 页面显示
  210. if (userInfo && isLogin) {
  211. this.setData({
  212. userInfo: userInfo,
  213. isLogin: isLogin
  214. });
  215. if ((!userInfo.userId && userInfo.userId != 0)){
  216. log.info('[首页]', '[已登陆未有用户ID跳转登录界面]');
  217. let url = `/pages/login/phone_login/phone_login`;
  218. wx.navigateTo({
  219. url
  220. });
  221. return;
  222. }
  223. } else {
  224. //未登录信息
  225. this.setData({
  226. userInfo: {}
  227. });
  228. let url = `/pages/login/phone_login/phone_login`;
  229. wx.navigateTo({
  230. url
  231. });
  232. return;
  233. }
  234. let scene = this.data.scene;
  235. let that = this
  236. wx.request({
  237. url: getApp().globalData.postHeadAgreement + '/restapi/wechat/vcodeInfo',
  238. data: {
  239. scene,
  240. },
  241. method: 'POST',
  242. success(res) {
  243. that.setData({
  244. vcodeInfo:false,
  245. vcodeTimeOut:res.data.result.expire,
  246. vcodeTimeOutDefault:res.data.result.expire,
  247. vcodeLen:res.data.result.len
  248. })
  249. }
  250. });
  251. },
  252. /**
  253. * 生命周期函数--监听页面初次渲染完成
  254. */
  255. onReady: function () {
  256. },
  257. /**
  258. * 生命周期函数--监听页面显示
  259. */
  260. onShow: function () {
  261. },
  262. /**
  263. * 生命周期函数--监听页面隐藏
  264. */
  265. onHide: function () {
  266. },
  267. /**
  268. * 生命周期函数--监听页面卸载
  269. */
  270. onUnload: function () {
  271. },
  272. /**
  273. * 页面相关事件处理函数--监听用户下拉动作
  274. */
  275. onPullDownRefresh: function () {
  276. },
  277. /**
  278. * 页面上拉触底事件的处理函数
  279. */
  280. onReachBottom: function () {
  281. },
  282. /**
  283. * 用户点击右上角分享
  284. */
  285. onShareAppMessage: function () {
  286. }
  287. })