index.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <template>
  2. <div class="login-container">
  3. <el-form
  4. ref="loginForm"
  5. :model="loginForm"
  6. :rules="loginRules"
  7. class="login-form"
  8. autocomplete="on"
  9. label-position="left"
  10. >
  11. <div class="title-container">
  12. <h3 class="title">川仪READOPC后台管理系统</h3>
  13. </div>
  14. <el-form-item prop="userName">
  15. <span class="svg-container">
  16. <svg-icon icon-class="user" />
  17. </span>
  18. <el-input
  19. ref="userName"
  20. v-model="loginForm.userName"
  21. placeholder="帐号"
  22. name="userName"
  23. type="text"
  24. tabindex="1"
  25. auto-complete="off"
  26. />
  27. </el-form-item>
  28. <el-tooltip
  29. v-model="capsTooltip"
  30. content="已开启大写字母"
  31. placement="bottom-start"
  32. manual
  33. >
  34. <el-form-item prop="password">
  35. <span class="svg-container">
  36. <svg-icon icon-class="password" />
  37. </span>
  38. <el-input
  39. :key="passwordType"
  40. ref="password"
  41. v-model="loginForm.password"
  42. :type="passwordType"
  43. placeholder="密码"
  44. name="password"
  45. tabindex="2"
  46. auto-complete="off"
  47. @keyup.native="checkCapslock"
  48. @blur="capsTooltip = false"
  49. @keyup.enter.native="handleLogin"
  50. />
  51. <span
  52. class="show-pwd"
  53. @click="showPwd"
  54. >
  55. <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
  56. </span>
  57. </el-form-item>
  58. </el-tooltip>
  59. <el-form-item
  60. v-if="captchaEnabled"
  61. prop="code"
  62. class="code-div"
  63. >
  64. <div
  65. class="el-form-item"
  66. style="width: 63%;float: left;"
  67. >
  68. <span class="svg-container">
  69. <svg-icon icon-class="validCode" />
  70. </span>
  71. <el-input
  72. ref="code"
  73. v-model="loginForm.code"
  74. placeholder="验证码"
  75. name="code"
  76. type="text"
  77. tabindex="3"
  78. auto-complete="off"
  79. />
  80. </div>
  81. <div class="login-code">
  82. <img
  83. :src="codeUrl"
  84. class="login-code-img"
  85. @click="getCode"
  86. >
  87. </div>
  88. </el-form-item>
  89. <el-button
  90. :loading="loading"
  91. type="primary"
  92. style="width:100%;margin-bottom:30px;"
  93. @click.native.prevent="handleLogin"
  94. >
  95. 登录
  96. </el-button>
  97. </el-form>
  98. </div>
  99. </template>
  100. <script>
  101. import { getCode } from '@/api/user'
  102. import { Message } from 'element-ui'
  103. import errorCode from '@/utils/errorCode'
  104. export default {
  105. name: 'Login',
  106. data() {
  107. return {
  108. codeUrl: '',
  109. loginForm: {
  110. publicKey: '',
  111. userName: '',
  112. password: '',
  113. code: '',
  114. uid: ''
  115. },
  116. loginRules: {
  117. userName: [{ required: true, trigger: ['blur', 'change'], message: '请输入您的用户名', pattern: '[^ \x22]+' }],
  118. password: [{ required: true, trigger: ['blur', 'change'], message: '请输入您的密码', pattern: '[^ \x22]+' }],
  119. code: [{ required: true, trigger: 'change', message: '请输入验证码' }]
  120. },
  121. passwordType: 'password',
  122. // 大写字母开启提示
  123. capsTooltip: false,
  124. // 加载状态
  125. loading: false,
  126. // 验证码开关
  127. captchaEnabled: true,
  128. redirect: undefined,
  129. otherQuery: {}
  130. }
  131. },
  132. watch: {
  133. $route: {
  134. handler: function(route) {
  135. const query = route.query
  136. if (query) {
  137. this.redirect = query.redirect
  138. this.otherQuery = this.getOtherQuery(query)
  139. }
  140. },
  141. immediate: true
  142. }
  143. },
  144. created() {
  145. this.getCode()
  146. },
  147. mounted() {
  148. },
  149. destroyed() {
  150. },
  151. methods: {
  152. getCode() {
  153. getCode(true).then(res => {
  154. this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
  155. if (this.captchaEnabled) {
  156. const data = res.data
  157. this.loginForm.uid = data.uid
  158. this.loginForm.publicKey = data.publicKey
  159. this.codeUrl = 'data:image/gif;base64,' + data.verifyCode
  160. }
  161. })
  162. },
  163. checkCapslock(e) {
  164. const { key } = e
  165. this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
  166. },
  167. showPwd() {
  168. if (this.passwordType === 'password') {
  169. this.passwordType = ''
  170. } else {
  171. this.passwordType = 'password'
  172. }
  173. this.$nextTick(() => {
  174. this.$refs.password.focus()
  175. })
  176. },
  177. handleLogin() {
  178. if (!this.loginForm.publicKey) {
  179. const msg = errorCode[900]
  180. Message.error(msg)
  181. return
  182. }
  183. this.$refs.loginForm.validate(valid => {
  184. if (valid) {
  185. console.log(this.loginForm)
  186. this.loading = true
  187. this.$store.dispatch('user/Login', this.loginForm).then(() => {
  188. this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
  189. this.loading = false
  190. }).catch(() => {
  191. this.loading = false
  192. if (this.captchaEnabled) {
  193. this.getCode()
  194. }
  195. })
  196. }
  197. })
  198. },
  199. getOtherQuery(query) {
  200. return Object.keys(query).reduce((acc, cur) => {
  201. if (cur !== 'redirect') {
  202. acc[cur] = query[cur]
  203. }
  204. return acc
  205. }, {})
  206. }
  207. }
  208. }
  209. </script>
  210. <style lang="scss">
  211. $imgBg: url("~@/assets/images/loginbg.png");
  212. $dark_gray: #889aa4;
  213. .login-container {
  214. .el-input {
  215. display: inline-block;
  216. width: 85%;
  217. input {
  218. background: transparent;
  219. border: 0px;
  220. -webkit-appearance: none;
  221. border-radius: 0px;
  222. }
  223. }
  224. .el-form-item {
  225. border: 1px solid rgba(0, 0, 0, 0.1);
  226. border-radius: 5px;
  227. color: #454545;
  228. }
  229. }
  230. .login-container {
  231. display: flex;
  232. justify-content: center;
  233. align-items: center;
  234. height: 100%;
  235. background-color: #2c3e50;
  236. //background-image: $imgBg;
  237. //background-size: cover;
  238. overflow: hidden;
  239. .login-form {
  240. border-radius: 6px;
  241. background: #ffffff;
  242. width: 400px;
  243. padding: 25px 25px 5px 25px;
  244. .el-input {
  245. height: 38px;
  246. input {
  247. height: 38px;
  248. }
  249. }
  250. .input-icon {
  251. height: 39px;
  252. width: 14px;
  253. margin-left: 2px;
  254. }
  255. }
  256. .svg-container {
  257. padding-left: 15px;
  258. color: $dark_gray;
  259. vertical-align: middle;
  260. width: 30px;
  261. display: inline-block;
  262. }
  263. .title {
  264. margin: 0px auto 30px auto;
  265. text-align: center;
  266. color: #707070;
  267. }
  268. .show-pwd {
  269. position: absolute;
  270. right: 10px;
  271. top: 7px;
  272. font-size: 16px;
  273. color: $dark_gray;
  274. cursor: pointer;
  275. user-select: none;
  276. }
  277. .login-code {
  278. width: 33%;
  279. height: 38px;
  280. float: right;
  281. display: flex;
  282. justify-content: flex-end;
  283. img {
  284. cursor: pointer;
  285. vertical-align: middle;
  286. }
  287. }
  288. .code-div {
  289. border: 0;
  290. position: relative;
  291. }
  292. .login-code-img {
  293. height: 38px;
  294. }
  295. }
  296. </style>