帧视界壹通告,付费看视频的微信小程序
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.

137 lines
2.2 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year 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.code == 401 ||
  22. res.data.message == '操作失败,token非法无效!' ||
  23. res.data.message == '操作失败,用户不存在!'){
  24. uni.removeStorageSync('token')
  25. console.error('登录过期');
  26. uni.navigateTo({
  27. url: '/pages/auth/login'
  28. })
  29. }
  30. if(res.statusCode == 200 &&
  31. res.data.code != 200){
  32. uni.showToast({
  33. mask: true,
  34. duration: 1000,
  35. title: res.data.message,
  36. icon:'none'
  37. });
  38. }
  39. callback(res.data)
  40. },
  41. fail: () => {
  42. uni.showLoading({})
  43. setTimeout(()=>{
  44. uni.hideLoading()
  45. uni.showToast({icon:"none", title:"网络异常"})
  46. }, 3000)
  47. if(showLoading){
  48. uni.hideLoading();
  49. }
  50. }
  51. });
  52. }
  53. function deleted(uri, data, callback) {
  54. http(uri, data, callback, 'DELETE')
  55. }
  56. function post(uri, data, callback) {
  57. http(uri, data, callback, 'POST')
  58. }
  59. function get(uri, data, callback) {
  60. http(uri, data, callback, 'GET')
  61. }
  62. function enhanceData(data) {
  63. const userid = uni.getStorageSync("userid")
  64. if (!data) {
  65. data = {}
  66. }
  67. if (userid) {
  68. data.userid = userid
  69. }
  70. return data
  71. }
  72. function sync(method, uri, data) {
  73. return new Promise((resolve, reject) => {
  74. uni.request({
  75. url: uri,
  76. data: data,
  77. method: method,
  78. header: {
  79. 'auth': '1AS9F1HPC4FBC9EN00J7KX2L5RJ99XHZ'
  80. },
  81. success: (res) => {
  82. resolve(res.data)
  83. },
  84. fail: (err) => {
  85. reject(err);
  86. }
  87. })
  88. })
  89. }
  90. let cache = null
  91. function async (method, uri, data) {
  92. const promise = sync(method, uri, data).then(res => {
  93. cache = res
  94. }).catch(err => {
  95. })
  96. }
  97. function syncHttp(uri, data, method = 'GET') {
  98. async (method, uri, data)
  99. }
  100. export default {
  101. http: http,
  102. delete: deleted,
  103. post: post,
  104. get: get,
  105. syncHttp: syncHttp
  106. }