index.vue 11 KB

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