webview.js
3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import {
parse,
stringify
} from '../../vendors/query-stringify';
import config from '../../common/config';
import udid from '../../common/udid';
import event from '../../common/event';
import {
verify
} from '../../common/api';
import {
tapGoLogin
} from '../../common/login';
import {
getQRCodeSource
} from '../../common/miniQRCodeRoute';
Page({
/**
* 页面的初始数据
*/
data: {
title: '',
url: '',
shareUrl: ''
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function(options) {
console.log(options);
options.url = decodeURIComponent(options.url || '');
if (options && options.scene && options.scene.length > 0) {
let code = options.scene;
getQRCodeSource(code).then(json => {
console.log('json:', json);
if (json) {
this.generateUrl(json);
}
}).catch(error => {});
} else {
if (options.url) {
let uid = this.getUid();
const search = options.url.split('?')[1] || '';
const qs = parse(search);
if (Number(qs.needLogin) === 1 && uid === 0) {
event.on('user-login-success', () => {
this.generateUrl(options);
});
tapGoLogin();
} else {
this.generateUrl(options);
}
}
}
},
/**
* 获取 UID
*/
getUid: function() {
const app = getApp() || {};
return app.globalData &&
app.globalData.userInfo &&
app.globalData.userInfo.uid ?
app.globalData.userInfo.uid : 0;
},
/**
* 取得URL中的参数
*/
getQueryObj(link) {
let loc = decodeURIComponent(link);
let variables = '';
let variableArr = [];
let finalArr = [];
if (loc.indexOf('?') > 0) {
variables = loc.split('?')[1];
}
if (variables.length > 0) {
variableArr = variables.split('#')[0].split('&');
}
for (let i = 0; i < variableArr.length; i++) {
let obj = {};
obj.name = variableArr[i].split('=')[0];
obj.value = variableArr[i].split('=')[1];
finalArr.push(obj);
}
let query_obj = {};
for (let i = 0; i < finalArr.length; i++) {
query_obj[finalArr[i].name] = finalArr[i].value;
}
return query_obj;
},
/**
* 生成链接
*/
generateUrl: function(options) {
console.log(options);
let isSign = true;
if (options.url) {
let queryObj = this.getQueryObj(options.url);
console.log('query=', queryObj);
if (queryObj.title) {
this.setData({
title: queryObj.title
});
}
if (queryObj.shareURL) {
this.setData({
shareUrl: queryObj.shareURL
});
}
if (queryObj.noSign) {
isSign = false;
}
}
console.log('option.url=', decodeURIComponent(options.url));
const app = getApp() || {};
let param = {
app_version: config.apiParams.app_version,
session_key: app.globalData && app.globalData.sessionKey || '',
client_type: config.apiParams.client_type,
udid: udid.get()
};
let uid = this.getUid();
if (uid) {
param.uid = uid;
}
let url = options.url;
let params = verify.computeSecret(param);
const [uri, search] = url.split('?');
const qs = parse(search);
url = uri + '?' + stringify(Object.assign({}, params, qs));
console.log('webview-url:', url);
this.setData({
url: url
});
},
bindGetMsg: function(e) {
if (e.detail && e.detail.data && e.detail.data[0] && e.detail.data[0].title) {
this.setData({
title: e.detail.data[0].title,
});
}
},
});