register.js 12 KB

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