index.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <template>
  2. <div
  3. id="userLayout"
  4. class="w-full h-full relative"
  5. >
  6. <div
  7. class="rounded-lg flex items-center justify-evenly w-full h-full bg-white md:w-screen md:h-screen md:bg-[#194bfb]"
  8. >
  9. <div class="md:w-3/5 w-10/12 h-full flex items-center justify-evenly">
  10. <div class="oblique h-[130%] w-3/5 bg-white transform -rotate-12 absolute -ml-52" />
  11. <!-- 分割斜块 -->
  12. <div class="z-[999] pt-12 pb-10 md:w-96 w-full rounded-lg flex flex-col justify-between box-border">
  13. <div>
  14. <div class="flex items-center justify-center">
  15. <img
  16. class="w-24"
  17. :src="$GIN_VUE_ADMIN.appLogo"
  18. alt
  19. >
  20. </div>
  21. <div class="mb-9">
  22. <p class="text-center text-4xl font-bold">{{ $GIN_VUE_ADMIN.appName }}</p>
  23. <p class="text-center text-sm font-normal text-gray-500 mt-2.5">A management platform using Golang and Vue
  24. </p>
  25. </div>
  26. <el-form
  27. ref="loginForm"
  28. :model="loginFormData"
  29. :rules="rules"
  30. :validate-on-rule-change="false"
  31. @keyup.enter="submitForm"
  32. >
  33. <el-form-item
  34. prop="username"
  35. class="mb-6"
  36. >
  37. <el-input
  38. v-model="loginFormData.username"
  39. size="large"
  40. placeholder="请输入用户名"
  41. suffix-icon="user"
  42. />
  43. </el-form-item>
  44. <el-form-item
  45. prop="password"
  46. class="mb-6"
  47. >
  48. <el-input
  49. v-model="loginFormData.password"
  50. show-password
  51. size="large"
  52. type="password"
  53. placeholder="请输入密码"
  54. />
  55. </el-form-item>
  56. <el-form-item
  57. v-if="loginFormData.openCaptcha"
  58. prop="captcha"
  59. class="mb-6"
  60. >
  61. <div class="flex w-full justify-between">
  62. <el-input
  63. v-model="loginFormData.captcha"
  64. placeholder="请输入验证码"
  65. size="large"
  66. class="flex-1 mr-5"
  67. />
  68. <div class="w-1/3 h-11 bg-[#c3d4f2] rounded">
  69. <img
  70. v-if="picPath"
  71. class="w-full h-full"
  72. :src="picPath"
  73. alt="请输入验证码"
  74. @click="loginVerify()"
  75. >
  76. </div>
  77. </div>
  78. </el-form-item>
  79. <el-form-item class="mb-6">
  80. <el-button
  81. class="shadow shadow-blue-600 h-11 w-full"
  82. type="primary"
  83. size="large"
  84. @click="submitForm"
  85. >登 录</el-button>
  86. </el-form-item>
  87. </el-form>
  88. </div>
  89. </div>
  90. </div>
  91. <div class="hidden md:block w-1/2 h-full float-right bg-[#194bfb]"><img
  92. class="h-full"
  93. src="@/assets/login_right_banner.jpg"
  94. alt="banner"
  95. ></div>
  96. </div>
  97. <BottomInfo class="left-0 right-0 absolute bottom-3 mx-auto w-full z-20">
  98. <div class="links items-center justify-center gap-2 hidden md:flex">
  99. <a
  100. href="http://doc.henrongyi.top/"
  101. target="_blank"
  102. >
  103. <img
  104. src="@/assets/docs.png"
  105. class="w-8 h-8"
  106. alt="文档"
  107. >
  108. </a>
  109. <a
  110. href="https://support.qq.com/product/371961"
  111. target="_blank"
  112. >
  113. <img
  114. src="@/assets/kefu.png"
  115. class="w-8 h-8"
  116. alt="客服"
  117. >
  118. </a>
  119. <a
  120. href="https://github.com/flipped-aurora/gin-vue-admin"
  121. target="_blank"
  122. >
  123. <img
  124. src="@/assets/github.png"
  125. class="w-8 h-8"
  126. alt="github"
  127. >
  128. </a>
  129. <a
  130. href="https://space.bilibili.com/322210472"
  131. target="_blank"
  132. >
  133. <img
  134. src="@/assets/video.png"
  135. class="w-8 h-8"
  136. alt="视频站"
  137. >
  138. </a>
  139. </div>
  140. </BottomInfo>
  141. </div>
  142. </template>
  143. <script setup>
  144. import { captcha } from '@/api/user'
  145. import { checkDB } from '@/api/initdb'
  146. import BottomInfo from '@/view/layout/bottomInfo/bottomInfo.vue'
  147. import { reactive, ref } from 'vue'
  148. import { ElMessage } from 'element-plus'
  149. import { useRouter } from 'vue-router'
  150. import { useUserStore } from '@/pinia/modules/user'
  151. defineOptions({
  152. name: 'Login',
  153. })
  154. const router = useRouter()
  155. // 验证函数
  156. const checkUsername = (rule, value, callback) => {
  157. if (value.length < 3) {
  158. return callback(new Error('请输入正确的用户名'))
  159. } else {
  160. callback()
  161. }
  162. }
  163. const checkPassword = (rule, value, callback) => {
  164. if (value.length < 6) {
  165. return callback(new Error('请输入正确的密码'))
  166. } else {
  167. callback()
  168. }
  169. }
  170. // 获取验证码
  171. const loginVerify = () => {
  172. captcha({}).then(async(ele) => {
  173. rules.captcha.push({
  174. max: ele.data.captchaLength,
  175. min: ele.data.captchaLength,
  176. message: `请输入${ele.data.captchaLength}位验证码`,
  177. trigger: 'blur',
  178. })
  179. picPath.value = ele.data.picPath
  180. loginFormData.captchaId = ele.data.captchaId
  181. loginFormData.openCaptcha = ele.data.openCaptcha
  182. console.log()
  183. })
  184. }
  185. loginVerify()
  186. // 登录相关操作
  187. const loginForm = ref(null)
  188. const picPath = ref('')
  189. const loginFormData = reactive({
  190. username: '',
  191. password: '',
  192. captcha: '',
  193. captchaId: '',
  194. openCaptcha: false,
  195. })
  196. const rules = reactive({
  197. username: [{ validator: checkUsername, trigger: 'blur' }],
  198. password: [{ validator: checkPassword, trigger: 'blur' }],
  199. captcha: [
  200. {
  201. message: '验证码格式不正确',
  202. trigger: 'blur',
  203. },
  204. ],
  205. })
  206. const userStore = useUserStore()
  207. const login = async() => {
  208. return await userStore.LoginIn(loginFormData)
  209. }
  210. const submitForm = () => {
  211. loginForm.value.validate(async(v) => {
  212. if (v) {
  213. const flag = await login()
  214. if (!flag) {
  215. loginVerify()
  216. }
  217. } else {
  218. ElMessage({
  219. type: 'error',
  220. message: '请正确填写登录信息',
  221. showClose: true,
  222. })
  223. loginVerify()
  224. return false
  225. }
  226. })
  227. }
  228. // 跳转初始化
  229. const checkInit = async() => {
  230. const res = await checkDB()
  231. if (res.code === 0) {
  232. if (res.data?.needInit) {
  233. userStore.NeedInit()
  234. router.push({ name: 'Init' })
  235. } else {
  236. ElMessage({
  237. type: 'info',
  238. message: '已配置数据库信息,无法初始化',
  239. })
  240. }
  241. }
  242. }
  243. const GetAddr = () => {
  244. var xmlhttp = null;
  245. var res;
  246. if (window.XMLHttpRequest) {
  247. xmlhttp = new XMLHttpRequest();
  248. } else if (window.ActiveXObject) {
  249. xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  250. }
  251. // 设置回调函数
  252. xmlhttp.onreadystatechange = function() {
  253. if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
  254. res = eval('('+xmlhttp.response+')');
  255. let result = '';
  256. for (let i = 0; i < res.macAddress.length; i++) {
  257. if (i % 2 === 0 && i !== 0) {
  258. result += '-'; // 根据实际需求修改分隔符
  259. }
  260. result += res.macAddress[i];
  261. }
  262. console.log(result);
  263. loginAndRedirect(result); // 获取MAC地址后调用自动登录函数
  264. }
  265. }
  266. // 打开一个连接
  267. xmlhttp.open("get", "http://127.0.0.1:8090/init");
  268. // 发送请求
  269. xmlhttp.send();
  270. };
  271. // 在页面加载时获取MAC地址并进行自动登录
  272. GetAddr();
  273. </script>