register.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // pages/register/register.js
  2. var http = require('../../utils/http')
  3. var util = require('../../utils/util.js');
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. registerStep: 1, // 注册步骤
  10. errorTips: 0, // 输入错误提示: 1手机号输入错误 2验证码输入错误 3账号输入错误 4密码输入错误 5验证密码输入错误
  11. mobile: '', //手机号
  12. validCode: '', //验证码
  13. userName: '', //用户名
  14. password: '', //密码
  15. confirmPwd: '', //确认密码
  16. checkRegisterSmsFlag: '', // 校验登陆注册验证码成功的标识
  17. // 验证码相关
  18. show: true,
  19. count: '',
  20. timer: null,
  21. hadGotCode: false,
  22. isResting: false,
  23. },
  24. /**
  25. * 生命周期函数--监听页面加载
  26. */
  27. onLoad: function (options) {
  28. },
  29. /**
  30. * 输入框的值
  31. */
  32. getInputVal: function (e) {
  33. const type = e.currentTarget.dataset.type
  34. if (type == 'mobile') {
  35. this.setData({
  36. mobile: e.detail.value
  37. })
  38. } else if (type == 'validCode') {
  39. this.setData({
  40. validCode: e.detail.value
  41. })
  42. } else if (type == "userName") {
  43. this.setData({
  44. userName: e.detail.value
  45. })
  46. } else if (type == "password") {
  47. this.setData({
  48. password: e.detail.value
  49. })
  50. } else if (type == "confirmPwd") {
  51. this.setData({
  52. confirmPwd: e.detail.value
  53. })
  54. }
  55. },
  56. /**
  57. * 获取验证码
  58. */
  59. getCode: function () {
  60. if (!util.checkPhoneNumber(this.data.mobile)) {
  61. this.setData({
  62. errorTips: 1
  63. })
  64. return
  65. }
  66. if (this.data.hadGotCode) {
  67. return
  68. }
  69. this.setData({
  70. hadGotCode: true,
  71. errorTips: 0
  72. })
  73. console.log('获取验证码')
  74. var params = {
  75. url: "/user/sendRegisterSms",
  76. method: "put",
  77. data: {
  78. mobile: this.data.mobile,
  79. },
  80. callBack: res => {
  81. this.setData({
  82. hadGotCode: true
  83. })
  84. const timeCount = 60;
  85. if (!this.data.timer) {
  86. let count = timeCount
  87. let timer = this.data.timer
  88. setTimeout(() => {
  89. this.setData({
  90. count: count,
  91. show: false
  92. })
  93. }, 1000)
  94. timer = setInterval(() => {
  95. if (count > 0 && count <= timeCount) {
  96. this.setData({
  97. count: count--
  98. })
  99. } else {
  100. clearInterval(timer);
  101. this.setData({
  102. timer: null,
  103. show: true,
  104. hadGotCode: false
  105. })
  106. }
  107. }, 1000)
  108. }
  109. }
  110. };
  111. http.request(params);
  112. },
  113. /**
  114. * 注册-下一步按钮
  115. */
  116. registerNext: function () {
  117. if (this.data.registerStep == 1) {
  118. console.log('第一步')
  119. if (!util.checkPhoneNumber(this.data.mobile)) {
  120. console.log('手机号出错')
  121. this.setData({
  122. errorTips: 1
  123. })
  124. return
  125. }
  126. else if (!this.data.validCode.trim()) {
  127. console.log(this.data.validCode)
  128. this.setData({
  129. errorTips: 2
  130. })
  131. return
  132. } else {
  133. console.log('校验验证码')
  134. this.setData({
  135. errorTips: 0
  136. })
  137. // 校验验证码
  138. var params = {
  139. url: "/user/checkRegisterSms",
  140. method: "put",
  141. data: {
  142. mobile: this.data.mobile,
  143. validCode: this.data.validCode
  144. },
  145. callBack: res => {
  146. let registerStep = this.data.registerStep
  147. registerStep += 1
  148. this.setData({
  149. registerStep,
  150. checkRegisterSmsFlag: res
  151. })
  152. console.log(this.data.registerStep)
  153. },
  154. };
  155. http.request(params);
  156. // let registerStep = this.registerStep += 1;
  157. // this.registerStep = registerStep
  158. }
  159. } else if (this.data.registerStep == 2) {
  160. console.log('第二步')
  161. if (!util.checkUserName(this.data.userName)) {
  162. this.setData({
  163. errorTips: 3
  164. })
  165. return
  166. } else if (!this.data.password.trim()) {
  167. this.setData({
  168. errorTips: 4
  169. })
  170. return
  171. } else if (this.data.password != this.data.confirmPwd) {
  172. this.setData({
  173. errorTips: 5
  174. })
  175. return
  176. } else {
  177. this.setData({
  178. errorTips: 0
  179. })
  180. wx.showLoading()
  181. console.log('请求注册接口')
  182. // 请求注册
  183. var params = {
  184. url: "/user/registerOrBindUser",
  185. method: "put",
  186. data: {
  187. appType: 1, // 应用类型 1小程序 2微信公众号 3 PC 4 H5
  188. checkRegisterSmsFlag: this.data.checkRegisterSmsFlag, // 校验登陆注册验证码成功的标识
  189. mobile: this.data.mobile,
  190. userName: this.data.userName,
  191. password: this.data.password,
  192. validateType: 1, // 验证类型 1验证码验证 2 小程序encryptedData验证 3 密码验证
  193. registerOrBind: 1, // 验证类型 1注册 2绑定
  194. },
  195. callBack: res => {
  196. wx.hideLoading()
  197. let registerStep = this.data.registerStep
  198. registerStep += 1
  199. this.setData({
  200. registerStep
  201. })
  202. wx.setStorageSync('loginResult', res)
  203. wx.setStorageSync('token', 'bearer' + res.access_token);
  204. }
  205. };
  206. http.request(params);
  207. console.log('注册成功')
  208. }
  209. }
  210. },
  211. /**
  212. * 去登陆
  213. */
  214. toLogin: function() {
  215. console.log('去登录')
  216. wx.navigateTo({
  217. url: "/pages/accountLogin/accountLogin"
  218. })
  219. },
  220. /**
  221. * 回到首页
  222. */
  223. toIndex: function () {
  224. wx.switchTab({
  225. url: '/pages/index/index'
  226. });
  227. },
  228. /**
  229. * 生命周期函数--监听页面初次渲染完成
  230. */
  231. onReady: function () {
  232. },
  233. /**
  234. * 生命周期函数--监听页面显示
  235. */
  236. onShow: function () {
  237. },
  238. /**
  239. * 生命周期函数--监听页面隐藏
  240. */
  241. onHide: function () {
  242. },
  243. /**
  244. * 生命周期函数--监听页面卸载
  245. */
  246. onUnload: function () {
  247. },
  248. /**
  249. * 页面相关事件处理函数--监听用户下拉动作
  250. */
  251. onPullDownRefresh: function () {
  252. },
  253. /**
  254. * 页面上拉触底事件的处理函数
  255. */
  256. onReachBottom: function () {
  257. },
  258. /**
  259. * 用户点击右上角分享
  260. */
  261. onShareAppMessage: function () {
  262. }
  263. })