帧视界壹通告,付费看视频的微信小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
2.1 KiB

11 months ago
11 months ago
11 months ago
11 months ago
  1. import Vue from 'vue'
  2. function http(uri, data, callback, method = 'GET', showLoading, title) {
  3. if(showLoading){
  4. uni.showLoading({
  5. title: title || '正在提交...'
  6. });
  7. }
  8. uni.request({
  9. url: Vue.prototype.$config.baseUrl + uri,
  10. data: enhanceData(data),
  11. method: method,
  12. header: {
  13. 'X-Access-Token': uni.getStorageSync('token'),
  14. 'Content-Type' : method == 'POST' ? 'application/x-www-form-urlencoded' : 'application/json'
  15. },
  16. success: (res) => {
  17. if(showLoading){
  18. uni.hideLoading();
  19. }
  20. if(res.statusCode == 401 ||
  21. res.data.message == '操作失败,token非法无效!'){
  22. uni.removeStorageSync('token')
  23. console.error('登录过期');
  24. uni.navigateTo({
  25. url: '/pages/auth/login'
  26. })
  27. }
  28. if(res.statusCode == 200 && res.data.code != 200){
  29. uni.showToast({
  30. mask: true,
  31. duration: 1000,
  32. title: res.data.message,
  33. });
  34. }
  35. callback(res.data)
  36. },
  37. fail: () => {
  38. uni.showLoading({})
  39. setTimeout(()=>{
  40. uni.hideLoading()
  41. uni.showToast({icon:"none", title:"网络异常"})
  42. }, 3000)
  43. if(showLoading){
  44. uni.hideLoading();
  45. }
  46. }
  47. });
  48. }
  49. function deleted(uri, data, callback) {
  50. http(uri, data, callback, 'DELETE')
  51. }
  52. function post(uri, data, callback) {
  53. http(uri, data, callback, 'POST')
  54. }
  55. function get(uri, data, callback) {
  56. http(uri, data, callback, 'GET')
  57. }
  58. function enhanceData(data) {
  59. const userid = uni.getStorageSync("userid")
  60. if (!data) {
  61. data = {}
  62. }
  63. if (userid) {
  64. data.userid = userid
  65. }
  66. return data
  67. }
  68. function sync(method, uri, data) {
  69. return new Promise((resolve, reject) => {
  70. uni.request({
  71. url: uri,
  72. data: data,
  73. method: method,
  74. header: {
  75. 'auth': '1AS9F1HPC4FBC9EN00J7KX2L5RJ99XHZ'
  76. },
  77. success: (res) => {
  78. resolve(res.data)
  79. },
  80. fail: (err) => {
  81. reject(err);
  82. }
  83. })
  84. })
  85. }
  86. let cache = null
  87. function async (method, uri, data) {
  88. const promise = sync(method, uri, data).then(res => {
  89. cache = res
  90. }).catch(err => {
  91. })
  92. }
  93. function syncHttp(uri, data, method = 'GET') {
  94. async (method, uri, data)
  95. }
  96. export default {
  97. http: http,
  98. delete: deleted,
  99. post: post,
  100. get: get,
  101. syncHttp: syncHttp
  102. }