rpc.js
7.87 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/**
* Created by tdzl2003 on 6/18/16.
*/
import {AsyncStorage, NativeModules} from 'react-native';
import {EventEmitter} from 'fbemitter';
import URI from 'urijs';
import {observable} from 'mobx';
const MD5 = require("crypto-js/md5");
import {apiUrl,tenantId,uploadUrl,uploadUrl2} from '../config';
import { tenant } from '../../../index';
class ResponseError extends Error {
constructor(message, code, origin) {
super(message);
this.code = code;
this.origin = origin;
}
}
const RPC = new EventEmitter();
const emit = RPC.emit.bind(RPC);
export default RPC;
const tenantid = 300029
const KEY_TOKEN = 'accessToken';
const ROOT_URL = '';
const UPLOAD_TOKEN_URL = 'upload';
const token = observable.box(null);
export function getToken() {
return token.get();
}
export function saveToken(_token) {
token.set(_token);
return AsyncStorage.setItem(KEY_TOKEN, token);
}
export function mobShare(p1, p2, p3, p4) {
p3 = 'http://192.168.1.30:9010/app.html';
if (__ANDROID__) {
const {MobLogin} = NativeModules;
MobLogin.showShare(p1, p2, p3, p4);
}
else {
var customShare = NativeModules.CustomShare;
customShare.showShare(p1, p2, p3, p4);
}
}
export async function loadToken() {
token.set(await AsyncStorage.getItem(KEY_TOKEN));
return token.get();
}
export async function clearToken() {
await AsyncStorage.removeItem(KEY_TOKEN);
token.set(null);
}
export const isArray = (o) => {
return Object.prototype.toString.call(o) === '[object Array]';
}
const getPassportId = () => {
if (global.cache.passport) {
return global.cache.passport.id;
}
return 0;
}
const getIdentity = () => {
return global.cache.identityId;
}
// appKey需要提供给商城使用,保持export
export const app_key = '0617CA8376F9901F28FF46B69BF9CF44'; //dev
export const secret = '28570C9D069ED51226DD9F028BD5E6DD'; //dev
// if(require('electron').remote.getGlobal('isDev') != true)
// {//生产
// app_key = '0617CA8376F9901F28FF46B69BF9CF47';//
// secret = '28570C9D069ED51226DD9F028BD5E6DC';
// }
// else
// {
// app_key = '0617CA8376F9901F28FF46B69BF9CF44'; //dev
// secret = '28570C9D069ED51226DD9F028BD5E6DD'; //dev
// }
const getPostParameter = async (request) => {
var post_data = '';
var param_array = new Object();
// this.passportId = getPassportId();
// this.identityId = getIdentity();
// if (this.passportId != undefined && this.passportId != null && this.passportId > 0) {
// param_array.passportId = this.passportId;
// }
// if (this.identityId != undefined && this.identityId != null && this.identityId > 0) {
// param_array.identityId = this.identityId;
// }
param_array.format = 'json';
param_array.sign_method = 'md5';
param_array.timestamp = Date.parse(new Date());
param_array.app_key = app_key;
param_array.v = '1.0';
const session=await AsyncStorage.getItem("session")
if(session){
param_array.session=session
}
for (var p in request) { // 方法
if (request[p] != null && request[p] != undefined && typeof (request[p]) != "function") {
param_array[p] = request[p];
}
}
{
var param_sign;
var arrayKey = [];
var strTemp;
var arrayKeyTemp = [];
for (var p in param_array) { // 方法
if (typeof (param_array[p]) != "function") {
arrayKeyTemp.push(p);
}
}
// 最后显示所有的属性
arrayKeyTemp.sort();
var strTemp = secret;
for (var i = 0; i < arrayKeyTemp.length; i++) {
if (isArray(param_array[arrayKeyTemp[i]])) {
strTemp = strTemp + arrayKeyTemp[i] + JSON.stringify(param_array[arrayKeyTemp[i]]);
} else {
strTemp = strTemp + arrayKeyTemp[i] + param_array[arrayKeyTemp[i]];
}
}
strTemp = strTemp + secret;
param_sign = MD5(strTemp).toString().toUpperCase();
post_data = 'sign=' + param_sign.toUpperCase();
for (var i = 0; i < arrayKeyTemp.length; i++) {
if (isArray(param_array[arrayKeyTemp[i]])) {
post_data = post_data + '&' + arrayKeyTemp[i] + '=' + encodeURI(JSON.stringify(param_array[arrayKeyTemp[i]]));
} else {
post_data = post_data + '&' + arrayKeyTemp[i] + '=' + encodeURI(param_array[arrayKeyTemp[i]]);
}
}
}
// // 本地环境
// const LOCAL_URL = "http://192.168.80.8:9010/router?";
// // 开发环境
// const DEV_URL = "https://api-daily.xiniunet.com/router?";
// // 测试环境
// const TEST_URL = "http://www-test.herentech.com:9010/router?";
// // 生产环境
// const ONLINE_URL = "http://www.herentech.com:9010/router?";
return {
url: apiUrl, // 切换环境
data: post_data
};
}
// const uploadUrl = 'https://my-daily.xiniunet.com/api/fileUpload.do';
// const uploadUrl2="https://my-daily.xiniunet.com/api/attachmentUpload.do"
async function request(url, _options) {
const uri = new URI(ROOT_URL + url);
const options = _options || {};
options.method = options.method || 'GET';
options.headers = options.headers || {};
if (token) {
options.headers['x-accesstoken'] = token.get();
}
// console.log(__DEV__,'>>>>>')
if (__DEV__) {
console.log(`${options.method} ${uri}`);
if (options.body) {
console.log(options.body);
}
}
const resp = await fetch(uri.toString(), options);
const text = await resp.text();
console.log('RESP:', text);
const json = JSON.parse(text);
// 如果请求失败
if (resp.status !== 200) {
if (resp.status === 401) {
// HTTP 401 表示授权验证失败(通常是token已过期)
emit('invalidToken');
token.set(null);
}
throw new ResponseError(json.message, resp.status, json);
}
console.log(json)
return json;
}
// file: {uri}
//上传
export async function upload(type,uri) {
const body = new FormData();
body.append('File', {
uri,
type: 'image/jpeg',
name: new URI(uri).filename()});
body.append("Type", type);
// body.append("Ext", "png");
// body.append("FileName", "1111");
const options = {
method: 'POST',
headers: {
},
body,
};
const passportId=await AsyncStorage.getItem("session")
const url =`${uploadUrl}?passportId=${passportId}`
console.log(url)
const resp = await fetch(url, options);
const text = await resp.text();
console.log('RESP:', text);
const json = JSON.parse(text);
// 如果请求失败
if (resp.status !== 200) {
throw new ResponseError(json.message, resp.status, json);
}
console.log(json)
return json;
}
export async function upload2(uri,id,type,category) {
const body = new FormData();
body.append('file', {
uri,
type: 'image/jpeg',
name: new URI(uri).filename()});
body.append("method", "api.foundation.attachment.upload");
body.append("businessId",id);
body.append("businessType",type);
body.append("Ext","jpg")
if(category){
body.append("businessCategory",category);
}
console.log(body)
const options = {
method: 'POST',
headers: {
},
body,
};
const passportId=await AsyncStorage.getItem("session")
const url =`${uploadUrl2}?passportId=${passportId}`
console.log(url)
const resp = await fetch(url, options);
const text = await resp.text();
console.log('RESP:', text);
const json = JSON.parse(text);
// 如果请求失败
if (resp.status !== 200) {
throw new ResponseError(json.message, resp.status, json);
}
console.log(json)
return json;
}
export async function post(data, options) {
// if(data.loginPassword){
// data.loginPassword=MD5(data.loginPassword).toString()
// }
if(data.password){
data.password=MD5(data.password).toString()
}
if(data.transactionPassword){
data.transactionPassword=MD5(data.transactionPassword).toString()
}
// data.tenantNumber="300044";
data.tenantid=tenantId;
data.tenantId=tenantId;
data.mobileType="MOBILE"
// if(!data.tenantid||!!data.tenantid){
// data.tenantId="658896334535139328"
// }
if(!data.systemType){
data.systemType="B2C"
}
if(!data.deviceType){
data.deviceType="MOBILE"
}else if(data.deviceType==="null"){
data.deviceType=null
}
var postParameter = await getPostParameter(data);
return request(postParameter.url, {
method: 'POST',
mode: "cors",
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: postParameter.data,
...options,
});
}