request.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. export const service = (params = {}) => {
  2. // 发送请求
  3. // console.log('begin request!')
  4. // console.log('request params....',params)
  5. return new Promise((resolve, reject) => {
  6. uni.request({
  7. url: 'http://127.0.0.1:8888' + params.url || '', // 接收请求的API
  8. method: params.method || 'POST', // 接收请求的方式,如果不传默认为POST
  9. data: params.data || {}, // 接收请求的data,不传默认为空
  10. header: {
  11. 'Access-Control-Allow-Origin': 'http://localhost:8888',
  12. 'Authorization': 'Bearer '+ params.token || '',
  13. },
  14. success: res => {
  15. if (res.statusCode !== 200 && res.statusCode !== 201) {
  16. return uni.$showMsg('请求失败!请联系平台客服', 2000)
  17. } else {
  18. // console.log('api request response>>>', res.data)
  19. resolve(res)
  20. }
  21. },
  22. fail: err => {
  23. return uni.$showMsg('请求接口失败!', 2000)
  24. reject(err)
  25. }
  26. })
  27. })
  28. };
  29. // 直接通过uni.login获取微信接口提供的code
  30. export const login = (params = {}) => {
  31. return new Promise((resolve, reject) => {
  32. uni.request({
  33. url: 'http://127.0.0.1:8888' + '/login/v2',
  34. method: params.method || 'POST',
  35. data: params.data || {},
  36. header: {
  37. 'Content-Type': 'application/json;charset=UTF-8;',
  38. 'Accept':'application/json, text/plain, */*',
  39. 'Accept-Encoding':'gzip, deflate',
  40. 'Accept-Language':'zh-CN,zh;q=0.9',
  41. },
  42. success: res => {
  43. if (res.statusCode !== 200 && res.statusCode !== 201) {
  44. return uni.$showMsg('请求失败!请联系平台客服', 2000)
  45. } else {
  46. // console.log('api request response>>>', res.data)
  47. resolve(res)
  48. }
  49. },
  50. fail: err => {
  51. return uni.$showMsg('请求接口失败!', 2000)
  52. reject(err)
  53. }
  54. })
  55. })
  56. };
  57. export const wxAuth = (code) => {
  58. return new Promise((resolve, reject) => {
  59. uni.request({
  60. url: config.BASE_URL + '/wxBase/auth',
  61. method: 'POST',
  62. data: {
  63. appSecrtKey: config.API_SECRET_KEY,
  64. appid: config.APP_ID,
  65. merchantId: config.MERCHANT_ID,
  66. code: code
  67. },
  68. header: {
  69. 'Content-Type': 'application/json'
  70. },
  71. success: res => {
  72. if (res.statusCode !== 200 && res.statusCode !== 201) {
  73. return uni.$showMsg('请求失败!请联系平台客服', 2000)
  74. } else {
  75. console.log(res.data)
  76. resolve(res)
  77. }
  78. },
  79. fail: err => {
  80. return uni.$showMsg('微信接口认证失败!', 2000)
  81. reject(err)
  82. }
  83. })
  84. })
  85. };
  86. //
  87. export const wxAppInit = () => {
  88. return new Promise((resolve, reject) => {
  89. uni.request({
  90. url: config.BASE_URL + '/wxBase/appInit',
  91. method: 'POST',
  92. data: {
  93. // appSecrtKey: config.API_SECRET_KEY,
  94. // appid: config.APP_ID,
  95. merchantId: config.MERCHANT_ID,
  96. // code: code
  97. },
  98. header: {
  99. 'Content-Type': 'application/json'
  100. },
  101. success: res => {
  102. if (res.statusCode !== 200 && res.statusCode !== 201) {
  103. return uni.$showMsg('请求失败!请联系平台客服', 2000)
  104. } else {
  105. console.log(res.data)
  106. resolve(res)
  107. }
  108. },
  109. fail: err => {
  110. return uni.$showMsg('微信接口认证失败!', 2000)
  111. reject(err)
  112. }
  113. })
  114. })
  115. };
  116. export const getToken = async () => {
  117. let userInfo = {
  118. token: uni.getStorageSync('token') || '',
  119. expiresAt: uni.getStorageSync('expiresAt') || 0,
  120. }
  121. if (userInfo.token && userInfo.expiresAt > new Date().getTime()) {
  122. return userInfo;
  123. }
  124. let wxLogin = await wxLogin()
  125. let wxAuth = await wxAuth(wxLogin.code)
  126. console.log('request get token', wxAuth)
  127. // uni.setStorageSync('token',res.data); // 需要存入token的值和过期时间{token:'', expiresAt:''}
  128. // return res.data.token;
  129. }