utils.js 4.9 KB

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