index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <template>
  2. <div class="login-container">
  3. <el-form ref="loginForm" :model="loginForm" class="login-form" autocomplete="on" label-position="left">
  4. <div class="title-container">
  5. <h3 class="title">
  6. {{ $t('login.title') }}
  7. </h3>
  8. <lang-select class="set-language" />
  9. </div>
  10. <el-form-item prop="username">
  11. <span class="svg-container">
  12. <svg-icon icon-class="user" />
  13. </span>
  14. <el-input
  15. ref="username"
  16. v-model="loginForm.username"
  17. :placeholder="$t('login.username')"
  18. name="username"
  19. type="text"
  20. tabindex="1"
  21. autocomplete="on"
  22. />
  23. </el-form-item>
  24. <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
  25. <el-form-item prop="password">
  26. <span class="svg-container">
  27. <svg-icon icon-class="password" />
  28. </span>
  29. <el-input
  30. :key="passwordType"
  31. ref="password"
  32. v-model="loginForm.password"
  33. :type="passwordType"
  34. :placeholder="$t('login.password')"
  35. name="password"
  36. tabindex="2"
  37. autocomplete="on"
  38. @keyup.native="checkCapslock"
  39. @blur="capsTooltip = false"
  40. @keyup.enter.native="handleLogin"
  41. />
  42. <span class="show-pwd" @click="showPwd">
  43. <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
  44. </span>
  45. </el-form-item>
  46. </el-tooltip>
  47. <el-form-item label-width="0px" class="border-bottom key-name">
  48. <span class="svg-container">
  49. <i class="el-icon-camera-solid" />
  50. </span>
  51. <el-input
  52. v-model="loginForm.verifyCode"
  53. type="text"
  54. auto-complete="off"
  55. :placeholder="$t('login.verifyCode')"
  56. @keyup.enter.native="handleLogin"
  57. />
  58. </el-form-item>
  59. <el-image :src="captchaUrl" class="login-captcha" @click="changeCaptcha" />
  60. <el-button :loading="loading" type="primary" style="width: 100%; margin-bottom: 30px;" @click.native.prevent="handleLogin">
  61. {{ $t('login.logIn') }}
  62. </el-button>
  63. </el-form>
  64. </div>
  65. </template>
  66. <script>
  67. import LangSelect from '@/components/LangSelect'
  68. import SocialSign from './components/SocialSignin'
  69. import { daysDiff } from '@/api/site'
  70. import baseInfo from '@/utils/baseInfo'
  71. import usersInfo from '@/utils/usersInfo'
  72. import Cookies from "js-cookie";
  73. export default {
  74. name: 'Login',
  75. components: { LangSelect, SocialSign },
  76. data() {
  77. return {
  78. loginForm: {
  79. username: '',
  80. password: '',
  81. verifyCode: '',
  82. lang: '',
  83. },
  84. passwordType: 'password',
  85. capsTooltip: false,
  86. loading: false,
  87. showDialog: false,
  88. redirect: undefined,
  89. otherQuery: {},
  90. pageId: '',
  91. captchaUrl: ''
  92. }
  93. },
  94. watch: {
  95. // $route: {
  96. // handler: function(route) {
  97. // const query = route.query
  98. // if (query) {
  99. // this.redirect = query.redirect
  100. // this.otherQuery = this.getOtherQuery(query)
  101. // }
  102. // },
  103. // immediate: true
  104. // }
  105. },
  106. beforeCreate() {
  107. // 获取浏览器语言类型
  108. let lang = navigator.language
  109. if (lang === 'zh-CN') {
  110. Cookies.set('language', 'zh')
  111. } else {
  112. Cookies.set('language', 'en')
  113. }
  114. this.$store.dispatch('settings/getPageData', {})
  115. .then(response => {
  116. this.pageId = response.data.pageId
  117. this.captchaUrl = process.env.VUE_APP_BASE_API + '/v1/site/captcha?page_id=' + this.pageId + '&v=' + Math.random()
  118. })
  119. .catch((error) => {
  120. console.log(error)
  121. })
  122. },
  123. created() {
  124. // window.addEventListener('storage', this.afterQRScan)
  125. },
  126. mounted() {
  127. if (this.loginForm.username === '') {
  128. this.$refs.username.focus()
  129. } else if (this.loginForm.password === '') {
  130. this.$refs.password.focus()
  131. }
  132. },
  133. destroyed() {
  134. // window.removeEventListener('storage', this.afterQRScan)
  135. },
  136. methods: {
  137. checkCapslock(e) {
  138. const { key } = e
  139. this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
  140. },
  141. showPwd() {
  142. if (this.passwordType === 'password') {
  143. this.passwordType = ''
  144. } else {
  145. this.passwordType = 'password'
  146. }
  147. this.$nextTick(() => {
  148. this.$refs.password.focus()
  149. })
  150. },
  151. handleLogin() {
  152. if (!this.loginForm.username) {
  153. this.$message({
  154. message: this.$t('login.username') + this.$t('common.canNotBeBlank'),
  155. type: 'error'
  156. })
  157. return false
  158. }
  159. if (!this.loginForm.password) {
  160. this.$message({
  161. message: this.$t('login.password') + this.$t('common.canNotBeBlank'),
  162. type: 'error'
  163. })
  164. return false
  165. }
  166. if (!this.loginForm.verifyCode) {
  167. this.$message({
  168. message: this.$t('login.verifyCode') + this.$t('common.canNotBeBlank'),
  169. type: 'error'
  170. })
  171. return false
  172. }
  173. this.$refs.loginForm.validate(valid => {
  174. if (valid) {
  175. this.loading = true
  176. const loginData = {
  177. username: this.loginForm.username,
  178. password: this.loginForm.password,
  179. verifyCode: this.loginForm.verifyCode,
  180. pageId: this.pageId,
  181. lang: Cookies.get('language')
  182. }
  183. if (!baseInfo.daysDiff()) {
  184. daysDiff().then(response => {
  185. // 更新本地baseInfo
  186. baseInfo.setDaysDiff(response.data.daysDiff)
  187. }).catch(error => {
  188. this.loading = false
  189. this.$message({
  190. message: error,
  191. type: 'error'
  192. })
  193. return false
  194. })
  195. }
  196. const self = this;
  197. let loginName = this.loginForm.username;
  198. // 登录
  199. this.$store.dispatch('user/login', loginData)
  200. .then(() => {
  201. Cookies.remove('language')
  202. })
  203. .then(() => {
  204. return this.$store.dispatch('user/getUserInfo', {})
  205. }).then(() => {
  206. return this.$store.dispatch('user/getBaseInfo', {})
  207. }).then(() => {
  208. this.loading = false
  209. this.$router.push({ path: this.redirect || '/dashboard/index', query: this.otherQuery })
  210. }).then(() => {
  211. // 强制刷新页面
  212. location.reload()
  213. }).catch(error => {
  214. this.loading = false
  215. let msg = error + ' '
  216. console.log('error-------->'+error)
  217. // 强制修改密码
  218. if (msg.indexOf('ERROR_IS_MODIFY_PASSWORD') != -1) {
  219. this.$message({
  220. message: error,
  221. type: 'error'
  222. })
  223. console.log('loginName--->'+loginName)
  224. // window.location.href=`/modify-password/${loginName}`
  225. self.$router.push(`/modify-password/${loginName}`)
  226. return false;
  227. } else {
  228. // 清除登录数据
  229. usersInfo.clear()
  230. // 刷新验证码
  231. this.changeCaptcha()
  232. this.loading = false
  233. this.$message({
  234. message: error,
  235. type: 'error'
  236. })
  237. return false
  238. }
  239. })
  240. } else {
  241. console.log('error submit!!')
  242. return false
  243. }
  244. })
  245. },
  246. getOtherQuery(query) {
  247. return Object.keys(query).reduce((acc, cur) => {
  248. if (cur !== 'redirect') {
  249. acc[cur] = query[cur]
  250. }
  251. return acc
  252. }, {})
  253. },
  254. changeCaptcha() {
  255. this.captchaUrl = process.env.VUE_APP_BASE_API + '/v1/site/captcha?page_id=' + this.pageId + '&v=' + Math.random()
  256. }
  257. // afterQRScan() {
  258. // if (e.key === 'x-admin-oauth-code') {
  259. // const code = getQueryObject(e.newValue)
  260. // const codeMap = {
  261. // wechat: 'code',
  262. // tencent: 'code'
  263. // }
  264. // const type = codeMap[this.auth_type]
  265. // const codeName = code[type]
  266. // if (codeName) {
  267. // this.$store.dispatch('LoginByThirdparty', codeName).then(() => {
  268. // this.$router.push({ path: this.redirect || '/' })
  269. // })
  270. // } else {
  271. // alert('第三方登录失败')
  272. // }
  273. // }
  274. // }
  275. }
  276. }
  277. </script>
  278. <style lang="scss">
  279. /* 修复input 背景不协调 和光标变色 */
  280. /* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */
  281. $bg:#283443;
  282. $light_gray:#fff;
  283. $cursor: #fff;
  284. @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  285. .login-container .el-input input {
  286. color: $cursor;
  287. }
  288. }
  289. /* reset element-ui css */
  290. .login-container {
  291. .el-input {
  292. display: inline-block;
  293. height: 47px;
  294. width: 85%;
  295. input {
  296. background: transparent;
  297. border: 0px;
  298. -webkit-appearance: none;
  299. border-radius: 0px;
  300. padding: 12px 5px 12px 15px;
  301. color: $light_gray;
  302. height: 47px;
  303. caret-color: $cursor;
  304. &:-webkit-autofill {
  305. box-shadow: 0 0 0px 1000px $bg inset !important;
  306. -webkit-text-fill-color: $cursor !important;
  307. }
  308. }
  309. }
  310. .el-image{
  311. background-color: #e3e3e3;
  312. }
  313. .el-form-item {
  314. border: 1px solid rgba(255, 255, 255, 0.1);
  315. background: rgba(0, 0, 0, 0.1);
  316. border-radius: 5px;
  317. color: #454545;
  318. }
  319. }
  320. </style>
  321. <style lang="scss" scoped>
  322. $bg:#2d3a4b;
  323. $dark_gray:#889aa4;
  324. $light_gray:#eee;
  325. .login-container {
  326. min-height: 100%;
  327. width: 100%;
  328. background-color: $bg;
  329. overflow: hidden;
  330. .login-form {
  331. position: relative;
  332. width: 520px;
  333. max-width: 100%;
  334. padding: 160px 35px 0;
  335. margin: 0 auto;
  336. overflow: hidden;
  337. }
  338. .tips {
  339. font-size: 14px;
  340. color: #fff;
  341. margin-bottom: 10px;
  342. span {
  343. &:first-of-type {
  344. margin-right: 16px;
  345. }
  346. }
  347. }
  348. .svg-container {
  349. padding: 6px 5px 6px 15px;
  350. color: $dark_gray;
  351. vertical-align: middle;
  352. width: 30px;
  353. display: inline-block;
  354. }
  355. .title-container {
  356. position: relative;
  357. .title {
  358. font-size: 26px;
  359. color: $light_gray;
  360. margin: 0px auto 40px auto;
  361. text-align: center;
  362. font-weight: bold;
  363. }
  364. .set-language {
  365. color: #fff;
  366. position: absolute;
  367. top: 3px;
  368. font-size: 18px;
  369. right: 0px;
  370. cursor: pointer;
  371. }
  372. }
  373. .show-pwd {
  374. position: absolute;
  375. right: 10px;
  376. top: 7px;
  377. font-size: 16px;
  378. color: $dark_gray;
  379. cursor: pointer;
  380. user-select: none;
  381. }
  382. .thirdparty-button {
  383. position: absolute;
  384. right: 0;
  385. bottom: 6px;
  386. }
  387. @media only screen and (max-width: 470px) {
  388. .thirdparty-button {
  389. display: none;
  390. }
  391. }
  392. }
  393. </style>