register.js 13 KB

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