index.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. export default {
  73. name: 'Login',
  74. components: { LangSelect, SocialSign },
  75. data() {
  76. return {
  77. loginForm: {
  78. username: '',
  79. password: '',
  80. verifyCode: ''
  81. },
  82. passwordType: 'password',
  83. capsTooltip: false,
  84. loading: false,
  85. showDialog: false,
  86. redirect: undefined,
  87. otherQuery: {},
  88. pageId: '',
  89. captchaUrl: ''
  90. }
  91. },
  92. watch: {
  93. // $route: {
  94. // handler: function(route) {
  95. // const query = route.query
  96. // if (query) {
  97. // this.redirect = query.redirect
  98. // this.otherQuery = this.getOtherQuery(query)
  99. // }
  100. // },
  101. // immediate: true
  102. // }
  103. },
  104. beforeCreate() {
  105. this.$store.dispatch('settings/getPageData', {})
  106. .then(response => {
  107. this.pageId = response.data.pageId
  108. this.captchaUrl = process.env.VUE_APP_BASE_API + '/v1/site/captcha?page_id=' + this.pageId + '&v=' + Math.random()
  109. })
  110. .catch((error) => {
  111. console.log(error)
  112. })
  113. },
  114. created() {
  115. // window.addEventListener('storage', this.afterQRScan)
  116. },
  117. mounted() {
  118. if (this.loginForm.username === '') {
  119. this.$refs.username.focus()
  120. } else if (this.loginForm.password === '') {
  121. this.$refs.password.focus()
  122. }
  123. },
  124. destroyed() {
  125. // window.removeEventListener('storage', this.afterQRScan)
  126. },
  127. methods: {
  128. checkCapslock(e) {
  129. const { key } = e
  130. this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
  131. },
  132. showPwd() {
  133. if (this.passwordType === 'password') {
  134. this.passwordType = ''
  135. } else {
  136. this.passwordType = 'password'
  137. }
  138. this.$nextTick(() => {
  139. this.$refs.password.focus()
  140. })
  141. },
  142. handleLogin() {
  143. if (!this.loginForm.username) {
  144. this.$message({
  145. message: this.$t('login.username') + this.$t('common.canNotBeBlank'),
  146. type: 'error'
  147. })
  148. return false
  149. }
  150. if (!this.loginForm.password) {
  151. this.$message({
  152. message: this.$t('login.password') + this.$t('common.canNotBeBlank'),
  153. type: 'error'
  154. })
  155. return false
  156. }
  157. if (!this.loginForm.verifyCode) {
  158. this.$message({
  159. message: this.$t('login.verifyCode') + this.$t('common.canNotBeBlank'),
  160. type: 'error'
  161. })
  162. return false
  163. }
  164. this.$refs.loginForm.validate(valid => {
  165. if (valid) {
  166. this.loading = true
  167. const loginData = {
  168. username: this.loginForm.username,
  169. password: this.loginForm.password,
  170. verifyCode: this.loginForm.verifyCode,
  171. pageId: this.pageId
  172. }
  173. if (!baseInfo.daysDiff()) {
  174. daysDiff().then(response => {
  175. // 更新本地baseInfo
  176. baseInfo.setDaysDiff(response.data.daysDiff)
  177. }).catch(error => {
  178. this.loading = false
  179. this.$message({
  180. message: error,
  181. type: 'error'
  182. })
  183. return false
  184. })
  185. }
  186. // 登录
  187. this.$store.dispatch('user/login', loginData)
  188. .then(() => {})
  189. .then(() => {
  190. return this.$store.dispatch('user/getUserInfo', {})
  191. }).then(() => {
  192. return this.$store.dispatch('user/getBaseInfo', {})
  193. }).then(() => {
  194. this.loading = false
  195. this.$router.push({ path: this.redirect || '/dashboard/index', query: this.otherQuery })
  196. }).catch(error => {
  197. this.loading = false
  198. // 强制修改密码
  199. if (error.message === 'ERROR_IS_MODIFY_PASSWORD') {
  200. this.$router.push(`/modify-password/${this.loginForm.adminName}`)
  201. } else {
  202. // 清除登录数据
  203. usersInfo.clear()
  204. // 刷新验证码
  205. this.changeCaptcha()
  206. this.loading = false
  207. this.$message({
  208. message: error,
  209. type: 'error'
  210. })
  211. return false
  212. }
  213. })
  214. } else {
  215. console.log('error submit!!')
  216. return false
  217. }
  218. })
  219. },
  220. getOtherQuery(query) {
  221. return Object.keys(query).reduce((acc, cur) => {
  222. if (cur !== 'redirect') {
  223. acc[cur] = query[cur]
  224. }
  225. return acc
  226. }, {})
  227. },
  228. changeCaptcha() {
  229. this.captchaUrl = process.env.VUE_APP_BASE_API + '/v1/site/captcha?page_id=' + this.pageId + '&v=' + Math.random()
  230. }
  231. // afterQRScan() {
  232. // if (e.key === 'x-admin-oauth-code') {
  233. // const code = getQueryObject(e.newValue)
  234. // const codeMap = {
  235. // wechat: 'code',
  236. // tencent: 'code'
  237. // }
  238. // const type = codeMap[this.auth_type]
  239. // const codeName = code[type]
  240. // if (codeName) {
  241. // this.$store.dispatch('LoginByThirdparty', codeName).then(() => {
  242. // this.$router.push({ path: this.redirect || '/' })
  243. // })
  244. // } else {
  245. // alert('第三方登录失败')
  246. // }
  247. // }
  248. // }
  249. }
  250. }
  251. </script>
  252. <style lang="scss">
  253. /* 修复input 背景不协调 和光标变色 */
  254. /* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */
  255. $bg:#283443;
  256. $light_gray:#fff;
  257. $cursor: #fff;
  258. @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  259. .login-container .el-input input {
  260. color: $cursor;
  261. }
  262. }
  263. /* reset element-ui css */
  264. .login-container {
  265. .el-input {
  266. display: inline-block;
  267. height: 47px;
  268. width: 85%;
  269. input {
  270. background: transparent;
  271. border: 0px;
  272. -webkit-appearance: none;
  273. border-radius: 0px;
  274. padding: 12px 5px 12px 15px;
  275. color: $light_gray;
  276. height: 47px;
  277. caret-color: $cursor;
  278. &:-webkit-autofill {
  279. box-shadow: 0 0 0px 1000px $bg inset !important;
  280. -webkit-text-fill-color: $cursor !important;
  281. }
  282. }
  283. }
  284. .el-form-item {
  285. border: 1px solid rgba(255, 255, 255, 0.1);
  286. background: rgba(0, 0, 0, 0.1);
  287. border-radius: 5px;
  288. color: #454545;
  289. }
  290. }
  291. </style>
  292. <style lang="scss" scoped>
  293. $bg:#2d3a4b;
  294. $dark_gray:#889aa4;
  295. $light_gray:#eee;
  296. .login-container {
  297. min-height: 100%;
  298. width: 100%;
  299. background-color: $bg;
  300. overflow: hidden;
  301. .login-form {
  302. position: relative;
  303. width: 520px;
  304. max-width: 100%;
  305. padding: 160px 35px 0;
  306. margin: 0 auto;
  307. overflow: hidden;
  308. }
  309. .tips {
  310. font-size: 14px;
  311. color: #fff;
  312. margin-bottom: 10px;
  313. span {
  314. &:first-of-type {
  315. margin-right: 16px;
  316. }
  317. }
  318. }
  319. .svg-container {
  320. padding: 6px 5px 6px 15px;
  321. color: $dark_gray;
  322. vertical-align: middle;
  323. width: 30px;
  324. display: inline-block;
  325. }
  326. .title-container {
  327. position: relative;
  328. .title {
  329. font-size: 26px;
  330. color: $light_gray;
  331. margin: 0px auto 40px auto;
  332. text-align: center;
  333. font-weight: bold;
  334. }
  335. .set-language {
  336. color: #fff;
  337. position: absolute;
  338. top: 3px;
  339. font-size: 18px;
  340. right: 0px;
  341. cursor: pointer;
  342. }
  343. }
  344. .show-pwd {
  345. position: absolute;
  346. right: 10px;
  347. top: 7px;
  348. font-size: 16px;
  349. color: $dark_gray;
  350. cursor: pointer;
  351. user-select: none;
  352. }
  353. .thirdparty-button {
  354. position: absolute;
  355. right: 0;
  356. bottom: 6px;
  357. }
  358. @media only screen and (max-width: 470px) {
  359. .thirdparty-button {
  360. display: none;
  361. }
  362. }
  363. }
  364. </style>