register.js 12 KB

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