新建http文件夹=》新建api.js
const { request } = require('./request.js')
//项目中用到的各种业务接口的封装
module.exports={
//轮播图
getBanner: () =>
request('banner/list','GET',{},true)
,
//九宫格接口
getGoodsCate: () => request('shop/goods/category/all','GET','',true),
//搜索接口
goodsSearch:(k)=>request('shop/goods/list','POST',{k},true),
//商品列表接口
getList:()=>request('/shop/goods/list','GET','',true),
//商品详情接口
getDetail:(id)=>{
return request('shop/goods/detail','GET',{id:id},true)
},
// 检测登录 token 是否有效
checkToken: (token) => {
return request('user/check-token', 'GET', {
token
}, true)
},
//详细注册
getwxre: (data) => {
return request('user/wxapp/register/complex', 'POST', data, true)
},
// 登录
login_wx: (code) => {
return request('user/wxapp/login', 'POST', {
code,
type: 2
}, true)
},
// 获取用户信息
userDetail: (token) => {
return request('user/detail', 'GET', {
token
}, true)
},
}
request.js
const { baseUrl } = require('./env.js').prod
const Url = 'cy258'
module.exports ={
request:function(url,method="get",data={},isSubDomain = true){
let fullUrl = `${baseUrl}/${isSubDomain ? Url : ''}/${url}`;
wx.showLoading({
title:"加载接口"
})
return new Promise((resolve,reject) =>{
wx.request({
url: fullUrl,
method,
data,
header:{
'Content-type':'application/x-www-form-urlencoded'
},
success(res){
//下面判断有时有这个&& res.data.code === 0
if(res.statusCode === 200 ){
resolve(res.data)
wx.hideLoading()
}
else{
reject('接口有问题')
}
},
fail(error){
reject('数据接口有问题')
}
})
})
}
}