api.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. let Promises = Promise
  2. import url from "@/util/url";
  3. let {host} = url
  4. Promises.prototype.finally = function(callback) {
  5. let P = this.constructor
  6. return this.then(
  7. value => P.resolve(callback()).then(() => value),
  8. reason => P.resolve(callback()).then(() => {
  9. throw reason
  10. }))
  11. }
  12. class Https {
  13. request(url, method, data,loading) {
  14. var loading=loading||false
  15. if(!loading){
  16. uni.showLoading({
  17. title: '加载中'
  18. })
  19. }
  20. var token = uni.getStorageSync('token')
  21. // var token = "df057579-73c8-4bdd-8312-f64db8efd699"
  22. return new Promises((resolve, reject) => {
  23. uni.request({
  24. // // #ifdef H5
  25. // url:'/apid'+url,
  26. // // #endif
  27. url: host + '/' + url, //
  28. method: method,
  29. data: data,
  30. header: {
  31. // application/json
  32. 'content-type': 'application/json', // 默认值
  33. 'Authorization': token || ''
  34. },
  35. success: function(res) {
  36. // console.log(res.data.code,98765)
  37. if(!loading){
  38. uni.hideLoading()
  39. }
  40. if (res.statusCode == 401) {
  41. uni.navigateTo({
  42. url: '/pages/load/roles'
  43. })
  44. }else if(res.data.code == '00040000'){
  45. uni.showToast({
  46. title: res.data.message,
  47. duration: 1000,
  48. icon: 'none'
  49. });
  50. }else if(res.code == 1002){
  51. uni.navigateTo({
  52. url: '/pages/load/roles'
  53. })
  54. }else if(res.data.code == 1002){
  55. uni.navigateTo({
  56. url: '/pages/load/roles'
  57. })
  58. }else if(res.data.code == 401){
  59. uni.clearStorage()
  60. uni.reLaunch({
  61. url: '/pages/login/login'
  62. })
  63. }else if(res.code == 401){
  64. uni.navigateTo({
  65. url: '/pages/load/roles'
  66. })
  67. }
  68. else{
  69. resolve(res.data)
  70. }
  71. },
  72. fail: function(error) {
  73. uni.hideLoading()
  74. reject(error)
  75. }
  76. })
  77. })
  78. }
  79. upLoad(params) {
  80. console.log(params)
  81. var token = uni.getStorageSync('token')
  82. let urls = ''
  83. return new Promises((resolve, reject) => {
  84. console.log(params.data)
  85. uni.uploadFile({
  86. url: url + '/common/upload',
  87. filePath: params.path,
  88. data: params,
  89. header: {
  90. 'content-type': 'multipart/form-data',
  91. 'Authorization': token
  92. },
  93. formData: {
  94. 'token': token
  95. },
  96. name: params.name || 'file',
  97. success: function(res) {
  98. console.log(res.data,9875)
  99. urls = JSON.parse(res.data);
  100. let urliu = urls.url
  101. console.log(urliu)
  102. let url = urls.url
  103. resolve(url)
  104. },
  105. fail: function(error) {
  106. reject(error)
  107. }
  108. })
  109. })
  110. }
  111. upDown(params) {
  112. console.log(params)
  113. var token = uni.getStorageSync('token')
  114. return new Promises((resolve, reject) => {
  115. uni.uploadFile({
  116. url: host + '/common/download', //
  117. header: {
  118. 'Authorization': token
  119. },
  120. filePath: params.path,
  121. name: params.name || 'img',
  122. success: function(res) {
  123. console.log(res,4)
  124. resolve(res.data)
  125. },
  126. fail: function(error) {
  127. reject(error)
  128. }
  129. })
  130. })
  131. }
  132. get(url, data) {
  133. return this.request(url, 'GET', data)
  134. }
  135. dele(url, data) {
  136. return this.request(url, 'delete', data)
  137. }
  138. post(url, data,loading) {
  139. return this.request(url, 'POST', data,loading)
  140. }
  141. put(url, data) {
  142. return this.request(url, 'put', data)
  143. }
  144. uniApi(params, data) {
  145. return new Promises((resolve, reject) => {
  146. params.events({
  147. ...data,
  148. success(res) {
  149. resolve(res)
  150. },
  151. fail(err) {
  152. reject(err)
  153. }
  154. })
  155. })
  156. }
  157. }
  158. const https = new Https()
  159. export default https