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

136 lines
2.2 KiB

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