LoginView.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <script setup lang="ts">
  2. import { onMounted, reactive } from 'vue'
  3. import type { RuleInfo } from '@/types/input'
  4. import { useRequest } from '@/hooks/useRequest'
  5. import router from '@/router'
  6. import axiosInstance from '@/utils/axios/axiosInstance'
  7. import MyButton from '@/components/form/MyButton.vue'
  8. import MyInput from '@/components/form/MyInput.vue'
  9. import { initLoadResouce } from '@/utils/resource'
  10. const { AllApi, analysisResCode } = useRequest()
  11. interface LoginInfoType {
  12. userName: string
  13. password: string
  14. }
  15. const loginInfo = reactive<LoginInfoType>({
  16. userName: '',
  17. password: ''
  18. })
  19. const userLogin = () => {
  20. let vaild = Object.keys(loginInfo).every((key) => {
  21. return formFieldsRules[key].rules.every((item) => item.validator())
  22. })
  23. if (vaild) {
  24. axiosInstance
  25. .post(AllApi.userLogin, loginInfo)
  26. .then((data) => {
  27. let result = JSON.parse(JSON.stringify(data))
  28. analysisResCode(result, 'login')
  29. .then(() => {
  30. localStorage.setItem('token', result.token)
  31. localStorage.setItem('refreshToken', result.refreshToken)
  32. router.push('/')
  33. })
  34. .catch((err) => {
  35. console.log(err)
  36. })
  37. })
  38. .catch((err) => {
  39. console.log(err)
  40. })
  41. } else {
  42. console.log('不通过')
  43. }
  44. }
  45. const formFieldsRules = reactive<{
  46. [key: string]: RuleInfo
  47. }>({
  48. userName: {
  49. name: 'username',
  50. placeholder: '请输入用户名',
  51. rules: [
  52. {
  53. validator: (): boolean => {
  54. return loginInfo.userName.trim().length > 0
  55. },
  56. errMsg: '用户名不能为空'
  57. },
  58. {
  59. validator: (): boolean => {
  60. return loginInfo.userName.trim().length >= 1 && loginInfo.userName.trim().length <= 16
  61. },
  62. errMsg: '用户名长度为1-16位'
  63. }
  64. ]
  65. },
  66. password: {
  67. name: 'password',
  68. placeholder: '请输入密码',
  69. rules: [
  70. {
  71. validator: (): boolean => {
  72. return loginInfo.password.trim().length > 0
  73. },
  74. errMsg: '密码不能为空'
  75. },
  76. {
  77. validator: (): boolean => {
  78. return loginInfo.password.trim().length >= 1 && loginInfo.password.trim().length <= 16
  79. },
  80. errMsg: '密码长度为1-16位'
  81. }
  82. ]
  83. }
  84. })
  85. // 资源的加载路径
  86. const resourceInfo: Record<string, string> = {
  87. logo: `/img/logo.svg`
  88. }
  89. // 使用blob的资源路径数组
  90. const blobUrlInfo = reactive<Record<string, string>>({})
  91. onMounted(() => {
  92. // 去加载所有需要的资源
  93. initLoadResouce(resourceInfo).then((data) => {
  94. Object.assign(blobUrlInfo, data)
  95. })
  96. })
  97. </script>
  98. <template>
  99. <!-- <img :src="urlList.head" style="width: 50px; height: 50px" alt="" /> -->
  100. <div class="container">
  101. <div class="loginBox">
  102. <div class="banner"></div>
  103. <div class="logoBox">
  104. <div class="logoImg">
  105. <el-image :fit="'fill'" class="logoImg" :src="blobUrlInfo.logo"></el-image>
  106. </div>
  107. <span class="logoText">淳皓科技</span>
  108. </div>
  109. <div class="content">
  110. <div class="loginFormBox">
  111. <div class="loginFormTitle">登录淳皓游戏管理系统</div>
  112. <div class="loginFormSubTitle">登录管理系统</div>
  113. <form class="loginForm" @submit.prevent>
  114. <!-- 一定要阻止默认提交事件,不然会导致第一次自动跳转 -->
  115. <div class="loginFormItem">
  116. <MyInput
  117. :p-input-type="'text'"
  118. :pinput-rules="formFieldsRules.userName"
  119. :placeholder="formFieldsRules.userName.placeholder"
  120. v-model="loginInfo.userName"
  121. class="userName"
  122. >
  123. <template #icon>
  124. <UserFilled />
  125. </template>
  126. </MyInput>
  127. </div>
  128. <div class="loginFormItem">
  129. <MyInput
  130. :p-input-type="'password'"
  131. :pinput-rules="formFieldsRules.password"
  132. :placeholder="formFieldsRules.password.placeholder"
  133. v-model="loginInfo.password"
  134. class="password"
  135. >
  136. <template #icon>
  137. <icon-mdi-password></icon-mdi-password>
  138. </template>
  139. </MyInput>
  140. </div>
  141. <div class="loginFormItem loginBtn">
  142. <MyButton @click-event="userLogin" :btn-info="{ text: '登录' }"></MyButton>
  143. </div>
  144. </form>
  145. </div>
  146. </div>
  147. </div>
  148. </div>
  149. </template>
  150. <style scoped lang="less">
  151. .container {
  152. width: 100vw;
  153. height: 100vh;
  154. // background-color: lightblue;
  155. position: relative;
  156. font-family:
  157. Inter,
  158. -apple-system,
  159. BlinkMacSystemFont,
  160. PingFang SC,
  161. Hiragino Sans GB,
  162. noto sans,
  163. Microsoft YaHei,
  164. Helvetica Neue,
  165. Helvetica,
  166. Arial,
  167. sans-serif;
  168. }
  169. .loginBox {
  170. position: absolute;
  171. left: 50%;
  172. top: 50%;
  173. transform: translate(-50%, -50%);
  174. width: 900px;
  175. height: 500px;
  176. // background-color: lightcoral;
  177. display: flex;
  178. }
  179. .banner {
  180. width: 400px;
  181. height: 100%;
  182. background-image: linear-gradient(to top, #30cfd0 0%, #330867 100%);
  183. // background-color: lightgreen;
  184. }
  185. .logoBox {
  186. box-sizing: border-box;
  187. position: fixed;
  188. left: 24px;
  189. top: 22px;
  190. min-height: 33px;
  191. display: flex;
  192. align-items: center;
  193. }
  194. .logoImg {
  195. width: 33px;
  196. height: 33px;
  197. }
  198. .logoText {
  199. display: inline-flex;
  200. align-items: center;
  201. height: 100%;
  202. min-height: 32px;
  203. margin-right: 4px;
  204. margin-left: 10px;
  205. color: white;
  206. font-size: 20px;
  207. font-weight: 600;
  208. }
  209. .content {
  210. width: 500px;
  211. position: relative;
  212. background-color: white;
  213. display: flex;
  214. align-items: center;
  215. justify-content: center;
  216. border: 1px solid #e5e6eb;
  217. border-left: none;
  218. }
  219. .loginFormBox {
  220. width: 320px;
  221. }
  222. .loginFormTitle {
  223. color: black;
  224. font-weight: 500;
  225. font-size: 24px;
  226. line-height: 32px;
  227. }
  228. .loginFormSubTitle {
  229. color: rgb(134, 144, 156);
  230. font-size: 16px;
  231. line-height: 24px;
  232. }
  233. .loginForm {
  234. width: 100%;
  235. display: flex;
  236. flex-direction: column;
  237. margin-top: 15px;
  238. align-items: center;
  239. }
  240. .loginFormItem {
  241. width: 100%;
  242. // min-height: 52px;
  243. }
  244. .loginBtn {
  245. margin-bottom: 30px;
  246. }
  247. .userName {
  248. margin: 1% 0;
  249. }
  250. </style>