axios.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import {
  2. base_url,
  3. appId
  4. } from '@/utils/config.js';
  5. import {
  6. urlEncode
  7. } from '@/utils/utils.js';
  8. import store from '../store/index.js';
  9. //封装的通用请求方法
  10. export const axios = function(obj = {}) {
  11. if (typeof obj !== 'object') {
  12. throw new Error('arguments must be object');
  13. }
  14. if (!obj.url) {
  15. throw new Error('url not defined');
  16. }
  17. if (obj.isLoading) {
  18. uni.showLoading({
  19. title: '加载中',
  20. mask: true,
  21. })
  22. }
  23. let _url = base_url + obj.url;
  24. let isGet = obj.method && obj.method.toLowerCase() == 'get';
  25. if (isGet) {
  26. _url += '?' + urlEncode(obj.params)
  27. }
  28. if (
  29. // 已经获取用户详情以及当前商家数据
  30. (uni.getStorageSync('token') && uni.getStorageSync('websitIdSj')) ||
  31. // 获取用户详情
  32. !!~_url.indexOf('/user/auth') ||
  33. // 获取当前商家数据
  34. (uni.getStorageSync('token') && !!~_url.indexOf("/user/userWebsit"))
  35. ) {
  36. return new Promise((resolve, reject) => {
  37. uni.request({
  38. url: _url,
  39. data: isGet ? '' : (obj.params || ''),
  40. header: {
  41. 'content-type': obj.type || 'application/x-www-form-urlencoded',
  42. "x-token": uni.getStorageSync('token') || '',
  43. 'APPID': appId
  44. },
  45. method: obj.method ? obj.method : "POST",
  46. success: function(res) {
  47. if (obj.isLoading) {
  48. uni.hideLoading();
  49. }
  50. if (res.statusCode === 200) {
  51. if (res.data.code == 200 || res.data.code == 1100) {
  52. resolve(res.data)
  53. } else if (res.data.code == 1001) {
  54. uni.showToast({
  55. title: '未登录',
  56. icon: 'none'
  57. });
  58. store.commit('changeIsLogin', false);
  59. uni.removeStorageSync('token');
  60. uni.navigateTo({
  61. url: '/pages/login/index',
  62. })
  63. reject(res.data);
  64. } else {
  65. uni.showToast({
  66. title: res.data.message,
  67. icon: 'none'
  68. });
  69. reject(res.data);
  70. }
  71. } else {
  72. uni.showToast({
  73. title: '错误码:' + res.statusCode,
  74. icon: 'none'
  75. })
  76. reject(res.data);
  77. }
  78. },
  79. fail: function(err) {
  80. uni.showModal({
  81. title: '提示',
  82. content: '网络连接失败' + JSON.stringify(err),
  83. })
  84. reject(err);
  85. },
  86. })
  87. })
  88. } else {
  89. return new Promise((resolve, reject) => {
  90. var digui = function() {
  91. if (
  92. (uni.getStorageSync('token') && uni.getStorageSync('websitIdSj')) ||
  93. (uni.getStorageSync('token') && !!~_url.indexOf("/user/userWebsit")) ||
  94. (uni.getStorageSync('token') && !!~_url.indexOf("/user/detail"))
  95. ) {
  96. uni.request({
  97. url: _url,
  98. data: isGet ? '' : (obj.params || ''),
  99. header: {
  100. 'content-type': obj.type || 'application/x-www-form-urlencoded',
  101. "x-token": uni.getStorageSync('token') || '',
  102. 'APPID': appId
  103. },
  104. method: obj.method ? obj.method : "POST",
  105. success: function(res) {
  106. if (obj.isLoading) {
  107. uni.hideLoading();
  108. }
  109. if (res.statusCode === 200) {
  110. if (res.data.code == 200 || res.data.code == 1100) {
  111. resolve(res.data)
  112. } else if (res.data.code == 1001) {
  113. uni.showToast({
  114. title: '未登录',
  115. icon: 'none'
  116. });
  117. store.commit('changeIsLogin', false);
  118. uni.removeStorageSync('token');
  119. uni.navigateTo({
  120. url: '/pages/login/index',
  121. })
  122. reject(res.data);
  123. } else {
  124. uni.showToast({
  125. title: res.data.message,
  126. icon: 'none'
  127. });
  128. reject(res.data);
  129. }
  130. } else {
  131. uni.showToast({
  132. title: '错误码:' + res.statusCode,
  133. icon: 'none'
  134. })
  135. reject(res.data);
  136. }
  137. },
  138. fail: function(err) {
  139. uni.showModal({
  140. title: '提示',
  141. content: '网络连接失败' + JSON.stringify(err),
  142. })
  143. reject(err);
  144. },
  145. })
  146. } else {
  147. setTimeout(digui, 100)
  148. }
  149. }
  150. digui()
  151. })
  152. }
  153. }
  154. const getUUID = function() {
  155. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
  156. return (c === 'x' ? (Math.random() * 16) | 0 : 'r&0x3' | '0x8').toString(16)
  157. })
  158. }
  159. const createName = function(name) {
  160. const date = Date.now()
  161. const uuid = getUUID()
  162. const fileSuffix = name.substring(name.lastIndexOf('.') + 1)
  163. return `${date}${uuid}.${fileSuffix}`
  164. }
  165. // 图片上传
  166. export const uploadImg = async function(file) {
  167. console.log(file);
  168. uni.showLoading({
  169. mask: true,
  170. })
  171. // 获取oss配置
  172. const par = await axios({
  173. url: '/common/oss/config',
  174. method: 'get'
  175. }).then(res => {
  176. return res.data;
  177. }).catch(err => {
  178. uni.hideLoading();
  179. })
  180. const fileKey = par.dir + createName(file.path);
  181. return new Promise((resolve, reject) => {
  182. uni.uploadFile({
  183. url: par.host,
  184. // header: {
  185. // "Content-Type": 'multipart/form-data',
  186. // },
  187. name: 'file',
  188. formData: {
  189. ...par,
  190. name: file.name,
  191. key: fileKey
  192. },
  193. filePath: file.path,
  194. success(res) {
  195. resolve({
  196. url: fileKey
  197. })
  198. },
  199. fail(err) {
  200. reject(err)
  201. },
  202. complete(res) {
  203. uni.hideLoading();
  204. }
  205. })
  206. })
  207. }
  208. export default {
  209. axios,
  210. uploadImg,
  211. }