register.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. },
  19. /**
  20. * 生命周期函数--监听页面加载
  21. */
  22. onLoad: function(options) {
  23. if (wx.getUserProfile) {
  24. this.setData({
  25. canIUseGetUserProfile: true
  26. })
  27. }
  28. //var that = this;
  29. //查看是否授权
  30. //wx.getSetting({
  31. //success(res) {
  32. //console.log(res);
  33. //if (res.authSetting['scope.userInfo']) {
  34. //that.setData({
  35. //authSet: true
  36. //});
  37. //已经授权,可以直接调用 getUserInfo 获取头像昵称
  38. //wx.getUserInfo({
  39. //success(res) {
  40. //console.log(res)
  41. //that.setData({
  42. //userInfo: res.userInfo
  43. //});
  44. //}
  45. //});
  46. //}
  47. //}
  48. //});
  49. },
  50. getUserProfile(e) {
  51. // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认
  52. // 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
  53. let that=this
  54. wx.getUserProfile({
  55. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  56. success: (res) => {
  57. that.setData({
  58. userInfo: res.userInfo,
  59. hasUserInfo: true
  60. })
  61. that.bindGetUserInfo(res.userInfo);
  62. }
  63. })
  64. },
  65. bindGetUserInfo(userInfo) {
  66. //console.log(e.detail.userInfo)
  67. console.info(this.userInfo)
  68. let that = this;
  69. if (userInfo) {
  70. let {
  71. nickName: userName,
  72. gender: sex,
  73. avatarUrl: avatar
  74. } = userInfo;
  75. let {
  76. userName: loginName,
  77. password,
  78. phone,
  79. carNum,
  80. email
  81. } = this.data;
  82. wx.login({
  83. success(res) {
  84. //console.log(res);
  85. if (res.code) {
  86. wx.request({
  87. url: getApp().globalData.postHeadAgreement + '/restapi/wechat/register',
  88. data: {
  89. userName,
  90. sex,
  91. avatar,
  92. loginName,
  93. password,
  94. phonenumber: phone,
  95. email,
  96. license_number: carNum,
  97. code: res.code
  98. },
  99. method: 'POST',
  100. success(res1) {
  101. //console.log(res1);
  102. //console.log(res1.data.code == 1);
  103. if (res1.data.code == 1) {
  104. that.setData({
  105. errorMsg: res1.data.msg
  106. })
  107. } else {
  108. //返回该用户
  109. //跳转到上一页
  110. wx.setStorageSync("userInfo", res1.data);
  111. wx.setStorageSync("isLogin", true);
  112. wx.navigateBack();
  113. //let url = `/pages/ucenter/index/index`;
  114. //wx.redirectTo({
  115. //url
  116. //});
  117. }
  118. }
  119. });
  120. } else {
  121. console.log('登录失败!' + res.errMsg)
  122. }
  123. }
  124. });
  125. }
  126. },
  127. inputAccount(e) {
  128. this.setData({
  129. userName: e.detail.value
  130. });
  131. this.checkAll();
  132. },
  133. inputPassword(e) {
  134. this.setData({
  135. password: e.detail.value
  136. });
  137. this.checkAll();
  138. },
  139. inputRepassword(e) {
  140. this.setData({
  141. rePassword: e.detail.value
  142. });
  143. this.checkAll();
  144. },
  145. inputPhone(e) {
  146. this.setData({
  147. phone: e.detail.value
  148. });
  149. this.checkAll();
  150. },
  151. inputEmail(e) {
  152. this.setData({
  153. email: e.detail.value
  154. });
  155. this.checkAll();
  156. },
  157. inputCarNum(e){
  158. this.setData({
  159. carNum: e.detail.value
  160. });
  161. this.checkAll();
  162. },
  163. checkPassword() {
  164. if (!this.data.password || !this.data.rePassword || this.data.password != this.data.rePassword) {
  165. return false;
  166. }
  167. return true;
  168. },
  169. checkPhone() {
  170. if (!(/^1[3456789]\d{9}$/.test(this.data.phone))) {
  171. return false;
  172. }
  173. return true;
  174. },
  175. checkMail() {
  176. if (!(/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/.test(this.data.email))) {
  177. return false;
  178. }
  179. return true;
  180. },
  181. checkCarNum(){
  182. if (this.data.carNum){
  183. // 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))) {
  184. if (!/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领].*$/.test(this.data.carNum)) {
  185. return false;
  186. }
  187. }
  188. return true;
  189. },
  190. checkAll() {
  191. if (!this.data.userName || !this.data.userName.replace(/(^\s*)|(\s*$)/g,"")) {
  192. this.setData({
  193. errorMsg: '账号不能为空'
  194. });
  195. this.setData({
  196. checkPass: false
  197. });
  198. return;
  199. } else if (this.data.userName.length < 2 || this.data.userName.length > 20) {
  200. this.setData({
  201. errorMsg: '账号长度应为2-20'
  202. });
  203. this.setData({
  204. checkPass: false
  205. });
  206. return;
  207. } else if (!(/^[a-zA-Z0-9_.\u4e00-\u9fa5]+$/.test(this.data.userName))) {
  208. this.setData({
  209. errorMsg: '账号只能包含字母、数字、中文、下划线和点'
  210. });
  211. this.setData({
  212. checkPass: false
  213. });
  214. return;
  215. }else{
  216. this.setData({
  217. errorMsg: ''
  218. });
  219. }
  220. if (!this.checkPhone()) {
  221. this.setData({
  222. errorMsg: '手机号输入有误,请重新输入'
  223. });
  224. this.setData({
  225. checkPass: false
  226. });
  227. return;
  228. } else {
  229. this.setData({
  230. errorMsg: ''
  231. });
  232. }
  233. if (!this.checkMail()) {
  234. this.setData({
  235. errorMsg: '邮箱输入有误,请重新输入'
  236. });
  237. this.setData({
  238. checkPass: false
  239. });
  240. return;
  241. } else {
  242. this.setData({
  243. errorMsg: ''
  244. });
  245. }
  246. if (!this.checkCarNum()) {
  247. this.setData({
  248. errorMsg: '车牌号输入有误,请重新输入'
  249. });
  250. this.setData({
  251. checkPass: false
  252. });
  253. return;
  254. } else {
  255. this.setData({
  256. errorMsg: ''
  257. });
  258. }
  259. if (this.data.password && (this.data.password.length < 5 || this.data.password.length > 20)) {
  260. this.setData({
  261. errorMsg: '密码长度应为5-20'
  262. });
  263. this.setData({
  264. checkPass: false
  265. });
  266. return;
  267. }
  268. if (!this.checkPassword()) {
  269. this.setData({
  270. errorMsg: '两次密码输入不一致,请重新输入'
  271. });
  272. this.setData({
  273. checkPass: false
  274. });
  275. return;
  276. } else {
  277. this.setData({
  278. errorMsg: ''
  279. });
  280. }
  281. this.setData({
  282. checkPass: true
  283. });
  284. },
  285. /**
  286. * 生命周期函数--监听页面初次渲染完成
  287. */
  288. onReady: function() {
  289. },
  290. /**
  291. * 生命周期函数--监听页面显示
  292. */
  293. onShow: function() {
  294. },
  295. /**
  296. * 生命周期函数--监听页面隐藏
  297. */
  298. onHide: function() {
  299. },
  300. /**
  301. * 生命周期函数--监听页面卸载
  302. */
  303. onUnload: function() {
  304. },
  305. /**
  306. * 页面相关事件处理函数--监听用户下拉动作
  307. */
  308. onPullDownRefresh: function() {
  309. },
  310. /**
  311. * 页面上拉触底事件的处理函数
  312. */
  313. onReachBottom: function() {
  314. },
  315. /**
  316. * 用户点击右上角分享
  317. */
  318. onShareAppMessage: function() {
  319. }
  320. })