utils.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import { MessageEvent, PageMessageEvent } from './event';
  2. export const clearToken = () => {
  3. localStorage.setItem('JWT-TOKEN', '');
  4. };
  5. export const storeToken = token => {
  6. localStorage.setItem('JWT-TOKEN', token);
  7. };
  8. export const getToken = () => {
  9. return localStorage.getItem('JWT-TOKEN');
  10. };
  11. /**
  12. *
  13. * @param {String} url
  14. *
  15. * create a DOM Element
  16. * <a href="url" download/>
  17. */
  18. export const downloadFile = (url, fileName, encode = true) => {
  19. const downloadLink = document.createElement('a');
  20. const body = document.documentElement || document.body;
  21. body.appendChild(downloadLink);
  22. var tempUrl = url;
  23. if (encode) {
  24. var urlArr = tempUrl.split('/');
  25. urlArr[urlArr.length - 1] = encodeURIComponent(urlArr[urlArr.length - 1]);
  26. tempUrl = urlArr.join('/');
  27. }
  28. downloadLink.href = tempUrl;
  29. downloadLink.download = fileName;
  30. downloadLink.click();
  31. body.removeChild(downloadLink);
  32. // }
  33. };
  34. window.downloadFile = downloadFile;
  35. export function IsImageFile(fileName) {
  36. return (
  37. fileName.lastIndexOf('.jpg') !== -1 ||
  38. fileName.lastIndexOf('.png') !== -1 ||
  39. fileName.lastIndexOf('.JPG') !== -1 ||
  40. fileName.lastIndexOf('.PNG') !== -1
  41. );
  42. }
  43. export function IsVedio(fileName) {
  44. return fileName.lastIndexOf('.mp4') !== -1 || fileName.lastIndexOf('.avi') !== -1;
  45. }
  46. export function GetTokenFromUrl() {
  47. const params = GetRequest(window.location.href);
  48. if (params['JWT-TOKEN'] == 'undefined') return '';
  49. return params['JWT-TOKEN'];
  50. }
  51. /**
  52. * [获取URL中的参数名及参数值的集合]
  53. * 示例URL:http://htmlJsTest/getrequest.html?uid=admin&rid=1&fid=2&name=小明
  54. * @param {[string]} urlStr [当该参数不为空的时候,则解析该url中的参数集合]
  55. * @return {[string]} [参数集合]
  56. */
  57. function GetRequest(urlStr) {
  58. if (typeof urlStr == 'undefined') {
  59. var url = decodeURI(location.search); //获取url中"?"符后的字符串
  60. } else {
  61. var url = '?' + urlStr.split('?')[1];
  62. }
  63. var theRequest = new Object();
  64. if (url.indexOf('?') != -1) {
  65. var str = url.substr(1);
  66. var strs = str.split('&');
  67. for (var i = 0; i < strs.length; i++) {
  68. theRequest[strs[i].split('=')[0]] = decodeURI(strs[i].split('=')[1]);
  69. }
  70. }
  71. return theRequest;
  72. }
  73. export function GetFileType(url) {
  74. if (!url) return;
  75. if (url.match('pid-list')) {
  76. return 18;
  77. } else if (url.match('layout-plan')) {
  78. return 19;
  79. }
  80. }
  81. export function getUser(params) {
  82. let arr = [];
  83. if (!params) {
  84. return;
  85. } else {
  86. return (arr = params
  87. .map(item => {
  88. return item.Operator?.CName;
  89. })
  90. .join(','));
  91. }
  92. }
  93. export const UnityAction = {
  94. event: {},
  95. on(type, callback) {
  96. if (!UnityAction.event[type]) {
  97. UnityAction.event[type] = [];
  98. }
  99. UnityAction.event[type].push(callback);
  100. },
  101. off(type, callback) {
  102. if (callback) {
  103. let index = UnityAction.event[type].indexOf(callback);
  104. if (index == -1) return;
  105. UnityAction.event[type].splice(index, 1);
  106. } else {
  107. UnityAction.event[type] = [];
  108. }
  109. window[type] = null;
  110. },
  111. emit(type, e) {
  112. if (!UnityAction.event[type]) return;
  113. console.log('emit====== type:', type, '====== event:', e);
  114. UnityAction.event[type].forEach(item => {
  115. item && item(e);
  116. });
  117. },
  118. addEventListener(type, callback) {
  119. if (window.vuplex) {
  120. UnityAction.on(type, callback);
  121. } else {
  122. window[type] = callback;
  123. }
  124. },
  125. sendMsg(type, message) {
  126. console.log(`type====${type}`);
  127. console.log('message====', message);
  128. if (window.vuplex) {
  129. window.vuplex.postMessage({ type, message });
  130. } else if (window[type]) {
  131. window[type](message);
  132. }
  133. },
  134. };
  135. // if (window.vuplex) {
  136. // window.vuplex.addEventListener('message', e => {
  137. // console.log('============================getMessageForUnity============================');
  138. // const data = JSON.parse(e.data);
  139. // console.log(data);
  140. // UnityAction.emit(data.type, data.message);
  141. // // 将消息广播给子页面
  142. // // let iframeDom = document.getElementsByClassName('iframe');
  143. // // for (let i = 0; i < iframeDom.length; i++) {
  144. // // const item = iframeDom[i];
  145. // // item.contentWindow.postMessage(data.type, data.message);
  146. // // }
  147. // });
  148. // }
  149. // export const EventBus = new MessageEvent();
  150. // export const UnityAction = new UnityMessageEvent();
  151. // export const PageAction = new PageMessageEvent();
  152. export function getGlobalData(key) {
  153. let data;
  154. try {
  155. data = JSON.parse(localStorage.GLOBAL_DATA);
  156. } catch (error) {
  157. data = {};
  158. }
  159. return key ? data[key] : data;
  160. }
  161. export function setGlobalData(key, value) {
  162. let data;
  163. if (!key) return;
  164. try {
  165. data = JSON.parse(localStorage.GLOBAL_DATA);
  166. } catch (error) {
  167. data = {};
  168. }
  169. data[key] = value;
  170. localStorage.GLOBAL_DATA = JSON.stringify(data);
  171. }
  172. // 根据token缓存数据
  173. export function saveData(key, data) {
  174. let token = GetTokenFromUrl() || getToken();
  175. let cache = JSON.stringify({
  176. data,
  177. token,
  178. });
  179. localStorage.setItem(key, cache);
  180. }
  181. // 根据token获取缓存数据
  182. export function getData(key) {
  183. let token = GetTokenFromUrl() || getToken();
  184. let oldData = {};
  185. try {
  186. oldData = JSON.parse(localStorage.getItem(key)) || {};
  187. } catch (error) {}
  188. // token不一致时认为数据失效,返回null
  189. if (oldData.token != token) return null;
  190. return oldData.data;
  191. }
  192. // 清空缓存
  193. export function clearData(key) {
  194. localStorage.setItem(key, '');
  195. }