commonUtils.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // commonUtils.js
  2. const http = uni.$u.http
  3. // 判断数据是否为空
  4. function isDataEmpty(data) {
  5. if (data === null || data === undefined) {
  6. return true;
  7. }
  8. if (typeof data === 'string' && data.trim() === '') {
  9. return true;
  10. }
  11. if (Array.isArray(data) && data.length === 0) {
  12. return true;
  13. }
  14. if (typeof data === 'object' && Object.keys(data).length === 0) {
  15. return true;
  16. }
  17. return false;
  18. }
  19. // 日期转换
  20. function formatDate(timestamp) {
  21. const date = new Date(timestamp);
  22. const year = date.getFullYear();
  23. const month = (date.getMonth() + 1).toString().padStart(2, '0');
  24. const day = date.getDate().toString().padStart(2, '0');
  25. return `${year}-${month}-${day}`;
  26. }
  27. // 日期转换
  28. function dateFormatYmd(dateStr) {
  29. const timestamp = new Date(dateStr).getTime();
  30. const date = new Date(timestamp);
  31. const year = date.getFullYear();
  32. const month = (date.getMonth() + 1).toString().padStart(2, '0');
  33. const day = date.getDate().toString().padStart(2, '0');
  34. return `${year}-${month}-${day}`;
  35. }
  36. // 获取服务器图片
  37. async function getImgUrlByOssId(ossId){
  38. console.log(ossId)
  39. if(ossId){
  40. try {
  41. const res = await uni.$u.http.get('/resource/oss/listByIds/'+ossId,{data: {auth: true}});
  42. if(res.length > 0){
  43. console.log(res[0].url);
  44. const url = res[0].url;
  45. return url.replace(/^http:/, "https:");
  46. }
  47. } catch (err) {
  48. await uni.showToast({
  49. title: "操作失败"
  50. });
  51. }
  52. }
  53. return null;
  54. }
  55. // 刷新用户登录信息
  56. async function refreshUserLoginInfo(){
  57. uni.$u.http.get('/member/wechat/getUserInfo',{data: {auth: true}}).then((res)=>{
  58. // 数据获取token
  59. this.$store.commit('updateLoginUserInfo', res);
  60. console.log(this.$store.state.loginUserInfo)
  61. }).catch(() =>{
  62. uni.showToast({
  63. title: "操作失败"
  64. })
  65. });
  66. }
  67. // 替换http为https
  68. function replaceHttpWithHttps(url){
  69. return url.replace(/^http:/, "https:");
  70. }
  71. // 导出工具类方法
  72. export default {
  73. isDataEmpty,
  74. formatDate,
  75. dateFormatYmd,
  76. getImgUrlByOssId,
  77. refreshUserLoginInfo,
  78. replaceHttpWithHttps
  79. };