login.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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:require('../../assets/images/login/login-ty.jpg'), //背景图片
  69. systemTitle:{
  70. title:'通榆县乡村振兴综合监管平台', //标题
  71. subTitle:'数字环保' ,//副标题
  72. },
  73. codeUrl: '',
  74. loginForm: {
  75. username: 'admin',
  76. password: 'admin123',
  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. //登录前先获取密码加密传输的公钥,对密码进行加密
  168. getSecretKey().then(res => {
  169. const encryptedPassword = encryptedData(res.data,this.loginForm.password);
  170. this.loginForm.password = encryptedPassword;
  171. this.$store.dispatch('Login', this.loginForm).then(() => {
  172. this.$router.push({
  173. //path: this.redirect || '/'
  174. path: '/'
  175. })
  176. }).catch(() => {
  177. this.loading = false
  178. if (this.captchaOnOff) {
  179. this.getCode()
  180. }
  181. })
  182. })
  183. }
  184. })
  185. }
  186. }
  187. }
  188. </script>
  189. <style rel="stylesheet/scss" lang="scss" scoped>
  190. @import '@/assets/styles/base.scss';
  191. .login {
  192. display: flex;
  193. justify-content: center;
  194. align-items: center;
  195. height: 100%;
  196. background: url("../../assets/images/login/login-ty.jpg") center;
  197. background-size: cover;
  198. }
  199. .m-b-15 {
  200. margin-bottom: 15px;
  201. }
  202. .title {
  203. margin: 0px auto 30px auto;
  204. text-align: center;
  205. color: #fff;
  206. font-family: $fontFk;
  207. font-size: 38px;
  208. }
  209. .login-form {
  210. border-radius: 6px;
  211. display: flex;
  212. flex-direction: column;
  213. align-items: center;
  214. h3 {
  215. text-shadow: 0 0 15px rgba($color: #00f6ff, $alpha: .7);
  216. }
  217. .login-container {
  218. border-radius: 6px;
  219. background: rgba($color: #fff, $alpha: .7);
  220. width: 450px;
  221. padding: 30px;
  222. box-shadow: 0 0 20px rgba($color: #fff, $alpha: .5);
  223. h4 {
  224. border-bottom: #3e6bfb 1px solid;
  225. width: fit-content;
  226. display: inline-block;
  227. margin-top: 0;
  228. padding-bottom: 5px;
  229. margin-bottom: 20px;
  230. }
  231. }
  232. .el-input {
  233. height: 38px;
  234. input {
  235. height: 38px;
  236. }
  237. }
  238. .input-icon {
  239. height: 39px;
  240. width: 14px;
  241. margin-left: 2px;
  242. color: #3e69fb;
  243. }
  244. .el-button--primary {
  245. font-size: 20px;
  246. background: #3e69fb;
  247. padding: 15px
  248. }
  249. }
  250. .login-tip {
  251. font-size: 13px;
  252. text-align: center;
  253. color: #bfbfbf;
  254. }
  255. .login-code {
  256. width: 33%;
  257. height: 38px;
  258. float: right;
  259. img {
  260. cursor: pointer;
  261. vertical-align: middle;
  262. }
  263. }
  264. .el-login-footer {
  265. height: 40px;
  266. line-height: 40px;
  267. position: fixed;
  268. bottom: 0;
  269. width: 100%;
  270. text-align: center;
  271. color: #fff;
  272. font-family: Arial;
  273. font-size: 12px;
  274. letter-spacing: 1px;
  275. }
  276. .login-code-img {
  277. height: 38px;
  278. }
  279. </style>