login.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <template>
  2. <div class="login" :style="{backgroundImage: 'url('+backgroudImg+')'}">
  3. <StarBackground />
  4. <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
  5. <h3 class="sub-title" v-if="systemTitle.subTitle">{{systemTitle.subTitle}}</h3>
  6. <h3 class="title">{{systemTitle.title}}</h3>
  7. <div class="login-container">
  8. <h4>请登录</h4>
  9. <el-form-item prop="username">
  10. <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号"
  11. class="m-b-15">
  12. <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
  13. </el-input>
  14. </el-form-item>
  15. <el-form-item prop="password">
  16. <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码"
  17. @keyup.enter.native="handleLogin" class="m-b-15">
  18. <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
  19. </el-input>
  20. </el-form-item>
  21. <el-form-item prop="code" v-if="captchaOnOff">
  22. <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 63%"
  23. @keyup.enter.native="handleLogin">
  24. <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
  25. </el-input>
  26. <div class="login-code">
  27. <img :src="codeUrl" @click="getCode" class="login-code-img" />
  28. </div>
  29. </el-form-item>
  30. <!-- <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>-->
  31. <el-form-item style="width:100%;">
  32. <el-button :loading="loading" size="medium" type="primary" style="width:100%;"
  33. @click.native.prevent="handleLogin">
  34. <span v-if="!loading">登 录</span>
  35. <span v-else>登 录 中...</span>
  36. </el-button>
  37. <div style="float: right;" v-if="register">
  38. <router-link class="link-type" :to="'/register'">立即注册</router-link>
  39. </div>
  40. </el-form-item>
  41. </div>
  42. </el-form>
  43. </div>
  44. </template>
  45. <script>
  46. import StarBackground from '@/components/star'
  47. import {
  48. getCodeImg,fontConfig,getSecretKey
  49. } from '@/api/login'
  50. import {
  51. encryptedData
  52. } from '@/api/encrypt'
  53. import Cookies from 'js-cookie'
  54. import {
  55. encrypt,
  56. decrypt
  57. } from '@/utils/jsencrypt'
  58. export default {
  59. beforeCreate: function() {
  60.     document.getElementsByTagName('body')[0].style.overflow = 'hidden'
  61.   },
  62. name: 'Login',
  63. components:{
  64. StarBackground
  65. },
  66. data() {
  67. return {
  68. backgroudImg:'', //背景图片
  69. systemTitle: {
  70. title: '', //标题
  71. subTitle: '',//副标题 数字应急
  72. },
  73. codeUrl: '',
  74. loginForm: {
  75. username: '',
  76. password: '',
  77. rememberMe: false,
  78. code: '',
  79. uuid: ''
  80. },
  81. loginRules: {
  82. username: [{
  83. required: true,
  84. trigger: 'blur',
  85. message: '请输入您的账号'
  86. }],
  87. password: [{
  88. required: true,
  89. trigger: 'blur',
  90. message: '请输入您的密码'
  91. }],
  92. code: [{
  93. required: true,
  94. trigger: 'change',
  95. message: '请输入验证码'
  96. }]
  97. },
  98. loading: false,
  99. // 验证码开关
  100. captchaOnOff: true,
  101. // 注册开关
  102. register: false,
  103. redirect: undefined
  104. }
  105. },
  106. watch: {
  107. $route: {
  108. handler: function(route) {
  109. this.redirect = route.query && route.query.redirect
  110. },
  111. immediate: true
  112. }
  113. },
  114. created() {
  115. this.getCode()
  116. this.getCookie()
  117. this.fontConfig()
  118. },
  119. methods: {
  120. fontConfig(){
  121. fontConfig().then(res => {
  122. this.systemTitle.title = res.data.fontTitle;
  123. this.systemTitle.subTitle = res.data.subTitle;
  124. this.backgroudImg = res.data.picUrl;
  125. })
  126. },
  127. getCode() {
  128. getCodeImg().then(res => {
  129. this.captchaOnOff = res.captchaOnOff === undefined ? true : res.captchaOnOff
  130. if (this.captchaOnOff) {
  131. this.codeUrl = 'data:image/gif;base64,' + res.img
  132. this.loginForm.uuid = res.uuid
  133. }
  134. })
  135. },
  136. getCookie() {
  137. const username = Cookies.get('username')
  138. const password = Cookies.get('password')
  139. const rememberMe = Cookies.get('rememberMe')
  140. this.loginForm = {
  141. username: username === undefined ? this.loginForm.username : username,
  142. password: password === undefined ? this.loginForm.password : decrypt(password),
  143. rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  144. }
  145. },
  146. handleLogin() {
  147. this.$refs.loginForm.validate(valid => {
  148. if (valid) {
  149. this.loading = true
  150. // if (this.loginForm.rememberMe) {
  151. //无论如何我都记-孙一石
  152. if (true) {
  153. Cookies.set('username', this.loginForm.username, {
  154. expires: 30
  155. })
  156. Cookies.set('password', encrypt(this.loginForm.password), {
  157. expires: 30
  158. })
  159. Cookies.set('rememberMe', this.loginForm.rememberMe, {
  160. expires: 30
  161. })
  162. } else {
  163. Cookies.remove('username')
  164. Cookies.remove('password')
  165. Cookies.remove('rememberMe')
  166. }
  167. // this.$store.dispatch('Login', this.loginForm).then(() => {
  168. // console.log('!@!@!@', this.redirect)
  169. // this.$router.push({
  170. // //path: this.redirect || '/'
  171. // path: '/'
  172. // }).catch(() => {})
  173. // }).catch(() => {
  174. // this.loading = false
  175. // if (this.captchaOnOff) {
  176. // this.getCode()
  177. // }
  178. // })
  179. //登录前先获取密码加密传输的公钥,对密码进行加密
  180. getSecretKey().then(res => {
  181. const encryptedPassword = encryptedData(res.data,this.loginForm.password);
  182. this.loginForm.password = encryptedPassword;
  183. this.$store.dispatch('Login', this.loginForm).then(() => {
  184. this.$router.push({
  185. //path: this.redirect || '/'
  186. path: '/'
  187. })
  188. }).catch(() => {
  189. this.loading = false
  190. if (this.captchaOnOff) {
  191. this.getCode()
  192. }
  193. })
  194. })
  195. }
  196. })
  197. }
  198. }
  199. }
  200. </script>
  201. <style rel="stylesheet/scss" lang="scss" scoped>
  202. @import '@/assets/styles/base.scss';
  203. .login {
  204. display: flex;
  205. justify-content: center;
  206. align-items: center;
  207. height: 100%;
  208. background: url("../../assets/images/login/login-ty.jpg") center;
  209. background-size: cover;
  210. }
  211. .m-b-15 {
  212. margin-bottom: 15px;
  213. }
  214. .sub-title{
  215. margin: 0px auto 10px auto;
  216. text-align: center;
  217. color: #fff;
  218. font-family: $fontFk;
  219. font-size: 55px;
  220. }
  221. .title {
  222. margin: 0px auto 30px auto;
  223. text-align: center;
  224. color: #fff;
  225. font-family: $fontFk;
  226. font-size: 38px;
  227. }
  228. .login-form {
  229. border-radius: 6px;
  230. display: flex;
  231. flex-direction: column;
  232. align-items: center;
  233. h3 {
  234. text-shadow: 0 0 15px rgba($color: #00f6ff, $alpha: .7);
  235. }
  236. .login-container {
  237. border-radius: 6px;
  238. background: rgba($color: #fff, $alpha: .7);
  239. width: 450px;
  240. padding: 30px;
  241. box-shadow: 0 0 20px rgba($color: #fff, $alpha: .5);
  242. h4 {
  243. border-bottom: #3e6bfb 1px solid;
  244. width: fit-content;
  245. display: inline-block;
  246. margin-top: 0;
  247. padding-bottom: 5px;
  248. margin-bottom: 20px;
  249. }
  250. }
  251. .el-input {
  252. height: 38px;
  253. input {
  254. height: 38px;
  255. }
  256. }
  257. .input-icon {
  258. height: 39px;
  259. width: 14px;
  260. margin-left: 2px;
  261. color: #3e69fb;
  262. }
  263. .el-button--primary {
  264. font-size: 20px;
  265. background: #3e69fb;
  266. padding: 15px
  267. }
  268. }
  269. .login-tip {
  270. font-size: 13px;
  271. text-align: center;
  272. color: #bfbfbf;
  273. }
  274. .login-code {
  275. width: 33%;
  276. height: 38px;
  277. float: right;
  278. img {
  279. cursor: pointer;
  280. vertical-align: middle;
  281. }
  282. }
  283. .el-login-footer {
  284. height: 40px;
  285. line-height: 40px;
  286. position: fixed;
  287. bottom: 0;
  288. width: 100%;
  289. text-align: center;
  290. color: #fff;
  291. font-family: Arial;
  292. font-size: 12px;
  293. letter-spacing: 1px;
  294. }
  295. .login-code-img {
  296. height: 38px;
  297. }
  298. </style>