commonUtils.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. const hour = date.getHours().toString().padStart(2,'0');
  26. const minutes = date.getMinutes().toString().padStart(2,'0');
  27. const secoonds = date.getSeconds().toString().padStart(2,'0');
  28. return `${year}-${month}-${day} ${hour}:${minutes}:${secoonds}`;
  29. }
  30. // 日期转换
  31. function dateFormatYmd(dateStr) {
  32. const timestamp = new Date(dateStr).getTime();
  33. const date = new Date(timestamp);
  34. const year = date.getFullYear();
  35. const month = (date.getMonth() + 1).toString().padStart(2, '0');
  36. const day = date.getDate().toString().padStart(2, '0');
  37. return `${year}-${month}-${day}`;
  38. }
  39. // 获取服务器图片
  40. async function getImgUrlByOssId(ossId){
  41. console.log(ossId)
  42. if(ossId){
  43. try {
  44. const res = await uni.$u.http.get('/resource/oss/listByIds/'+ossId,{data: {auth: true}});
  45. if(res.length > 0){
  46. console.log(res[0].url);
  47. const url = res[0].url;
  48. return url.replace(/^http:/, "https:");
  49. }
  50. } catch (err) {
  51. await uni.showToast({
  52. title: "操作失败"
  53. });
  54. }
  55. }
  56. return null;
  57. }
  58. // 刷新用户登录信息
  59. async function refreshUserLoginInfo(){
  60. uni.$u.http.get('/member/wechat/getUserInfo',{data: {auth: true}}).then((res)=>{
  61. // 数据获取token
  62. this.$store.commit('updateLoginUserInfo', res);
  63. console.log(this.$store.state.loginUserInfo)
  64. }).catch(() =>{
  65. uni.showToast({
  66. title: "操作失败"
  67. })
  68. });
  69. }
  70. // 替换http为https
  71. function replaceHttpWithHttps(url){
  72. return url.replace(/^http:/, "https:");
  73. }
  74. // 导出工具类方法
  75. export default {
  76. isDataEmpty,
  77. formatDate,
  78. dateFormatYmd,
  79. getImgUrlByOssId,
  80. refreshUserLoginInfo,
  81. replaceHttpWithHttps
  82. };