App.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <!--
  2. * @Author: your name
  3. * @Date: 2021-10-14 17:17:53
  4. * @LastEditTime: 2022-03-02 10:48:32
  5. * @LastEditors: Please set LastEditors
  6. * @Description: In User Settings Edit
  7. * @FilePath: \Foshan4A\src\App.vue
  8. -->
  9. <template>
  10. <div id="app">
  11. <!--全局休眠弹框-->
  12. <Dialog :flag="dialog" customClass="dormancyDialog" width="496px">
  13. <div class="dormancy">
  14. <div class="title">系统休眠,请输入密码解锁</div>
  15. <div class="content">
  16. <el-form @submit.native.prevent ref="form" :rules="rules" :model="form">
  17. <el-form-item prop="pwd">
  18. <el-input show-password placeholder="请输入密码" tabindex="1" @keyup.enter.native="onSubmit('form')" v-model="form.pwd"></el-input>
  19. </el-form-item>
  20. <div v-if="Number(errorNum) >= 2" class="flex-wrap">
  21. <el-form-item class="flex1" prop="identify">
  22. <el-input ref="identify" v-model.trim="form.identify" @keyup.enter.native="onSubmit('form')" placeholder="请输入验证码" name="identify" tabindex="1" />
  23. </el-form-item>
  24. <Identify @changeCode="changeCode" :identifyCode="form.CheckCode" :contentHeight="48" style="margin-left: 24px" />
  25. </div>
  26. <el-form-item>
  27. <el-button :loading="loading" style="line-height: normal" type="primary" class="button-public-shadow onSubmit" @click="onSubmit('form')">
  28. 确定
  29. </el-button>
  30. </el-form-item>
  31. </el-form>
  32. </div>
  33. </div>
  34. </Dialog>
  35. <!--路由出口-->
  36. <router-view />
  37. </div>
  38. </template>
  39. <script>
  40. import Dialog from "@/layout/components/Dialog/index.vue";
  41. import Identify from "./views/login/identify.vue";
  42. import { mapGetters } from "vuex";
  43. import { SsoLogin } from "@/api/apiHome";
  44. import { GetSSOCheckCode } from "@/api/login";
  45. import { getCodeToken } from "@/utils/auth";
  46. export default {
  47. name: "App",
  48. components: {
  49. Dialog,
  50. Identify,
  51. },
  52. data () {
  53. return {
  54. arr: [], //鼠标移动screenX值数组
  55. arrLen: [], //一段时间后上报的screenX值数组
  56. timer: null, //定时器
  57. time: null, //定时器时间 1=1S
  58. desc: null, //固定的定时器时间/和time保持一致
  59. form: {
  60. //表单数据
  61. pwd: "",
  62. identify: "",
  63. CheckCode: "",
  64. },
  65. rules: {
  66. //表单验证
  67. pwd: [{ required: true, message: "请输入密码", trigger: "blur" }],
  68. },
  69. flag: false,
  70. loading: false,
  71. LoginError: null
  72. };
  73. },
  74. computed: {
  75. ...mapGetters([
  76. "dialog",
  77. "token",
  78. "name",
  79. "systemSet",
  80. "roles",
  81. "errorNum"
  82. ]),
  83. },
  84. watch: {
  85. "$store.state.user.token": {
  86. handler (val) {
  87. if (val) {
  88. this.handleInit();
  89. } else {
  90. this.clearAll();
  91. }
  92. },
  93. deep: true,
  94. },
  95. $route: {
  96. handler () {
  97. this.$store.dispatch("auth/changeAuthMsg", []);
  98. this.$store.dispatch("auth/changeAuthArrs", []);
  99. this.$store.dispatch("auth/changeAuthList", []);
  100. this.$store.dispatch("auth/changeAuthId", null);
  101. },
  102. deep: true,
  103. },
  104. },
  105. created () {
  106. const { LoginError } = typeof this.systemSet === "string" ? JSON.parse(this.systemSet) : this.systemSet;
  107. this.LoginError = LoginError;
  108. },
  109. mounted () {
  110. const num = Number(this.errorNum);
  111. if (this.dialog && num >= 2) {
  112. // this.flag = true;
  113. this.getCheckCode();
  114. }
  115. this.beforeUnload();
  116. },
  117. beforeDestroy () {
  118. //结束定时器和释放timer
  119. this.clearAll();
  120. },
  121. methods: {
  122. // 初始化
  123. handleInit () {
  124. this.handleMove();
  125. if (!this.dialog) {
  126. this.handleTimer();
  127. }
  128. },
  129. /**
  130. * @description: 清除页面定时器和监听
  131. * @param {*}
  132. * @return {*}
  133. */
  134. clearAll () {
  135. clearInterval(this.timer);
  136. this.timer = null;
  137. this.time = null;
  138. this.desc = null;
  139. this.arr = [];
  140. this.arrLen = [];
  141. this.handleRmove();
  142. sessionStorage.setItem("token", "");
  143. },
  144. // 页面刷新 重新启用方法
  145. beforeUnload () {
  146. window.addEventListener("beforeunload", () => {
  147. if (this.token) {
  148. sessionStorage.setItem("token", this.token);
  149. }
  150. });
  151. let oldViews = sessionStorage.getItem("token") || "";
  152. if (oldViews) {
  153. this.handleInit();
  154. }
  155. },
  156. /**
  157. * @description: 定时器方法
  158. * @param {*}
  159. * @return {*}
  160. */
  161. handleTimer () {
  162. const obj =
  163. typeof this.systemSet === "string"
  164. ? JSON.parse(this.systemSet)
  165. : this.systemSet;
  166. const { LockMins } = obj;
  167. this.time = LockMins * 60;
  168. this.desc = LockMins * 60;
  169. this.timer = setInterval(() => {
  170. this.time--;
  171. if (this.time === 0) {
  172. const result = this.arrLen;
  173. const rut = this.arr;
  174. result.push(rut.length);
  175. if (result.length >= 2) {
  176. if (result[result.length - 2] === result[result.length - 1]) {
  177. // 相同时 结束倒计时
  178. this.$store.dispatch("app/toggleDialog", true);
  179. this.getCheckCode();
  180. this.clearAll();
  181. } else {
  182. this.time = this.desc;
  183. }
  184. } else {
  185. this.time = this.desc;
  186. }
  187. }
  188. }, 1000);
  189. },
  190. /**
  191. * @description: 监听鼠标移动方法/防抖
  192. * @param {*}
  193. * @return {*}
  194. */
  195. handleMove () {
  196. window.addEventListener(
  197. "mousemove",
  198. _.debounce(this.handleDebounce, 100)
  199. );
  200. },
  201. /**
  202. * @description: 移除鼠标移动监听
  203. * @param {*}
  204. * @return {*}
  205. */
  206. handleRmove () {
  207. window.removeEventListener("mousemove", () => {
  208. this.arr = [];
  209. });
  210. },
  211. /**
  212. * @description: 防抖方法
  213. * @param {*} e
  214. * @return {*}
  215. */
  216. handleDebounce (e) {
  217. const screenX = e.screenX;
  218. this.arr.push(screenX);
  219. },
  220. /**
  221. * @description: 提交
  222. * @param {*}
  223. * @return {*}
  224. */
  225. onSubmit (formName) {
  226. this.$refs[formName].validate((valid) => {
  227. if (valid) {
  228. this.ssoLogin();
  229. } else {
  230. return false;
  231. }
  232. });
  233. },
  234. //锁屏验证
  235. async ssoLogin () {
  236. try {
  237. this.loading = true;
  238. const obj = {
  239. LoginName: this.name,
  240. LoginPwd: this.form.pwd,
  241. token: getCodeToken(),
  242. userType: sessionStorage.getItem('UserType')
  243. };
  244. if (Number(this.errorNum) >= 2) {
  245. obj.CheckCode = this.form.identify;
  246. }
  247. const res = await SsoLogin(obj);
  248. if (res.code === 0) {
  249. this.$store.dispatch("app/toggleDialog", false);
  250. this.$store.dispatch("app/getErrorNum", 0);
  251. this.arr = [];
  252. this.arrLen = [];
  253. this.time = this.desc;
  254. this.form.pwd = "";
  255. this.form.identify = "";
  256. this.form.CheckCode = "";
  257. this.handleTimer();
  258. this.loading = false;
  259. } else {
  260. if (res.returnData) {
  261. const num = Number(res.returnData);
  262. this.form.identify = '';
  263. this.$store.dispatch("app/getErrorNum", num);
  264. if (num >= 2) {
  265. this.getCheckCode();
  266. }
  267. if (num >= Number(this.LoginError)) {
  268. await this.$store.dispatch("user/logout");
  269. this.$store.dispatch("app/getErrorNum", 0);
  270. this.clearAll();
  271. this.$store.dispatch("app/toggleDialog", false);
  272. this.$router.push(`/login?redirect=${this.$route.fullPath}`);
  273. location.reload();
  274. }
  275. } else {
  276. await this.$store.dispatch("user/logout");
  277. this.$store.dispatch("app/getErrorNum", 0);
  278. this.clearAll();
  279. this.$store.dispatch("app/toggleDialog", false);
  280. this.$router.push(`/login?redirect=${this.$route.fullPath}`);
  281. location.reload();
  282. }
  283. this.$message.error(res.message);
  284. this.loading = false;
  285. }
  286. } catch (error) {
  287. console.log(error);
  288. this.loading = false;
  289. }
  290. },
  291. //验证码重新获取
  292. changeCode () {
  293. this.getCheckCode();
  294. },
  295. //获取动态验证码
  296. async getCheckCode () {
  297. const res = await GetSSOCheckCode({
  298. token: getCodeToken()
  299. });
  300. if (res.code === 0) {
  301. this.form.CheckCode = res.returnData;
  302. } else {
  303. this.$message.error(res.message);
  304. }
  305. },
  306. },
  307. };
  308. </script>
  309. <style lang="scss" scoped>
  310. ::v-deep .dormancyDialog {
  311. .el-input__inner {
  312. height: 48px;
  313. line-height: 48px;
  314. background: #f5f7fa;
  315. border: 1px solid #ebeef5;
  316. border-radius: 6px;
  317. }
  318. .onSubmit {
  319. margin-top: 10px;
  320. }
  321. }
  322. </style>