login.vue 7.5 KB

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