api.js 5.1 KB

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