app.html 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <!DOCTYPE html>
  2. <html lang="zh">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>ROMA</title>
  6. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  7. <link rel="icon" type="image/x-ico" href=""/>
  8. <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
  9. <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
  10. <link rel="stylesheet" href="./public/static/amis-sdk/helper.css">
  11. <link rel="stylesheet" href="./public/static/amis-sdk/sdk.css">
  12. <link rel="stylesheet" href="./public/static/amis-sdk/iconfont.css">
  13. <link rel="stylesheet" href="./public/static/fonts/remixicon.css">
  14. <!-- 引入自定义样式 -->
  15. <link rel="stylesheet" href="./public/static/css/app.css">
  16. </head>
  17. <body>
  18. <div id="root" class="app-wrapper"></div>
  19. <script type='text/javascript' src='./public/static/amis-sdk/sdk.js'></script>
  20. <script type='text/javascript' src='./public/static/amis-static/history.js'></script>
  21. <!-- 引入语言包 -->
  22. <script src="./lang/en-US.js"></script>
  23. <script src="./lang/zh-CN.js"></script>
  24. <!-- 引入多语言js处理文件,必须先引入语言包 -->
  25. <script src="./public/static/tools/common.js"></script>
  26. <!-- 引入header的JSON -->
  27. <script src="./pages/app/header-json.js"></script>
  28. <script>
  29. (function () {
  30. let amis = amisRequire('amis/embed');
  31. const match = amisRequire('path-to-regexp').match;
  32. // 如果想用 browserHistory 请切换下这处代码, 其他不用变
  33. // const history = History.createBrowserHistory();
  34. const history = History.createHashHistory();
  35. const header = getHeaderJson()
  36. const app = {
  37. type: 'app',
  38. brandName: 'ROMA',
  39. // logo: './public/static/images/logo.jpg',
  40. // footer: '<div class="p-2 text-center bg-light">底部区域</div>',
  41. // asideBefore: '<div class="p-2 text-center">菜单前面区域</div>',
  42. // asideAfter: '<div class="p-2 text-center">菜单后面区域</div>',
  43. // // api: '/pages/site.json'
  44. header,
  45. api: {
  46. "url": 'http://localhost:8081/page/menu'
  47. }
  48. };
  49. function normalizeLink(to, location = history.location) {
  50. to = to || '';
  51. if (to && to[0] === '#') {
  52. to = location.pathname + location.search + to;
  53. } else if (to && to[0] === '?') {
  54. to = location.pathname + to;
  55. }
  56. const idx = to.indexOf('?');
  57. const idx2 = to.indexOf('#');
  58. let pathname = ~idx
  59. ? to.substring(0, idx)
  60. : ~idx2
  61. ? to.substring(0, idx2)
  62. : to;
  63. let search = ~idx ? to.substring(idx, ~idx2 ? idx2 : undefined) : '';
  64. let hash = ~idx2 ? to.substring(idx2) : location.hash;
  65. if (!pathname) {
  66. pathname = location.pathname;
  67. } else if (pathname[0] != '/' && !/^https?\:\/\//.test(pathname)) {
  68. let relativeBase = location.pathname;
  69. const paths = relativeBase.split('/');
  70. paths.pop();
  71. let m;
  72. while ((m = /^\.\.?\//.exec(pathname))) {
  73. if (m[0] === '../') {
  74. paths.pop();
  75. }
  76. pathname = pathname.substring(m[0].length);
  77. }
  78. pathname = paths.concat(pathname).join('/');
  79. }
  80. return pathname + search + hash;
  81. }
  82. function isCurrentUrl(to, ctx) {
  83. if (!to) {
  84. return false;
  85. }
  86. const pathname = history.location.pathname;
  87. const link = normalizeLink(to, {
  88. ...location,
  89. pathname,
  90. hash: ''
  91. });
  92. if (!~link.indexOf('http') && ~link.indexOf(':')) {
  93. let strict = ctx && ctx.strict;
  94. return match(link, {
  95. decode: decodeURIComponent,
  96. strict: typeof strict !== 'undefined' ? strict : true
  97. })(pathname);
  98. }
  99. return decodeURI(pathname) === link;
  100. }
  101. let languageInfo = getLanguageInfo();//获取语言包内容
  102. let amisInstance = amis.embed(
  103. '#root',
  104. app,
  105. {
  106. location: history.location,
  107. locale: languageInfo.lang
  108. },
  109. {
  110. replaceText: languageInfo.langReplaceText,
  111. langReplaceTextIgnoreKeys: languageInfo.langReplaceTextIgnoreKeys,
  112. // watchRouteChange: fn => {
  113. // return history.listen(fn);
  114. // },
  115. fetcher: (params) => {
  116. var method = params.method;
  117. var data = params.data;
  118. var body = params.body;
  119. var url = params.url;
  120. var form = new FormData();
  121. form = JSON.stringify(params.data);
  122. var ContentType = 'application/json';
  123. let header = {
  124. // "Content-Type": "application/json",
  125. "Content-Type": ContentType,
  126. "Authorization":'Bearer ' + getToken(),
  127. }
  128. let isFormData = (typeof FormData !== 'undefined') && (params['data'] instanceof FormData);
  129. if (isFormData) {
  130. delete header['Content-Type'];
  131. form = data;
  132. }
  133. let headers = new Headers(header);
  134. return new Promise(function (resolve, reject) {
  135. fetch(url,{
  136. method: method,
  137. body: form,
  138. headers: headers
  139. }).then(function (response) {
  140. return response.json();
  141. }).then(function (res) {
  142. // if (isFormData) {
  143. // data['data'] = res.data;
  144. // }
  145. resolve({ "data": res });
  146. });
  147. });
  148. },
  149. updateLocation: (location, replace) => {
  150. location = normalizeLink(location);
  151. if (location === 'goBack') {
  152. return history.goBack();
  153. } else if (
  154. (!/^https?\:\/\//.test(location) &&
  155. location ===
  156. history.location.pathname + history.location.search) ||
  157. location === history.location.href
  158. ) {
  159. // 目标地址和当前地址一样,不处理,免得重复刷新
  160. return;
  161. } else if (/^https?\:\/\//.test(location) || !history) {
  162. return (window.location.href = location);
  163. }
  164. history[replace ? 'replace' : 'push'](location);
  165. },
  166. jumpTo: (to, action) => {
  167. if (to === 'goBack') {
  168. return history.goBack();
  169. }
  170. to = normalizeLink(to);
  171. if (isCurrentUrl(to)) {
  172. return;
  173. }
  174. if (action && action.actionType === 'url') {
  175. action.blank === false
  176. ? (window.location.href = to)
  177. : window.open(to, '_blank');
  178. return;
  179. } else if (action && action.blank) {
  180. window.open(to, '_blank');
  181. return;
  182. }
  183. if (/^https?:\/\//.test(to)) {
  184. window.location.href = to;
  185. } else if (
  186. (!/^https?\:\/\//.test(to) &&
  187. to === history.pathname + history.location.search) ||
  188. to === history.location.href
  189. ) {
  190. // do nothing
  191. } else {
  192. history.push(to);
  193. }
  194. },
  195. isCurrentUrl: isCurrentUrl,
  196. theme: 'cxd'
  197. }
  198. );
  199. history.listen(state => {
  200. amisInstance.updateProps({
  201. location: state.location || state
  202. });
  203. });
  204. })();
  205. // 获取登录的token-登录请求不需要登录token
  206. function getToken() {
  207. return localStorage.getItem("apiToken");
  208. }
  209. </script>
  210. </body>
  211. </html>