index.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. import {
  2. isWeixin
  3. } from '@/common/utils/common';
  4. import {
  5. webViewHref,
  6. navToPage,
  7. crossPage
  8. } from '@/common/utils/navPag';
  9. import {
  10. firstPerform
  11. } from "js-perform-lock";
  12. import store from '@/store/index.js'
  13. import {
  14. setStorage,
  15. getStorage,
  16. removeStorage
  17. } from "@/common/utils/storage.js"
  18. // #ifdef H5
  19. import wx from 'weixin-js-sdk'
  20. // #endif
  21. // 微信小程序页面劫持
  22. export const goLoginPage = (new firstPerform(1000)).refactor(function (pageObj) {
  23. navToPage({
  24. url: pageObj ? ("/pages/login/indexs?pageObj=" + encodeURIComponent(pageObj)) : "/pages/login/indexs"
  25. }, "navigateTo")
  26. // store.dispatch('user/resetToken')
  27. });
  28. // #ifdef MP-WEIXIN
  29. export const routerBeforeEach = (new firstPerform(200)).refactor(function () {
  30. function getrouter() {
  31. let routes = getCurrentPages();
  32. if (routes.length) {
  33. let curRoute = routes[routes.length - 1].route
  34. let whitePages = [
  35. 'pages/login/index',
  36. 'pages/login/indexs',
  37. 'pages/index/index',
  38. 'pages/goods/index',
  39. 'pages/mine/index',
  40. ]
  41. if (!store.getters.token && whitePages.indexOf(curRoute) == -1) {
  42. goLoginPage()
  43. }
  44. } else {
  45. setTimeout(getrouter, 300)
  46. }
  47. }
  48. getrouter()
  49. });
  50. // #endif
  51. const updatePageLay = function () {
  52. var uni_page_head = document.querySelector("uni-page-head[uni-page-head-type='default']")
  53. if (uni_page_head) {
  54. uni_page_head.remove()
  55. }
  56. }
  57. // (new firstPerform(1000)).refactor(function() {
  58. // // 去除顶部
  59. // var uni_page_head = document.querySelector("uni-page-head[uni-page-head-type='default']")
  60. // if (uni_page_head) {
  61. // uni_page_head.remove()
  62. // }
  63. // // 去除底部
  64. // // var uni_tabbar_bottom = document.getElementsByClassName("uni-tabbar-bottom")
  65. // // if (uni_tabbar_bottom.length) {
  66. // // uni_tabbar_bottom[0].remove()
  67. // // var uni_page_wrapper = document.getElementsByTagName("uni-page-wrapper")
  68. // // if (uni_page_wrapper.length) {
  69. // // loadStyleString(
  70. // // "uni-page-wrapper{height: calc(100% - env(safe-area-inset-bottom)) !important} uni-page-wrapper::after{height: env(safe-area-inset-bottom) !important}"
  71. // // );
  72. // // }
  73. // // }
  74. // });
  75. window.backPage = function () {
  76. // 获取当前页面栈的实例数组
  77. const pages = getCurrentPages();
  78. if(pages.length > 1) {
  79. navToPage({delta: 1}, 'navigateBack')
  80. }else {
  81. var searchParams = window.location.search;
  82. navToPage({url: `/pages/index/index${searchParams}`}, 'reLaunch')
  83. }
  84. }
  85. // 插入样式
  86. function loadStyleString(css) {
  87. var style = document.createElement("style");
  88. style.type = "text/css";
  89. try {
  90. style.appendChild(document.createTextNode(css));
  91. } catch (ex) {
  92. style.styleSheet.cssText = css;
  93. }
  94. var head = document.getElementsByTagName('head')[0];
  95. head.appendChild(style);
  96. }
  97. var insertFloatButton = (function () {
  98. var isDragging = false;
  99. var offsetX, offsetY;
  100. var div;
  101. let blackList = [`${process.env.VUE_APP_BASE_PATH}pages/index/index`, `${process.env.VUE_APP_BASE_PATH}pages/workorder/index`, `${process.env.VUE_APP_BASE_PATH}pages/goods/index`, `${process.env.VUE_APP_BASE_PATH}pages/mine/index`];
  102. // 获取手机信息配置接口
  103. const sys = uni.getSystemInfoSync();
  104. // 屏幕的宽高
  105. var windowWidth = sys.windowWidth;
  106. var windowHeight = sys.windowHeight;
  107. // 设置边界值
  108. var edge = 10;
  109. // 设置块宽度
  110. var divWidth = 52;
  111. // 触摸移动时更新块的位置
  112. document.addEventListener('touchmove', function (event) {
  113. event.stopPropagation();
  114. if (div && isDragging) {
  115. var touch = event.touches[0];
  116. div.style.left = touch.pageX - offsetX + 'px';
  117. let clientY = touch.clientY - divWidth;
  118. let edgeBottom = windowHeight - (divWidth * 2) - edge;
  119. let moveTop = 0;
  120. // 上下触及边界
  121. if (clientY < edge) {
  122. moveTop = edge;
  123. } else if (clientY > edgeBottom) {
  124. moveTop = edgeBottom;
  125. } else {
  126. moveTop = clientY
  127. }
  128. div.style.top = moveTop + 'px';
  129. setStorage('top', moveTop);
  130. }
  131. });
  132. // 触摸结束时停止拖动
  133. document.addEventListener('touchend', function (event) {
  134. event.stopPropagation();
  135. let edgeRigth = windowWidth - divWidth - edge;
  136. let moveLeft = div.style.left;
  137. // 左右触及边界
  138. if (moveLeft < windowWidth / 2 - divWidth) {
  139. moveLeft = edge;
  140. } else {
  141. moveLeft = edgeRigth;
  142. }
  143. div.style.left = moveLeft + 'px';
  144. setStorage('left', moveLeft);
  145. isDragging = false;
  146. // div.style.cursor = 'grab';
  147. });
  148. return function () {
  149. // 设置默认值
  150. if (!getStorage('left')) {
  151. setStorage('left', windowWidth - divWidth - edge);
  152. }
  153. if (!getStorage('top')) {
  154. setStorage('top', windowHeight - (divWidth * 2) - edge - 100);
  155. }
  156. let hasDiv = document.getElementById('floatButtonBox');
  157. let isBlack = blackList.includes(window.location.pathname);
  158. if (isBlack && hasDiv) {
  159. hasDiv.remove()
  160. } else if (!isBlack && !hasDiv) {
  161. // 获取当前页面的查询参数
  162. var searchParams = window.location.search;
  163. div = document.createElement('div');
  164. div.id = 'floatButtonBox';
  165. div.style.width = divWidth + 'px';
  166. div.style.position = 'fixed';
  167. div.style.top = getStorage('top') + 'px';
  168. div.style.left = getStorage('left') + 'px';
  169. div.style.zIndex = '99999';
  170. div.innerHTML = `
  171. <img style="width: 100%; height: 100%; display: block;" src='${process.env.VUE_APP_BASE_PATH}static/icon/floatButton1.png' onclick="(function(){navToPage({url: '/pages/index/index${searchParams}'}, 'reLaunch')})()"/>
  172. <img style="width: 100%; height: 100%; display: block;" src='${process.env.VUE_APP_BASE_PATH}static/icon/floatButton2.png' onclick="backPage()"/>
  173. `;
  174. // 鼠标按下时开始拖动
  175. div.addEventListener('touchstart', function (event) {
  176. event.stopPropagation();
  177. isDragging = true;
  178. var touch = event.touches[0];
  179. offsetX = touch.pageX - div.getBoundingClientRect().left;
  180. offsetY = touch.pageY - div.getBoundingClientRect().top;
  181. // div.style.cursor = 'grabbing';
  182. });
  183. document.body.appendChild(div);
  184. }
  185. }
  186. })()
  187. // 存放页面公用数据、方法...
  188. export default {
  189. data() {
  190. return {
  191. crossPage: crossPage,
  192. }
  193. },
  194. computed: {
  195. crossPagePam() {
  196. var num = this.$store.state.user.crossPagePam || 0
  197. var city = this.$store.state.user.city || ''
  198. var province = this.$store.state.user.province || ''
  199. var data = getStorage("crossPageTs")
  200. return {
  201. crossPageTs: data,
  202. timestamp: num,
  203. city: city,
  204. province: province,
  205. }
  206. }
  207. },
  208. onLoad() {
  209. // #ifdef MP-WEIXIN
  210. // routerBeforeEach()
  211. // #endif
  212. },
  213. onShow() {
  214. insertFloatButton()
  215. // const originalTitle = document.title;
  216. // const fixedDescription = ` - ${'商户名称'}`;
  217. // if(!~document.title.indexOf(fixedDescription)){
  218. // document.title = originalTitle + fixedDescription;
  219. // }
  220. },
  221. mounted() {
  222. // #ifdef MP-WEIXIN
  223. // routerBeforeEach()
  224. // #endif
  225. if (isWeixin()) {
  226. updatePageLay()
  227. }
  228. },
  229. methods: {
  230. webViewHref: webViewHref,
  231. }
  232. }