newServices.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. const requestFns = {};
  57. Object.keys(urls).forEach(function(key) {
  58. requestFns[key] = function(
  59. data = {},
  60. method = "GET",
  61. success = () => {},
  62. fail = () => {},
  63. complete = () => {}
  64. ) {
  65. console.log(`request ${urls[key]}`);
  66. let commonData = {
  67. loginSessionKey: wx.getStorageSync('token') || ""
  68. };
  69. const url = `${serverName}${urls[key]}`;
  70. return wx.request({
  71. method: method || 'GET',
  72. url,
  73. data: {
  74. ...data,
  75. ...commonData
  76. },
  77. header: {
  78. 'content-type': 'application/x-www-form-urlencoded'
  79. },
  80. success: function(res) {
  81. if (res.data.code == 0) {
  82. console.log(`request ${urls[key]}, success:`, res);
  83. success(res);
  84. } else if (res.data.code == 102) {
  85. // 未登录
  86. wx.showModal({
  87. title: '提示',
  88. content: '登录才能进行以上操作,确定登录吗?',
  89. confirmColor: '#e83828',
  90. success: function (res) {
  91. if (res.confirm) {
  92. wx.navigateTo({
  93. url: '/pages/login_page/index',
  94. success: function (res) { },
  95. fail: function (res) { },
  96. complete: function (res) { },
  97. })
  98. } else if (res.cancel) {
  99. console.log('用户点击取消')
  100. }
  101. }
  102. })
  103. fail(res);
  104. } else if (res.data.code == 11) {
  105. Toast.showToast('warn', '提交信息不完整');
  106. fail(res);
  107. } else if (res.data.code == 101) {
  108. Toast.showToast('warn', '网络超时,请重新登录');
  109. fail(res);
  110. } else if (res.data.code == -1) {
  111. Toast.showToast('warn', '请求发送失败');
  112. fail(res);
  113. } else if (res.data.code == 15001) {
  114. Toast.showToast('warn', '您已经发表过评论,不能重复评论');
  115. fail(res);
  116. } else if(res.data.code == 16001){
  117. Toast.showToast('warn', '未登录,无法操作');
  118. fail(res);
  119. } else {
  120. // Toast.showToast('warn', res.data.msg || '网络超时,请检查网络');
  121. fail(res);
  122. }
  123. },
  124. fail: function(res) {
  125. fail(res);
  126. },
  127. complete: function() {
  128. complete()
  129. }
  130. });
  131. };
  132. });
  133. // 归藏的api写在这里了
  134. const request = {
  135. getAntiqueList: function(data, method, success, fail) {
  136. wx.request({
  137. url: serverName + urls.getAntiqueList,
  138. data: data,
  139. method: method,
  140. header: {
  141. 'content-type': 'application/json'
  142. },
  143. success: function(res) {
  144. if (res.statusCode === 200) {
  145. if (res.data.code === 0) {
  146. success(res);
  147. } else if (res.data.code === 401) {
  148. // 未登录
  149. wx.showToast({
  150. title: '请先登录',
  151. icon: 'none'
  152. });
  153. fail(res);
  154. } else if (res.data.code === 400) {
  155. // 提交信息不完整
  156. wx.showToast({
  157. title: res.data.msg || '提交信息不完整',
  158. icon: 'none'
  159. });
  160. fail(res);
  161. } else {
  162. // 其他错误
  163. wx.showToast({
  164. title: res.data.msg || '请求失败',
  165. icon: 'none'
  166. });
  167. fail(res);
  168. }
  169. } else {
  170. // 网络错误
  171. wx.showToast({
  172. title: '网络错误',
  173. icon: 'none'
  174. });
  175. fail(res);
  176. }
  177. },
  178. fail: function(err) {
  179. // 请求失败
  180. wx.showToast({
  181. title: '请求失败',
  182. icon: 'none'
  183. });
  184. fail(err);
  185. }
  186. });
  187. },
  188. getAntiqueDetail: function(data, method, success, fail) {
  189. // 从页面参数中获取id
  190. const pages = getCurrentPages();
  191. const currentPage = pages[pages.length - 1];
  192. const id = currentPage.data.id;
  193. if (!id) {
  194. wx.showToast({
  195. title: '缺少文物ID',
  196. icon: 'none'
  197. });
  198. fail({ msg: '缺少文物ID' });
  199. return;
  200. }
  201. wx.request({
  202. url: serverName + urls.getAntiqueDetail + '/' + id,
  203. data: data,
  204. method: method,
  205. header: {
  206. 'content-type': 'application/json'
  207. },
  208. success: function(res) {
  209. if (res.statusCode === 200) {
  210. if (res.data.code === 0) {
  211. success(res);
  212. } else if (res.data.code === 401) {
  213. // 未登录
  214. wx.showToast({
  215. title: '请先登录',
  216. icon: 'none'
  217. });
  218. fail(res);
  219. } else if (res.data.code === 400) {
  220. // 提交信息不完整
  221. wx.showToast({
  222. title: res.data.msg || '提交信息不完整',
  223. icon: 'none'
  224. });
  225. fail(res);
  226. } else {
  227. // 其他错误
  228. wx.showToast({
  229. title: res.data.msg || '请求失败',
  230. icon: 'none'
  231. });
  232. fail(res);
  233. }
  234. } else {
  235. // 网络错误
  236. wx.showToast({
  237. title: '网络错误',
  238. icon: 'none'
  239. });
  240. fail(res);
  241. }
  242. },
  243. fail: function(err) {
  244. // 请求失败
  245. wx.showToast({
  246. title: '请求失败',
  247. icon: 'none'
  248. });
  249. fail(err);
  250. }
  251. });
  252. }
  253. };
  254. module.exports.newRequest = request;
  255. module.exports.newRequestFns = requestFns;
  256. module.exports.serverName = serverName;
  257. module.exports.newServerName = newServerName;
  258. module.exports.cosBaseUrl = cosBaseUrl;
  259. module.exports.fileBaseURL = fileBaseURL;