index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // index.js
  2. let myPro = require("../../../utils/wxRequest.js");
  3. let util = require("../../../utils/util.js");
  4. Page({
  5. data: {
  6. phone: null,
  7. code: null,
  8. codeLoading: false,
  9. codeBtnName: "获取验证码",
  10. delyTime: 60, // 两次发验证码间隔时长/秒
  11. loading: false // 全局loading
  12. },
  13. onLoad: function (options) {
  14. // Do some initialize when page load.
  15. },
  16. onShow: function () {
  17. // Do something when page show.
  18. let that = this;
  19. that.setData({
  20. userInfo: getApp().globalData.userInfo
  21. });
  22. // console.log('用户信息',that.data.userInfo)
  23. // 店铺
  24. wx.getStorage({
  25. key: "store",
  26. success: function(res){
  27. that.setData({
  28. store: JSON.parse(res.data)
  29. })
  30. }
  31. });
  32. },
  33. onReady: function () {
  34. // Do something when page ready.
  35. },
  36. onHide: function () {
  37. // Do something when page hide.
  38. },
  39. onUnload: function () {
  40. // Do something when page close.
  41. },
  42. onPullDownRefresh: function () {
  43. // Do something when pull down.
  44. },
  45. onReachBottom: function () {
  46. // Do something when page reach bottom.
  47. },
  48. onPageScroll: function () {
  49. // Do something when page scroll
  50. },
  51. onResize: function () {
  52. // Do something when page resize
  53. },
  54. // 获取输入信息 手机号
  55. getPhone: function (event) {
  56. let that = this;
  57. that.setData({
  58. phone: event.detail,
  59. });
  60. },
  61. // 发送验证码
  62. sendCode(){
  63. let that = this;
  64. if(!that.data.phone || !(/^1[3456789]\d{9}$/.test(that.data.phone))){
  65. wx.showToast({
  66. title: "手机号有误,请重新填写",
  67. icon: "none",
  68. });
  69. return
  70. };
  71. let params = {
  72. phone: that.data.phone
  73. };
  74. myPro.wxRequest("user/send/verify-code","POST",params).then(res=>{
  75. wx.showToast({
  76. title: res.msg,
  77. icon: "none"
  78. });
  79. that.setData({
  80. codeBtnName: that.data.delyTime +
  81. "s",
  82. delyTime: that.data.delyTime,
  83. codeLoading: true
  84. });
  85. let s = setInterval(function() {
  86. let dely1 = parseInt(that.data.delyTime) -
  87. 1;
  88. // console.log(dely1);
  89. that.setData({
  90. codeBtnName: dely1 +
  91. "s",
  92. delyTime: dely1
  93. });
  94. if (dely1 == 0) {
  95. clearInterval(s);
  96. that.setData({
  97. codeBtnName: "获取验证码",
  98. delyTime: that.data.delyTime,
  99. codeLoading: false
  100. });
  101. }
  102. }, 1000);
  103. }).catch(err=>{
  104. that.setData({
  105. codeLoading: false
  106. });
  107. console.log('报错信息');
  108. wx.showToast({
  109. title: err,
  110. icon: "none"
  111. })
  112. })
  113. },
  114. // 获取输入信息 验证码
  115. getPhoneCode: function (event) {
  116. let that = this;
  117. that.setData({
  118. code: event.detail,
  119. });
  120. },
  121. // 手机号验证码登录
  122. phoneLogin() {
  123. let that = this;
  124. if (!that.data.phone || !/^1[3456789]\d{9}$/.test(that.data.phone)) {
  125. wx.showToast({
  126. title: "手机号有误,请重新填写",
  127. icon: "none",
  128. });
  129. return;
  130. }
  131. if (!that.data.code) {
  132. wx.showToast({
  133. title: "请填写验证码",
  134. icon: "none",
  135. });
  136. return;
  137. };
  138. let params = {
  139. phone: that.data.phone,
  140. code: that.data.code, // 手机号验证码
  141. openid: getApp().globalData.openid,
  142. nickname: getApp().globalData.userInfo.nickname,
  143. thumb: getApp().globalData.userInfo.thumb,
  144. sex: getApp().globalData.userInfo.sex,
  145. store_id: that.data.store ? that.data.store.id : 1 // 门店id
  146. };
  147. myPro.wxRequest("user/mini-login","POST",params).then(res=>{
  148. // 存下token
  149. getApp().globalData.token = res.result.token;
  150. wx.setStorage({
  151. key: "token",
  152. data: res.result.token
  153. });
  154. // 存下角色
  155. if(res.result.user.store){
  156. getApp().globalData.role = 1 // 有店铺,即为门店
  157. };
  158. wx.reLaunch({
  159. url: '/pages/index/index'
  160. });
  161. }).catch(err=>{
  162. console.log('报错信息',err);
  163. wx.showToast({
  164. title: err,
  165. icon: "none"
  166. })
  167. })
  168. },
  169. });