api.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. const GET = 'GET';
  2. const POST = 'POST';
  3. const PUT = 'PUT';
  4. const FORM = 'FORM';
  5. const DELETE = 'DELETE';
  6. const baseURL = 'https://wx.scxjc.club';
  7. // const baseURL = 'https://www.hunanwanzhu.com';
  8. function request(method, url, data) {
  9. var token='';
  10. return new Promise(function (resolve, reject) {
  11. wx.getStorage({
  12. key: 'userInfo',
  13. complete: res => {
  14. if (res.data) {
  15. token = res.data.token
  16. }
  17. let header = {
  18. 'content-type': 'application/json',
  19. 'Authorization': token
  20. };
  21. wx.request({
  22. url: baseURL + url,
  23. method: method,
  24. data: data,
  25. header: header,
  26. success(res) {
  27. //请求成功
  28. //判断状态码---errCode状态根据后端定义来判断
  29. if (res.data.code == 0) {
  30. resolve(res);
  31. } else if (res.data.code == 403) {
  32. //未登录
  33. reject(403)
  34. wx.switchTab({
  35. url: '../user/user',
  36. })
  37. }else{
  38. wx.hideNavigationBarLoading()
  39. wx.hideLoading()
  40. wx.showToast({
  41. title: res.data.message,
  42. icon:'none',
  43. duration:3000
  44. })
  45. }
  46. },
  47. fail(err) {
  48. //请求失败
  49. wx.showToast({
  50. icon:'none',
  51. title: '服务器开小差了',
  52. })
  53. reject(err)
  54. }
  55. })
  56. }
  57. })
  58. })
  59. }
  60. function uploadFile(url, filePath, name, formData, isSubDomain) {
  61. var token = '';
  62. return new Promise((resolve, reject) => {
  63. wx.getStorage({
  64. key: 'userInfo',
  65. complete: res => {
  66. if (res.data) {
  67. token = res.data.token
  68. }
  69. let header = {
  70. 'content-type': 'application/json',
  71. 'Authorization': token
  72. };
  73. wx.uploadFile({
  74. url: baseURL + url,
  75. filePath: filePath,
  76. name: name,
  77. // formData: formData,
  78. header: header,
  79. success: res => {
  80. resolve(JSON.parse(res.data))
  81. },
  82. fail(err) {
  83. //请求失败
  84. reject(err)
  85. }
  86. })
  87. }
  88. })
  89. })
  90. }
  91. const API = {
  92. getOpenid: (data) => request(GET, `/api/wx/openid`, data),
  93. login: (data) => request(POST, `/api/wx/login`, data),
  94. getAuthinfo: (data) => request(GET, `/api/wx/authinfo`, data),
  95. getEnumList: (data) => request(GET, `/api/wx/v2/enum/list`, data),
  96. getIndex: (data) => request(GET, `/api/wx/index`, data),
  97. getRank: (data) => request(GET, `/api/wx/group/rank/list`, data),
  98. getRankList: (data) => request(GET, `/api/wx/group/rank`, data),
  99. getPlayerMatch: (data) => request(GET, `/api/wx/player/match`, data),//参赛资料
  100. updateFollow: (data) => request(POST, `/api/wx/v2/user/follow`, data),
  101. getRinrate: (data) => request(GET, `/api/wx/v2/winrate/rank`, data),
  102. getDefend: (data) => request(GET, `/api/wx/v2/defend/rank`, data),
  103. getHotbuyList: (data) => request(GET, `/api/wx/v2/hot/stock/buy/list`, data),
  104. getHotsellList: (data) => request(GET, `/api/wx/v2/hot/stock/sell/list`, data),
  105. getSellStock: (data) => request(GET, `/api/wx/v2/hot/stock/sell/players`, data),
  106. getChampionlList: (data) => request(GET, `/api/wx/v2/champion/article/list`, data),
  107. upload: (filePath, name) => uploadFile(`/api/wx/uploadfile`, filePath, name),
  108. updateStyle: (data) => request(PUT, `/api/wx/v2/mine/style`, data),
  109. getMyStyle: (data) => request(GET, `/api/wx/v2/mine/style`, data),
  110. getMyFollow: (data) => request(GET, `/api/wx/v2/user/follow/list`, data),
  111. getMyMatch: (data) => request(GET, `/api/wx/player/match/list`, data),
  112. getComment: (data) => request(GET, `/api/wx/v2/wanzhu/comment/list`, data),
  113. getRecordList: (data) => request(GET, `/api/wx/player/match/record/list`, data),
  114. follow: (data) => request(POST, `/api/wx/v2/user/follow`, data),
  115. getPlayerList: (data) => request(GET, `/api/wx/v2/player/list`, data),
  116. updateRecord: (data) => request(POST, `/api/wx/player/record`, data),
  117. getRecord: (data) => request(GET, `/api/wx/player/record`, data),
  118. getCurrecord: (data) => request(GET, `/api/wx/player/currecord`, data),
  119. getArticle: (data) => request(GET, `/api/wx/article`, data),
  120. getStock: (data) => request(GET, `/api/wx/v2/stock`, data),
  121. searchStock: (data) => request(GET, `/api/wx/stock/search`, data),
  122. getNotoice: (data) => request(GET, `/api/wx/v2/notices/list`, data),
  123. getDate: (data) => request(GET, `/api/wx/v2/default/date`, data),
  124. getHotFollow: (data) => request(GET, `/api/wx/v2/hot/follow/list`, data),
  125. getCalendar: (data) => request(GET, `/api/wx/v2/player/match/calendar`, data),
  126. };
  127. module.exports = {
  128. API: API
  129. }