标签:格式 eject size var base head else 无数据 数据
1.配置访问服务器的地址config.js:
const config = {//192.18.1.2:8083 https://www.so.com/ api_base_url: ‘http://192.168.1.12:8083‘, // api_base_url:"https://www.baidu.com", img_base_url: ‘‘ } export { config }
2.封装http请求http.js:
import { config } from ‘./config.js‘
const tips = {
1: ‘抱歉,出现了一个错误‘,
205: ‘暂无数据!‘,
300:‘页面重定向,注意信息安全!‘,
400:‘语法格式有误!,请检查错误输入‘,
401: ‘登录失败!‘,
404:‘页面资源错误!‘,
500:‘服务器错误!‘
}
class HTTP {
request({ url, data = {}, method = ‘GET‘ }) {
return new Promise((resolve, reject) => {
this._request(url, resolve, reject, data, method)
})
}
_request(url, resolve, reject, data = {}, method = ‘GET‘) {
wx.request({
url: config.api_base_url + url,
method: method,
data: data,
header: {
// ‘content-type‘: ‘application/json‘
‘content-type‘: method == ‘GET‘ ? ‘application/json‘ : ‘application/x-www-form-urlencoded‘,
‘Accept‘: ‘application/json‘
},
success: (res) => {
// console.log(‘你好请求成功‘, res.statusCode, code.startsWith(‘2‘))
const code = res.statusCode.toString()
// console.log(‘http你好请求成功‘, res,res.statusCode, code.startsWith(‘2‘))
if (code.startsWith(‘2‘)) { // 一般情况下正确的请求结果是以2开头的(200)
// console.log("code.startsWith(‘2‘)", code)
resolve(res.data)
}
else {
reject(res)
const error_code = res.data.code
this._show_error(error_code, res.data)
}
},
fail: (err) => {
reject(err)
this._show_error(1)
}
})
}
_show_error(error_code,data) {
// console.log("tips[error_code]", error_code)
if (!error_code) {
error_code = 1
}
const tip = tips[error_code] //= data.data
wx.showToast({
title: tip ? tip : tips[1], // tips内没找到对应的错误代码时,使用1代替
icon: ‘none‘,
duration: 2000
})
}
}
export { HTTP }
3.分装每一个或者通用的请求itemapi.js:
import { HTTP } from ‘./http.js‘
class DemoModel extends HTTP {
// 1、学生端
// 验证微信号是否注册
verificationWecat(data) {
return this.request({ url: ‘/Wec/verificationWecat‘, data: data, method: ‘POST‘ })
}
// 修改用户信息
updateUserInfo(data) {
return this.request({ url: ‘/Wec/updateUser‘, data: data, method: ‘POST‘ })
}
// 添加用户
addUser(data) {
return this.request({ url: ‘/Wec/addUser‘, data: data, method: ‘POST‘ })
}
// 小程序获取学校内店铺信息
getStoreList(data) {
return this.request({url: ‘/Wec/queryDepStoreBySchId‘,data: data,method: ‘GET‘})
};
// 获取店铺下的所有产品,并按照分类进行查询
getStoreProduct(data) {
return this.request({ url: ‘/Wec/queryProductByDsId‘, data: data, method: ‘GET‘ })
};
// 根据用户id查询用户地址列表
userCheckAdress(data) {
return this.request({ url: ‘/Wec/queryUserAddressByUsrId‘, data: data, method: ‘GET‘ })
};
// 根据地址id查询用户地址
adressCheckUser(data) {
return this.request({ url: ‘/Wec/queryUserAddressById‘, data: data, method: ‘GET‘ })
};
// 修改用户地址
adressCheckUser(data) {
return this.request({ url: ‘/Wec/updateUserAddress‘, data: data, method: ‘POST‘ })
};
// 添加用户地址
addAdress(data){
return this.request({ url: ‘/Wec/addUserAddress‘, data: data, method: ‘POST‘ })
}
// 根据用户id查询用户地址列表
checkAdress(data) {
return this.request({ url: ‘/Wec/queryUserAddressByUsrId‘, data: data, method: ‘GET‘ })
}
commonfunc(url, data, method) {
return this.request({ url: url, data: data, method: method })
}
// 2、管理端
}
export {
DemoModel
}
4.使用方法:
import { DemoModel } from ‘../../../wxapi/itemapi.js‘
const apiModel = new DemoModel()
lifetimes: {
attached: function () {
var that =this
// 在组件实例进入页面节点树时执行
apiModel.getStoreList({ schId: 1,}).then(res => {
if (res.code ==‘200‘){
this.setData({ list: res.data });
}else{
wx.showToast({
title: res.data,
icon:‘none‘
})
}
}).catch(err => {
console.log(‘请求失败err‘,err)
});
},
detached: function () {
// 在组件实例被从页面节点树移除时执行
},
标签:格式 eject size var base head else 无数据 数据
原文地址:https://www.cnblogs.com/Dark-fire-liehuo/p/11982083.html