UNPKG

7.79 kBJavaScriptView Raw
1const Light = require("../lib/core");
2import { parse2query,resolvePath,query2params } from "../lib/utils"
3
4let routerStack = [];
5function 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 //167503 小程序olight模块支持openGMU 时 传入pageId
27 params.__page_id = args.pageId;
28
29 let router = Light.getRouter();
30 //201407 【lighting小程序】小程序pageid 稳定性和正确性的问题,vue框架小程序持有的pageId在reload时更新丢失问题
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 //160945 navigate接口支持replace:true
40 routerStack.push({
41 pageId:args.pageId,
42 router
43 });
44 app.$mount()
45 }
46};
47function 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
62module.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 //160945 【Light小程序】【双进程渲染】支持小程序地址互相跳转
85 let router = routerStack[routerStack.length - 1].router;
86 var distUrl = '';
87
88 //166093【框架】优化light.navigate路由跳转时无入参的路由
89 if (path.view) {
90 //175679 【框架】统一小程序与h5/jsn的视图跳转参数形式
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 //167515 小程序olight模块支持navigate跳转区分lightweb和miniapp
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 //185505 【框架】小程序lightweb容器支持跳转gmu://协议路径
110 //196697 兼容国君项目:pageId作为第四个参数传入
111 if(path.module) {
112 //231717 支持打开gmu://web?startPage=xxx(lightweb)
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 //get 不支持 body 传参
138 let body = options.data;
139 if (!options.headers || !options.headers['Content-Type'] || options.headers['Content-Type'] === 'application/x-www-form-urlencoded') {
140 // 166775【Light小程序】【框架】请求data为空时不报错
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 //166947【Light小程序】【双进程渲染】Light.ajax()支持相对路径
157 if (!/^http/.test(settings.url)) {
158 settings.url = __base__.bundlePath + '/' + settings.url;
159 }
160 //S2101040111 【type-vue】vue小程序支持查看network面板信息
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 //向webview发送请求结果
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 //194797 小程序支持在success中获取响应头
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};