api.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. reject(err)
  50. }
  51. })
  52. }
  53. })
  54. })
  55. }
  56. function uploadFile(url, filePath, name, formData, isSubDomain) {
  57. var token = '';
  58. return new Promise((resolve, reject) => {
  59. wx.getStorage({
  60. key: 'userInfo',
  61. complete: res => {
  62. if (res.data) {
  63. token = res.data.token
  64. }
  65. let header = {
  66. 'content-type': 'application/json',
  67. 'Authorization': token
  68. };
  69. wx.uploadFile({
  70. url: baseURL + url,
  71. filePath: filePath,
  72. name: name,
  73. // formData: formData,
  74. header: header,
  75. success: res => {
  76. resolve(JSON.parse(res.data))
  77. },
  78. fail(err) {
  79. //请求失败
  80. reject(err)
  81. }
  82. })
  83. }
  84. })
  85. })
  86. }
  87. const API = {
  88. getOpenid: (data) => request(GET, `/api/wx/openid`, data),
  89. login: (data) => request(POST, `/api/wx/login`, data),
  90. getAuthinfo: (data) => request(GET, `/api/wx/authinfo`, data),
  91. getEnumList: (data) => request(GET, `/api/wx/v2/enum/list`, data),
  92. getIndex: (data) => request(GET, `/api/wx/index`, data),
  93. getRank: (data) => request(GET, `/api/wx/group/rank/list`, data),
  94. getRankList: (data) => request(GET, `/api/wx/group/rank`, data),
  95. getPlayerMatch: (data) => request(GET, `/api/wx/player/match`, data),//参赛资料
  96. updateFollow: (data) => request(POST, `/api/wx/v2/user/follow`, data),
  97. getRinrate: (data) => request(GET, `/api/wx/v2/winrate/rank`, data),
  98. getDefend: (data) => request(GET, `/api/wx/v2/defend/rank`, data),
  99. getHotbuyList: (data) => request(GET, `/api/wx/v2/hot/stock/buy/list`, data),
  100. getHotsellList: (data) => request(GET, `/api/wx/v2/hot/stock/sell/list`, data),
  101. getChampionlList: (data) => request(GET, `/api/wx/v2/champion/article/list`, data),
  102. upload: (filePath, name) => uploadFile(`/api/wx/uploadfile`, filePath, name),
  103. updateStyle: (data) => request(PUT, `/api/wx/v2/mine/style`, data),
  104. getMyStyle: (data) => request(GET, `/api/wx/v2/mine/style`, data),
  105. getMyFollow: (data) => request(GET, `/api/wx/v2/user/follow/list`, data),
  106. getMyMatch: (data) => request(GET, `/api/wx/player/match/list`, data),
  107. getComment: (data) => request(GET, `/api/wx/v2/wanzhu/comment/list`, data),
  108. getRecordList: (data) => request(GET, `/api/wx/player/match/record/list`, data),
  109. follow: (data) => request(POST, `/api/wx/v2/user/follow`, data),
  110. getPlayerList: (data) => request(GET, `/api/wx/v2/player/list`, data),
  111. updateRecord: (data) => request(POST, `/api/wx/player/record`, data),
  112. getRecord: (data) => request(GET, `/api/wx/player/record`, data),
  113. getCurrecord: (data) => request(GET, `/api/wx/player/currecord`, data),
  114. getArticle: (data) => request(GET, `/api/wx/article`, data),
  115. getStock: (data) => request(GET, `/api/wx/v2/stock`, data),
  116. searchStock: (data) => request(GET, `/api/wx/stock/search`, data),
  117. getNotoice: (data) => request(GET, `/api/wx/v2/notices/list`, data),
  118. };
  119. module.exports = {
  120. API: API
  121. }