register.js 11 KB

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