register.js 5.8 KB

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