register.js 6.7 KB

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