common.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import { removeToken } from '@/utils/token-util';
  2. import axios from 'axios';
  3. export async function downloadOssLink(url, name) {
  4. const res = await axios.get(url, {
  5. responseType: 'blob' // 设置响应类型为blob,以便处理二进制数据
  6. });
  7. await checkDownloadRes(res);
  8. download(res.data, `${name}_${Date.now()}.xlsx`);
  9. }
  10. /**
  11. * 退出登录
  12. * @param route 是否使用路由跳转
  13. * @param from 登录后跳转的地址
  14. * @param push 路由跳转方法
  15. */
  16. export function logout(route, from, push) {
  17. removeToken();
  18. if (route && push) {
  19. push({
  20. path: '/login',
  21. query: from ? { from: encodeURIComponent(from) } : void 0
  22. });
  23. return;
  24. }
  25. // 这样跳转避免再次登录重复注册动态路由, hash 路由模式使用 location.reload();
  26. const BASE_URL = import.meta.env.BASE_URL;
  27. const url = BASE_URL + 'login';
  28. location.replace(from ? `${url}?from=${encodeURIComponent(from)}` : url);
  29. }
  30. /**
  31. * 下载文件
  32. * @param data 二进制数据
  33. * @param name 文件名
  34. * @param type 文件类型
  35. */
  36. export function download(data, name, type) {
  37. const blob = new Blob([data], { type: type || 'application/octet-stream' });
  38. const url = URL.createObjectURL(blob);
  39. const a = document.createElement('a');
  40. a.href = url;
  41. a.download = name;
  42. a.style.display = 'none';
  43. document.body.appendChild(a);
  44. a.click();
  45. document.body.removeChild(a);
  46. URL.revokeObjectURL(url);
  47. }
  48. /**
  49. * 参数转url字符串
  50. * @param params 参数
  51. * @param url 需要拼接参数的地址
  52. */
  53. export function toURLSearch(params, url) {
  54. if (typeof params !== 'object' || params == null) {
  55. return '';
  56. }
  57. const result = transformParams(params)
  58. .map((d) => `${encodeURIComponent(d[0])}=${encodeURIComponent(d[1])}`)
  59. .join('&');
  60. if (!url) {
  61. return result;
  62. }
  63. return (url.includes('?') ? `${url}&` : `${url}?`) + result;
  64. }
  65. /**
  66. * 参数转表单数据
  67. * @param params 参数
  68. */
  69. export function toFormData(params) {
  70. const formData = new FormData();
  71. if (typeof params !== 'object' || params == null) {
  72. return formData;
  73. }
  74. transformParams(params).forEach((d) => {
  75. formData.append(d[0], d[1]);
  76. });
  77. return formData;
  78. }
  79. /**
  80. * get请求处理数组和对象类型参数
  81. * @param params 参数
  82. */
  83. export function transformParams(params) {
  84. const result = [];
  85. if (params != null && typeof params === 'object') {
  86. Object.keys(params).forEach((key) => {
  87. const value = params[key];
  88. if (value != null && value !== '') {
  89. if (Array.isArray(value) && value.length && isBlobFile(value[0])) {
  90. value.forEach((file) => {
  91. result.push([key, file]);
  92. });
  93. } else if (typeof value === 'object' && !isBlobFile(value)) {
  94. getObjectParamsArray(value).forEach((item) => {
  95. result.push([`${key}${item[0]}`, item[1]]);
  96. });
  97. } else {
  98. result.push([key, value]);
  99. }
  100. }
  101. });
  102. }
  103. return result;
  104. }
  105. /**
  106. * 对象转参数数组
  107. * @param obj 对象
  108. */
  109. export function getObjectParamsArray(obj) {
  110. const result = [];
  111. Object.keys(obj).forEach((key) => {
  112. const value = obj[key];
  113. if (value != null && value !== '') {
  114. const name = `[${key}]`;
  115. if (typeof value === 'object' && !isBlobFile(value)) {
  116. getObjectParamsArray(value).forEach((item) => {
  117. result.push([`${name}${item[0]}`, item[1]]);
  118. });
  119. } else {
  120. result.push([name, value]);
  121. }
  122. }
  123. });
  124. return result;
  125. }
  126. /**
  127. * 判断是否是文件
  128. * @param obj 对象
  129. */
  130. export function isBlobFile(obj) {
  131. return obj != null && (obj instanceof Blob || obj instanceof File);
  132. }
  133. /**
  134. * 检查下载文件的请求结果
  135. * @param res 请求结果
  136. */
  137. export async function checkDownloadRes(res) {
  138. if (res.headers['content-type'].startsWith('application/json')) {
  139. const json = await res.data.text();
  140. return Promise.reject(
  141. new Error(JSON.parse(json).msg || '系统未知错误,请反馈给管理员')
  142. );
  143. }
  144. return true;
  145. }
  146. /**
  147. * 切换主题过渡动画
  148. * @param callback 执行的方法
  149. * @param el 过渡动画触发元素
  150. * @param isOut 是否是退出方向
  151. */
  152. export function doWithTransition(callback, el, isOut) {
  153. if (!el || typeof document.startViewTransition !== 'function') {
  154. callback().then(() => { });
  155. return;
  156. }
  157. document.documentElement.classList.add('disabled-transition');
  158. el.classList.add('view-transition-trigger');
  159. el.style.setProperty('view-transition-name', 'view-transition-trigger');
  160. const rect = el.getBoundingClientRect();
  161. const x = rect.left + rect.width / 2;
  162. const y = rect.top + rect.height / 2;
  163. const endRadius = Math.hypot(
  164. Math.max(x, innerWidth - x),
  165. Math.max(y, innerHeight - y)
  166. );
  167. document.startViewTransition(callback).ready.then(() => {
  168. const clipPath = [
  169. `circle(0px at ${x}px ${y}px)`,
  170. `circle(${endRadius}px at ${x}px ${y}px)`
  171. ];
  172. const anim = document.documentElement.animate(
  173. { clipPath: isOut ? [...clipPath].reverse() : clipPath },
  174. {
  175. duration: 400,
  176. easing: 'ease-in',
  177. pseudoElement: isOut
  178. ? `::view-transition-old(root)`
  179. : `::view-transition-new(root)`
  180. }
  181. );
  182. anim.onfinish = () => {
  183. el.style.removeProperty('view-transition-name');
  184. el.classList.remove('view-transition-trigger');
  185. document.documentElement.classList.remove('disabled-transition');
  186. };
  187. });
  188. }
  189. //格式化手机号
  190. export function formatPhone(phone) {
  191. if (!phone) return '-';
  192. return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
  193. }
  194. //格式化姓名 只保留第一个字,其他用*代替
  195. export function formatName(name) {
  196. if (!name) return '-';
  197. return name.substring(0, 1) + '*'.repeat(name.length - 1);
  198. }