Navbar.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. <template>
  2. <div class="navbar">
  3. <hamburger
  4. :is-active="sidebar.opened"
  5. class="hamburger-container"
  6. @toggleClick="toggleSideBar"
  7. />
  8. <breadcrumb class="breadcrumb-container" v-if="showBreadcrumb" />
  9. <!-- <NavMenu class="navmenu" /> -->
  10. <div class="right-menu">
  11. <!-- <div class="right-menu-item hover-effect">
  12. <el-tooltip effect="dark" content="家用工程机登录" placement="bottom">
  13. <i
  14. class="el-icon-s-platform"
  15. style="font-size: 24px; line-height: 50px"
  16. @click="toEngine"
  17. ></i>
  18. </el-tooltip>
  19. </div> -->
  20. <div class="right-menu-item hover-effect" @click="toEngine">
  21. <i class="el-icon-s-platform" style="font-size: 18px;"></i>
  22. <span style="font-size: 16px; margin-left: 6px;">家用工程机登录</span>
  23. </div>
  24. <el-badge
  25. :value="noticeCount"
  26. :max="10"
  27. :hidden="!noticeVisible"
  28. class="right-menu-item hover-effect"
  29. >
  30. <el-tooltip effect="dark" content="系统消息" placement="bottom">
  31. <i
  32. class="el-icon-message-solid"
  33. @click="goNotice"
  34. style="font-size: 24px; line-height: 50px"
  35. ></i>
  36. </el-tooltip>
  37. <!-- <el-button icon="el-icon-message-solid" type="text" class="notice-icon" @click="goNotice"></el-button> -->
  38. </el-badge>
  39. <template v-if="device !== 'mobile'">
  40. <screenfull id="screenfull" class="right-menu-item hover-effect" />
  41. </template>
  42. <el-dropdown class="user-container" trigger="click">
  43. <div class="user right-menu-item hover-effect">
  44. <i class="el-icon-user-solid"></i>
  45. <span>{{ name }}</span>
  46. </div>
  47. <el-dropdown-menu slot="dropdown" class="user-dropdown">
  48. <router-link to="/setting/personal">
  49. <el-dropdown-item>个人信息</el-dropdown-item>
  50. </router-link>
  51. <el-dropdown-item divided @click.native="logout">
  52. <span style="display: block">退出登录</span>
  53. </el-dropdown-item>
  54. </el-dropdown-menu>
  55. </el-dropdown>
  56. </div>
  57. <el-dialog
  58. title="绑定工程机系统"
  59. :visible.sync="dialogFormVisible"
  60. :modal="false"
  61. width="30%"
  62. >
  63. <el-form ref="engineForm" :model="engineForm" :rules="engineFormRules">
  64. <el-form-item label="账号" :label-width="formLabelWidth" prop="account">
  65. <el-input
  66. v-model="engineForm.account"
  67. placeholder="请输入账号"
  68. autocomplete="off"
  69. ></el-input>
  70. </el-form-item>
  71. <el-form-item
  72. label="密码"
  73. :label-width="formLabelWidth"
  74. prop="password"
  75. >
  76. <el-input
  77. v-model="engineForm.password"
  78. placeholder="请输入密码"
  79. autocomplete="off"
  80. show-password
  81. ></el-input>
  82. </el-form-item>
  83. </el-form>
  84. <div slot="footer" class="dialog-footer">
  85. <el-button @click="dialogFormVisible = false">取 消</el-button>
  86. <el-button type="primary" @click="submitForm">确 定</el-button>
  87. </div>
  88. </el-dialog>
  89. <!-- 工程机多帐号-->
  90. <!-- <el-dialog-->
  91. <!-- title="绑定工程机系统"-->
  92. <!-- :visible.sync="dialogFormVisible"-->
  93. <!-- :modal="false"-->
  94. <!-- width="60%"-->
  95. <!-- >-->
  96. <!-- <el-form ref="engineForm" :model="engineForm">-->
  97. <!-- <el-row v-for="(row, index) in engineForm.engineList" :key="row.id" :gutter="20">-->
  98. <!-- <el-col :span="8">-->
  99. <!-- <el-form-item-->
  100. <!-- :label="'账号' + (index+1)"-->
  101. <!-- :label-width="formLabelWidth"-->
  102. <!-- :prop="'engineList.' + index + '.enginUserName'"-->
  103. <!-- :rules="{required: true, message: '请输入工程机账号', trigger: 'blur'}"-->
  104. <!-- >-->
  105. <!-- <el-input v-model="row.enginUserName" placeholder="请输入账号" autocomplete="off"></el-input>-->
  106. <!-- </el-form-item>-->
  107. <!-- </el-col>-->
  108. <!-- <el-col :span="8">-->
  109. <!-- <el-form-item-->
  110. <!-- :label="'密码' + (index+1)"-->
  111. <!-- :label-width="formLabelWidth"-->
  112. <!-- :prop="'engineList.' + index + '.enginPassword'"-->
  113. <!-- :rules="{required: true, message: '请输入工程机密码', trigger: 'blur'}"-->
  114. <!-- >-->
  115. <!-- <el-input v-model="row.enginPassword" placeholder="请输入密码" autocomplete="off" show-password></el-input>-->
  116. <!-- </el-form-item>-->
  117. <!-- </el-col>-->
  118. <!-- <el-col :span="8">-->
  119. <!-- <template v-if="row.adminUserId">-->
  120. <!-- <el-button @click="submitForm(index)">重验</el-button>-->
  121. <!-- </template>-->
  122. <!-- <template v-else>-->
  123. <!-- <el-button @click="submitForm(index)">验证</el-button>-->
  124. <!-- </template>-->
  125. <!-- <el-button v-if="row.adminUserId" @click="toEngine(index)">打开</el-button>-->
  126. <!-- <el-button @click="delAccountForm(index)">删除</el-button>-->
  127. <!-- </el-col>-->
  128. <!-- </el-row>-->
  129. <!-- </el-form>-->
  130. <!-- <div slot="footer" class="dialog-footer">-->
  131. <!-- <el-button @click="addRowEngine">新增账号</el-button>-->
  132. <!-- <el-button @click="dialogFormVisible = false">取 消</el-button>-->
  133. <!-- </div>-->
  134. <!-- </el-dialog>-->
  135. </div>
  136. </template>
  137. <script>
  138. import { getRebateOrderMsg } from '@/api/dashboard'
  139. import { mapGetters } from 'vuex'
  140. import Breadcrumb from '@/components/Breadcrumb'
  141. import Hamburger from '@/components/Hamburger'
  142. import Screenfull from '@/components/Screenfull'
  143. import NavMenu from '@/components/NavMenu'
  144. import { getNoticeListCount } from '@/api/notice'
  145. import { bindEngineAccount, checkEngineAccount } from '@/api/setting'
  146. export default {
  147. data() {
  148. return {
  149. intivalId: "",
  150. timer: "",
  151. noticeCount: 0,
  152. userInfo: "",
  153. engineForm: {
  154. account: "",
  155. password: "",
  156. // engineList: []
  157. },
  158. engineFormRules: {
  159. account: [
  160. { required: true, message: "请输入工程机账号", trigger: "blur" },
  161. ],
  162. password: [
  163. { required: true, message: "请输入工程机密码", trigger: "blur" },
  164. ],
  165. },
  166. formLabelWidth: "100px",
  167. dialogFormVisible: false,
  168. //websocket错误连接次数
  169. wsConnectErrorTime:1,
  170. websock: null,
  171. lockReconnect:false,
  172. };
  173. },
  174. mounted() {
  175. const that = this;
  176. // 开定时器轮询未读消息接口(写在全局vuex里比较好)
  177. // that.initNotice();
  178. that.tcMessage();
  179. this.intivalId = setInterval(function () {
  180. that.tcMessage();
  181. }, 5000);
  182. // this.timer = setInterval(function () {
  183. // that.initNotice();
  184. // }, 3000);
  185. // 长链接
  186. // this.initWebSocket();
  187. },
  188. created() {
  189. this.userInfo = JSON.parse(localStorage.getItem("supply_user"));
  190. },
  191. beforeDestroy() {
  192. window.clearInterval(this.intivalId);
  193. window.clearInterval(this.timer);
  194. console.log("退出清理定时器" + this.timer);
  195. this.websocketOnclose();
  196. },
  197. components: {
  198. Breadcrumb,
  199. Hamburger,
  200. Screenfull,
  201. NavMenu,
  202. },
  203. computed: {
  204. showBreadcrumb() {
  205. return this.$store.state.settings.breadcrumb;
  206. },
  207. noticeVisible() {
  208. return this.noticeCount > 0;
  209. },
  210. ...mapGetters(["sidebar", "avatar", "device", "name"]),
  211. },
  212. methods: {
  213. toggleSideBar() {
  214. this.$store.dispatch("app/toggleSideBar");
  215. },
  216. async logout() {
  217. await this.$store.dispatch("user/logout");
  218. // this.$router.push(`/login?redirect=${this.$route.fullPath}`)
  219. this.$store.commit('tagsView/SET_RESET_VIES')
  220. this.$router.push(`/login`);
  221. },
  222. initNotice() {
  223. getNoticeListCount().then((res) => {
  224. if (res.data > 0 && this.noticeCount !== res.data) {
  225. this.noticeCount = res.data;
  226. } else if (res.data === 0 && this.noticeCount !== res.data) {
  227. this.noticeCount = 0;
  228. }
  229. });
  230. },
  231. async tcMessage() {
  232. // 长链接
  233. // let data = {
  234. // type: 'RebateOrderMsg',
  235. // params: {
  236. // }
  237. // }
  238. // this.websocketSend(JSON.stringify(data))
  239. let res = await getRebateOrderMsg();
  240. if (res.data.hasMessage) {
  241. this.$notify.info({
  242. title: "消息",
  243. message: res.data.messages,
  244. position: "bottom-right",
  245. duration: 4000,
  246. showClose: false,
  247. });
  248. }
  249. },
  250. goNotice() {
  251. this.$router.push("/notice/index");
  252. },
  253. openEngineAccount() {
  254. this.engineForm.engineList = this.userInfo.bindEnginList;
  255. this.dialogFormVisible = true;
  256. },
  257. toEngine() {
  258. const userInfoCopy = this.userInfo;
  259. if (userInfoCopy && userInfoCopy.bindEngin) {
  260. checkEngineAccount().then((res) => {
  261. if (res.code === 200) {
  262. // this.$refs.engineSubmit.click()
  263. const { href } = this.$router.resolve({
  264. name: "open_engin",
  265. });
  266. window.open(href, "_blank");
  267. } else {
  268. this.$errorMsg("账号密码错误,请重新绑定");
  269. this.dialogFormVisible = true;
  270. }
  271. });
  272. } else {
  273. this.dialogFormVisible = true;
  274. }
  275. },
  276. // toEngine(index) {
  277. // 工程机多帐号
  278. // const { href } = this.$router.resolve({
  279. // name: "open_engin",
  280. // query: {
  281. // index: index
  282. // }
  283. // });
  284. // window.open(href, "_blank");
  285. // },
  286. // addRowEngine() {
  287. // this.engineForm.engineList.push({
  288. // adminUserId: '',
  289. // enginPassword: '',
  290. // enginUserName: '',
  291. // status: '',
  292. // })
  293. // },
  294. submitForm() {
  295. this.$refs.engineForm.validate((valid) => {
  296. if (valid) {
  297. const params = {
  298. enginUserName: this.engineForm.account,
  299. enginPassword: this.engineForm.password,
  300. };
  301. bindEngineAccount(params).then((res) => {
  302. if (res.code === 200) {
  303. this.$successMsg("绑定成功,正在打开工程机系统");
  304. this.dialogFormVisible = false;
  305. this.$store.dispatch("user/getInfo").then(() => {
  306. this.userInfo = JSON.parse(localStorage.getItem("supply_user"));
  307. const { href } = this.$router.resolve({
  308. name: "open_engin",
  309. });
  310. window.open(href, "_blank");
  311. });
  312. } else {
  313. this.$errorMsg(res.message);
  314. }
  315. });
  316. }
  317. });
  318. },
  319. // submitForm(index) {
  320. // 工程机多帐号
  321. // this.$refs.engineForm.validate((valid) => {
  322. // if (valid) {
  323. // const params = {
  324. // enginUserName: this.engineForm.engineList[index].enginUserName,
  325. // enginPassword: this.engineForm.engineList[index].enginPassword,
  326. // isReset: this.engineForm.engineList[index].adminUserId !== ''
  327. // };
  328. // bindEngineAccount(params).then((res) => {
  329. // if (res.code === 200) {
  330. // this.$successMsg("绑定成功,工程机系统");
  331. // this.$store.dispatch("user/getInfo").then(() => {
  332. // this.userInfo = JSON.parse(localStorage.getItem("supply_user"));
  333. // this.engineForm.engineList = this.userInfo.bindEnginList
  334. // });
  335. // } else {
  336. // this.$errorMsg(res.message);
  337. // }
  338. // });
  339. // }
  340. // });
  341. // },
  342. // delAccountForm(index) {
  343. // if (this.engineForm.engineList[index].id) {
  344. // this.$confirm('此操作将删除账号, 是否继续?', '提示', {
  345. // confirmButtonText: '确定',
  346. // cancelButtonText: '取消',
  347. // type: 'warning',
  348. // center: true
  349. // }).then(() => {
  350. // delEngineAccount({id: this.engineForm.engineList[index].id}).then(() => {
  351. // this.$store.dispatch("user/getInfo").then(() => {
  352. // this.userInfo = JSON.parse(localStorage.getItem("supply_user"));
  353. // this.engineForm.engineList = this.userInfo.bindEnginList
  354. // });
  355. // this.$successMsg("删除成功");
  356. // })
  357. // }).catch(() => {
  358. // });
  359. // } else {
  360. // this.engineForm.engineList.splice(index, 1)
  361. // }
  362. // }
  363. initWebSocket: function () {
  364. // WebSocket与普通的请求所用协议有所不同,ws等同于http,wss等同于https
  365. // var userId = this.$store.getters.userid;
  366. //
  367. // var url = process.env.VUE_APP_BASE_API.replace("https://","wss://").replace("http://","ws://").replace("api","supply")+"websocket/"+userId;
  368. // console.debug(userId, url);
  369. // let token = getToken()
  370. // this.websock = new WebSocket(url, [token]);
  371. // this.websock.onopen = this.websocketOnopen;
  372. // this.websock.onerror = this.websocketOnerror;
  373. // this.websock.onmessage = this.websocketOnmessage;
  374. // this.websock.onclose = this.websocketOnclose;
  375. },
  376. websocketOnopen: function () {
  377. console.debug("WebSocket连接成功");
  378. //心跳检测重置
  379. //this.heartCheck.reset().start();
  380. },
  381. websocketOnerror: function (e) {
  382. console.debug("WebSocket连接发生错误,第%s次",this.wsConnectErrorTime);
  383. this.wsConnectErrorTime = this.wsConnectErrorTime + 1;
  384. if(this.wsConnectErrorTime>5){
  385. console.debug("WebSocket连接错误超过5次,就不再重新连了!");
  386. this.lockReconnect = true
  387. return;
  388. }
  389. this.reconnect();
  390. },
  391. websocketOnmessage: function (e) {
  392. console.debug("-----接收消息-------",e.data);
  393. let data = eval("(" + e.data + ")"); //解析对象
  394. if (data.type === 'RebateOrderMsg') {
  395. if (data.res.hasMessage) {
  396. this.$notify.info({
  397. title: "消息",
  398. message: res.data.messages,
  399. position: "bottom-right",
  400. duration: 4000,
  401. showClose: false,
  402. });
  403. }
  404. }
  405. },
  406. websocketOnclose: function (e) {
  407. console.debug("connection closed (" + e + ")");
  408. if(e){
  409. console.debug("connection closed (" + e.code + ")");
  410. }
  411. this.reconnect();
  412. },
  413. websocketSend(text) { // 数据发送
  414. try {
  415. this.websock.send(text);
  416. } catch (err) {
  417. console.debug("send failed (" + err.code + ")");
  418. }
  419. },
  420. reconnect() {
  421. var that = this;
  422. if(that.lockReconnect) return;
  423. that.lockReconnect = true;
  424. //没连接上会一直重连,设置延迟避免请求过多
  425. setTimeout(function () {
  426. console.debug("尝试重连...");
  427. that.initWebSocket();
  428. that.lockReconnect = false;
  429. }, 20000);
  430. },
  431. },
  432. };
  433. </script>
  434. <style lang="scss" scoped>
  435. @import "~@/styles/variables.scss";
  436. .navbar {
  437. width: 100%;
  438. height: 50px;
  439. overflow: hidden;
  440. background: #{$navbarBg};
  441. box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  442. .hamburger-container {
  443. line-height: 46px;
  444. height: 100%;
  445. color: #{$navbarText};
  446. float: left;
  447. cursor: pointer;
  448. transition: background 0.3s;
  449. -webkit-tap-highlight-color: transparent;
  450. border-right: 1px solid #eaeaea;
  451. &:hover {
  452. background: rgba(0, 0, 0, 0.025);
  453. }
  454. }
  455. .navmenu {
  456. float: left;
  457. }
  458. .breadcrumb-container {
  459. float: left;
  460. }
  461. .right-menu {
  462. float: right;
  463. height: 100%;
  464. line-height: 50px;
  465. & > div {
  466. float: left;
  467. }
  468. &:focus {
  469. outline: none;
  470. }
  471. .user-container {
  472. height: 50px;
  473. .user {
  474. i {
  475. font-size: 18px;
  476. margin-right: 5px;
  477. }
  478. span {
  479. font-size: 16px;
  480. }
  481. }
  482. }
  483. .right-menu-item {
  484. display: inline-block;
  485. padding: 0 15px;
  486. height: 100%;
  487. font-size: 18px;
  488. color: #{$navbarText};
  489. vertical-align: text-bottom;
  490. border-left: 1px solid #eaeaea;
  491. &.hover-effect {
  492. cursor: pointer;
  493. transition: background 0.3s;
  494. &:hover {
  495. background: rgba(0, 0, 0, 0.025);
  496. }
  497. }
  498. .notice-icon {
  499. padding-top: 0;
  500. padding-bottom: 0;
  501. }
  502. }
  503. }
  504. }
  505. </style>
  506. <style lang="scss">
  507. .navbar {
  508. .right-menu {
  509. .right-menu-item {
  510. .notice-icon > i {
  511. font-size: 18px;
  512. }
  513. sup {
  514. top: 12px;
  515. right: 25px;
  516. }
  517. }
  518. }
  519. }
  520. </style>