loginPage.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <template>
  2. <gui-page :is-loading="isLoading">
  3. <template #gBody>
  4. <view style="padding:50rpx;">
  5. <view class="server-config" @click="handleSetServerIP">
  6. <text class="font-icons">&#xe6bc;</text>
  7. </view>
  8. <view style="height:150rpx;" />
  9. <view class="gui-flex gui-row gui-justify-content-center login-title">
  10. <image src="../../static/logo.png" style="width: 90rpx;height: 90rpx;" mode="aspectFit" />
  11. <text class="login-title-text">东智-MOMPlus</text>
  12. </view>
  13. <view style="margin-top:80rpx;">
  14. <form @submit="submit">
  15. <view>
  16. <text class="gui-text-small gui-color-gray">账户</text>
  17. </view>
  18. <view class="gui-border-b">
  19. <input
  20. v-model="loginForm.username"
  21. type="text"
  22. class="gui-form-input"
  23. name="username"
  24. placeholder="登录账户"
  25. >
  26. </view>
  27. <view class="gui-margin-top">
  28. <text class="gui-text-small gui-color-gray">密码</text>
  29. </view>
  30. <view class="gui-border-b">
  31. <input
  32. v-model="loginForm.password"
  33. type="password"
  34. class="gui-form-input"
  35. name="password"
  36. placeholder="密码"
  37. >
  38. </view>
  39. <view class="gui-margin-top gui-flex gui-rows gui-space-between" hover-class="gui-tap">
  40. <text class="gui-text gui-color-gray gui-block" @tap="forgetPwd">忘记密码</text>
  41. <text class="gui-text gui-color-gray gui-block gui-text-right" @tap="loginbymsg">短信登录</text>
  42. </view>
  43. <view style="margin-top:68rpx;">
  44. <button
  45. type="default"
  46. class="gui-button gui-bg-primary gui-noborder"
  47. formType="submit"
  48. style="border-radius:50rpx;"
  49. >
  50. <text class="gui-color-white gui-button-text">登 录</text>
  51. </button>
  52. </view>
  53. </form>
  54. </view>
  55. </view>
  56. </template>
  57. </gui-page>
  58. </template>
  59. <script>
  60. import graceChecker from '@/Grace6/js/checker.js'
  61. import { getFileStorage } from '@/unit/fileStorage.js'
  62. export default {
  63. data() {
  64. return {
  65. isLoading: false,
  66. loginForm: {
  67. username: '',
  68. password: '',
  69. validCode: 'akiooa'
  70. }
  71. }
  72. },
  73. beforeCreate() {
  74. // #ifdef APP-PLUS
  75. // 3秒超时保护,防止文件读取卡死
  76. const timeout = setTimeout(() => {
  77. console.warn('读取服务配置超时,使用默认配置')
  78. uni.$baseUrl = ''
  79. }, 3000)
  80. getFileStorage('serverConfig.txt')
  81. .then(data => {
  82. clearTimeout(timeout)
  83. if (data === null) {
  84. uni.$baseUrl = ''
  85. } else {
  86. try {
  87. const ipconfig = JSON.parse(data)
  88. uni.$baseUrl = ipconfig.deal + '://' + ipconfig.ip + ':' + ipconfig.port
  89. } catch (e) {
  90. console.error('解析服务配置失败:', e)
  91. uni.$baseUrl = ''
  92. }
  93. }
  94. })
  95. .catch((e) => {
  96. clearTimeout(timeout)
  97. uni.$baseUrl = ''
  98. console.error('获取服务配置失败:', e)
  99. uni.showToast({
  100. title: '获取服务配置失败, 请检查本地文件',
  101. duration: 2000,
  102. icon: 'none'
  103. })
  104. })
  105. // #endif
  106. },
  107. methods: {
  108. forgetPwd: function() {},
  109. loginbymsg: function() {},
  110. handleSetServerIP: function() {
  111. uni.navigateTo({
  112. url: '/pages/personCenter/accountConfig'
  113. })
  114. },
  115. submit: async function(e) {
  116. // 校验服务器地址是否配置
  117. // if (!uni.$baseUrl) {
  118. // uni.showToast({
  119. // title: '服务器地址未配置,请点击右上角设置',
  120. // icon: 'none',
  121. // duration: 2000
  122. // });
  123. // return;
  124. // }
  125. // 表单数据
  126. var formData = e.detail.value
  127. // 定义表单规则
  128. var rule = [
  129. {
  130. name: 'username',
  131. checkType: 'string',
  132. checkRule: '4,50',
  133. errorMsg: '登录账户输入有误, 至少4个字符'
  134. },
  135. {
  136. name: 'password',
  137. checkType: 'string',
  138. checkRule: '4,16',
  139. errorMsg: '登录密码至少4个字符'
  140. }
  141. ]
  142. var checkRes = graceChecker.check(formData, rule)
  143. if (!checkRes) {
  144. uni.showToast({
  145. title: graceChecker.error,
  146. icon: 'none'
  147. })
  148. return
  149. }
  150. // 使用原生 Loading,更可靠
  151. uni.showLoading({
  152. title: '登录中...',
  153. mask: true // 防止触摸穿透
  154. })
  155. try {
  156. // 1. 调用登录接口
  157. const loginRes = await uni.$reqPost('login', {
  158. ...formData,
  159. tenantName: '瑞泰克',
  160. tenantId: 1,
  161. rememberMe: true
  162. })
  163. if (loginRes.code !== 0) {
  164. uni.showToast({
  165. title: loginRes.msg || '登录失败',
  166. duration: 2000,
  167. icon: 'none'
  168. })
  169. return
  170. }
  171. // 保存 token
  172. uni.setStorageSync('token', loginRes.data?.accessToken)
  173. // 2. 获取用户信息(await 等待完成)
  174. const infoRes = await uni.$reqGet('loginInfo')
  175. if (infoRes.data) {
  176. uni.setStorageSync('permissions', JSON.stringify(infoRes.data.permissions || []))
  177. uni.setStorageSync('user', JSON.stringify(infoRes.data.user || {}))
  178. }
  179. // 3. 登录成功,跳转首页
  180. uni.$emit('loginSuccess', true)
  181. uni.switchTab({
  182. url: '/pages/tabbar/tabbarPanel'
  183. })
  184. } catch (err) {
  185. console.error('登录异常:', err)
  186. uni.showToast({
  187. title: err?.message || '网络异常,请检查网络连接',
  188. duration: 3000,
  189. icon: 'none'
  190. })
  191. } finally {
  192. // 确保 Loading 一定关闭,避免卡住
  193. uni.hideLoading()
  194. }
  195. }
  196. }
  197. }
  198. </script>
  199. <style lang="scss" scoped>
  200. .login-logo {
  201. width: 180rpx;
  202. height: 180rpx;
  203. font-size: 80rpx;
  204. text-align: center;
  205. line-height: 120rpx;
  206. padding: 30rpx;
  207. border-radius: 18rpx;
  208. }
  209. .login-title {
  210. height: 140rpx;
  211. display: flex;
  212. align-items: center;
  213. .login-title-text {
  214. font-size: 54rpx;
  215. font-weight: bold;
  216. color: rgba(0, 160, 233, 1);
  217. }
  218. }
  219. .other-login-icons {
  220. width: 88rpx;
  221. height: 88rpx;
  222. text-align: center;
  223. font-size: 70rpx;
  224. margin: 20rpx;
  225. }
  226. .server-config {
  227. position: fixed;
  228. top: 20px;
  229. right: 20px;
  230. text {
  231. font-size: 32px;
  232. color: #808080;
  233. }
  234. }
  235. </style>