You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

255 lines
5.3KB

  1. // pages/register/register.js
  2. const app = getApp()
  3. let time;
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. yesSend:false,//是否发送验证码
  10. minter: 60,//发送验证码60秒后才可再次发送
  11. num:60,
  12. phone: "",//手机号
  13. phoneCode: "",//验证码
  14. yesRegister:false,//防止重复点击注册
  15. showAll:false,
  16. },
  17. /**
  18. * 生命周期函数--监听页面加载
  19. */
  20. onLoad: function (options) {
  21. if (app.globalData.state==1){//已注册
  22. wx.switchTab({
  23. url: '/pages/index/index'
  24. })
  25. } else if (app.globalData.state == 0){//未注册
  26. this.setData({
  27. showAll:true
  28. })
  29. } else if (app.globalData.state == -1){
  30. app.globalData.userInfoBackFn = res=>{//等待结果
  31. console.log(res)
  32. if (res){
  33. wx.switchTab({
  34. url: '/pages/index/index'
  35. })
  36. }else{
  37. this.setData({
  38. showAll: true
  39. })
  40. }
  41. }
  42. }
  43. },
  44. /**
  45. * 生命周期函数--监听页面初次渲染完成
  46. */
  47. onReady: function () {
  48. },
  49. /**
  50. * 生命周期函数--监听页面显示
  51. */
  52. onShow: function () {
  53. if (wx.canIUse('hideHomeButton')) {
  54. wx.hideHomeButton()
  55. }
  56. },
  57. /**
  58. * 生命周期函数--监听页面隐藏
  59. */
  60. onHide: function () {
  61. },
  62. /**
  63. * 生命周期函数--监听页面卸载
  64. */
  65. onUnload: function () {
  66. },
  67. /**
  68. * 页面相关事件处理函数--监听用户下拉动作
  69. */
  70. onPullDownRefresh: function () {
  71. },
  72. /**
  73. * 页面上拉触底事件的处理函数
  74. */
  75. onReachBottom: function () {
  76. },
  77. /**
  78. * 用户点击右上角分享
  79. */
  80. onShareAppMessage: function () {
  81. },
  82. yesCodeFn:function(){//验证码接口
  83. app.wxRequest(app.globalData.httpUrl + 'smscode', { user_phone: this.data.phone }, e => {
  84. console.log(e)
  85. if (e.code == 200) {
  86. wx.showToast({
  87. title: e.message,
  88. icon: 'success',
  89. duration: 500
  90. })
  91. }else{
  92. wx.showToast({
  93. title: e.message,
  94. icon: 'none',
  95. duration: 500
  96. })
  97. }
  98. }, this)
  99. },
  100. sendTouch:function(){//发送验证码
  101. if (this.data.phone){
  102. if (!(/^1[3456789]\d{9}$/.test(this.data.phone))) {
  103. wx.showToast({
  104. title: '手机号错误',
  105. icon: 'none',
  106. duration: 500
  107. })
  108. return;
  109. }
  110. }else{
  111. wx.showToast({
  112. title: '请先输入手机号',
  113. icon: 'none',
  114. duration: 500
  115. })
  116. return;
  117. }
  118. this.setData({
  119. yesSend: true,
  120. })
  121. if (this.data.yesSend) {
  122. time = setInterval(this.setIntervalFn,1000)
  123. }
  124. if (app.globalData.openId) {
  125. this.yesCodeFn();
  126. } else {
  127. app.globalData.openidSuccessFuc = this.yesCodeFn;
  128. }
  129. },
  130. setIntervalFn:function(){//定时器
  131. if (this.data.num > 0) {
  132. this.data.num--;
  133. if (this.data.num == 0) {
  134. this.setData({
  135. yesSend: false,
  136. })
  137. if (!this.data.yesSend){
  138. this.setData({
  139. num: 60,
  140. minter: 60,
  141. })
  142. }
  143. clearInterval(time);
  144. }
  145. }
  146. this.setData({
  147. minter: this.data.num
  148. })
  149. console.log(this.data.minter)
  150. },
  151. registerBtn:function(){//点击按钮注册
  152. if (app.globalData.openId) {
  153. this.registerFn();
  154. } else {
  155. app.globalData.openidSuccessFuc = this.registerFn;
  156. }
  157. },
  158. registerFn:function(){//注册接口
  159. if (this.data.yesRegister){
  160. return;
  161. }
  162. if(this.data.phone){
  163. if (!(/^1[3456789]\d{9}$/.test(this.data.phone))) {
  164. wx.showToast({
  165. title: '手机号错误',
  166. icon: 'none',
  167. duration: 500
  168. })
  169. return;
  170. }
  171. }else{
  172. wx.showToast({
  173. title: '请先输入手机号',
  174. icon: 'none',
  175. duration: 500
  176. })
  177. return;
  178. }
  179. if (!this.data.phoneCode){
  180. wx.showToast({
  181. title: '请先输入验证码',
  182. icon: 'none',
  183. duration: 500
  184. })
  185. return;
  186. }
  187. app.wxRequest(app.globalData.httpUrl + 'register', { user_phone: this.data.phone, code_num: this.data.phoneCode }, e => {
  188. console.log(e)
  189. this.data.yesRegister = true;
  190. if (e.code == 200) {
  191. clearInterval(time);
  192. app.globalData.user_phone = e.data.user_phone
  193. this.setData({
  194. minter: 60,
  195. num: 60
  196. })
  197. wx.showToast({
  198. title: '注册成功',
  199. icon: 'success',
  200. duration: 500
  201. })
  202. setTimeout(function () {
  203. if (app.globalData.present_id){
  204. wx.reLaunch({
  205. url: '/pages/receiveTicket/receiveTicket?shareId=' + app.globalData.present_id + "&number=" + app.globalData.number
  206. })
  207. } else {
  208. wx.switchTab({
  209. url: '../index/index'
  210. })
  211. }
  212. }, 1000)
  213. } else {
  214. this.data.yesRegister = false;
  215. wx.showToast({
  216. title: e.message,
  217. icon:"none",
  218. duration:500
  219. })
  220. }
  221. }, this,"POST")
  222. },
  223. getPhone: function (e) {//获取手机号
  224. console.log(e.detail.value);
  225. this.setData({
  226. phone: e.detail.value
  227. })
  228. },
  229. getCode: function (e) {//获取验证码
  230. console.log(e.detail.value);
  231. this.setData({
  232. phoneCode: e.detail.value
  233. })
  234. },
  235. })