register.js 14 KB

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