index.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. <template>
  2. <div
  3. :class="[
  4. 'login-wrapper',
  5. ['', 'login-form-right', 'login-form-left'][direction]
  6. ]"
  7. >
  8. <el-form
  9. ref="form"
  10. size="large"
  11. :model="form"
  12. :rules="rules"
  13. class="login-form ele-bg-white"
  14. @keyup.enter.native="submit"
  15. >
  16. <h4>{{ $t('login.title') }}</h4>
  17. <el-form-item prop="loginName">
  18. <el-input
  19. clearable
  20. v-model="form.loginName"
  21. prefix-icon="el-icon-user"
  22. placeholder="请输入登录账号"
  23. />
  24. </el-form-item>
  25. <el-form-item prop="loginPwd">
  26. <el-input
  27. show-password
  28. v-model="form.loginPwd"
  29. prefix-icon="el-icon-lock"
  30. placeholder="请输入登录密码"
  31. />
  32. </el-form-item>
  33. <!-- <el-form-item prop="code">
  34. <div class="login-input-group">
  35. <el-input
  36. clearable
  37. v-model="form.code"
  38. prefix-icon="el-icon-_vercode"
  39. :placeholder="$t('login.code')"
  40. />
  41. <img
  42. alt=""
  43. v-if="captcha"
  44. :src="captcha"
  45. class="login-captcha"
  46. @click="changeCaptcha"
  47. />
  48. </div>
  49. </el-form-item> -->
  50. <div class="el-form-item">
  51. <el-checkbox v-model="form.remember">
  52. {{ $t('login.remember') }}
  53. </el-checkbox>
  54. <el-link
  55. type="primary"
  56. :underline="false"
  57. class="ele-pull-right"
  58. @click="$router.push('/forget')"
  59. >
  60. 忘记密码
  61. </el-link>
  62. </div>
  63. <div class="el-form-item">
  64. <el-button
  65. size="large"
  66. type="primary"
  67. class="login-btn"
  68. :loading="loading"
  69. @click="submit"
  70. >
  71. {{ loading ? $t('login.loading') : $t('login.login') }}
  72. </el-button>
  73. </div>
  74. <div class="ele-text-center" style="margin-bottom: 10px">
  75. <i class="login-oauth-icon el-icon-_qq" style="background: #3492ed"></i>
  76. <i
  77. class="login-oauth-icon el-icon-_wechat"
  78. style="background: #4daf29"
  79. ></i>
  80. <i
  81. class="login-oauth-icon el-icon-_weibo"
  82. style="background: #cf1900"
  83. ></i>
  84. </div>
  85. </el-form>
  86. <div class="login-copyright">
  87. Hunan Zoomwin Intelligent Technology Co., Ltd.
  88. </div>
  89. <!-- 多语言切换 -->
  90. <div style="position: absolute; right: 30px; top: 20px">
  91. <i18n-icon
  92. :icon-style="{ fontSize: '22px', color: '#fff', cursor: 'pointer' }"
  93. />
  94. </div>
  95. <!-- 实际项目去掉这段 -->
  96. <div
  97. class="hidden-xs-only"
  98. style="position: absolute; right: 30px; bottom: 20px; z-index: 9"
  99. >
  100. <el-radio-group v-model="direction" size="mini">
  101. <el-radio-button label="2">居左</el-radio-button>
  102. <el-radio-button label="0">居中</el-radio-button>
  103. <el-radio-button label="1">居右</el-radio-button>
  104. </el-radio-group>
  105. </div>
  106. </div>
  107. </template>
  108. <script>
  109. import I18nIcon from '@/layout/components/i18n-icon.vue';
  110. import { getToken } from '@/utils/token-util';
  111. import { login, getCaptcha, sub } from '@/api/login';
  112. export default {
  113. // eslint-disable-next-line vue/multi-word-component-names
  114. name: 'Login',
  115. components: { I18nIcon },
  116. data() {
  117. return {
  118. // 登录框方向, 0居中, 1居右, 2居左
  119. direction: 0,
  120. // 加载状态
  121. loading: false,
  122. // 表单数据
  123. form: {
  124. loginName: localStorage.getItem('accountInfo')
  125. ? JSON.parse(localStorage.getItem('accountInfo')).loginName
  126. : '',
  127. loginPwd: localStorage.getItem('accountInfo')
  128. ? JSON.parse(localStorage.getItem('accountInfo')).loginPwd
  129. : '',
  130. remember: localStorage.getItem('accountInfo')
  131. ? JSON.parse(localStorage.getItem('accountInfo')).remember
  132. : false
  133. },
  134. // 验证码base64数据
  135. captcha: '',
  136. // 验证码内容, 实际项目去掉
  137. text: ''
  138. };
  139. },
  140. computed: {
  141. // 表单验证规则
  142. rules() {
  143. return {
  144. loginName: [
  145. {
  146. required: true,
  147. message: this.$t('login.loginName'),
  148. type: 'string',
  149. trigger: 'blur'
  150. }
  151. ],
  152. loginPwd: [
  153. {
  154. required: true,
  155. message: this.$t('login.loginPwd'),
  156. type: 'string',
  157. trigger: 'blur'
  158. }
  159. ]
  160. };
  161. }
  162. },
  163. created() {
  164. if (getToken()) {
  165. this.goHome();
  166. } else {
  167. // 重置菜单权限
  168. this.$store.commit('user/setMenus', null);
  169. }
  170. },
  171. methods: {
  172. /* 提交 */
  173. submit() {
  174. this.$refs.form.validate((valid) => {
  175. if (!valid) {
  176. return false;
  177. }
  178. this.loading = true;
  179. login(this.form)
  180. .then((res) => {
  181. localStorage.setItem('userId', res.data.userId);
  182. // 用户信息
  183. if (res.data?.loginChangeGroupVOList.length > 0) {
  184. res.data['currentGroupId'] =
  185. res.data.loginChangeGroupVOList[0].groupId;
  186. res.data['currentRoleId'] =
  187. res.data.loginChangeGroupVOList[0].loginChangeRoleVOList[0].roleId;
  188. }
  189. this.$store.commit('user/setUserInfo', res.data);
  190. this.loading = false;
  191. this.$message.success(res.message);
  192. if (this.form.remember) {
  193. localStorage.setItem('accountInfo', JSON.stringify(this.form));
  194. } else {
  195. localStorage.removeItem('accountInfo');
  196. }
  197. this.$store.dispatch('user/getCurrentUserAuthorityDept');
  198. this.goHome();
  199. })
  200. .catch((e) => {
  201. this.loading = false;
  202. // this.$message.error(e.message);
  203. });
  204. });
  205. },
  206. /* 跳转到首页 */
  207. goHome() {
  208. this.$router.push(this.$route?.query?.from ?? '/').catch(() => {});
  209. },
  210. /* 更换图形验证码 */
  211. changeCaptcha() {
  212. // 这里演示的验证码是后端返回base64格式的形式, 如果后端地址直接是图片请参考忘记密码页面
  213. getCaptcha()
  214. .then((data) => {
  215. this.captcha = data.base64;
  216. // 实际项目后端一般会返回验证码的key而不是直接返回验证码的内容, 登录用key去验证, 可以根据自己后端接口修改
  217. this.text = data.text;
  218. // 自动回填验证码, 实际项目去掉这个
  219. this.form.code = this.text;
  220. this.$refs?.form?.clearValidate();
  221. })
  222. .catch((e) => {
  223. // this.$message.error(e.message);
  224. });
  225. }
  226. }
  227. };
  228. </script>
  229. <style lang="scss" scoped>
  230. /* 背景 */
  231. .login-wrapper {
  232. padding: 50px 20px;
  233. position: relative;
  234. box-sizing: border-box;
  235. background-image: url('@/assets/bg-login.jpg');
  236. background-repeat: no-repeat;
  237. background-size: cover;
  238. min-height: 100vh;
  239. &:before {
  240. content: '';
  241. background-color: rgba(0, 0, 0, 0.2);
  242. position: absolute;
  243. top: 0;
  244. left: 0;
  245. right: 0;
  246. bottom: 0;
  247. }
  248. }
  249. /* 卡片 */
  250. .login-form {
  251. margin: 0 auto;
  252. width: 360px;
  253. max-width: 100%;
  254. padding: 25px 30px;
  255. position: relative;
  256. box-shadow: 0 3px 6px rgba(0, 0, 0, 0.15);
  257. box-sizing: border-box;
  258. border-radius: 4px;
  259. z-index: 2;
  260. h4 {
  261. text-align: center;
  262. margin: 0 0 25px 0;
  263. }
  264. & > .el-form-item {
  265. margin-bottom: 25px;
  266. }
  267. }
  268. .login-form-right .login-form {
  269. margin: 0 15% 0 auto;
  270. }
  271. .login-form-left .login-form {
  272. margin: 0 auto 0 15%;
  273. }
  274. /* 验证码 */
  275. .login-input-group {
  276. display: flex;
  277. align-items: center;
  278. :deep(.el-input) {
  279. flex: 1;
  280. }
  281. }
  282. .login-captcha {
  283. height: 38px;
  284. width: 102px;
  285. margin-left: 10px;
  286. border-radius: 4px;
  287. border: 1px solid #dcdfe6;
  288. text-align: center;
  289. cursor: pointer;
  290. &:hover {
  291. opacity: 0.75;
  292. }
  293. }
  294. .login-btn {
  295. display: block;
  296. width: 100%;
  297. }
  298. /* 第三方登录图标 */
  299. .login-oauth-icon {
  300. color: #fff;
  301. padding: 5px;
  302. margin: 0 10px;
  303. font-size: 18px;
  304. border-radius: 50%;
  305. cursor: pointer;
  306. }
  307. /* 底部版权 */
  308. .login-copyright {
  309. color: #eee;
  310. padding-top: 20px;
  311. text-align: center;
  312. position: relative;
  313. z-index: 1;
  314. }
  315. /* 响应式 */
  316. @media screen and (min-height: 550px) {
  317. .login-form {
  318. position: absolute;
  319. top: 50%;
  320. left: 50%;
  321. transform: translateX(-50%);
  322. margin-top: -220px;
  323. }
  324. .login-form-right .login-form,
  325. .login-form-left .login-form {
  326. left: auto;
  327. right: 15%;
  328. transform: translateX(0);
  329. margin: -220px auto auto auto;
  330. }
  331. .login-form-left .login-form {
  332. right: auto;
  333. left: 15%;
  334. }
  335. .login-copyright {
  336. position: absolute;
  337. bottom: 20px;
  338. right: 0;
  339. left: 0;
  340. }
  341. }
  342. @media screen and (max-width: 768px) {
  343. .login-form-right .login-form,
  344. .login-form-left .login-form {
  345. left: 50%;
  346. right: auto;
  347. transform: translateX(-50%);
  348. margin-right: auto;
  349. }
  350. }
  351. </style>