App.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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" 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" 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. export default {
  46. name: "App",
  47. components: {
  48. Dialog,
  49. Identify
  50. },
  51. data () {
  52. return {
  53. arr: [], //鼠标移动screenX值数组
  54. arrLen: [], //一段时间后上报的screenX值数组
  55. timer: null, //定时器
  56. time: null, //定时器时间 1=1S
  57. desc: null, //固定的定时器时间/和time保持一致
  58. form: {
  59. //表单数据
  60. pwd: "",
  61. identify: "",
  62. CheckCode: ""
  63. },
  64. rules: {
  65. //表单验证
  66. pwd: [{ required: true, message: "请输入密码", trigger: "blur" }],
  67. },
  68. flag: false,
  69. loading: false
  70. };
  71. },
  72. computed: {
  73. ...mapGetters(["dialog", "token", "name", "systemSet", "roles", "errorNum"]),
  74. },
  75. watch: {
  76. "$store.state.user.token": {
  77. handler (val) {
  78. if (val) {
  79. this.handleInit();
  80. } else {
  81. this.clearAll();
  82. }
  83. },
  84. deep: true,
  85. },
  86. $route: {
  87. handler () {
  88. this.$store.dispatch("auth/changeAuthMsg", []);
  89. this.$store.dispatch("auth/changeAuthArrs", []);
  90. this.$store.dispatch("auth/changeAuthList", []);
  91. this.$store.dispatch("auth/changeAuthId", null);
  92. },
  93. deep: true,
  94. },
  95. },
  96. mounted () {
  97. const num = Number(this.errorNum);
  98. if (this.dialog && num >= 2) {
  99. // this.flag = true;
  100. this.getCheckCode();
  101. }
  102. this.beforeUnload();
  103. },
  104. beforeDestroy () {
  105. //结束定时器和释放timer
  106. this.clearAll();
  107. },
  108. methods: {
  109. // 初始化
  110. handleInit () {
  111. this.handleMove();
  112. if (!this.dialog) {
  113. this.handleTimer();
  114. }
  115. },
  116. /**
  117. * @description: 清除页面定时器和监听
  118. * @param {*}
  119. * @return {*}
  120. */
  121. clearAll () {
  122. clearInterval(this.timer);
  123. this.timer = null;
  124. this.time = null;
  125. this.desc = null;
  126. this.arr = [];
  127. this.arrLen = [];
  128. this.handleRmove();
  129. sessionStorage.setItem("token", "");
  130. },
  131. // 页面刷新 重新启用方法
  132. beforeUnload () {
  133. window.addEventListener("beforeunload", () => {
  134. if (this.token) {
  135. sessionStorage.setItem("token", this.token);
  136. }
  137. });
  138. let oldViews = sessionStorage.getItem("token") || "";
  139. if (oldViews) {
  140. this.handleInit();
  141. }
  142. },
  143. /**
  144. * @description: 定时器方法
  145. * @param {*}
  146. * @return {*}
  147. */
  148. handleTimer () {
  149. const obj =
  150. typeof this.systemSet === "string"
  151. ? JSON.parse(this.systemSet)
  152. : this.systemSet;
  153. const { LockMins } = obj;
  154. this.time = LockMins * 60;
  155. this.desc = LockMins * 60;
  156. this.timer = setInterval(() => {
  157. this.time--;
  158. if (this.time === 0) {
  159. const result = this.arrLen;
  160. const rut = this.arr;
  161. result.push(rut.length);
  162. if (result.length >= 2) {
  163. if (result[result.length - 2] === result[result.length - 1]) {
  164. // 相同时 结束倒计时
  165. this.$store.dispatch("app/toggleDialog", true);
  166. this.getCheckCode();
  167. this.clearAll();
  168. } else {
  169. this.time = this.desc;
  170. }
  171. } else {
  172. this.time = this.desc;
  173. }
  174. }
  175. }, 1000);
  176. },
  177. /**
  178. * @description: 监听鼠标移动方法/防抖
  179. * @param {*}
  180. * @return {*}
  181. */
  182. handleMove () {
  183. window.addEventListener(
  184. "mousemove",
  185. _.debounce(this.handleDebounce, 100)
  186. );
  187. },
  188. /**
  189. * @description: 移除鼠标移动监听
  190. * @param {*}
  191. * @return {*}
  192. */
  193. handleRmove () {
  194. window.removeEventListener("mousemove", () => {
  195. this.arr = [];
  196. });
  197. },
  198. /**
  199. * @description: 防抖方法
  200. * @param {*} e
  201. * @return {*}
  202. */
  203. handleDebounce (e) {
  204. const screenX = e.screenX;
  205. this.arr.push(screenX);
  206. },
  207. /**
  208. * @description: 提交
  209. * @param {*}
  210. * @return {*}
  211. */
  212. onSubmit (formName) {
  213. this.$refs[formName].validate((valid) => {
  214. if (valid) {
  215. this.ssoLogin();
  216. } else {
  217. return false;
  218. }
  219. });
  220. },
  221. //锁屏验证
  222. async ssoLogin () {
  223. try {
  224. this.loading = true;
  225. const obj = {
  226. LoginName: this.name,
  227. LoginPwd: this.form.pwd,
  228. }
  229. if (Number(this.errorNum) >= 2) {
  230. obj.CheckCode = this.form.identify;
  231. }
  232. const res = await SsoLogin(obj);
  233. if (res.code === 0) {
  234. this.$store.dispatch("app/toggleDialog", false);
  235. this.$store.dispatch("app/getErrorNum", 0);
  236. this.arr = [];
  237. this.arrLen = [];
  238. this.time = this.desc;
  239. this.form.pwd = "";
  240. this.form.identify = "";
  241. this.form.CheckCode = "";
  242. this.handleTimer();
  243. this.loading = false;
  244. } else {
  245. const num = Number(res.returnData);
  246. this.$store.dispatch("app/getErrorNum", num);
  247. if (num >= 2) {
  248. this.getCheckCode();
  249. }
  250. if (num >= 5) {
  251. await this.$store.dispatch('user/logout')
  252. this.$router.push(`/login?redirect=${this.$route.fullPath}`)
  253. }
  254. this.$message.error(res.message);
  255. this.loading = false;
  256. }
  257. } catch (error) {
  258. console.log(error);
  259. this.loading = false;
  260. }
  261. },
  262. //验证码重新获取
  263. changeCode () {
  264. this.getCheckCode()
  265. },
  266. //获取动态验证码
  267. async getCheckCode () {
  268. const res = await GetSSOCheckCode();
  269. if (res.code === 0) {
  270. this.form.CheckCode = res.returnData;
  271. } else {
  272. this.$message.error(res.message);
  273. }
  274. },
  275. },
  276. };
  277. </script>
  278. <style lang="scss" scoped>
  279. ::v-deep .dormancyDialog {
  280. .el-input__inner {
  281. height: 48px;
  282. line-height: 48px;
  283. background: #f5f7fa;
  284. border: 1px solid #ebeef5;
  285. border-radius: 6px;
  286. }
  287. .onSubmit {
  288. margin-top: 10px;
  289. }
  290. }
  291. </style>