Login.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <template>
  2. <div class="login-container">
  3. <div class="login-form" style="margin-top:0px">
  4. <h3 style="margin-left:-12px">
  5. <img src="../assets/logo_cy_login.png"/>
  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. <el-input
  20. class="inText"
  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. <el-input
  30. class="inText"
  31. name="password"
  32. :type="pwdType"
  33. @keyup.enter.native="handleLogin"
  34. v-model="loginForm.password"
  35. autocomplete="on"
  36. placeholder="请输入密码"
  37. >
  38. <i slot="append" class="el-icon-view" @click="showPwd"></i>
  39. </el-input>
  40. </el-form-item>
  41. <el-form-item prop="captCha">
  42. <el-input placeholder="请输入验证码" v-model="loginForm.captCha" >
  43. <el-button slot="append" @click="sendVerificationCode" :disabled="isSending">
  44. {{ countdown > 0 ? countdown + '秒后重新发送' : '获取验证码' }}
  45. </el-button>
  46. </el-input>
  47. </el-form-item>
  48. <el-button
  49. type="primary"
  50. style="width:100%;margin-bottom:30px;"
  51. :loading="loading"
  52. @click.native.prevent="handleLogin"
  53. class="blue-btn"
  54. >登录</el-button>
  55. </el-form>
  56. </el-tab-pane>
  57. <!-- <el-tab-pane label="微信扫码登录" name="second">
  58. <div style="text-align:center;" v-loading="loading">
  59. <img :src="qrcodeUrl" style="width:300px;fit-object:cover;"/>
  60. <div>扫码后,页面将自动跳转......</div>
  61. </div>
  62. </el-tab-pane> -->
  63. </el-tabs>
  64. </el-card>
  65. </div>
  66. </div>
  67. </template>
  68. <script>
  69. import userApi from "@/api/sys/user";
  70. import Constant from "@/constant";
  71. import { Message } from "element-ui";
  72. import { setToken} from '@/utils/auth'
  73. import { ref,watch,onMounted } from 'vue';
  74. export default {
  75. name: "login",
  76. data() {
  77. return {
  78. activeName: "first",
  79. rnd : Math.ceil(Math.random()*1000000),
  80. loginForm: {
  81. userName: "",
  82. password: "",
  83. captCha:"",
  84. },
  85. loginRules: {
  86. userName: [
  87. { required: true, message: "请填写用户名", trigger: "blur" }
  88. ],
  89. password: [
  90. { required: true, message: "请填写密码", trigger: "blur" },
  91. {
  92. type: "string",
  93. min: 2,
  94. message: "密码长度不能小于2位",
  95. trigger: "blur"
  96. }
  97. ],
  98. captCha: [
  99. { required: true, message: "请填写验证码", trigger: "blur" }
  100. ],
  101. },
  102. pwdType: "password",
  103. loading: false,
  104. redirect: undefined,
  105. qrcodeUrl: "",
  106. queryScanResultHandler: 0,
  107. countdown: 0, // 倒计时的秒数
  108. isSending: false, // 控制发送按钮的状态
  109. timer: null // 定时器ID
  110. };
  111. },
  112. watch: {
  113. $route: {
  114. handler: function(route) {
  115. const query = route.query;
  116. if (query) {
  117. this.redirect = query.redirect;
  118. }
  119. },
  120. immediate: true
  121. }
  122. },
  123. methods: {
  124. async sendVerificationCode() {
  125. // 在这里实现发送验证码的逻辑,例如调用后端API
  126. // await sendSmsCodeToServer(this.mobile);
  127. this.sendSms();
  128. },
  129. sendSms(){
  130. var self = this;
  131. var formData = new FormData();
  132. formData.append("phoneNumber", self.loginForm.userName);
  133. userApi.generateCaptCha(formData).then(function(response) {
  134. var jsonData = response.data;
  135. if(jsonData.result) {
  136. self.countdown = 60;
  137. // 假设发送成功,开始倒计时
  138. self.startCountdown();
  139. // 可以在这里添加发送成功的提示
  140. // this.$toast('验证码已发送,请注意查收');
  141. self.$message.success("验证码发送成功!");
  142. }
  143. else{
  144. self.$message.warning(jsonData.message);
  145. }
  146. }).catch((error)=>{
  147. self.loading = false;
  148. });
  149. },
  150. startCountdown() {
  151. this.isSending = true;
  152. this.timer = setInterval(() => {
  153. this.countdown--;
  154. if (this.countdown <= 0) {
  155. clearInterval(this.timer);
  156. this.timer = null;
  157. this.countdown = 0; // 重置倒计时
  158. this.isSending = false; // 重新启用发送按钮
  159. }
  160. }, 1000); // 每秒更新一次
  161. },
  162. showPwd() {
  163. if (this.pwdType === "password") {
  164. this.pwdType = "";
  165. } else {
  166. this.pwdType = "password";
  167. }
  168. },
  169. handleLogin() {
  170. var self = this;
  171. this.$refs.loginForm.validate(valid => {
  172. if (valid) {
  173. this.loading = true;
  174. self.$store
  175. .dispatch("user/login", self.loginForm)
  176. .then((result) => {
  177. self.loading = false;
  178. if(result){
  179. self.$router.push({ path: this.redirect || "/home" });
  180. }
  181. })
  182. .catch(error => {
  183. self.loading = false;
  184. // self.$message.error(error || 'Has Error')
  185. self.$message.error(error);
  186. // self.$notify({
  187. // title: "系统提示",
  188. // message: error || "Has Error",
  189. // type: "warning"
  190. // });
  191. });
  192. }
  193. });
  194. },
  195. showQrcode() {
  196. this.loading = true;
  197. userApi.scanLogin(this.rnd).then(response=>{
  198. var jsonData = response.data;
  199. this.loading = false;
  200. if(jsonData.result){
  201. this.qrcodeUrl = jsonData.data;
  202. }
  203. else{
  204. this.$message.warning(jsonData.message);
  205. }
  206. })
  207. this.queryScanResult();
  208. },
  209. queryScanResult() {
  210. userApi.queryScanResult(this.rnd).then(response=>{
  211. var jsonData = response.data;
  212. if(jsonData.result){
  213. var token = jsonData.data;
  214. //写vuex状态
  215. this.$store.commit("user/SET_TOKEN", token);
  216. //写cookie
  217. setToken(token);
  218. this.$router.push({ path: "/home" });
  219. }
  220. else{
  221. if(this.activeName=="second"){
  222. setTimeout(() => {
  223. this.queryScanResult();
  224. }, 3000);
  225. }
  226. }
  227. });
  228. },
  229. handleTabClick(tab, event) {
  230. if(this.activeName=='second'){
  231. this.showQrcode();
  232. }
  233. }
  234. },
  235. created() {
  236. // window.addEventListener('hashchange', this.afterQRScan)
  237. },
  238. destroyed() {
  239. // window.removeEventListener('hashchange', this.afterQRScan)
  240. },
  241. mounted() {}
  242. };
  243. </script>
  244. <style rel="stylesheet/scss" lang="scss">
  245. @import "src/styles/mixin.scss";
  246. $bg:rgba(242, 247, 253, 1);
  247. $icon_color: rgba(207,134,146,1);
  248. $text_color: black;
  249. .login-container {
  250. @include relative;
  251. height: 100vh;
  252. background-color: $bg;
  253. background-image:url('../assets/login_bg_element.png');
  254. background-size:615px 258px;
  255. background-position: bottom right;
  256. background-repeat: no-repeat;
  257. h3{
  258. text-align:center;
  259. }
  260. .tips {
  261. font-size: 14px;
  262. color: #fff;
  263. margin-bottom: 10px;
  264. }
  265. .title {
  266. font-size: 18px;
  267. font-weight:normal;
  268. color: $text_color;
  269. margin: 0px auto 20px auto;
  270. text-align: center;
  271. }
  272. .login-form {
  273. position: absolute;
  274. left: 0;
  275. right: 0;
  276. width: 400px;
  277. padding: 35px 35px 15px 35px;
  278. margin: 60px auto;
  279. }
  280. .box-card{
  281. }
  282. .login-form i {
  283. color: $icon_color;
  284. font-size: 14px;
  285. }
  286. @media only screen and (max-width: 768px) {
  287. .login-form {
  288. position: absolute;
  289. left: 0;
  290. right: 0;
  291. width: 320px;
  292. padding: 15px 15px 15px 15px;
  293. margin: 120px auto;
  294. }
  295. }
  296. .el-form-item {
  297. border-radius: 5px;
  298. padding-left:5px;
  299. }
  300. .show-pwd {
  301. position: absolute;
  302. right: 10px;
  303. top: 7px;
  304. font-size: 16px;
  305. cursor: pointer;
  306. }
  307. .thirdparty-button {
  308. position: absolute;
  309. right: 35px;
  310. bottom: 28px;
  311. }
  312. .green-btn{
  313. background-color:#64a63c;
  314. border-color: #64a63c;
  315. }
  316. .blue-btn{
  317. background-color:#3385FF;
  318. border-color: #3385FF;
  319. }
  320. }
  321. </style>