App.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <!--
  2. * @Author: your name
  3. * @Date: 2021-10-14 17:17:53
  4. * @LastEditTime: 2022-01-06 12:03:00
  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="false" customClass="dormancyDialog" width="496px">
  13. <div class="dormancy">
  14. <div class="title">系统休眠,请输入密码解锁</div>
  15. <div class="content">
  16. <el-form ref="form" :rules="rules" :model="form">
  17. <el-form-item prop="pwd">
  18. <el-input show-password placeholder="请输入密码" v-model="form.pwd"></el-input>
  19. </el-form-item>
  20. <el-form-item>
  21. <button class="button-public-shadow onSubmit" @click="onSubmit('form')">
  22. 确定
  23. </button>
  24. </el-form-item>
  25. </el-form>
  26. </div>
  27. </div>
  28. </Dialog>
  29. <!--路由出口-->
  30. <router-view />
  31. </div>
  32. </template>
  33. <script>
  34. import Dialog from "@/layout/components/Dialog/index.vue";
  35. import { mapGetters } from "vuex";
  36. import { SsoLogin } from "@/api/apiHome";
  37. export default {
  38. name: "App",
  39. components: {
  40. Dialog,
  41. },
  42. data () {
  43. return {
  44. arr: [], //鼠标移动screenX值数组
  45. arrLen: [], //一段时间后上报的screenX值数组
  46. timer: null, //定时器
  47. time: null, //定时器时间 1=1S
  48. desc: null, //固定的定时器时间/和time保持一致
  49. form: {
  50. //表单数据
  51. pwd: "",
  52. },
  53. rules: {
  54. //表单验证
  55. pwd: [{ required: true, message: "请输入密码", trigger: "blur" }],
  56. },
  57. };
  58. },
  59. computed: {
  60. ...mapGetters(["dialog", "token", "name", "systemSet"]),
  61. },
  62. watch: {
  63. "$store.state.user.token": {
  64. handler (val) {
  65. if (val) {
  66. this.handleInit();
  67. } else {
  68. this.clearAll();
  69. }
  70. },
  71. deep: true,
  72. },
  73. $route: {
  74. handler () {
  75. this.$store.dispatch("auth/changeAuthMsg", []);
  76. this.$store.dispatch("auth/changeAuthArrs", []);
  77. this.$store.dispatch("auth/changeAuthList", []);
  78. this.$store.dispatch("auth/changeAuthId", null);
  79. },
  80. deep: true,
  81. },
  82. },
  83. mounted () {
  84. this.beforeUnload();
  85. },
  86. beforeDestroy () {
  87. //结束定时器和释放timer
  88. this.clearAll();
  89. },
  90. methods: {
  91. // 初始化
  92. handleInit () {
  93. this.handleMove();
  94. if (!this.dialog) {
  95. this.handleTimer();
  96. }
  97. },
  98. /**
  99. * @description: 清除页面定时器和监听
  100. * @param {*}
  101. * @return {*}
  102. */
  103. clearAll () {
  104. clearInterval(this.timer);
  105. this.timer = null;
  106. this.time = null;
  107. this.desc = null;
  108. this.arr = [];
  109. this.arrLen = [];
  110. this.handleRmove();
  111. sessionStorage.setItem("token", "");
  112. },
  113. // 页面刷新 重新启用方法
  114. beforeUnload () {
  115. window.addEventListener("beforeunload", () => {
  116. if (this.token) {
  117. sessionStorage.setItem("token", this.token);
  118. }
  119. });
  120. let oldViews = sessionStorage.getItem("token") || "";
  121. if (oldViews) {
  122. this.handleInit();
  123. }
  124. },
  125. /**
  126. * @description: 定时器方法
  127. * @param {*}
  128. * @return {*}
  129. */
  130. handleTimer () {
  131. const obj =
  132. typeof this.systemSet === "string"
  133. ? JSON.parse(this.systemSet)
  134. : this.systemSet;
  135. const { LockMins } = obj;
  136. this.time = LockMins * 60;
  137. this.desc = LockMins * 60;
  138. this.timer = setInterval(() => {
  139. this.time--;
  140. if (this.time === 0) {
  141. const result = this.arrLen;
  142. const rut = this.arr;
  143. result.push(rut.length);
  144. if (result.length >= 2) {
  145. if (result[result.length - 2] === result[result.length - 1]) {
  146. // 相同时 结束倒计时
  147. this.$store.dispatch("app/toggleDialog", true);
  148. this.clearAll();
  149. } else {
  150. this.time = this.desc;
  151. }
  152. } else {
  153. this.time = this.desc;
  154. }
  155. }
  156. }, 1000);
  157. },
  158. /**
  159. * @description: 监听鼠标移动方法/防抖
  160. * @param {*}
  161. * @return {*}
  162. */
  163. handleMove () {
  164. window.addEventListener(
  165. "mousemove",
  166. _.debounce(this.handleDebounce, 100)
  167. );
  168. },
  169. /**
  170. * @description: 移除鼠标移动监听
  171. * @param {*}
  172. * @return {*}
  173. */
  174. handleRmove () {
  175. window.removeEventListener("mousemove", () => {
  176. this.arr = [];
  177. });
  178. },
  179. /**
  180. * @description: 防抖方法
  181. * @param {*} e
  182. * @return {*}
  183. */
  184. handleDebounce (e) {
  185. const screenX = e.screenX;
  186. this.arr.push(screenX);
  187. },
  188. /**
  189. * @description: 提交
  190. * @param {*}
  191. * @return {*}
  192. */
  193. onSubmit (formName) {
  194. this.$refs[formName].validate((valid) => {
  195. if (valid) {
  196. this.ssoLogin();
  197. } else {
  198. return false;
  199. }
  200. });
  201. },
  202. //锁屏验证
  203. async ssoLogin () {
  204. const res = await SsoLogin({
  205. LoginName: this.name,
  206. LoginPwd: this.form.pwd,
  207. });
  208. if (res.code === 0) {
  209. this.$store.dispatch("app/toggleDialog", false);
  210. this.arr = [];
  211. this.arrLen = [];
  212. this.time = this.desc;
  213. this.form.pwd = "";
  214. this.handleTimer();
  215. } else {
  216. this.$message.error(res.message);
  217. }
  218. },
  219. },
  220. };
  221. </script>
  222. <style lang="scss" scoped>
  223. ::v-deep .dormancyDialog {
  224. .el-input__inner {
  225. height: 48px;
  226. line-height: 48px;
  227. background: #f5f7fa;
  228. border: 1px solid #ebeef5;
  229. border-radius: 6px;
  230. }
  231. .onSubmit {
  232. margin-top: 10px;
  233. }
  234. }
  235. </style>