1 | const Light = require("../lib/core");
|
2 | import { parse2query,resolvePath,query2params } from "../lib/utils"
|
3 |
|
4 | let routerStack = [];
|
5 | function processJSCoreEvent(type, args) {
|
6 | Light.trigger(type, args)
|
7 | if (type === "navigateBack") {
|
8 | if (routerStack.length > 1) {
|
9 | routerStack.pop();
|
10 | __base__.currentRoute = routerStack[routerStack.length - 1].pageId;
|
11 | }
|
12 | }
|
13 | if (type === "navigate") {
|
14 | let pathParsed = args.path.split("?");
|
15 | let params = {};
|
16 | if (pathParsed.length === 2) {
|
17 | pathParsed[1].split("&").forEach(function (o) {
|
18 | let arg = o.split("=");
|
19 |
|
20 | params[arg[0]] = decodeURIComponent(arg[1]);
|
21 | })
|
22 | }
|
23 | __base__.currentRoute = args && args.pageId;
|
24 | __base__.postPatch(`addCssNative("${__base__.bundlePath + "/app.miniapp.css"}")`, args.pageId || '');
|
25 |
|
26 |
|
27 | params.__page_id = args.pageId;
|
28 |
|
29 | let router = Light.getRouter();
|
30 |
|
31 | const app = new Light.Vue({
|
32 | router,
|
33 | template: `<div page-id="${args.pageId}"><router-view :$page="{id:'${args.pageId}'}" /></div>`
|
34 | });
|
35 | router.push({
|
36 | path: pathParsed[0],
|
37 | query: params
|
38 | });
|
39 |
|
40 | routerStack.push({
|
41 | pageId:args.pageId,
|
42 | router
|
43 | });
|
44 | app.$mount()
|
45 | }
|
46 | };
|
47 | function reloadCheck(){
|
48 | let myId = new Date().getTime()+Math.random();
|
49 | if(process.env.NODE_ENV === "dev" && typeof LightJSBridge !== "undefined" && __base__.bundlePath && __base__.bundlePath.indexOf("http://") === 0){
|
50 | setInterval(()=>{
|
51 | LightJSBridge.call("stream.fetch",{
|
52 | url:__base__.bundlePath+'/check_reload_me?id='+myId
|
53 | },function(data){
|
54 | if(data.data && data.data.data && JSON.parse(data.data.data).reload){
|
55 | LightJSBridge.call("miniapp.reload",{})
|
56 | }
|
57 | })
|
58 | },5000)
|
59 | }
|
60 | }
|
61 |
|
62 | module.exports = {
|
63 | start(router,options){
|
64 | },
|
65 | init() {
|
66 | __base__.bundlePath = /^(.*)\/[^\/]*$/ig.exec(__base__.bundleUrl.split("#")[0])[1];
|
67 | if(__base__.EventHub){
|
68 | __base__.EventHub.subscribe((type,data)=>{
|
69 | processJSCoreEvent(type,data);
|
70 | })
|
71 | }else{
|
72 | __thread__.triggerEvent = processJSCoreEvent;
|
73 | }
|
74 | reloadCheck();
|
75 | },
|
76 | navigateBack(number) {
|
77 | if (routerStack[routerStack.length - 1].router.history.stack.length > 1) {
|
78 | routerStack[routerStack.length - 1].router.go(-1);
|
79 | } else {
|
80 | LightJSBridge.call("native.back", { number: number }, null);
|
81 | }
|
82 | },
|
83 | navigateTo(path, params, options) {
|
84 |
|
85 | let router = routerStack[routerStack.length - 1].router;
|
86 | var distUrl = '';
|
87 |
|
88 |
|
89 | if (path.view) {
|
90 |
|
91 | path.view = path.view.replace(/^\//,'');
|
92 |
|
93 | distUrl = `${__base__.bundleUrl.split("#")[0]}#/${resolvePath(path.view, params)}`;
|
94 | path.type = __base__.config.env.container;
|
95 | } else {
|
96 | distUrl = resolvePath(path.url, params);
|
97 |
|
98 | if(path.type==='lightweb') {
|
99 | path.type = options.miniappContainer || __base__.config.env.container;
|
100 | }
|
101 | }
|
102 |
|
103 | if (options.replace === true && path.view) {
|
104 | router.push({
|
105 | path: "/" + path.view,
|
106 | query: params
|
107 | });
|
108 | } else {
|
109 |
|
110 |
|
111 | if(path.module) {
|
112 |
|
113 | let query = path.url.indexOf('?')>-1 ? path.url.split('?')[1] : ''
|
114 | let queryParams = query=='' ? {} : query2params(query)
|
115 | LightJSBridge.call("miniapp.openGmu", {
|
116 | gmuName: path.module,
|
117 | params: {
|
118 | ...queryParams,
|
119 | ...params
|
120 | }
|
121 | }, ()=>{}, options.pageId || __base__.currentRoute)
|
122 | } else {
|
123 | LightJSBridge.call("miniapp.openGmu", {
|
124 | gmuName: path.type,
|
125 | params: {
|
126 | startPage: distUrl,
|
127 | ...options
|
128 | }
|
129 | }, ()=>{}, options.pageId || __base__.currentRoute)
|
130 | }
|
131 | }
|
132 | },
|
133 | getCurrentFile() {
|
134 | return '';
|
135 | },
|
136 | ajax(options) {
|
137 |
|
138 | let body = options.data;
|
139 | if (!options.headers || !options.headers['Content-Type'] || options.headers['Content-Type'] === 'application/x-www-form-urlencoded') {
|
140 |
|
141 | body = parse2query(options.data || {});
|
142 | }
|
143 | var settings = {
|
144 | method: options.type,
|
145 | headers: options.headers || {},
|
146 | type: options.dataType || "json",
|
147 | timeout: options.timeout
|
148 | };
|
149 |
|
150 | if (options.type.toLowerCase() !== 'get') {
|
151 | settings.body = body
|
152 | settings.url = options.url;
|
153 | } else {
|
154 | settings.url = (options.url) + "?" + parse2query(options.data || {});
|
155 | }
|
156 |
|
157 | if (!/^http/.test(settings.url)) {
|
158 | settings.url = __base__.bundlePath + '/' + settings.url;
|
159 | }
|
160 |
|
161 | let startTime = new Date(), endTime;
|
162 |
|
163 | LightJSBridge.call("stream.fetch", settings, function (data) {
|
164 | if (+data.info.error_code !== 0) {
|
165 | options.error(data.data.data);
|
166 | } else {
|
167 | endTime = new Date();
|
168 |
|
169 | if (__base__.DEBUG_MODE == true && __base__.DEBUG_SHOW_NETWORK_INFO == true) {
|
170 | if(settings.body) settings.body = JSON.stringify(settings.body)
|
171 | let reqObj = settings;
|
172 | reqObj.headers = data['data']&&data.data.headers;
|
173 | reqObj.costTime = endTime - startTime;
|
174 | reqObj.callBackData = data['data']&&data.data['data'];
|
175 | reqObj.statusCode = data['data']&&data.data['status'];
|
176 | __base__.postPatch(`window.XMLHttpRequestUser(${JSON.stringify(reqObj)})`, __base__.currentRoute)
|
177 | }
|
178 |
|
179 | let request = {
|
180 | getAllResponseHeaders() {
|
181 | return JSON.stringify(data.data.headers || {})
|
182 | },
|
183 | getResponseHeader(key) {
|
184 | return data.data.headers[key]
|
185 | }
|
186 | };
|
187 | options.success(data.data.data, data.data.status, request)
|
188 | }
|
189 | }, __base__.currentRoute)
|
190 | }
|
191 | };
|