login.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //index.js
  2. //获取应用实例
  3. const app = getApp()
  4. Page({
  5. data: {
  6. text: "This is page data.",
  7. windowHeight: app.globalData.windowHeight
  8. },
  9. onLoad: function (options) {
  10. var that = this
  11. // 页面创建时执行
  12. //获取本地数据
  13. wx.getStorage({
  14. key: 'userName',
  15. success: function (res) {
  16. console.log(res.data);
  17. that.setData({
  18. userName: res.data
  19. });
  20. }
  21. });
  22. wx.getStorage({
  23. key: 'userPassword',
  24. success: function (res) {
  25. console.log(res.data);
  26. that.setData({
  27. userPassword: res.data
  28. });
  29. }
  30. });
  31. },
  32. onShow: function () {
  33. // 页面出现在前台时执行
  34. },
  35. onReady: function () {
  36. // 页面首次渲染完毕时执行
  37. },
  38. onHide: function () {
  39. // 页面从前台变为后台时执行
  40. },
  41. onUnload: function () {
  42. // 页面销毁时执行
  43. },
  44. onPullDownRefresh: function () {
  45. // 触发下拉刷新时执行
  46. },
  47. onReachBottom: function () {
  48. // 页面触底时执行
  49. },
  50. onShareAppMessage: function () {
  51. // 页面被用户分享时执行
  52. },
  53. onPageScroll: function () {
  54. // 页面滚动时执行
  55. },
  56. onResize: function () {
  57. // 页面尺寸变化时执行
  58. },
  59. onTabItemTap(item) {
  60. // tab 点击时执行
  61. console.log(item.index)
  62. console.log(item.pagePath)
  63. console.log(item.text)
  64. },
  65. // 事件响应函数
  66. viewTap: function () {
  67. this.setData({
  68. text: 'Set some data for updating view.'
  69. }, function () {
  70. // this is setData callback
  71. })
  72. },
  73. // 自由数据
  74. customData: {
  75. hi: 'MINA'
  76. },
  77. //测试登录
  78. formSubmit: function (e) {
  79. //获得表单数据
  80. var objData = e.detail.value;
  81. if (objData.userName && objData.userPassword) {
  82. // 同步方式存储表单数据
  83. wx.setStorage({
  84. key: 'userName',
  85. data: objData.userName
  86. });
  87. wx.setStorage({
  88. key: 'userPassword',
  89. data: objData.userPassword
  90. });
  91. var userName = e.detail.value.userName
  92. var userPassword = e.detail.value.userPassword
  93. var that = this;
  94. var isrightful = that.checkInput(userName, userPassword);
  95. if (isrightful) {
  96. wx.request({
  97. url: 'https://wx.yfzhwlw.com/wechat_login',
  98. header: {
  99. "Content-Type": "application/x-www-form-urlencoded"
  100. },
  101. method: "post",
  102. data: {
  103. username: userName,
  104. password: userPassword
  105. },
  106. success: function (res) {
  107. if (res.statusCode != 200) {
  108. app.showError()
  109. } else {
  110. wx.hideLoading()
  111. //判断是否登录成功
  112. if (res.data.code == '1') {
  113. wx.showToast({
  114. title: '密码错误,请重试', //这里成功
  115. icon: 'none',
  116. duration: 1000
  117. });
  118. } else if (res.data.code == '2') {
  119. wx.showToast({
  120. title: '用户名错误,请重试', //这里成功
  121. icon: 'none',
  122. duration: 1000
  123. });
  124. } else if (res.data.code == '0') {
  125. wx.showToast({
  126. title: '登陆成功', //这里成功
  127. icon: 'success',
  128. duration: 1000
  129. });
  130. that.setData({
  131. isLogin: true,
  132. })
  133. //将账号名保存在本地
  134. wx.setStorage({
  135. key: "login",
  136. data: res.data.username
  137. })
  138. //跳转到home页面
  139. wx.switchTab({
  140. url: '../home/home'
  141. })
  142. }
  143. }
  144. },
  145. fail: function (err) {
  146. app.showError()
  147. }
  148. });
  149. }
  150. }
  151. },
  152. //检测输入值
  153. checkInput: function (userName, userPassword) {
  154. var that = this
  155. if (userName == "" || userName == null ||
  156. userName == undefined) {
  157. that.showErrorToastUtils('请输入用户名');
  158. return false;
  159. } else if (userPassword == "" || userPassword == null || userPassword == undefined) {
  160. that.showErrorToastUtils('请输入密码');
  161. return false;
  162. } else if (userPassword.length < 6) {
  163. that.showErrorToastUtils('密码至少要6位');
  164. return false;
  165. } else {
  166. // wx.showToast({
  167. // title: '填写正确',
  168. // icon: 'success',
  169. // duration: 1500
  170. // })
  171. wx.showLoading({
  172. title: '登录中...',
  173. })
  174. return true;
  175. }
  176. },
  177. })