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.

263 line
5.5KB

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