App.vue 8.2 KB

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