bindPhone.js 7.8 KB

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