Authored by QC-L

更新网络请求封装写法 review by 黄敬囿

... ... @@ -95,30 +95,18 @@ const _publicParams = () => {
return publicParams;
}
function _signParam(params, encode = false) {
let allParams = trimObject(params); // 去除首尾空格
let paramsPair = queryString.stringify(allParams, { encode: false });
let client_secret = MD5(paramsPair);
delete allParams.private_key;
const resultParams = objectAssign(allParams, { client_secret });
const _signParam = (params, encode = false) => {
const resultParams = _signResultParams(params, encode);
const resultString = queryString.stringify(resultParams, { encode });
return resultString;
}
function _signResultParams(params, encode = false) {
const _signResultParams = (params, encode = false) => {
let allParams = trimObject(params); // 去除首尾空格
let paramsPair = queryString.stringify(allParams, { encode: false });
let client_secret = MD5(paramsPair);
delete allParams.private_key;
const resultParams = objectAssign(allParams, { client_secret });
return resultParams;
}
... ... @@ -148,9 +136,14 @@ const sendRequest = (resolve, reject, options) => {
const resultParams = _signResultParams(newParams, options.encode);
// 处理请求头
const header = handleHeader(options, newParams);
// path 路径
let url = options.url;
if (options.path) {
url = url + options.path;
}
// 请求
wx.request({
url: options.url,
url: url,
data: resultParams,
header: header,
method: options.methods,
... ... @@ -196,4 +189,4 @@ export const POST = request('POST');
export const PUT = request('PUT');
export const DELETE = request('DELETE');
// export const UPLOAD_LOG = uploadLogData('POST')
// export const APP_REPORT = appReport('POST')
\ No newline at end of file
// export const APP_REPORT = appReport('POST')
... ...
const { GET } = require('../request/request');
import config from '../../config.js'
export default class BaseService {
constructor(url) {
this.url = config.domains.api;
if (url) {
this.url = url;
}
}
async GET(params, path) {
return await GET({
url: this.url,
path: path,
params: params
}).then(data => {
return data;
}).catch(error => {
return error;
})
}
}
\ No newline at end of file
... ...
import { GET } from '../../libs/request/request.js';
import config from '../../config.js'
import NativeTestService from './nativeTestService.js'
Page({
... ... @@ -7,26 +6,34 @@ Page({
* 页面的初始数据
*/
data: {
text: '哈哈哈哈'
text: '哈哈哈哈',
api: new NativeTestService()
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
console.log(config.api);
GET({
url: config.domains.api + '/resources',
params: {
method: 'ufo.product.search.list',
page: 1,
limit: 20
}
}).then(data => {
let api = new NativeTestService();
this.setData({
api:api
})
this.data.api.getHomePageList({page: 1, limit: 20}).then(data => {
console.log(data);
}).catch(error => {
console.error(error);
})
// 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);
// })
},
/**
... ...
import BaseService from '../../libs/services/baseService.js';
const UFO_SEARCH_LIST = 'ufo.product.search.list';
export default class NativeTestService extends BaseService {
async getHomePageList(params) {
return await this.GET(
{
...params,
method: UFO_SEARCH_LIST
},
'/resources'
).then((data) => {
return data;
}).catch((error) => {
return error;
})
}
}
\ No newline at end of file
... ...