register.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. multipleOrders:0,
  132. refund:1,
  133. userName,
  134. sex:"0",
  135. avatar:'http://cdglyy.pjnes.com/web/profile/avatar/c134f314cbbe985a8aae1787b45e6d6a.jpg',
  136. loginName,
  137. password,
  138. phonenumber: phone,
  139. email,
  140. vcode,
  141. license_number: carNum,
  142. code: res.code,
  143. },
  144. method: 'POST',
  145. success(res1) {
  146. //console.log(res1);
  147. //console.log(res1.data.code == 1);
  148. if (res1.data.code == 1 || res1.data.code == 2) {
  149. if(res1.data.msg!=null && res1.data.msg.startsWith("503")){
  150. getApp().showNetworkError();
  151. return;
  152. }
  153. // that.setData({
  154. // errorMsg: res1.data.msg
  155. // })
  156. wx.showModal({
  157. title: '提示',
  158. content: res1.data.msg,
  159. showCancel:false,
  160. confirmColor:'#00AADD',
  161. });
  162. } else {
  163. //返回该用户
  164. //跳转到上一页
  165. wx.setStorageSync("userInfo", res1.data);
  166. wx.setStorageSync("isLogin", true);
  167. wx.navigateBack();
  168. //let url = `/pages/ucenter/index/index`;
  169. //wx.redirectTo({
  170. //url
  171. //});
  172. }
  173. }
  174. });
  175. } else {
  176. console.log('登录失败!' + res.errMsg)
  177. }
  178. }
  179. });
  180. }
  181. },
  182. inputAccount(e) {
  183. this.setData({
  184. userName: e.detail.value
  185. });
  186. this.checkAll();
  187. },
  188. inputPassword(e) {
  189. this.setData({
  190. password: e.detail.value,
  191. showMsg: false
  192. });
  193. this.checkAll();
  194. },
  195. inputRepassword(e) {
  196. this.setData({
  197. rePassword: e.detail.value,
  198. showMsg: false
  199. });
  200. this.checkAll();
  201. },
  202. inputPhone(e) {
  203. this.setData({
  204. phone: e.detail.value,
  205. showMsg: false
  206. });
  207. this.checkAll();
  208. },
  209. inputVcode(e) {
  210. this.setData({
  211. vcode: e.detail.value,
  212. showMsg: false
  213. });
  214. if(!this.msgVcode()){
  215. return;
  216. }
  217. this.checkAll();
  218. },
  219. inputEmail(e) {
  220. this.setData({
  221. email: e.detail.value
  222. });
  223. this.checkAll();
  224. },
  225. inputCarNum(e){
  226. this.setData({
  227. carNum: e.detail.value
  228. });
  229. this.checkAll();
  230. },
  231. checkPassword() {
  232. if (!this.data.password || !this.data.rePassword || this.data.password != this.data.rePassword) {
  233. return false;
  234. }
  235. return true;
  236. },
  237. checkPhone() {
  238. if (!(/^1[3456789]\d{9}$/.test(this.data.phone))) {
  239. this.setData({
  240. vcodeFlag:false
  241. })
  242. return false;
  243. }
  244. if(this.data.vcodeTimeOut<=0){
  245. this.setData({
  246. vcodeFlag:true,
  247. })
  248. }
  249. return true;
  250. },
  251. checkMail() {
  252. if (!(/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/.test(this.data.email))) {
  253. return false;
  254. }
  255. return true;
  256. },
  257. checkVcode() {
  258. if (this.data.vcode==undefined || this.data.vcode=="") {
  259. return false;
  260. }
  261. if(this.data.vcode.length==this.data.vcodeLen){
  262. return true;
  263. }
  264. return false;
  265. },
  266. checkCarNum(){
  267. if (this.data.carNum){
  268. // 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))) {
  269. if (!/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领].*$/.test(this.data.carNum)) {
  270. return false;
  271. }
  272. }
  273. return true;
  274. },
  275. sendVcode(e){
  276. let that = this
  277. if (!that.checkPhone()) {
  278. that.setData({
  279. errorMsg: '手机号输入有误,请重新输入'
  280. });
  281. that.setData({
  282. checkPass: false
  283. });
  284. return;
  285. } else {
  286. that.setData({
  287. errorMsg: ''
  288. });
  289. }
  290. let phone = that.data.phone;
  291. that.setData({
  292. vcodeTimeOut:that.data.vcodeTimeOutDefault
  293. })
  294. let vcodeInterval = setInterval(that.vcodeFun,1000);
  295. that.setData({
  296. vcodeInterval
  297. })
  298. wx.request({
  299. url: getApp().globalData.postHeadAgreement + '/restapi/wechat/sendPhoneVcodeUnique',
  300. // url: getApp().globalData.postHeadAgreement + '/restapi/wechat/sendPhoneVcode',
  301. data: {
  302. scene: that.data.scene,
  303. terminal: phone,
  304. },
  305. method: 'POST',
  306. success(res) {
  307. //console.log(res1);
  308. //console.log(res1.data.code == 1);
  309. if (res.data.code == 1) {
  310. if(res.data.msg!=null && res.data.msg.startsWith("503")){
  311. getApp().showNetworkError();
  312. return;
  313. }
  314. that.setData({
  315. vcodeTimeOut:0,
  316. vcodeFlag:true,
  317. vcode_button_text:'获取验证码'
  318. })
  319. wx.showModal({
  320. title: '提示',
  321. content:res.data.msg,
  322. showCancel:false,
  323. confirmColor:'#00AADD',
  324. });
  325. }else if (res.data.code == 2) {
  326. that.setData({
  327. vcodeTimeOut:0,
  328. vcodeFlag:true,
  329. vcode_button_text:'获取验证码'
  330. })
  331. // 跳转
  332. wx.showModal({
  333. title: '提示',
  334. content: '手机号已绑定账户,请检查手机号是否填写正确或联系客服',
  335. showCancel:true,
  336. confirmText: '去登录',
  337. cancelText:"确定",
  338. confirmColor:'#00AADD',
  339. success: function (res1) {
  340. if (res1.confirm) {
  341. let url = '/pages/login/phone_login/phone_login';
  342. wx.redirectTo({
  343. url
  344. })
  345. }
  346. }
  347. });
  348. }else{
  349. wx.showToast({
  350. title: '验证码已发送',
  351. icon: 'success',
  352. duration: 1000 //持续的时间
  353. })
  354. }
  355. }
  356. });
  357. },
  358. vcodeFun(){
  359. if(this.data.vcodeTimeOut<=0){
  360. this.setData({
  361. vcodeFlag:true,
  362. vcode_button_text:'获取验证码',
  363. timeing:true
  364. })
  365. clearInterval(this.data.vcodeInterval)
  366. }else{
  367. this.setData({
  368. vcodeTimeOut:this.data.vcodeTimeOut-1,
  369. vcodeFlag:false,
  370. vcode_button_text:'获取验证码(' + this.data.vcodeTimeOut+'s)',
  371. timeing:false
  372. })
  373. }
  374. },
  375. msgVcode(){
  376. if (!this.checkVcode()) {
  377. this.setData({
  378. // errorMsg: '验证码格式不正确'
  379. errorMsg: '验证码格式不正确'
  380. });
  381. this.setData({
  382. checkCode: false
  383. });
  384. return false;
  385. } else {
  386. this.setData({
  387. errorMsg: ''
  388. });
  389. }
  390. return true;
  391. },
  392. checkAll() {
  393. // if (!this.data.userName || !this.data.userName.replace(/(^\s*)|(\s*$)/g,"")) {
  394. // this.setData({
  395. // errorMsg: '账号不能为空'
  396. // });
  397. // this.setData({
  398. // checkPass: false
  399. // });
  400. // return;
  401. // } else if (this.data.userName.length < 2 || this.data.userName.length > 20) {
  402. // this.setData({
  403. // errorMsg: '账号长度应为2-20'
  404. // });
  405. // this.setData({
  406. // checkPass: false
  407. // });
  408. // return;
  409. // } else if (!(/^[a-zA-Z0-9_.\u4e00-\u9fa5]+$/.test(this.data.userName))) {
  410. // this.setData({
  411. // errorMsg: '账号只能包含字母、数字、中文、下划线和点'
  412. // });
  413. // this.setData({
  414. // checkPass: false
  415. // });
  416. // return;
  417. // }else{
  418. // this.setData({
  419. // errorMsg: ''
  420. // });
  421. // }
  422. if (!this.checkPhone()) {
  423. this.setData({
  424. errorMsg: '手机号输入有误,请重新输入'
  425. });
  426. this.setData({
  427. checkPass: false,
  428. vcodeFlag: false
  429. });
  430. return;
  431. } else {
  432. this.setData({
  433. errorMsg: '',
  434. vcodeFlag: true
  435. });
  436. }
  437. if(!this.msgVcode()){
  438. return;
  439. }
  440. // if (!this.checkMail()) {
  441. // this.setData({
  442. // errorMsg: '邮箱输入有误,请重新输入'
  443. // });
  444. // this.setData({
  445. // checkPass: false
  446. // });
  447. // return;
  448. // } else {
  449. // this.setData({
  450. // errorMsg: ''
  451. // });
  452. // }
  453. // if (!this.checkCarNum()) {
  454. // this.setData({
  455. // errorMsg: '车牌号输入有误,请重新输入'
  456. // });
  457. // this.setData({
  458. // checkPass: false
  459. // });
  460. // return;
  461. // } else {
  462. // this.setData({
  463. // errorMsg: ''
  464. // });
  465. // }
  466. if (this.data.password && (this.data.password.length < 5 || this.data.password.length > 20)) {
  467. this.setData({
  468. errorMsg: '密码长度应为5-20'
  469. });
  470. this.setData({
  471. checkPass: false
  472. });
  473. return;
  474. }
  475. if (!this.checkPassword()) {
  476. this.setData({
  477. errorMsg: '两次密码输入不一致,请重新输入'
  478. });
  479. this.setData({
  480. checkPass: false
  481. });
  482. return;
  483. } else {
  484. this.setData({
  485. errorMsg: ''
  486. });
  487. }
  488. this.setData({
  489. checkPass: true
  490. });
  491. },
  492. phoneCall(){
  493. wx.makePhoneCall({
  494. phoneNumber: this.data.servicetel,
  495. })
  496. },
  497. userAgreement(){
  498. let url = '/pages/agreement/userAgreement'
  499. wx.navigateTo({
  500. url
  501. });
  502. },
  503. /**
  504. * 生命周期函数--监听页面初次渲染完成
  505. */
  506. onReady: function() {
  507. },
  508. /**
  509. * 生命周期函数--监听页面显示
  510. */
  511. onShow: function() {
  512. },
  513. /**
  514. * 生命周期函数--监听页面隐藏
  515. */
  516. onHide: function() {
  517. },
  518. /**
  519. * 生命周期函数--监听页面卸载
  520. */
  521. onUnload: function() {
  522. },
  523. /**
  524. * 页面相关事件处理函数--监听用户下拉动作
  525. */
  526. onPullDownRefresh: function() {
  527. },
  528. /**
  529. * 页面上拉触底事件的处理函数
  530. */
  531. onReachBottom: function() {
  532. },
  533. /**
  534. * 用户点击右上角分享
  535. */
  536. onShareAppMessage: function() {
  537. },
  538. onCheckBoxChange(event) {
  539. this.setData({
  540. checked: event.detail,
  541. });
  542. },
  543. })