login.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // pages/login/login.js
  2. import {
  3. Home
  4. } from '../../utils/home.js';
  5. var home = new Home(); //实例化 首页 对象
  6. // pages/index/index.js
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. },
  13. // nameInput: function (e) {
  14. // let that = this
  15. // that.setData({
  16. // userName: e.detail.value
  17. // })
  18. // },
  19. telInput: function (e) {
  20. let that = this
  21. that.setData({
  22. tel: e.detail.value
  23. })
  24. },
  25. passWordInput: function (e) {
  26. let that = this
  27. that.setData({
  28. passWord: e.detail.value
  29. })
  30. },
  31. intoButton: function () {
  32. let that = this;
  33. if (!that.data.tel) {
  34. wx.showToast({
  35. title: '请将信息填写完整',
  36. icon: 'none'
  37. })
  38. return;
  39. }
  40. if (!(/^1[34578]\d{9}$/.test(that.data.tel))) {
  41. wx.showToast({
  42. title: '手机号格式有误,请重填',
  43. icon: 'none'
  44. })
  45. return;
  46. }
  47. if (!that.data.passWord) {
  48. wx.showToast({
  49. title: '登录密码不能为空',
  50. icon: 'none'
  51. })
  52. return;
  53. }
  54. // if (!that.data.userName) {
  55. // wx.showToast({
  56. // title: '姓名不能为空',
  57. // icon: 'none'
  58. // })
  59. // return;
  60. // }
  61. var params = {
  62. tel: that.data.tel,
  63. admin_password: that.data.passWord,
  64. }
  65. home.login(params, (res) => {
  66. if (res.code == 1) {
  67. wx.setStorageSync('cwxcx_token', res.data.token)
  68. wx.setStorageSync('user_id', res.data.user_id)
  69. wx.setStorageSync('user_name', res.data.user_name)
  70. wx.setStorageSync('tel', that.data.tel)
  71. wx.switchTab({
  72. url: '/pages/index/index',
  73. success: function(res) {},
  74. fail: function(res) {},
  75. complete: function(res) {},
  76. })
  77. } else {
  78. wx.showToast({
  79. title: res.msg,
  80. icon: 'none',
  81. duration: 1500,
  82. mask: true,
  83. success: function (res) { },
  84. fail: function (res) { },
  85. complete: function (res) { },
  86. })
  87. }
  88. })
  89. },
  90. /**
  91. * 生命周期函数--监听页面加载
  92. */
  93. onLoad: function (options) {
  94. var token = wx.getStorageSync('cwxcx_token')
  95. if (token) {
  96. wx.switchTab({
  97. url: '../index/index',
  98. success: function (res) { },
  99. fail: function (res) { },
  100. complete: function (res) { },
  101. })
  102. }
  103. },
  104. /**
  105. * 生命周期函数--监听页面初次渲染完成
  106. */
  107. onReady: function () {
  108. },
  109. /**
  110. * 生命周期函数--监听页面显示
  111. */
  112. onShow: function () {
  113. },
  114. /**
  115. * 生命周期函数--监听页面隐藏
  116. */
  117. onHide: function () {
  118. },
  119. /**
  120. * 生命周期函数--监听页面卸载
  121. */
  122. onUnload: function () {
  123. },
  124. /**
  125. * 页面相关事件处理函数--监听用户下拉动作
  126. */
  127. onPullDownRefresh: function () {
  128. },
  129. /**
  130. * 页面上拉触底事件的处理函数
  131. */
  132. onReachBottom: function () {
  133. },
  134. /**
  135. * 用户点击右上角分享
  136. */
  137. onShareAppMessage: function () {
  138. }
  139. })