const { Toast } = require('./util'); // 新的API服务域名 const serverName = 'https://test.4dmuseum.cn/api'; const newServerName = 'https://test.4dmuseum.cn/api'; const fileBaseURL = 'https://test.4dmuseum.cn/swkzModel/index.html'; const cosBaseUrl = 'https://swkz-1332577016.cos.ap-guangzhou.myqcloud.com/' const urls = { //用户API getLoginSessionKey: '/wx/user/getLoginSessionKey', // checkPaySessionKey:'/wx/api/user/checkLoginSessionKey', checkLoginSessionKey: '/wx/user/getBrowsedExhibitions', logout: '/wx/user/logout', isCollect: '/wx/exhibition/like', collectExhibitions: "/wx/user/getExhibitions", getOrders: '/wx/user/getOrders', //轮播图 getBannerList: '/wx/banner/list', getBannerDetail:'/wx/banner/detail', // 首页弹窗 getBombList: '/wx/exhibition/bomb/list', //展会 getExhibitionList: "/wx/exhibition/listByType", getExhibitionListOffline: "/wx/exhibition/offline", getExhibitionDetail: "/wx/exhibition/detail", getExhibitionSearch: "/wx/exhibition/search", getKeywordCode: "/wx/dataDictionary/keywordCode", //展馆 getPavilionDetail: "/wx/pavilion/detail", getPavilionSearch: "/wx/pavilion/search", //看过 getExhibitionsByIds: '/wx/exhibition/getExhibitionsByIds', //保存浏览 saveBrowsedExhibitions: '/wx/user/saveBrowsedExhibitions', //评论 getComments: '/wx/user/getComments', getCommentslist: '/wx/comment/list', //评论 writtenComments: '/wx/comment/written', //收藏 commentLike: '/wx/comment/like', //同城 getDomesticCity: '/wx/exhibition/domesticCity', getInternationalCity: '/wx/exhibition/internationalCity', getNearByList: '/wx/exhibition/nearByList', // 目测废弃了,没看到有使用的方法 //搜索的热门推荐 recommend: '/wx/exhibition/recommend', //线下展讯热门推荐 offlineRecommend: '/wx/exhibition/offlineRecommend', // 归藏轮播图接口 getAntiqueList: '/wx/antique/page', // 归藏详情接口 getAntiqueDetail: '/wx/antique', // 新足迹接口 getTrace: '/wx/user/trace' }; const requestFns = {}; Object.keys(urls).forEach(function(key) { requestFns[key] = function( data = {}, method = "GET", success = () => {}, fail = () => {}, complete = () => {} ) { console.log(`request ${urls[key]}`); let commonData = { loginSessionKey: wx.getStorageSync('token') || "" }; const url = `${serverName}${urls[key]}`; return wx.request({ method: method || 'GET', url, data: { ...data, ...commonData }, header: { 'content-type': 'application/x-www-form-urlencoded' }, success: function(res) { if (res.data.code == 0) { console.log(`request ${urls[key]}, success:`, res); success(res); } else if (res.data.code == 102) { // 未登录 wx.showModal({ title: '提示', content: '登录才能进行以上操作,确定登录吗?', confirmColor: '#e83828', success: function (res) { if (res.confirm) { wx.navigateTo({ url: '/pages/login_page/index', success: function (res) { }, fail: function (res) { }, complete: function (res) { }, }) } else if (res.cancel) { console.log('用户点击取消') } } }) fail(res); } else if (res.data.code == 11) { Toast.showToast('warn', '提交信息不完整'); fail(res); } else if (res.data.code == 101) { Toast.showToast('warn', '网络超时,请重新登录'); fail(res); } else if (res.data.code == -1) { Toast.showToast('warn', '请求发送失败'); fail(res); } else if (res.data.code == 15001) { Toast.showToast('warn', '您已经发表过评论,不能重复评论'); fail(res); } else if(res.data.code == 17001){ Toast.showToast('warn', '未登录,无法操作'); fail(res); } else if([16001, 18001, 14001].includes(res.data.code)){ // 页面不存在 fail(res); } else { // Toast.showToast('warn', res.data.msg || '网络超时,请检查网络'); fail(res); } }, fail: function(res) { fail(res); }, complete: function() { complete() } }); }; }); // 归藏的api写在这里了 const request = { getAntiqueList: function(data, method, success, fail) { wx.request({ url: serverName + urls.getAntiqueList, data: data, method: method, header: { 'content-type': 'application/json' }, success: function(res) { if (res.statusCode === 200) { if (res.data.code === 0) { success(res); } else if (res.data.code === 401) { // 未登录 wx.showToast({ title: '请先登录', icon: 'none' }); fail(res); } else if (res.data.code === 400) { // 提交信息不完整 wx.showToast({ title: res.data.msg || '提交信息不完整', icon: 'none' }); fail(res); } else { // 其他错误 wx.showToast({ title: res.data.msg || '请求失败', icon: 'none' }); fail(res); } } else { // 网络错误 wx.showToast({ title: '网络错误', icon: 'none' }); fail(res); } }, fail: function(err) { // 请求失败 wx.showToast({ title: '请求失败', icon: 'none' }); fail(err); } }); }, getAntiqueDetail: function(data, method, success, fail) { // 从页面参数中获取id const pages = getCurrentPages(); const currentPage = pages[pages.length - 1]; const id = currentPage.data.id; if (!id) { wx.showToast({ title: '缺少文物ID', icon: 'none' }); fail({ msg: '缺少文物ID' }); return; } wx.request({ url: serverName + urls.getAntiqueDetail + '/' + id, data: data, method: method, header: { 'content-type': 'application/json' }, success: function(res) { if (res.statusCode === 200) { if (res.data.code === 0) { success(res); } else if (res.data.code === 401) { // 未登录 wx.showToast({ title: '请先登录', icon: 'none' }); fail(res); } else if (res.data.code === 400) { // 提交信息不完整 wx.showToast({ title: res.data.msg || '提交信息不完整', icon: 'none' }); fail(res); } else { // 其他错误 wx.showToast({ title: res.data.msg || '请求失败', icon: 'none' }); fail(res); } } else { // 网络错误 wx.showToast({ title: '网络错误', icon: 'none' }); fail(res); } }, fail: function(err) { // 请求失败 wx.showToast({ title: '请求失败', icon: 'none' }); fail(err); } }); } }; module.exports.newRequest = request; module.exports.newRequestFns = requestFns; module.exports.serverName = serverName; module.exports.newServerName = newServerName; module.exports.cosBaseUrl = cosBaseUrl; module.exports.fileBaseURL = fileBaseURL;