index.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <template>
  2. <div
  3. class="login-container"
  4. @keyup.enter.native="handleLogin">
  5. <div class="login-weaper animated bounceInDown">
  6. <div class="login-left">
  7. <div class="login-time">
  8. 欢迎使用
  9. </div>
  10. <img
  11. class="img"
  12. src="@/assets/avatar.png"
  13. alt="">
  14. <p class="title">后台登录</p>
  15. </div>
  16. <div class="login-border">
  17. <div class="login-main">
  18. <h4 class="login-title">
  19. 登录
  20. </h4>
  21. <el-form
  22. ref="loginForm"
  23. :rules="loginRules"
  24. :model="loginForm"
  25. class="login-form"
  26. status-icon
  27. label-width="0">
  28. <el-form-item prop="username">
  29. <el-input
  30. v-model="loginForm.username"
  31. placeholder="请输入用户名"
  32. size="small"
  33. auto-complete="off"
  34. @keyup.enter.native="handleLogin">
  35. <i
  36. slot="prefix"
  37. class="icon-yonghu"/>
  38. </el-input>
  39. </el-form-item>
  40. <el-form-item prop="password">
  41. <el-input
  42. :type="passwordType"
  43. v-model="loginForm.password"
  44. placeholder="请输入密码"
  45. size="small"
  46. auto-complete="off"
  47. @keyup.enter.native="handleLogin">
  48. <i
  49. slot="suffix"
  50. class="el-icon-view el-input__icon"
  51. @click="showPwd"/>
  52. <i
  53. slot="prefix"
  54. class="icon-mima"/>
  55. </el-input>
  56. </el-form-item>
  57. <!-- <el-form-item prop="code">
  58. <el-input
  59. v-model="loginForm.verifyCode"
  60. placeholder="输入验证码"
  61. size="small"
  62. auto-complete="off"
  63. @keyup.enter.native="handleLogin">
  64. <i
  65. slot="prefix"
  66. class="icon-yanzhengma"
  67. style="margin-top:6px;"/>
  68. <template slot="append">
  69. <span
  70. :class="[{display:!show}]"
  71. class="msg-text"
  72. @click="sendShortMsg">{{ show ? '验证码' : count }}</span>
  73. </template>
  74. </el-input>
  75. </el-form-item> -->
  76. <el-form-item>
  77. <el-button
  78. type="primary"
  79. size="small"
  80. class="login-submit"
  81. @click.native.prevent="handleLogin">登录</el-button>
  82. </el-form-item>
  83. </el-form>
  84. </div>
  85. </div>
  86. </div>
  87. </div>
  88. </template>
  89. <script>
  90. import { sendMsg } from '@/api/login'
  91. export default {
  92. name: 'Login',
  93. data() {
  94. const validateUsername = (rule, value, callback) => {
  95. if (validateUsername == null) {
  96. callback(new Error('请输入正确的管理员用户名'))
  97. } else {
  98. callback()
  99. }
  100. }
  101. const validatePassword = (rule, value, callback) => {
  102. if (value.length < 6) {
  103. callback(new Error('管理员密码长度应大于6'))
  104. } else {
  105. callback()
  106. }
  107. }
  108. return {
  109. loginForm: {
  110. username: '',
  111. password: '',
  112. verifyCode: '666666'
  113. },
  114. loginRules: {
  115. username: [
  116. { required: true, trigger: 'blur', validator: validateUsername }
  117. ],
  118. password: [
  119. { required: true, trigger: 'blur', validator: validatePassword }
  120. ],
  121. verifyCode: [
  122. { required: true, trigger: 'blur', message: '验证码不能为空' }
  123. ]
  124. },
  125. passwordType: 'password',
  126. loading: false,
  127. verifyLoading: false,
  128. show: true,
  129. count: '',
  130. timer: null
  131. }
  132. },
  133. watch: {
  134. $route: {
  135. handler: function(route) {
  136. this.redirect = route.query && route.query.redirect
  137. },
  138. immediate: true
  139. }
  140. },
  141. created() {
  142. // window.addEventListener('hashchange', this.afterQRScan)
  143. },
  144. destroyed() {
  145. // window.removeEventListener('hashchange', this.afterQRScan)
  146. },
  147. methods: {
  148. sendShortMsg() {
  149. if (!this.show) {
  150. this.$notify.error({
  151. title: '失败',
  152. message: '请等待60s后重试'
  153. })
  154. return
  155. }
  156. if (this.loginForm.username == null || this.loginForm.username === '' || this.loginForm.password == null || this.loginForm.password === '') {
  157. this.$notify.error({
  158. title: '失败',
  159. message: '请先填写用户名和密码'
  160. })
  161. return false
  162. }
  163. this.verifyLoading = true
  164. sendMsg(this.loginForm)
  165. .then(response => {
  166. this.verifyLoading = false
  167. this.$notify.success({
  168. title: '成功',
  169. message: '信息发送成功'
  170. })
  171. const TIME_COUNT = 60
  172. if (!this.timer) {
  173. this.count = TIME_COUNT
  174. this.show = false
  175. this.timer = setInterval(() => {
  176. if (this.count > 0 && this.count <= TIME_COUNT) {
  177. this.count--
  178. } else {
  179. this.show = true
  180. clearInterval(this.timer)
  181. this.timer = null
  182. }
  183. }, 1000)
  184. }
  185. })
  186. .catch(response => {
  187. this.verifyLoading = false
  188. this.$notify.error({
  189. title: '失败',
  190. message: response.data.errmsg
  191. })
  192. this.verifyLoading = false
  193. })
  194. },
  195. showPwd() {
  196. if (this.passwordType === 'password') {
  197. this.passwordType = ''
  198. } else {
  199. this.passwordType = 'password'
  200. }
  201. },
  202. handleLogin() {
  203. this.$refs.loginForm.validate(valid => {
  204. if (valid && !this.loading) {
  205. this.loading = true
  206. this.$store
  207. .dispatch('LoginByUsername', this.loginForm)
  208. .then(() => {
  209. this.loading = false
  210. this.$router.push({ path: this.redirect || '/' })
  211. })
  212. .catch(response => {
  213. this.$notify.error({
  214. title: '失败',
  215. message: response.data.errmsg
  216. })
  217. this.loading = false
  218. })
  219. } else {
  220. return false
  221. }
  222. })
  223. }
  224. }
  225. }
  226. </script>
  227. <style>
  228. .msg-text {
  229. display: block;
  230. width: 60px;
  231. font-size: 12px;
  232. text-align: center;
  233. cursor: pointer;
  234. }
  235. .msg-text.display {
  236. color: #ccc;
  237. }
  238. </style>
  239. <style lang="scss">
  240. @import "@/styles/login.scss";
  241. </style>