axios.js 5.1 KB

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