import http from './http.js' import utils from '../utils/utils.js' const config = { // 示例 // wxLogin : {url : '/api/wxLogin', method : 'POST', // auth : false, showLoading : true, loadingTitle : '加载中...', // limit : 1000 // }, getConfig : {url : '/applet_index/commonConfig', method : 'GET', limit : 500}, // 微信登录接口 wxLogin: { url: '/login_common/appletLogin', method: 'GET', limit : 500, showLoading : true, }, // 获取个人信息接口 getInfo: { url: '/info_common/getInfo', method: 'GET', limit : 500, showLoading : true, }, // 修改个人信息接口 updateInfo: { url: '/info_common/updateInfo', method: 'POST', auth: true, limit : 500, showLoading : true, }, //首页-出现问题列表接口 problemList: { url: '/applet_index/problemList', method: 'GET', }, //首页-文章列表接口 articleList: { url: '/applet_index/articleList', method: 'GET', }, //首页-轮播图接口 bannerList: { url: '/applet_index/bannerList', method: 'GET', }, //提交问答记录 submitLog: { url: '/applet_post/submitLog', method: 'POST', limit : 1000, showLoading : true, auth: true, }, //提交问答记录 submit: { url: '/applet_post/submit', method: 'POST', limit : 1000, showLoading : true, auth: true, }, //查询我的问答记录 queryMyLog: { url: '/applet_post/queryMyLog', method: 'GET', auth: true, 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')) { utils.toLogin() console.error('需要登录') return } } http.http(req.url, data, callback, req.method, loadingTitle || req.showLoading, loadingTitle || req.loadingTitle) } export default api