loginPage.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. var formData = e.detail.value
  118. // 定义表单规则
  119. var rule = [
  120. {
  121. name: 'username',
  122. checkType: 'string',
  123. checkRule: '4,50',
  124. errorMsg: '登录账户输入有误, 至少4个字符'
  125. },
  126. {
  127. name: 'password',
  128. checkType: 'string',
  129. checkRule: '4,16',
  130. errorMsg: '登录密码至少4个字符'
  131. }
  132. ]
  133. var checkRes = graceChecker.check(formData, rule)
  134. if (!checkRes) {
  135. uni.showToast({
  136. title: graceChecker.error,
  137. icon: 'none'
  138. })
  139. return
  140. }
  141. // 使用原生 Loading,更可靠
  142. uni.showLoading({
  143. title: '登录中...',
  144. mask: true // 防止触摸穿透
  145. })
  146. try {
  147. // 1. 调用登录接口
  148. const loginRes = await uni.$reqPost('login', {
  149. ...formData,
  150. tenantName: '瑞泰克',
  151. tenantId: 1,
  152. rememberMe: true
  153. })
  154. if (loginRes.code !== 0) {
  155. uni.showToast({
  156. title: loginRes.msg || '登录失败',
  157. duration: 2000,
  158. icon: 'none'
  159. })
  160. return
  161. }
  162. // 保存 token
  163. uni.setStorageSync('token', loginRes.data?.accessToken)
  164. // 2. 获取用户信息(await 等待完成)
  165. const infoRes = await uni.$reqGet('loginInfo')
  166. if (infoRes.data) {
  167. uni.setStorageSync('permissions', JSON.stringify(infoRes.data.permissions || []))
  168. uni.setStorageSync('user', JSON.stringify(infoRes.data.user || {}))
  169. }
  170. // 3. 登录成功,跳转首页
  171. uni.$emit('loginSuccess', true)
  172. uni.switchTab({
  173. url: '/pages/tabbar/tabbarPanel'
  174. })
  175. } catch (err) {
  176. console.error('登录异常:', err)
  177. uni.showToast({
  178. title: err?.message || '网络异常,请检查网络连接',
  179. duration: 3000,
  180. icon: 'none'
  181. })
  182. } finally {
  183. // 确保 Loading 一定关闭,避免卡住
  184. uni.hideLoading()
  185. }
  186. }
  187. }
  188. }
  189. </script>
  190. <style lang="scss" scoped>
  191. .login-logo {
  192. width: 180rpx;
  193. height: 180rpx;
  194. font-size: 80rpx;
  195. text-align: center;
  196. line-height: 120rpx;
  197. padding: 30rpx;
  198. border-radius: 18rpx;
  199. }
  200. .login-title {
  201. height: 140rpx;
  202. display: flex;
  203. align-items: center;
  204. .login-title-text {
  205. font-size: 54rpx;
  206. font-weight: bold;
  207. color: rgba(0, 160, 233, 1);
  208. }
  209. }
  210. .other-login-icons {
  211. width: 88rpx;
  212. height: 88rpx;
  213. text-align: center;
  214. font-size: 70rpx;
  215. margin: 20rpx;
  216. }
  217. .server-config {
  218. position: fixed;
  219. top: 20px;
  220. right: 20px;
  221. text {
  222. font-size: 32px;
  223. color: #808080;
  224. }
  225. }
  226. </style>