register.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. var api = require('../../../config/api.js');
  2. var check = require('../../../utils/check.js');
  3. var app = getApp();
  4. Page({
  5. data: {
  6. username: '',
  7. password: '',
  8. confirmPassword: '',
  9. mobile: '',
  10. code: ''
  11. },
  12. onLoad: function(options) {
  13. // 页面初始化 options为页面跳转所带来的参数
  14. // 页面渲染完成
  15. },
  16. onReady: function() {
  17. },
  18. onShow: function() {
  19. // 页面显示
  20. },
  21. onHide: function() {
  22. // 页面隐藏
  23. },
  24. onUnload: function() {
  25. // 页面关闭
  26. },
  27. sendCode: function() {
  28. let that = this;
  29. if (this.data.mobile.length == 0) {
  30. wx.showModal({
  31. title: '错误信息',
  32. content: '手机号不能为空',
  33. showCancel: false
  34. });
  35. return false;
  36. }
  37. if (!check.isValidPhone(this.data.mobile)) {
  38. wx.showModal({
  39. title: '错误信息',
  40. content: '手机号输入不正确',
  41. showCancel: false
  42. });
  43. return false;
  44. }
  45. wx.request({
  46. url: api.AuthRegisterCaptcha,
  47. data: {
  48. mobile: that.data.mobile
  49. },
  50. method: 'POST',
  51. header: {
  52. 'content-type': 'application/json'
  53. },
  54. success: function(res) {
  55. if (res.data.errno == 0) {
  56. wx.showModal({
  57. title: '发送成功',
  58. content: '验证码已发送',
  59. showCancel: false
  60. });
  61. } else {
  62. wx.showModal({
  63. title: '错误信息',
  64. content: res.data.errmsg,
  65. showCancel: false
  66. });
  67. }
  68. }
  69. });
  70. },
  71. requestRegister: function(wxCode) {
  72. let that = this;
  73. wx.request({
  74. url: api.AuthRegister,
  75. data: {
  76. username: that.data.username,
  77. password: that.data.password,
  78. mobile: that.data.mobile,
  79. code: that.data.code,
  80. wxCode: wxCode
  81. },
  82. method: 'POST',
  83. header: {
  84. 'content-type': 'application/json'
  85. },
  86. success: function(res) {
  87. if (res.data.errno == 0) {
  88. app.globalData.hasLogin = true;
  89. wx.setStorageSync('userInfo', res.data.data.userInfo);
  90. wx.setStorage({
  91. key: "token",
  92. data: res.data.data.token,
  93. success: function() {
  94. wx.switchTab({
  95. url: '/pages/ucenter/index/index'
  96. });
  97. }
  98. });
  99. } else {
  100. wx.showModal({
  101. title: '错误信息',
  102. content: res.data.errmsg,
  103. showCancel: false
  104. });
  105. }
  106. }
  107. });
  108. },
  109. startRegister: function() {
  110. var that = this;
  111. if (this.data.password.length < 6 || this.data.username.length < 6) {
  112. wx.showModal({
  113. title: '错误信息',
  114. content: '用户名和密码不得少于6位',
  115. showCancel: false
  116. });
  117. return false;
  118. }
  119. if (this.data.password != this.data.confirmPassword) {
  120. wx.showModal({
  121. title: '错误信息',
  122. content: '确认密码不一致',
  123. showCancel: false
  124. });
  125. return false;
  126. }
  127. if (this.data.mobile.length == 0 || this.data.code.length == 0) {
  128. wx.showModal({
  129. title: '错误信息',
  130. content: '手机号和验证码不能为空',
  131. showCancel: false
  132. });
  133. return false;
  134. }
  135. if (!check.isValidPhone(this.data.mobile)) {
  136. wx.showModal({
  137. title: '错误信息',
  138. content: '手机号输入不正确',
  139. showCancel: false
  140. });
  141. return false;
  142. }
  143. wx.login({
  144. success: function(res) {
  145. if (!res.code) {
  146. wx.showModal({
  147. title: '错误信息',
  148. content: '注册失败',
  149. showCancel: false
  150. });
  151. }
  152. that.requestRegister(res.code);
  153. }
  154. });
  155. },
  156. bindUsernameInput: function(e) {
  157. this.setData({
  158. username: e.detail.value
  159. });
  160. },
  161. bindPasswordInput: function(e) {
  162. this.setData({
  163. password: e.detail.value
  164. });
  165. },
  166. bindConfirmPasswordInput: function(e) {
  167. this.setData({
  168. confirmPassword: e.detail.value
  169. });
  170. },
  171. bindMobileInput: function(e) {
  172. this.setData({
  173. mobile: e.detail.value
  174. });
  175. },
  176. bindCodeInput: function(e) {
  177. this.setData({
  178. code: e.detail.value
  179. });
  180. },
  181. clearInput: function(e) {
  182. switch (e.currentTarget.id) {
  183. case 'clear-username':
  184. this.setData({
  185. username: ''
  186. });
  187. break;
  188. case 'clear-password':
  189. this.setData({
  190. password: ''
  191. });
  192. break;
  193. case 'clear-confirm-password':
  194. this.setData({
  195. confirmPassword: ''
  196. });
  197. break;
  198. case 'clear-mobile':
  199. this.setData({
  200. mobile: ''
  201. });
  202. break;
  203. case 'clear-code':
  204. this.setData({
  205. code: ''
  206. });
  207. break;
  208. }
  209. }
  210. })