newServices.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. getAntiqueList: '/wx/antique/page',
  20. // 归藏详情接口
  21. getAntiqueDetail: '/wx/antique'
  22. };
  23. const requestFns = {};
  24. Object.keys(urls).forEach(function(key) {
  25. requestFns[key] = function(
  26. data = {},
  27. method = "GET",
  28. success = () => {},
  29. fail = () => {},
  30. complete = () => {}
  31. ) {
  32. console.log(`request ${urls[key]}`);
  33. let commonData = {
  34. loginSessionKey: wx.getStorageSync('token') || ""
  35. };
  36. const url = `${serverName}${urls[key]}`;
  37. return wx.request({
  38. method: method || 'GET',
  39. url,
  40. data: {
  41. ...data,
  42. ...commonData
  43. },
  44. header: {
  45. 'content-type': 'application/x-www-form-urlencoded'
  46. },
  47. success: function(res) {
  48. if (res.data.code == 0) {
  49. console.log(`request ${urls[key]}, success:`, res);
  50. success(res);
  51. } else if (res.data.code == 102) {
  52. // 未登录
  53. wx.showModal({
  54. title: '提示',
  55. content: '登录才能进行以上操作,确定登录吗?',
  56. confirmColor: '#e83828',
  57. success: function (res) {
  58. if (res.confirm) {
  59. wx.navigateTo({
  60. url: '/pages/login_page/index',
  61. success: function (res) { },
  62. fail: function (res) { },
  63. complete: function (res) { },
  64. })
  65. } else if (res.cancel) {
  66. console.log('用户点击取消')
  67. }
  68. }
  69. })
  70. fail(res);
  71. } else if (res.data.code == 11) {
  72. Toast.showToast('warn', '提交信息不完整');
  73. fail(res);
  74. } else if (res.data.code == 101) {
  75. Toast.showToast('warn', '网络超时,请重新登录');
  76. fail(res);
  77. } else if (res.data.code == -1) {
  78. Toast.showToast('warn', '请求发送失败');
  79. fail(res);
  80. } else {
  81. Toast.showToast('warn', res.data.msg || '网络超时,请检查网络');
  82. fail(res);
  83. }
  84. },
  85. fail: function(res) {
  86. fail(res);
  87. },
  88. complete: function() {
  89. complete()
  90. }
  91. });
  92. };
  93. });
  94. const request = {
  95. getAntiqueList: function(data, method, success, fail) {
  96. wx.request({
  97. url: serverName + urls.getAntiqueList,
  98. data: data,
  99. method: method,
  100. header: {
  101. 'content-type': 'application/json'
  102. },
  103. success: function(res) {
  104. if (res.statusCode === 200) {
  105. if (res.data.code === 0) {
  106. success(res);
  107. } else if (res.data.code === 401) {
  108. // 未登录
  109. wx.showToast({
  110. title: '请先登录',
  111. icon: 'none'
  112. });
  113. fail(res);
  114. } else if (res.data.code === 400) {
  115. // 提交信息不完整
  116. wx.showToast({
  117. title: res.data.msg || '提交信息不完整',
  118. icon: 'none'
  119. });
  120. fail(res);
  121. } else {
  122. // 其他错误
  123. wx.showToast({
  124. title: res.data.msg || '请求失败',
  125. icon: 'none'
  126. });
  127. fail(res);
  128. }
  129. } else {
  130. // 网络错误
  131. wx.showToast({
  132. title: '网络错误',
  133. icon: 'none'
  134. });
  135. fail(res);
  136. }
  137. },
  138. fail: function(err) {
  139. // 请求失败
  140. wx.showToast({
  141. title: '请求失败',
  142. icon: 'none'
  143. });
  144. fail(err);
  145. }
  146. });
  147. },
  148. getAntiqueDetail: function(data, method, success, fail) {
  149. // 从页面参数中获取id
  150. const pages = getCurrentPages();
  151. const currentPage = pages[pages.length - 1];
  152. const id = currentPage.data.id;
  153. if (!id) {
  154. wx.showToast({
  155. title: '缺少文物ID',
  156. icon: 'none'
  157. });
  158. fail({ msg: '缺少文物ID' });
  159. return;
  160. }
  161. wx.request({
  162. url: serverName + urls.getAntiqueDetail + '/' + id,
  163. data: data,
  164. method: method,
  165. header: {
  166. 'content-type': 'application/json'
  167. },
  168. success: function(res) {
  169. if (res.statusCode === 200) {
  170. if (res.data.code === 0) {
  171. success(res);
  172. } else if (res.data.code === 401) {
  173. // 未登录
  174. wx.showToast({
  175. title: '请先登录',
  176. icon: 'none'
  177. });
  178. fail(res);
  179. } else if (res.data.code === 400) {
  180. // 提交信息不完整
  181. wx.showToast({
  182. title: res.data.msg || '提交信息不完整',
  183. icon: 'none'
  184. });
  185. fail(res);
  186. } else {
  187. // 其他错误
  188. wx.showToast({
  189. title: res.data.msg || '请求失败',
  190. icon: 'none'
  191. });
  192. fail(res);
  193. }
  194. } else {
  195. // 网络错误
  196. wx.showToast({
  197. title: '网络错误',
  198. icon: 'none'
  199. });
  200. fail(res);
  201. }
  202. },
  203. fail: function(err) {
  204. // 请求失败
  205. wx.showToast({
  206. title: '请求失败',
  207. icon: 'none'
  208. });
  209. fail(err);
  210. }
  211. });
  212. }
  213. };
  214. module.exports.request = request;
  215. module.exports.serverName = serverName;
  216. module.exports.newServerName = newServerName;
  217. module.exports.cosBaseUrl = cosBaseUrl;
  218. module.exports.fileBaseURL = fileBaseURL;