index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. const self = this;
  187. let loginName = this.loginForm.username;
  188. // 登录
  189. this.$store.dispatch('user/login', loginData)
  190. .then(() => {})
  191. .then(() => {
  192. return this.$store.dispatch('user/getUserInfo', {})
  193. }).then(() => {
  194. return this.$store.dispatch('user/getBaseInfo', {})
  195. }).then(() => {
  196. this.loading = false
  197. this.$router.push({ path: this.redirect || '/dashboard/index', query: this.otherQuery })
  198. }).catch(error => {
  199. this.loading = false
  200. let msg = error + ' '
  201. console.log('error-------->'+error)
  202. // 强制修改密码
  203. if (msg.indexOf('ERROR_IS_MODIFY_PASSWORD') != -1) {
  204. this.$message({
  205. message: error,
  206. type: 'error'
  207. })
  208. console.log('loginName--->'+loginName)
  209. // window.location.href=`/modify-password/${loginName}`
  210. self.$router.push(`/modify-password/${loginName}`)
  211. return false;
  212. } else {
  213. // 清除登录数据
  214. usersInfo.clear()
  215. // 刷新验证码
  216. this.changeCaptcha()
  217. this.loading = false
  218. this.$message({
  219. message: error,
  220. type: 'error'
  221. })
  222. return false
  223. }
  224. })
  225. } else {
  226. console.log('error submit!!')
  227. return false
  228. }
  229. })
  230. },
  231. getOtherQuery(query) {
  232. return Object.keys(query).reduce((acc, cur) => {
  233. if (cur !== 'redirect') {
  234. acc[cur] = query[cur]
  235. }
  236. return acc
  237. }, {})
  238. },
  239. changeCaptcha() {
  240. this.captchaUrl = process.env.VUE_APP_BASE_API + '/v1/site/captcha?page_id=' + this.pageId + '&v=' + Math.random()
  241. }
  242. // afterQRScan() {
  243. // if (e.key === 'x-admin-oauth-code') {
  244. // const code = getQueryObject(e.newValue)
  245. // const codeMap = {
  246. // wechat: 'code',
  247. // tencent: 'code'
  248. // }
  249. // const type = codeMap[this.auth_type]
  250. // const codeName = code[type]
  251. // if (codeName) {
  252. // this.$store.dispatch('LoginByThirdparty', codeName).then(() => {
  253. // this.$router.push({ path: this.redirect || '/' })
  254. // })
  255. // } else {
  256. // alert('第三方登录失败')
  257. // }
  258. // }
  259. // }
  260. }
  261. }
  262. </script>
  263. <style lang="scss">
  264. /* 修复input 背景不协调 和光标变色 */
  265. /* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */
  266. $bg:#283443;
  267. $light_gray:#fff;
  268. $cursor: #fff;
  269. @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  270. .login-container .el-input input {
  271. color: $cursor;
  272. }
  273. }
  274. /* reset element-ui css */
  275. .login-container {
  276. .el-input {
  277. display: inline-block;
  278. height: 47px;
  279. width: 85%;
  280. input {
  281. background: transparent;
  282. border: 0px;
  283. -webkit-appearance: none;
  284. border-radius: 0px;
  285. padding: 12px 5px 12px 15px;
  286. color: $light_gray;
  287. height: 47px;
  288. caret-color: $cursor;
  289. &:-webkit-autofill {
  290. box-shadow: 0 0 0px 1000px $bg inset !important;
  291. -webkit-text-fill-color: $cursor !important;
  292. }
  293. }
  294. }
  295. .el-image{
  296. background-color: #e3e3e3;
  297. }
  298. .el-form-item {
  299. border: 1px solid rgba(255, 255, 255, 0.1);
  300. background: rgba(0, 0, 0, 0.1);
  301. border-radius: 5px;
  302. color: #454545;
  303. }
  304. }
  305. </style>
  306. <style lang="scss" scoped>
  307. $bg:#2d3a4b;
  308. $dark_gray:#889aa4;
  309. $light_gray:#eee;
  310. .login-container {
  311. min-height: 100%;
  312. width: 100%;
  313. background-color: $bg;
  314. overflow: hidden;
  315. .login-form {
  316. position: relative;
  317. width: 520px;
  318. max-width: 100%;
  319. padding: 160px 35px 0;
  320. margin: 0 auto;
  321. overflow: hidden;
  322. }
  323. .tips {
  324. font-size: 14px;
  325. color: #fff;
  326. margin-bottom: 10px;
  327. span {
  328. &:first-of-type {
  329. margin-right: 16px;
  330. }
  331. }
  332. }
  333. .svg-container {
  334. padding: 6px 5px 6px 15px;
  335. color: $dark_gray;
  336. vertical-align: middle;
  337. width: 30px;
  338. display: inline-block;
  339. }
  340. .title-container {
  341. position: relative;
  342. .title {
  343. font-size: 26px;
  344. color: $light_gray;
  345. margin: 0px auto 40px auto;
  346. text-align: center;
  347. font-weight: bold;
  348. }
  349. .set-language {
  350. color: #fff;
  351. position: absolute;
  352. top: 3px;
  353. font-size: 18px;
  354. right: 0px;
  355. cursor: pointer;
  356. }
  357. }
  358. .show-pwd {
  359. position: absolute;
  360. right: 10px;
  361. top: 7px;
  362. font-size: 16px;
  363. color: $dark_gray;
  364. cursor: pointer;
  365. user-select: none;
  366. }
  367. .thirdparty-button {
  368. position: absolute;
  369. right: 0;
  370. bottom: 6px;
  371. }
  372. @media only screen and (max-width: 470px) {
  373. .thirdparty-button {
  374. display: none;
  375. }
  376. }
  377. }
  378. </style>