newServices.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. const {
  2. Toast
  3. } = require('./util');
  4. // 新的API服务域名
  5. const serverName = 'https://test.4dmuseum.cn/api';
  6. const newServerName = 'https://test.4dmuseum.cn/api';
  7. const fileBaseURL = 'https://test.4dmuseum.cn/swkzModel/index.html';
  8. const cosBaseUrl = 'https://swkz-1332577016.cos.ap-guangzhou.myqcloud.com/'
  9. const urls = {
  10. //用户API
  11. getLoginSessionKey: '/wx/user/getLoginSessionKey',
  12. // checkPaySessionKey:'/wx/api/user/checkLoginSessionKey',
  13. checkLoginSessionKey: '/wx/user/getBrowsedExhibitions',
  14. logout: '/wx/user/logout',
  15. isCollect: '/wx/exhibition/like',
  16. collectExhibitions: "/wx/user/getExhibitions",
  17. getOrders: '/wx/user/getOrders',
  18. //轮播图
  19. getBannerList: '/wx/banner/list',
  20. getBannerDetail:'/wx/banner/detail',
  21. // 首页弹窗
  22. getBombList: '/wx/exhibition/bomb/list',
  23. //展会
  24. getExhibitionList: "/wx/exhibition/listByType",
  25. getExhibitionListOffline: "/wx/exhibition/offline",
  26. getExhibitionDetail: "/wx/exhibition/detail",
  27. getExhibitionSearch: "/wx/exhibition/search",
  28. getKeywordCode: "/wx/dataDictionary/keywordCode",
  29. //展馆
  30. getPavilionDetail: "/wx/pavilion/detail",
  31. getPavilionSearch: "/wx/pavilion/search",
  32. //看过
  33. getExhibitionsByIds: '/wx/exhibition/getExhibitionsByIds',
  34. //保存浏览
  35. saveBrowsedExhibitions: '/wx/user/saveBrowsedExhibitions',
  36. //评论
  37. getComments: '/wx/user/getComments',
  38. getCommentslist: '/wx/comment/list',
  39. //评论
  40. writtenComments: '/wx/comment/written',
  41. //收藏
  42. commentLike: '/wx/comment/like',
  43. //同城
  44. getDomesticCity: '/wx/exhibition/domesticCity',
  45. getInternationalCity: '/wx/exhibition/internationalCity',
  46. getNearByList: '/wx/exhibition/nearByList', // 目测废弃了,没看到有使用的方法
  47. //搜索的热门推荐
  48. recommend: '/wx/exhibition/recommend',
  49. //线下展讯热门推荐
  50. offlineRecommend: '/wx/exhibition/offlineRecommend',
  51. // 归藏轮播图接口
  52. getAntiqueList: '/wx/antique/page',
  53. // 归藏详情接口
  54. getAntiqueDetail: '/wx/antique',
  55. // 新足迹接口
  56. getTrace: '/wx/user/trace'
  57. };
  58. const requestFns = {};
  59. Object.keys(urls).forEach(function(key) {
  60. requestFns[key] = function(
  61. data = {},
  62. method = "GET",
  63. success = () => {},
  64. fail = () => {},
  65. complete = () => {}
  66. ) {
  67. console.log(`request ${urls[key]}`);
  68. let commonData = {
  69. loginSessionKey: wx.getStorageSync('token') || ""
  70. };
  71. const url = `${serverName}${urls[key]}`;
  72. return wx.request({
  73. method: method || 'GET',
  74. url,
  75. data: {
  76. ...data,
  77. ...commonData
  78. },
  79. header: {
  80. 'content-type': 'application/x-www-form-urlencoded'
  81. },
  82. success: function(res) {
  83. if (res.data.code == 0) {
  84. console.log(`request ${urls[key]}, success:`, res);
  85. success(res);
  86. } else if (res.data.code == 102) {
  87. // 未登录
  88. wx.showModal({
  89. title: '提示',
  90. content: '登录才能进行以上操作,确定登录吗?',
  91. confirmColor: '#e83828',
  92. success: function (res) {
  93. if (res.confirm) {
  94. wx.navigateTo({
  95. url: '/pages/login_page/index',
  96. success: function (res) { },
  97. fail: function (res) { },
  98. complete: function (res) { },
  99. })
  100. } else if (res.cancel) {
  101. console.log('用户点击取消')
  102. }
  103. }
  104. })
  105. fail(res);
  106. } else if (res.data.code == 11) {
  107. Toast.showToast('warn', '提交信息不完整');
  108. fail(res);
  109. } else if (res.data.code == 101) {
  110. Toast.showToast('warn', '网络超时,请重新登录');
  111. fail(res);
  112. } else if (res.data.code == -1) {
  113. Toast.showToast('warn', '请求发送失败');
  114. fail(res);
  115. } else if (res.data.code == 15001) {
  116. Toast.showToast('warn', '您已经发表过评论,不能重复评论');
  117. fail(res);
  118. } else if(res.data.code == 17001){
  119. Toast.showToast('warn', '未登录,无法操作');
  120. fail(res);
  121. } else if([16001, 18001, 14001].includes(res.data.code)){
  122. // 页面不存在
  123. fail(res);
  124. } else {
  125. // Toast.showToast('warn', res.data.msg || '网络超时,请检查网络');
  126. fail(res);
  127. }
  128. },
  129. fail: function(res) {
  130. fail(res);
  131. },
  132. complete: function() {
  133. complete()
  134. }
  135. });
  136. };
  137. });
  138. // 归藏的api写在这里了
  139. const request = {
  140. getAntiqueList: function(data, method, success, fail) {
  141. wx.request({
  142. url: serverName + urls.getAntiqueList,
  143. data: data,
  144. method: method,
  145. header: {
  146. 'content-type': 'application/json'
  147. },
  148. success: function(res) {
  149. if (res.statusCode === 200) {
  150. if (res.data.code === 0) {
  151. success(res);
  152. } else if (res.data.code === 401) {
  153. // 未登录
  154. wx.showToast({
  155. title: '请先登录',
  156. icon: 'none'
  157. });
  158. fail(res);
  159. } else if (res.data.code === 400) {
  160. // 提交信息不完整
  161. wx.showToast({
  162. title: res.data.msg || '提交信息不完整',
  163. icon: 'none'
  164. });
  165. fail(res);
  166. } else {
  167. // 其他错误
  168. wx.showToast({
  169. title: res.data.msg || '请求失败',
  170. icon: 'none'
  171. });
  172. fail(res);
  173. }
  174. } else {
  175. // 网络错误
  176. wx.showToast({
  177. title: '网络错误',
  178. icon: 'none'
  179. });
  180. fail(res);
  181. }
  182. },
  183. fail: function(err) {
  184. // 请求失败
  185. wx.showToast({
  186. title: '请求失败',
  187. icon: 'none'
  188. });
  189. fail(err);
  190. }
  191. });
  192. },
  193. getAntiqueDetail: function(data, method, success, fail) {
  194. // 从页面参数中获取id
  195. const pages = getCurrentPages();
  196. const currentPage = pages[pages.length - 1];
  197. const id = currentPage.data.id;
  198. if (!id) {
  199. wx.showToast({
  200. title: '缺少文物ID',
  201. icon: 'none'
  202. });
  203. fail({ msg: '缺少文物ID' });
  204. return;
  205. }
  206. wx.request({
  207. url: serverName + urls.getAntiqueDetail + '/' + id,
  208. data: data,
  209. method: method,
  210. header: {
  211. 'content-type': 'application/json'
  212. },
  213. success: function(res) {
  214. if (res.statusCode === 200) {
  215. if (res.data.code === 0) {
  216. success(res);
  217. } else if (res.data.code === 401) {
  218. // 未登录
  219. wx.showToast({
  220. title: '请先登录',
  221. icon: 'none'
  222. });
  223. fail(res);
  224. } else if (res.data.code === 400) {
  225. // 提交信息不完整
  226. wx.showToast({
  227. title: res.data.msg || '提交信息不完整',
  228. icon: 'none'
  229. });
  230. fail(res);
  231. } else {
  232. // 其他错误
  233. wx.showToast({
  234. title: res.data.msg || '请求失败',
  235. icon: 'none'
  236. });
  237. fail(res);
  238. }
  239. } else {
  240. // 网络错误
  241. wx.showToast({
  242. title: '网络错误',
  243. icon: 'none'
  244. });
  245. fail(res);
  246. }
  247. },
  248. fail: function(err) {
  249. // 请求失败
  250. wx.showToast({
  251. title: '请求失败',
  252. icon: 'none'
  253. });
  254. fail(err);
  255. }
  256. });
  257. }
  258. };
  259. module.exports.newRequest = request;
  260. module.exports.newRequestFns = requestFns;
  261. module.exports.serverName = serverName;
  262. module.exports.newServerName = newServerName;
  263. module.exports.cosBaseUrl = cosBaseUrl;
  264. module.exports.fileBaseURL = fileBaseURL;