import http from './http.js' const config = { // 示例 // wxLogin : {url : '/api/wxLogin', method : 'POST', // auth : false, showLoading : true, loadingTitle : '加载中...', // limit : 1000 // }, getConfig: { url: '/api/getConfig', method: 'GET' }, // banner列表 bannerList: { url: '/banner/list', method: 'GET', showLoading : true, }, // 公告列表 noticeList: { url: '/notice/list', method: 'GET', showLoading : true, }, // 公告详情 noticeOne: { url: '/notice/one', method: 'GET', showLoading : true, }, // 商品列表 goodsPage: { url: '/goods/page', method: 'GET', showLoading : true, }, // 商品详情 goodsOne: { url: '/goods/one', method: 'GET', showLoading : true, }, //分类列表 categoryList: { url: '/category/list', method: 'GET', showLoading : true, }, //新增地址 addressAdd: { url: '/address/add', method: 'POST', limit: 500, // auth : true, showLoading : true, }, //修改默认地址 addressDefault: { url: '/address/default', method: 'POST', // auth : true, showLoading : true, }, //删除地址 addressDelete: { url: '/address/delete', method: 'POST', // auth : true, showLoading : true, }, //修改地址 addressEdit: { url: '/address/edit', method: 'POST', limit: 500, // auth : true, showLoading : true, }, //分页查询地址 addressPage: { url: '/address/page', method: 'GET', // auth : true, showLoading : true, }, //分页查询订单 orderPage: { url: '/order/page', method: 'GET', // auth : true, showLoading : true, }, //查询订单详情 orderOne: { url: '/order/one', method: 'GET', // auth : true, showLoading : true, }, // 微信登录接口 wxLogin: { url: '/login/login', method: 'POST', limit : 500, showLoading : true, }, // 修改个人信息接口 updateInfo: { url: '/info/updateInfo', method: 'POST', auth: true, limit : 500, showLoading : true, }, } export function api(key, data, callback, loadingTitle) { let req = config[key] if (!req) { console.error('无效key' + key); return } if (typeof callback == 'string') { loadingTitle = callback } if (typeof data == 'function') { callback = data data = {} } // 接口限流 if (req.limit) { let storageKey = 'limit:' + req.url let storage = uni.getStorageSync(storageKey) if (storage && new Date().getTime() - parseInt(storage) < req.limit) { return } uni.setStorageSync(storageKey, new Date().getTime()) } //必须登录 if (req.auth) { if (!uni.getStorageSync('token')) { uni.navigateTo({ url: '/pages/login/mobile' }) console.error('需要登录') return } } http.http(req.url, data, callback, req.method, loadingTitle || req.showLoading, loadingTitle || req.loadingTitle) } export default api