register.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. // pages/register/register.js
  2. import Toast from '../../vant/toast/toast';
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. userName: null,
  9. phone: null,
  10. email: null,
  11. carNum:null,
  12. password: null,
  13. rePassword: null,
  14. checkPass: false,
  15. errorMsg: '',
  16. userInfo: {},
  17. hasUserInfo: false,
  18. canIUseGetUserProfile: false,
  19. vcodeFlag: false,
  20. vcodeTimeOut: 0,
  21. vcodeTimeOutDefault: 0,
  22. scene: "REGIST",
  23. vcodeLen:6,
  24. vcodeInterval:null,
  25. vcode_button_text:'获取验证码',
  26. servicetel: '4009608068',
  27. showMsg: false,
  28. timeing: true,
  29. checked:false
  30. },
  31. /**
  32. * 生命周期函数--监听页面加载
  33. */
  34. onLoad: function(options) {
  35. if (wx.getUserProfile) {
  36. this.setData({
  37. canIUseGetUserProfile: true
  38. })
  39. }
  40. let scene = this.data.scene;
  41. let that = this
  42. wx.request({
  43. url: getApp().globalData.postHeadAgreement + '/restapi/wechat/vcodeInfo',
  44. // 场景(LOGIN:登录;LN_PHONE:绑定手机;UN_PHONE:解绑手机;FORGET_PWD:忘记密码;REGIST:注册;MDF_PWD:修改密码)
  45. data: {
  46. scene,
  47. },
  48. method: 'POST',
  49. success(res) {
  50. that.setData({
  51. vcodeInfo:false,
  52. // vcodeTimeOut:res.data.result.expire,
  53. vcodeTimeOutDefault:res.data.result.expire,
  54. vcodeLen:res.data.result.len
  55. })
  56. }
  57. });
  58. //var that = this;
  59. //查看是否授权
  60. //wx.getSetting({
  61. //success(res) {
  62. //console.log(res);
  63. //if (res.authSetting['scope.userInfo']) {
  64. //that.setData({
  65. //authSet: true
  66. //});
  67. //已经授权,可以直接调用 getUserInfo 获取头像昵称
  68. //wx.getUserInfo({
  69. //success(res) {
  70. //console.log(res)
  71. //that.setData({
  72. //userInfo: res.userInfo
  73. //});
  74. //}
  75. //});
  76. //}
  77. //}
  78. //});
  79. },
  80. getUserProfile(e) {
  81. // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认
  82. // 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
  83. if(!this.data.checked){
  84. Toast('请先阅读并同意用户使用协议');
  85. return;
  86. }
  87. this.checkAll();
  88. this.setData({
  89. showMsg:true
  90. })
  91. if(!this.data.checkPass){
  92. return;
  93. }
  94. let that=this
  95. wx.getUserProfile({
  96. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  97. success: (res) => {
  98. that.setData({
  99. userInfo: res.userInfo,
  100. hasUserInfo: true
  101. })
  102. that.bindGetUserInfo(res.userInfo);
  103. }
  104. })
  105. },
  106. bindGetUserInfo(userInfo) {
  107. //console.log(e.detail.userInfo)
  108. console.info(this.userInfo)
  109. let that = this;
  110. if (userInfo) {
  111. let {
  112. nickName: userName,
  113. gender: sex,
  114. avatarUrl: avatar
  115. } = userInfo;
  116. let {
  117. userName: loginName,
  118. password,
  119. phone,
  120. carNum,
  121. email,
  122. vcode
  123. } = this.data;
  124. wx.login({
  125. success(res) {
  126. console.log(res);
  127. if (res.code) {
  128. wx.request({
  129. url: getApp().globalData.postHeadAgreement + '/restapi/wechat/register',
  130. data: {
  131. userName,
  132. sex:"0",
  133. avatar:'http://cdglyy.pjnes.com/web/profile/avatar/c134f314cbbe985a8aae1787b45e6d6a.jpg',
  134. loginName,
  135. password,
  136. phonenumber: phone,
  137. email,
  138. vcode,
  139. license_number: carNum,
  140. code: res.code,
  141. },
  142. method: 'POST',
  143. success(res1) {
  144. //console.log(res1);
  145. //console.log(res1.data.code == 1);
  146. if (res1.data.code == 1 || res1.data.code == 2) {
  147. if(res1.data.msg!=null && res1.data.msg.startsWith("503")){
  148. getApp().showNetworkError();
  149. return;
  150. }
  151. // that.setData({
  152. // errorMsg: res1.data.msg
  153. // })
  154. wx.showModal({
  155. title: '提示',
  156. content: res1.data.msg,
  157. showCancel:false,
  158. confirmColor:'#00AADD',
  159. });
  160. } else {
  161. //返回该用户
  162. //跳转到上一页
  163. wx.setStorageSync("userInfo", res1.data);
  164. wx.setStorageSync("isLogin", true);
  165. wx.navigateBack();
  166. //let url = `/pages/ucenter/index/index`;
  167. //wx.redirectTo({
  168. //url
  169. //});
  170. }
  171. }
  172. });
  173. } else {
  174. console.log('登录失败!' + res.errMsg)
  175. }
  176. }
  177. });
  178. }
  179. },
  180. inputAccount(e) {
  181. this.setData({
  182. userName: e.detail.value
  183. });
  184. this.checkAll();
  185. },
  186. inputPassword(e) {
  187. this.setData({
  188. password: e.detail.value,
  189. showMsg: false
  190. });
  191. this.checkAll();
  192. },
  193. inputRepassword(e) {
  194. this.setData({
  195. rePassword: e.detail.value,
  196. showMsg: false
  197. });
  198. this.checkAll();
  199. },
  200. inputPhone(e) {
  201. this.setData({
  202. phone: e.detail.value,
  203. showMsg: false
  204. });
  205. this.checkAll();
  206. },
  207. inputVcode(e) {
  208. this.setData({
  209. vcode: e.detail.value,
  210. showMsg: false
  211. });
  212. if(!this.msgVcode()){
  213. return;
  214. }
  215. this.checkAll();
  216. },
  217. inputEmail(e) {
  218. this.setData({
  219. email: e.detail.value
  220. });
  221. this.checkAll();
  222. },
  223. inputCarNum(e){
  224. this.setData({
  225. carNum: e.detail.value
  226. });
  227. this.checkAll();
  228. },
  229. checkPassword() {
  230. if (!this.data.password || !this.data.rePassword || this.data.password != this.data.rePassword) {
  231. return false;
  232. }
  233. return true;
  234. },
  235. checkPhone() {
  236. if (!(/^1[3456789]\d{9}$/.test(this.data.phone))) {
  237. this.setData({
  238. vcodeFlag:false
  239. })
  240. return false;
  241. }
  242. if(this.data.vcodeTimeOut<=0){
  243. this.setData({
  244. vcodeFlag:true,
  245. })
  246. }
  247. return true;
  248. },
  249. checkMail() {
  250. if (!(/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/.test(this.data.email))) {
  251. return false;
  252. }
  253. return true;
  254. },
  255. checkVcode() {
  256. if (this.data.vcode==undefined || this.data.vcode=="") {
  257. return false;
  258. }
  259. if(this.data.vcode.length==this.data.vcodeLen){
  260. return true;
  261. }
  262. return false;
  263. },
  264. checkCarNum(){
  265. if (this.data.carNum){
  266. // if (!(/^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z](([0-9]{5}[DF])|([DF]([A-HJ-NP-Z0-9])[0-9]{4})))|([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9挂学警港澳使领]))$/.test(this.data.carNum))) {
  267. if (!/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领].*$/.test(this.data.carNum)) {
  268. return false;
  269. }
  270. }
  271. return true;
  272. },
  273. sendVcode(e){
  274. let that = this
  275. if (!that.checkPhone()) {
  276. that.setData({
  277. errorMsg: '手机号输入有误,请重新输入'
  278. });
  279. that.setData({
  280. checkPass: false
  281. });
  282. return;
  283. } else {
  284. that.setData({
  285. errorMsg: ''
  286. });
  287. }
  288. let phone = that.data.phone;
  289. that.setData({
  290. vcodeTimeOut:that.data.vcodeTimeOutDefault
  291. })
  292. let vcodeInterval = setInterval(that.vcodeFun,1000);
  293. that.setData({
  294. vcodeInterval
  295. })
  296. wx.request({
  297. url: getApp().globalData.postHeadAgreement + '/restapi/wechat/sendPhoneVcodeUnique',
  298. // url: getApp().globalData.postHeadAgreement + '/restapi/wechat/sendPhoneVcode',
  299. data: {
  300. scene: that.data.scene,
  301. terminal: phone,
  302. },
  303. method: 'POST',
  304. success(res) {
  305. //console.log(res1);
  306. //console.log(res1.data.code == 1);
  307. if (res.data.code == 1) {
  308. if(res.data.msg!=null && res.data.msg.startsWith("503")){
  309. getApp().showNetworkError();
  310. return;
  311. }
  312. that.setData({
  313. vcodeTimeOut:0,
  314. vcodeFlag:true,
  315. vcode_button_text:'获取验证码'
  316. })
  317. wx.showModal({
  318. title: '提示',
  319. content:res.data.msg,
  320. showCancel:false,
  321. confirmColor:'#00AADD',
  322. });
  323. }else if (res.data.code == 2) {
  324. that.setData({
  325. vcodeTimeOut:0,
  326. vcodeFlag:true,
  327. vcode_button_text:'获取验证码'
  328. })
  329. // 跳转
  330. wx.showModal({
  331. title: '提示',
  332. content: '手机号已绑定账户,请检查手机号是否填写正确或联系客服',
  333. showCancel:true,
  334. confirmText: '去登录',
  335. cancelText:"确定",
  336. confirmColor:'#00AADD',
  337. success: function (res1) {
  338. if (res1.confirm) {
  339. let url = '/pages/login/phone_login/phone_login';
  340. wx.redirectTo({
  341. url
  342. })
  343. }
  344. }
  345. });
  346. }else{
  347. wx.showToast({
  348. title: '验证码已发送',
  349. icon: 'success',
  350. duration: 1000 //持续的时间
  351. })
  352. }
  353. }
  354. });
  355. },
  356. vcodeFun(){
  357. if(this.data.vcodeTimeOut<=0){
  358. this.setData({
  359. vcodeFlag:true,
  360. vcode_button_text:'获取验证码',
  361. timeing:true
  362. })
  363. clearInterval(this.data.vcodeInterval)
  364. }else{
  365. this.setData({
  366. vcodeTimeOut:this.data.vcodeTimeOut-1,
  367. vcodeFlag:false,
  368. vcode_button_text:'获取验证码(' + this.data.vcodeTimeOut+'s)',
  369. timeing:false
  370. })
  371. }
  372. },
  373. msgVcode(){
  374. if (!this.checkVcode()) {
  375. this.setData({
  376. // errorMsg: '验证码格式不正确'
  377. errorMsg: '验证码格式不正确'
  378. });
  379. this.setData({
  380. checkCode: false
  381. });
  382. return false;
  383. } else {
  384. this.setData({
  385. errorMsg: ''
  386. });
  387. }
  388. return true;
  389. },
  390. checkAll() {
  391. // if (!this.data.userName || !this.data.userName.replace(/(^\s*)|(\s*$)/g,"")) {
  392. // this.setData({
  393. // errorMsg: '账号不能为空'
  394. // });
  395. // this.setData({
  396. // checkPass: false
  397. // });
  398. // return;
  399. // } else if (this.data.userName.length < 2 || this.data.userName.length > 20) {
  400. // this.setData({
  401. // errorMsg: '账号长度应为2-20'
  402. // });
  403. // this.setData({
  404. // checkPass: false
  405. // });
  406. // return;
  407. // } else if (!(/^[a-zA-Z0-9_.\u4e00-\u9fa5]+$/.test(this.data.userName))) {
  408. // this.setData({
  409. // errorMsg: '账号只能包含字母、数字、中文、下划线和点'
  410. // });
  411. // this.setData({
  412. // checkPass: false
  413. // });
  414. // return;
  415. // }else{
  416. // this.setData({
  417. // errorMsg: ''
  418. // });
  419. // }
  420. if (!this.checkPhone()) {
  421. this.setData({
  422. errorMsg: '手机号输入有误,请重新输入'
  423. });
  424. this.setData({
  425. checkPass: false,
  426. vcodeFlag: false
  427. });
  428. return;
  429. } else {
  430. this.setData({
  431. errorMsg: '',
  432. vcodeFlag: true
  433. });
  434. }
  435. if(!this.msgVcode()){
  436. return;
  437. }
  438. // if (!this.checkMail()) {
  439. // this.setData({
  440. // errorMsg: '邮箱输入有误,请重新输入'
  441. // });
  442. // this.setData({
  443. // checkPass: false
  444. // });
  445. // return;
  446. // } else {
  447. // this.setData({
  448. // errorMsg: ''
  449. // });
  450. // }
  451. // if (!this.checkCarNum()) {
  452. // this.setData({
  453. // errorMsg: '车牌号输入有误,请重新输入'
  454. // });
  455. // this.setData({
  456. // checkPass: false
  457. // });
  458. // return;
  459. // } else {
  460. // this.setData({
  461. // errorMsg: ''
  462. // });
  463. // }
  464. if (this.data.password && (this.data.password.length < 5 || this.data.password.length > 20)) {
  465. this.setData({
  466. errorMsg: '密码长度应为5-20'
  467. });
  468. this.setData({
  469. checkPass: false
  470. });
  471. return;
  472. }
  473. if (!this.checkPassword()) {
  474. this.setData({
  475. errorMsg: '两次密码输入不一致,请重新输入'
  476. });
  477. this.setData({
  478. checkPass: false
  479. });
  480. return;
  481. } else {
  482. this.setData({
  483. errorMsg: ''
  484. });
  485. }
  486. this.setData({
  487. checkPass: true
  488. });
  489. },
  490. phoneCall(){
  491. wx.makePhoneCall({
  492. phoneNumber: this.data.servicetel,
  493. })
  494. },
  495. userAgreement(){
  496. let url = '/pages/agreement/userAgreement'
  497. wx.navigateTo({
  498. url
  499. });
  500. },
  501. /**
  502. * 生命周期函数--监听页面初次渲染完成
  503. */
  504. onReady: function() {
  505. },
  506. /**
  507. * 生命周期函数--监听页面显示
  508. */
  509. onShow: function() {
  510. },
  511. /**
  512. * 生命周期函数--监听页面隐藏
  513. */
  514. onHide: function() {
  515. },
  516. /**
  517. * 生命周期函数--监听页面卸载
  518. */
  519. onUnload: function() {
  520. },
  521. /**
  522. * 页面相关事件处理函数--监听用户下拉动作
  523. */
  524. onPullDownRefresh: function() {
  525. },
  526. /**
  527. * 页面上拉触底事件的处理函数
  528. */
  529. onReachBottom: function() {
  530. },
  531. /**
  532. * 用户点击右上角分享
  533. */
  534. onShareAppMessage: function() {
  535. },
  536. onCheckBoxChange(event) {
  537. this.setData({
  538. checked: event.detail,
  539. });
  540. },
  541. })