Authored by QC-L

1. 修正 request 在无网络情况下的问题 2. 修正 request 返回的 data 内容,添加 data.code 相关的判断 3. 异常处理相关 review by 黄敬囿

... ... @@ -2,7 +2,6 @@ import getPrivateKey from './getPrivateKey'
import config from '../../config.js'
import trimObject from './utils/trimObject.js'
import queryString from './utils/query-string/query-string.js'
import objectAssign from './utils/query-string/object-assign.js'
const crypto = require('./utils/cryptojs/cryptojs.js');
import MD5 from './utils/md5/md5.js'
... ... @@ -109,10 +108,9 @@ const _signParam = (params, encode = false) => {
const _signResultParams = (params, encode = false) => {
let allParams = trimObject(params); // 去除首尾空格
let paramsPair = queryString.stringify(allParams, { encode: false });
console.log(paramsPair);
let client_secret = MD5(paramsPair);
delete allParams.private_key;
const resultParams = objectAssign(allParams, { client_secret });
const resultParams = Object.assign(allParams, { client_secret });
return resultParams;
}
... ... @@ -125,7 +123,10 @@ const handleHeader = (options, newParams) => {
p2SecretKey = getYHStorageSync("p2SecretKey", 'request')
}
let param = _signParam(newParams, false);
let resultString = crypto.HMAC(crypto.SHA256, param, p2SecretKey, "")
let resultString = ''
if (p2SecretKey) {
resultString = crypto.HMAC(crypto.SHA256, param, p2SecretKey, "")
}
const session_key = newParams.session_key ? newParams.session_key : '';
let header = {
'x-yoho-verify': resultString,
... ... @@ -158,11 +159,10 @@ const sendRequest = (resolve, reject, options) => {
const statusCode = res.statusCode;
const errMsg = res.errMsg;
const data = res.data;
if (statusCode == 200) {
resolve(data);
if (data && data.code == 200 && data.data) {
resolve(data.data);
} else {
const code = statusCode;
const code = res.statusCode;
const message = res.errMsg ? res.errMsg : '';
reject({ code, message });
}
... ...
... ... @@ -28,7 +28,7 @@ export default class BaseService {
}).then(data => {
return data;
}).catch(error => {
return error;
throw error;
})
}
}
\ No newline at end of file
... ...
... ... @@ -25,21 +25,12 @@ Page({
this.data.api.getHomePageList({page: 1, limit: 20}, () => {
wx.hideLoading();
}).then(data => {
// 此处 data 为 response 中的 data.data
console.log(data);
})
// GET({
// url: config.domains.api,
// path: '/resources',
// params: {
// method: 'ufo.product.search.list',
// page: 1,
// limit: 20
// }
// }).then(data => {
// console.log(data);
// }).catch(error => {
// console.error(error);
// })
}).catch(error => {
// 此处会抛出响应码非 200 的情况
console.log(error);
});
},
/**
... ...
... ... @@ -16,7 +16,8 @@ export default class NativeTestService extends BaseService {
).then((data) => {
return data;
}).catch((error) => {
return error;
// 这里请抛出异常,不要return
throw error;
})
}
}
\ No newline at end of file
... ...