rpc.js
5.51 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
/**
* Created by tdzl2003 on 6/18/16.
*/
import {AsyncStorage, NativeModules} from 'react-native';
import config from '../../src/config';
import MD5 from "md5"
export let forumConfig={
apiUrl:null,
app_key:null,
secret:null,
uploadUrl:null,
}
class ResponseError extends Error {
constructor(message, code, origin) {
super(message);
this.code = code;
this.origin = origin;
}
}
export const isArray=(o)=> {
return Object.prototype.toString.call(o) === '[object Array]';
}
export function getPassportId(){
return new Promise(function (resolve, reject) {
// todo
// resolve("1215210952501366784");
resolve("1229253275946192896");
// NativeModules.security.getPassportId().then((result) => {
// resolve(result);
// }
// ).catch((error) => {
// // console.log(error)
// });
})
}
function identityId(){
return new Promise(function (resolve, reject) {
// NativeModules.security.getIdentityId().then((result) => {
// resolve(result);
// }
// ).catch((error) => {
// // console.log(error)
// });
resolve("");
})
}
async function getPostParameter (request){
var post_data = '';
var param_array = new Object();
const session = await AsyncStorage.getItem("session")
if(global.passportId != null ){
this.passportId = global.passportId;
}else {
this.passportId = session;
}
this.identityId = await identityId();
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 =forumConfig.app_key;
param_array.v = '1.0';
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 = forumConfig.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 + forumConfig.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] + '=' + encodeURIComponent(JSON.stringify(param_array[arrayKeyTemp[i]]));
} else {
post_data = post_data + '&' + arrayKeyTemp[i] + '=' + encodeURIComponent(param_array[arrayKeyTemp[i]]);
}
}
}
return post_data;
}
function escape1(text){
return text;
}
async function request(data, _options) {
const options = _options || {};
//config.apiUrl
options.method = options.method || 'GET';
options.headers = options.headers || {};
options.body=await getPostParameter(data)
// console.warn(JSON.stringify(options.body));
const resp = await fetch(forumConfig.apiUrl.toString(), options);
const text = await resp.text();
// console.log('RESP:', text);
const json = JSON.parse(text);
// 如果请求失败
if (resp.status !== 200) {
if (resp.status === 401) {
// console.log(resp.status);
}
console.log('请求失败:'+text);
throw new ResponseError(json.message, resp.status, json);
}
// 服务不可用
if (json.message) {
console.log('请求出错:'+text);
throw new ResponseError(json.message, resp.status, json);
}
return json;
}
//post
export function post(data, options) {
return request(data, {
method: 'POST',
mode: "cors",
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
...options,
});
}
//上传
export async function upload(file) {
//config.apiUrl
const body = new FormData();
body.append('File', file);
body.append("Type", "PHOTO");
const options = {
method: 'POST',
headers: {
'Content-Type': 'multipart/form-data',
},
body,
};
let passportId=await getPassportId();
const resp = await fetch(forumConfig.uploadUrl+"?passportId="+passportId, 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);
}
return json;
}
export const init=function (data) {
forumConfig=data
};
const forumRpc={
init:init,
upload:upload,
post:post
}
export default forumRpc;