login.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <template>
  2. <view class="login">
  3. <view class="content">
  4. <!-- 头部logo -->
  5. <view class="header">
  6. <image :src="`../../static/123.png`"></image>
  7. </view>
  8. <!-- 主体表单 -->
  9. <view class="main">
  10. <wInput v-model="username" type="text" placeholder="用户名" :focus="isFocus"></wInput>
  11. <wInput v-model="password" type="text" placeholder="登录密码"></wInput>
  12. </view>
  13. <wButton class="wbutton" text="登 录" :rotate="isRotate" @click="startLogin"></wButton>
  14. <view style="height: 100rpx;"></view>
  15. </view>
  16. </view>
  17. </template>
  18. <script>
  19. import wInput from '../../components/watch-login/watch-input.vue'
  20. import wButton from '../../components/watch-login/watch-button.vue'
  21. import API from '../../api/index'
  22. var _self
  23. var code
  24. export default {
  25. data() {
  26. return {
  27. logoImage:'',
  28. username: 'admin', //用户/电话
  29. password: '1111111', //密码
  30. isRotate: false, //是否加载旋转
  31. isFocus: true // 是否聚焦
  32. };
  33. },
  34. components: {
  35. wInput,
  36. wButton,
  37. },
  38. mounted() {
  39. },
  40. onLoad() {
  41. _self = this
  42. _self.isLogin()
  43. wx.login({
  44. success(res) {
  45. if (res.code) {
  46. code = res.code
  47. }
  48. }
  49. })
  50. },
  51. methods: {
  52. isLogin() {
  53. //判断缓存中是否登录过,直接登录
  54. try {
  55. const token = uni.getStorageSync('equipment_token');
  56. const equipmentManage = uni.getStorageSync('equipment_manage');
  57. if (token) {
  58. //有登录信息
  59. getApp().globalData.token = token
  60. getApp().globalData.equipmentManage = equipmentManage
  61. const eventChannel = _self.getOpenerEventChannel()
  62. eventChannel.emit('isLoginFromLogin', {
  63. isLogin: true
  64. })
  65. uni.$emit('loginStatusEvent', {
  66. isLogin: true
  67. })
  68. }
  69. } catch (e) {
  70. // error
  71. }
  72. },
  73. startLogin(e) {
  74. //登录
  75. if (_self.isRotate) {
  76. //判断是否加载中,避免重复点击请求
  77. return false
  78. }
  79. if (_self.username.length == "") {
  80. uni.showToast({
  81. title: '用户名不能为空',
  82. icon: 'none',
  83. position: 'center'
  84. })
  85. return
  86. }
  87. if (_self.password.length < 5) {
  88. uni.showToast({
  89. title: '登录密码不正确',
  90. icon: 'none',
  91. position: 'center'
  92. })
  93. return
  94. }
  95. _self.isRotate = true
  96. uni.showLoading({
  97. title: '登录中.....'
  98. })
  99. uni.request({
  100. url: API.Login,
  101. method: 'POST',
  102. data: {
  103. username: _self.username,
  104. password: _self.password
  105. },
  106. header: {
  107. 'content-type': 'application/json'
  108. },
  109. success: (res) => {
  110. uni.hideLoading()
  111. _self.isRotate = false
  112. console.log('登录用户响应返回数据信息:', res.data);
  113. // 检查响应是否有效
  114. if (!res.data) {
  115. _self.password = ''
  116. console.error('登录失败: 响应数据为空');
  117. uni.showToast({
  118. title: '登录失败: 服务器响应异常',
  119. icon: 'none',
  120. position: 'center'
  121. })
  122. return;
  123. }
  124. // 处理JSON响应
  125. let responseData = res.data;
  126. // 统一转换为对象格式(如果是字符串的话)
  127. if (typeof responseData === 'string') {
  128. try {
  129. responseData = JSON.parse(responseData);
  130. } catch (e) {
  131. console.error('解析响应数据失败:', e);
  132. uni.showToast({
  133. title: '登录失败: 服务器返回格式错误',
  134. icon: 'none',
  135. position: 'center'
  136. })
  137. return;
  138. }
  139. }
  140. // 根据code判断登录是否成功
  141. // 登录成功后增加本地存储
  142. if (responseData.code === 0 && responseData.msg === '登录成功') {
  143. // 登录成功,提取用户信息、token和过期时间
  144. const userInfo = responseData.data.user || {};
  145. const ID = userInfo.id || '';
  146. const token = responseData.data.token || '';
  147. const expireAt = responseData.data.expiresAt || '';
  148. const building = userInfo.building || '';
  149. const room = userInfo.room || '';
  150. const pen = userInfo.pen || '';
  151. // 设置全局登录状态和信息
  152. const app = getApp();
  153. // 确保globalData对象存在
  154. if (!app.globalData) {
  155. app.globalData = {};
  156. }
  157. // 统一设置全局数据
  158. app.globalData.ID = ID;
  159. app.globalData.token = token;
  160. app.globalData.userInfo = userInfo;
  161. app.globalData.isLoggedIn = true;
  162. app.globalData.expireAt = expireAt;
  163. // 设置全局数据
  164. app.globalData.building = building; // 栋舍编号
  165. app.globalData.room = room; // 房间编号
  166. app.globalData.pen = pen; // 栏位编号
  167. app.globalData.buildingName = building;
  168. app.globalData.roomName = room;
  169. app.globalData.penNo = pen;
  170. app.globalData.equipment_manage = '';
  171. // 持久化到本地存储,确保应用重启后仍能保留登录状态
  172. try {
  173. uni.setStorageSync('equipment_token', token);
  174. uni.setStorageSync('token_expire_time', expireAt);
  175. uni.setStorageSync('user_info', userInfo);
  176. uni.setStorageSync('building', building);
  177. uni.setStorageSync('room', room);
  178. uni.setStorageSync('pen', pen);
  179. console.log('登录信息已保存到本地存储');
  180. } catch (e) {
  181. console.error('保存登录信息到本地存储失败:', e);
  182. }
  183. // 触发登录状态更新事件
  184. uni.$emit('loginStatusEvent', {
  185. isLogin: true
  186. });
  187. setTimeout(function() {
  188. // 登录成功后跳转到首页,避免直接跳到个人中心导致页面切换问题
  189. uni.switchTab({
  190. url: '/pages/index/index',
  191. success: function(res) {
  192. console.log('跳转到首页成功:', res);
  193. // 跳转成功后,通知首页加载用户编号信息
  194. uni.$emit('reloadUserSettings', {});
  195. },
  196. fail: function(err) {
  197. console.error('跳转到首页失败:', err);
  198. // 如果switchTab失败,尝试使用redirectTo作为备选方案
  199. if (err.errMsg && err.errMsg.includes('tabBar')) {
  200. console.warn('switchTab失败,尝试使用redirectTo');
  201. uni.redirectTo({
  202. url: '/pages/index/index',
  203. success: function(res) {
  204. console.log('redirectTo到首页成功:', res);
  205. uni.$emit('reloadUserSettings', {});
  206. },
  207. fail: function(redirectErr) {
  208. console.error('redirectTo到首页也失败:', redirectErr);
  209. uni.showToast({
  210. title: '跳转失败,请手动进入首页',
  211. icon: 'none'
  212. });
  213. }
  214. });
  215. }
  216. }
  217. })
  218. }, 500)
  219. } else {
  220. const errorMsg = responseData.msg || '登录失败';
  221. console.log('登录失败:', errorMsg, res);
  222. uni.showToast({
  223. title: errorMsg,
  224. icon: 'none',
  225. position: 'center'
  226. })
  227. }
  228. },
  229. fail: (err) => {
  230. console.error('登录请求失败:', err);
  231. uni.hideLoading();
  232. _self.isRotate = false;
  233. uni.showToast({
  234. title: '网络异常,请检查网络连接',
  235. icon: 'none',
  236. position: 'center'
  237. });
  238. }
  239. })
  240. },
  241. login_weixin() {
  242. //微信登录
  243. uni.showToast({
  244. icon: 'none',
  245. position: 'bottom',
  246. title: '...'
  247. });
  248. },
  249. forget() {
  250. uni.showModal({
  251. title: '提示',
  252. content: '请联系系统管理员',
  253. showCancel: false,
  254. confirmText: '知道了',
  255. confirmColor: '#333333'
  256. });
  257. }
  258. }
  259. }
  260. </script>
  261. <style>
  262. @import url("../../components/watch-login/css/icon.css");
  263. @import url("./css/main.css");
  264. page {
  265. background-color: #FFFFFF;
  266. }
  267. </style>