riku
8 小时以前 8e3f3890e93d097df4be744648b9ac404d20a558
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
var utils = require('../common/utils.wxs');
 
function imageStyle(imageProps) {
  if (imageProps && imageProps.width && imageProps.height) {
    return utils._style({
      width: utils.addUnit(imageProps.width),
      height: utils.addUnit(imageProps.height),
    });
  }
  // 兜底逻辑:没有传入 width 和 height 时,使用默认最大宽高尺寸
  return utils._style({
    width: '400rpx',
    height: '400rpx',
  });
}
 
function getImageMode(imageProps) {
  if (imageProps && imageProps.width && imageProps.height) {
    return imageProps.width > imageProps.height ? 'widthFix' : 'heightFix';
  }
  // 兜底逻辑:没有传入 width 和 height 时,使用 aspectFit 保持图片比例
  return imageProps && imageProps.mode ? imageProps.mode : 'scaleToFill';
}
 
function getFileTypeClass(inChat, files) {
  // 如果 inChat 不为 true,返回空字符串
  if (!inChat) {
    return '';
  }
 
  // 如果 files 为空或不存在,返回空字符串
  if (!files || files.length === 0) {
    return '';
  }
 
  // 检查是否所有文件的 fileType 都是 'image'
  var allImages = true;
  for (var i = 0; i < files.length; i++) {
    if (files[i].fileType !== 'image') {
      allImages = false;
      break;
    }
  }
 
  // 根据判断结果返回相应的类名
  return allImages ? 'all_images' : 'all_files';
}
 
module.exports = {
  imageStyle: imageStyle,
  getImageMode: getImageMode,
  getFileTypeClass: getFileTypeClass,
};