Login.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <template>
  2. <div class="login-container">
  3. <div class="login-form">
  4. <h3>
  5. <img src="../assets/logo.png" width="269" height="65"/>
  6. </h3>
  7. <el-card class="box-card">
  8. <el-tabs v-model="activeName" @tab-click="handleTabClick">
  9. <el-tab-pane label="用户名登录" name="first">
  10. <el-form
  11. class="card-box"
  12. autocomplete="on"
  13. :model="loginForm"
  14. :rules="loginRules"
  15. ref="loginForm"
  16. label-position="left"
  17. >
  18. <el-form-item prop="userName">
  19. <i class="el-icon-user-solid"></i>
  20. <el-input
  21. name="userName"
  22. type="text"
  23. v-model="loginForm.userName"
  24. autocomplete="on"
  25. placeholder="请输入用户名"
  26. />
  27. </el-form-item>
  28. <el-form-item prop="password">
  29. <i class="el-icon-lock"></i>
  30. <el-input
  31. name="password"
  32. :type="pwdType"
  33. @keyup.enter.native="handleLogin"
  34. v-model="loginForm.password"
  35. autocomplete="on"
  36. placeholder="请输入密码"
  37. />
  38. <i class="el-icon-view" @click="showPwd"></i>
  39. </el-form-item>
  40. <el-button
  41. type="primary"
  42. style="width:100%;margin-bottom:30px;"
  43. :loading="loading"
  44. @click.native.prevent="handleLogin"
  45. class="blue-btn"
  46. >登录</el-button>
  47. </el-form>
  48. </el-tab-pane>
  49. <el-tab-pane label="微信扫码登录" name="second">
  50. <div style="text-align:center;" v-loading="loading">
  51. <img :src="qrcodeUrl" style="width:300px;fit-object:cover;"/>
  52. <div>扫码后,页面将自动跳转......</div>
  53. </div>
  54. </el-tab-pane>
  55. </el-tabs>
  56. </el-card>
  57. </div>
  58. </div>
  59. </template>
  60. <script>
  61. import userApi from "@/api/sys/user";
  62. import Constant from "@/constant";
  63. import { Message } from "element-ui";
  64. import { setToken} from '@/utils/auth'
  65. export default {
  66. name: "login",
  67. data() {
  68. return {
  69. activeName: "first",
  70. rnd : Math.ceil(Math.random()*1000000),
  71. loginForm: {
  72. userName: "",
  73. password: ""
  74. },
  75. loginRules: {
  76. userName: [
  77. { required: true, message: "请填写用户名", trigger: "blur" }
  78. ],
  79. password: [
  80. { required: true, message: "请填写密码", trigger: "blur" },
  81. {
  82. type: "string",
  83. min: 2,
  84. message: "密码长度不能小于2位",
  85. trigger: "blur"
  86. }
  87. ]
  88. },
  89. pwdType: "password",
  90. loading: false,
  91. redirect: undefined,
  92. qrcodeUrl: "",
  93. queryScanResultHandler: 0
  94. };
  95. },
  96. watch: {
  97. $route: {
  98. handler: function(route) {
  99. const query = route.query;
  100. if (query) {
  101. this.redirect = query.redirect;
  102. }
  103. },
  104. immediate: true
  105. }
  106. },
  107. methods: {
  108. showPwd() {
  109. if (this.pwdType === "password") {
  110. this.pwdType = "";
  111. } else {
  112. this.pwdType = "password";
  113. }
  114. },
  115. handleLogin() {
  116. var self = this;
  117. this.$refs.loginForm.validate(valid => {
  118. if (valid) {
  119. this.loading = true;
  120. self.$store
  121. .dispatch("user/login", self.loginForm)
  122. .then((result) => {
  123. self.loading = false;
  124. if(result){
  125. self.$router.push({ path: this.redirect || "/home" });
  126. }
  127. })
  128. .catch(error => {
  129. self.loading = false;
  130. // self.$message.error(error || 'Has Error')
  131. self.$message.error(error);
  132. // self.$notify({
  133. // title: "系统提示",
  134. // message: error || "Has Error",
  135. // type: "warning"
  136. // });
  137. });
  138. }
  139. });
  140. },
  141. showQrcode() {
  142. this.loading = true;
  143. userApi.scanLogin(this.rnd).then(response=>{
  144. var jsonData = response.data;
  145. this.loading = false;
  146. if(jsonData.result){
  147. this.qrcodeUrl = jsonData.data;
  148. }
  149. else{
  150. this.$message.warning(jsonData.message);
  151. }
  152. })
  153. this.queryScanResult();
  154. },
  155. queryScanResult() {
  156. userApi.queryScanResult(this.rnd).then(response=>{
  157. var jsonData = response.data;
  158. if(jsonData.result){
  159. var token = jsonData.data;
  160. //写vuex状态
  161. this.$store.commit("user/SET_TOKEN", token);
  162. //写cookie
  163. setToken(token);
  164. this.$router.push({ path: "/home" });
  165. }
  166. else{
  167. if(this.activeName=="second"){
  168. setTimeout(() => {
  169. this.queryScanResult();
  170. }, 3000);
  171. }
  172. }
  173. });
  174. },
  175. handleTabClick(tab, event) {
  176. if(this.activeName=='second'){
  177. this.showQrcode();
  178. }
  179. }
  180. },
  181. created() {
  182. // window.addEventListener('hashchange', this.afterQRScan)
  183. },
  184. destroyed() {
  185. // window.removeEventListener('hashchange', this.afterQRScan)
  186. },
  187. mounted() {}
  188. };
  189. </script>
  190. <style rel="stylesheet/scss" lang="scss">
  191. @import "src/styles/mixin.scss";
  192. $bg:rgba(242, 247, 253, 1);
  193. $icon_color: rgba(207,134,146,1);
  194. $text_color: black;
  195. .login-container {
  196. @include relative;
  197. height: 100vh;
  198. background-color: $bg;
  199. background-image:url('../assets/login_bg_element.png');
  200. background-size:615px 258px;
  201. background-position: bottom right;
  202. background-repeat: no-repeat;
  203. h3{
  204. text-align:center;
  205. }
  206. input:-webkit-autofill {
  207. -webkit-box-shadow: 0 0 0px 1000px #293444 inset !important;
  208. -webkit-text-fill-color: #fff !important;
  209. }
  210. input {
  211. background: transparent;
  212. border: 0px;
  213. -webkit-appearance: none;
  214. border-radius: 0px;
  215. padding: 12px 5px 12px 15px;
  216. color: $text_color;
  217. height: 47px;
  218. }
  219. .el-input {
  220. display: inline-block;
  221. height: 47px;
  222. width: 85%;
  223. }
  224. .tips {
  225. font-size: 14px;
  226. color: #fff;
  227. margin-bottom: 10px;
  228. }
  229. .title {
  230. font-size: 18px;
  231. font-weight:normal;
  232. color: $text_color;
  233. margin: 0px auto 20px auto;
  234. text-align: center;
  235. }
  236. .login-form {
  237. position: absolute;
  238. left: 0;
  239. right: 0;
  240. width: 400px;
  241. padding: 35px 35px 15px 35px;
  242. margin: 60px auto;
  243. }
  244. .box-card{
  245. }
  246. .login-form i {
  247. color: $icon_color;
  248. font-size: 14px;
  249. }
  250. @media only screen and (max-width: 768px) {
  251. .login-form {
  252. position: absolute;
  253. left: 0;
  254. right: 0;
  255. width: 320px;
  256. padding: 15px 15px 15px 15px;
  257. margin: 120px auto;
  258. }
  259. }
  260. .el-form-item {
  261. border: 1px solid rgba(228, 228, 228, 1);
  262. background: rgba(242, 242, 242, 1);
  263. border-radius: 5px;
  264. padding-left:5px;
  265. }
  266. .show-pwd {
  267. position: absolute;
  268. right: 10px;
  269. top: 7px;
  270. font-size: 16px;
  271. cursor: pointer;
  272. }
  273. .thirdparty-button {
  274. position: absolute;
  275. right: 35px;
  276. bottom: 28px;
  277. }
  278. .green-btn{
  279. background-color:#64a63c;
  280. border-color: #64a63c;
  281. }
  282. .blue-btn{
  283. background-color:#3385FF;
  284. border-color: #3385FF;
  285. }
  286. }
  287. </style>