register.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. return false;
  185. }
  186. }
  187. return true;
  188. },
  189. checkAll() {
  190. if (!this.data.userName || !this.data.userName.replace(/(^\s*)|(\s*$)/g,"")) {
  191. this.setData({
  192. errorMsg: '账号不能为空'
  193. });
  194. this.setData({
  195. checkPass: false
  196. });
  197. return;
  198. } else if (this.data.userName.length < 2 || this.data.userName.length > 20) {
  199. this.setData({
  200. errorMsg: '账号长度应为2-20'
  201. });
  202. this.setData({
  203. checkPass: false
  204. });
  205. return;
  206. } else if (!(/^[a-zA-Z0-9_.\u4e00-\u9fa5]+$/.test(this.data.userName))) {
  207. this.setData({
  208. errorMsg: '账号只能包含字母、数字、中文、下划线和点'
  209. });
  210. this.setData({
  211. checkPass: false
  212. });
  213. return;
  214. }else{
  215. this.setData({
  216. errorMsg: ''
  217. });
  218. }
  219. if (!this.checkPhone()) {
  220. this.setData({
  221. errorMsg: '手机号输入有误,请重新输入'
  222. });
  223. this.setData({
  224. checkPass: false
  225. });
  226. return;
  227. } else {
  228. this.setData({
  229. errorMsg: ''
  230. });
  231. }
  232. if (!this.checkMail()) {
  233. this.setData({
  234. errorMsg: '邮箱输入有误,请重新输入'
  235. });
  236. this.setData({
  237. checkPass: false
  238. });
  239. return;
  240. } else {
  241. this.setData({
  242. errorMsg: ''
  243. });
  244. }
  245. if (!this.checkCarNum()) {
  246. this.setData({
  247. errorMsg: '车牌号输入有误,请重新输入'
  248. });
  249. this.setData({
  250. checkPass: false
  251. });
  252. return;
  253. } else {
  254. this.setData({
  255. errorMsg: ''
  256. });
  257. }
  258. if (this.data.password && (this.data.password.length < 5 || this.data.password.length > 20)) {
  259. this.setData({
  260. errorMsg: '密码长度应为5-20'
  261. });
  262. this.setData({
  263. checkPass: false
  264. });
  265. return;
  266. }
  267. if (!this.checkPassword()) {
  268. this.setData({
  269. errorMsg: '两次密码输入不一致,请重新输入'
  270. });
  271. this.setData({
  272. checkPass: false
  273. });
  274. return;
  275. } else {
  276. this.setData({
  277. errorMsg: ''
  278. });
  279. }
  280. this.setData({
  281. checkPass: true
  282. });
  283. },
  284. /**
  285. * 生命周期函数--监听页面初次渲染完成
  286. */
  287. onReady: function() {
  288. },
  289. /**
  290. * 生命周期函数--监听页面显示
  291. */
  292. onShow: function() {
  293. },
  294. /**
  295. * 生命周期函数--监听页面隐藏
  296. */
  297. onHide: function() {
  298. },
  299. /**
  300. * 生命周期函数--监听页面卸载
  301. */
  302. onUnload: function() {
  303. },
  304. /**
  305. * 页面相关事件处理函数--监听用户下拉动作
  306. */
  307. onPullDownRefresh: function() {
  308. },
  309. /**
  310. * 页面上拉触底事件的处理函数
  311. */
  312. onReachBottom: function() {
  313. },
  314. /**
  315. * 用户点击右上角分享
  316. */
  317. onShareAppMessage: function() {
  318. }
  319. })