index2.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. <template>
  2. <div class="login-container">
  3. <el-form
  4. ref="loginForm"
  5. :model="loginForm"
  6. :rules="loginRules"
  7. class="login-form"
  8. auto-complete="on"
  9. label-position="left"
  10. >
  11. <!-- <div class="title-container">
  12. <img src="@/assets/login/title.png" alt="">
  13. </div> -->
  14. <div class="image-container">
  15. <img src="@/assets/login/image.png" alt="" />
  16. </div>
  17. <div class="right-container">
  18. <div class="empty-height" />
  19. <div class="form-container">
  20. <div class="title">供应链平台</div>
  21. <!-- <div class="logo">-->
  22. <!-- <img src="@/assets/login/logo.png" alt="">-->
  23. <!-- </div>-->
  24. <el-form-item prop="username">
  25. <span class="svg-container">
  26. <svg-icon icon-class="user" />
  27. </span>
  28. <el-input
  29. ref="username"
  30. v-model="loginForm.username"
  31. placeholder="请输入用户名"
  32. name="username"
  33. type="text"
  34. tabindex="1"
  35. auto-complete="on"
  36. />
  37. </el-form-item>
  38. <el-form-item prop="password">
  39. <span class="svg-container">
  40. <svg-icon icon-class="password" />
  41. </span>
  42. <el-input
  43. :key="passwordType"
  44. ref="password"
  45. v-model="loginForm.password"
  46. :type="passwordType"
  47. placeholder="请输入密码"
  48. name="password"
  49. tabindex="2"
  50. auto-complete="off"
  51. @keyup.enter.native="handleLogin"
  52. />
  53. <span class="show-pwd" @click="showPwd">
  54. <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
  55. </span>
  56. </el-form-item>
  57. <el-form-item prop="codeValue">
  58. <span class="svg-container">
  59. <svg-icon icon-class="password" />
  60. </span>
  61. <el-input
  62. ref="codeValue"
  63. v-model="loginForm.codeValue"
  64. placeholder="请输入验证码"
  65. name="codeValue"
  66. type="text"
  67. tabindex="3"
  68. auto-complete="off"
  69. @keyup.enter.native="handleLogin"
  70. />
  71. <div class="code" @click="getCode">
  72. <img :src="'data:image/jpeg;base64,' + codeImage" alt="" />
  73. </div>
  74. </el-form-item>
  75. <el-checkbox v-model="isRemenberPw">记住密码</el-checkbox>
  76. <div class="button-container">
  77. <el-button :loading="loading" type="primary" @click.native.prevent="handleLogin">登录</el-button>
  78. </div>
  79. </div>
  80. </div>
  81. </el-form>
  82. </div>
  83. </template>
  84. <script>
  85. import { validUsername } from '@/utils/validate'
  86. import Cookies from 'js-cookie'
  87. import { getCode } from '@/api/user'
  88. export default {
  89. name: 'Login',
  90. data() {
  91. const validateUsername = (rule, value, callback) => {
  92. if (value.length <= 0) {
  93. callback(new Error('请输入用户名'))
  94. } else {
  95. callback()
  96. }
  97. }
  98. const validatePassword = (rule, value, callback) => {
  99. if (value.length <= 0) {
  100. callback(new Error('请输入密码'))
  101. } else {
  102. callback()
  103. }
  104. }
  105. const validateCode = (rule, value, callback) => {
  106. if (value.length <= 0) {
  107. callback(new Error('请输入验证码'))
  108. } else {
  109. callback()
  110. }
  111. }
  112. return {
  113. loginForm: {
  114. username: '',
  115. password: '',
  116. code: '',
  117. codeValue: ''
  118. },
  119. loginRules: {
  120. username: [{ required: true, trigger: 'change', validator: validateUsername }],
  121. password: [{ required: true, trigger: 'change', validator: validatePassword }],
  122. codeValue: [{ required: true, trigger: 'change', validator: validateCode }]
  123. },
  124. loading: false,
  125. passwordType: 'password',
  126. redirect: undefined,
  127. isRemenberPw: false,
  128. codeImage: ''
  129. }
  130. },
  131. watch: {
  132. $route: {
  133. handler: function (route) {
  134. this.redirect = route.query && route.query.redirect
  135. },
  136. immediate: true
  137. }
  138. },
  139. created() {
  140. // 获取缓存信息
  141. if (localStorage.getItem('supply_login')) {
  142. let storageData = JSON.parse(localStorage.getItem('supply_login'))
  143. this.loginForm.username = storageData.username
  144. this.isRemenberPw = storageData.isRemenberPw
  145. }
  146. if (this.isRemenberPw) {
  147. this.getCookie()
  148. }
  149. this.getCode()
  150. },
  151. methods: {
  152. // 获取验证码
  153. getCode() {
  154. getCode().then(res => {
  155. console.log(res)
  156. this.loginForm.code = res.data.code
  157. this.codeImage = res.data.pic
  158. })
  159. },
  160. // 显示隐藏密码
  161. showPwd() {
  162. if (this.passwordType === 'password') {
  163. this.passwordType = ''
  164. } else {
  165. this.passwordType = 'password'
  166. }
  167. this.$nextTick(() => {
  168. this.$refs.password.focus()
  169. })
  170. },
  171. // 登录
  172. handleLogin() {
  173. console.log(this.loginForm)
  174. this.$refs.loginForm.validate(valid => {
  175. if (valid) {
  176. this.loading = true
  177. this.$store
  178. .dispatch('user/login', this.loginForm)
  179. .then(() => {
  180. console.log(this.redirect)
  181. this.$router.push({ path: this.redirect || '/' })
  182. this.$store.commit('user/showMessage', 'yes')
  183. this.saveUnAndPw()
  184. this.loading = false
  185. })
  186. .catch(() => {
  187. this.getCode()
  188. this.loginForm.codeValue = ''
  189. this.loading = false
  190. })
  191. } else {
  192. console.log('error submit!!')
  193. return false
  194. }
  195. })
  196. },
  197. // 处理账号密码的储存
  198. saveUnAndPw() {
  199. let storageData = {
  200. username: this.loginForm.username,
  201. isRemenberPw: this.isRemenberPw
  202. }
  203. localStorage.setItem('supply_login', JSON.stringify(storageData))
  204. if (this.isRemenberPw) {
  205. this.setCookie(this.loginForm.username, this.loginForm.password, 7)
  206. }
  207. },
  208. //设置cookie
  209. setCookie(c_name, c_pwd, exdays) {
  210. var exdate = new Date() //获取时间
  211. exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays) //保存的天数
  212. //字符串拼接cookie
  213. window.document.cookie = 'supply_username' + '=' + c_name + ';path=/;expires=' + exdate.toGMTString()
  214. window.document.cookie = 'supply_password' + '=' + c_pwd + ';path=/;expires=' + exdate.toGMTString()
  215. },
  216. //读取cookie
  217. getCookie: function () {
  218. if (document.cookie.length > 0) {
  219. var arr = document.cookie.split('; ') //这里显示的格式需要切割一下自己可输出看下
  220. for (var i = 0; i < arr.length; i++) {
  221. var arr2 = arr[i].split('=') //再次切割
  222. //判断查找相对应的值
  223. if (arr2[0] == 'supply_username') {
  224. this.loginForm.username = arr2[1] //保存到保存数据的地方
  225. } else if (arr2[0] == 'supply_password') {
  226. this.loginForm.password = arr2[1]
  227. }
  228. }
  229. }
  230. },
  231. //清除cookie
  232. clearCookie: function () {
  233. this.setCookie('', '', -1) //修改2值都为空,天数为负1天就好了
  234. }
  235. }
  236. }
  237. </script>
  238. <style lang="scss">
  239. /* 修复input 背景不协调 和光标变色 */
  240. /* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */
  241. $bg: #283443;
  242. $light_gray: #fff;
  243. $cursor: #fff;
  244. $back: #333;
  245. @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  246. .login-container .el-input input {
  247. color: $cursor;
  248. }
  249. }
  250. /* reset element-ui css */
  251. .login-container {
  252. background: url('~@/assets/login/background.png') center center;
  253. background-size: cover;
  254. .el-input {
  255. display: inline-block;
  256. height: 47px;
  257. width: 85%;
  258. input {
  259. background: transparent;
  260. border: 0px;
  261. -webkit-appearance: none;
  262. border-radius: 0px;
  263. padding: 12px 5px 12px 15px;
  264. color: $back;
  265. height: 47px;
  266. caret-color: $back;
  267. border-bottom: 1px solid #eaeaea;
  268. &:-webkit-autofill {
  269. box-shadow: 0 0 0px 1000px $cursor inset !important;
  270. -webkit-text-fill-color: $back !important;
  271. }
  272. }
  273. }
  274. .el-form-item {
  275. border: 1px solid rgba(255, 255, 255, 0.1);
  276. border-radius: 5px;
  277. color: #454545;
  278. margin-bottom: 25px !important ;
  279. }
  280. .el-checkbox {
  281. margin-top: 10px;
  282. margin-left: 6px;
  283. }
  284. .el-form-item__error {
  285. left: 30px;
  286. }
  287. }
  288. </style>
  289. <style lang="scss" scoped>
  290. $bg: #2d3a4b;
  291. $dark_gray: #889aa4;
  292. $light_gray: #eee;
  293. .login-container {
  294. min-height: 100%;
  295. width: 100%;
  296. background-color: $bg;
  297. overflow: hidden;
  298. .login-form {
  299. position: relative;
  300. width: 1070px;
  301. max-width: 100%;
  302. padding: 0 35px 0;
  303. padding-top: calc(50vh - 230px);
  304. margin: 0 auto;
  305. overflow: hidden;
  306. display: flex;
  307. }
  308. .image-container {
  309. img {
  310. width: 560px;
  311. height: 460px;
  312. }
  313. }
  314. .right-container {
  315. width: 440px;
  316. height: 460px;
  317. .empty-height {
  318. height: 77px;
  319. }
  320. .form-container {
  321. padding: 20px 30px 27px 40px;
  322. background: #fff;
  323. border-radius: 0 15px 15px 0;
  324. .title {
  325. font-size: 24px;
  326. font-weight: bold;
  327. letter-spacing: 4px;
  328. text-align: center;
  329. padding: 10px 0 13px;
  330. color: #3a8ee6;
  331. }
  332. }
  333. }
  334. .tips {
  335. font-size: 14px;
  336. color: #fff;
  337. margin-bottom: 10px;
  338. span {
  339. &:first-of-type {
  340. margin-right: 16px;
  341. }
  342. }
  343. }
  344. .svg-container {
  345. padding: 6px 5px 6px 5px;
  346. color: #33aef7;
  347. vertical-align: middle;
  348. width: 30px;
  349. display: inline-block;
  350. }
  351. .title-container {
  352. border-radius: 15px 15px 0 0;
  353. overflow: hidden;
  354. img {
  355. width: 100%;
  356. display: block;
  357. }
  358. }
  359. .show-pwd {
  360. position: absolute;
  361. right: 30px;
  362. top: 7px;
  363. font-size: 16px;
  364. color: $dark_gray;
  365. cursor: pointer;
  366. user-select: none;
  367. }
  368. .code {
  369. position: absolute;
  370. right: 30px;
  371. top: 7px;
  372. cursor: pointer;
  373. img {
  374. height: 30px;
  375. }
  376. }
  377. .button-container {
  378. text-align: center;
  379. margin-top: 20px;
  380. button {
  381. font-size: 16px;
  382. width: 100%;
  383. height: 45px;
  384. border-radius: 45px;
  385. background: #33aef7;
  386. box-shadow: 2px 3px 8px 0px rgba(5, 155, 245, 0.75);
  387. }
  388. }
  389. }
  390. @media only screen and (max-width: 600px) {
  391. .image-container {
  392. display: none;
  393. }
  394. .form-container {
  395. border-radius: 15px !important;
  396. }
  397. }
  398. </style>