| | |
| | | public function getList() |
| | | { |
| | | $model = new TemplateModel(); |
| | | $param=$this->request->param(); |
| | | $list=$model->lists($param); |
| | | foreach($list as $l=>$v ){ |
| | | $list[$l]['style']= json_decode($v['style'],true); |
| | | $width=round($param['screenWidth']/$v['style']['backdrop']['width'],2); |
| | | $v['width']=$width; |
| | | foreach ($v['style'] as $key=>$item){ |
| | | if(is_array($item)){ |
| | | foreach ($item as $i=>$y){ |
| | | if($i!='color'&&$i!='src'&&$i!='style'&&$i!='display'&&$i!='type'||is_array($y)){ |
| | | if(is_array($y)&&count($y)){ |
| | | foreach ($y as $u=>$n){ |
| | | if($u!='color'&&$u!='src'&&$u!='style'&&$n!=0){ |
| | | $data[$l]['style'][$key][$i][$u]= round($n*$width,2); |
| | | }else{ |
| | | $data[$l]['style'][$key][$i][$u]=$n; |
| | | } |
| | | } |
| | | }else{ |
| | | $data[$l]['style'][$key][$i]= round($y*$width,2); |
| | | } |
| | | }else{ |
| | | $data[$l]['style'][$key][$i]=$y; |
| | | } |
| | | } |
| | | }else{ |
| | | $data[$l]['style'][$key]=$item; |
| | | $param = $this->request->param(); |
| | | $list = $model->lists($param); |
| | | |
| | | // 获取屏幕宽度 |
| | | $screenWidth = isset($param['screenWidth']) ? floatval($param['screenWidth']) : 375; |
| | | |
| | | foreach($list as $index => $item) { |
| | | // 解析style JSON |
| | | $style = json_decode($item['style'], true); |
| | | |
| | | if (!$style) { |
| | | $list[$index]['style'] = []; |
| | | continue; |
| | | } |
| | | |
| | | // 计算缩放比例 (基于背景图宽度) |
| | | $backdropWidth = isset($style['backdrop']['width']) && $style['backdrop']['width'] > 0 |
| | | ? floatval($style['backdrop']['width']) |
| | | : 375; |
| | | |
| | | $scale = round($screenWidth / $backdropWidth, 4); |
| | | |
| | | // 需要缩放的数值字段(排除这些字段名) |
| | | $excludeKeys = ['color', 'src', 'style', 'display', 'type', 'src']; |
| | | |
| | | // 递归处理style数组,对所有数值字段进行缩放 |
| | | $this->scaleStyleValues($style, $scale, $excludeKeys); |
| | | |
| | | $list[$index]['style'] = $style; |
| | | } |
| | | |
| | | return $this->renderSuccess('', $list); |
| | | } |
| | | |
| | | /** |
| | | * 递归缩放style中的数值字段 |
| | | * @param array &$data 数据数组(引用传递) |
| | | * @param float $scale 缩放比例 |
| | | * @param array $excludeKeys 不需要缩放的键名 |
| | | */ |
| | | private function scaleStyleValues(&$data, $scale, $excludeKeys = []) |
| | | { |
| | | if (!is_array($data)) { |
| | | return; |
| | | } |
| | | |
| | | foreach ($data as $key => &$value) { |
| | | if (is_array($value)) { |
| | | // 递归处理子数组 |
| | | $this->scaleStyleValues($value, $scale, $excludeKeys); |
| | | } elseif (is_numeric($value) && !in_array($key, $excludeKeys)) { |
| | | // 对数值进行缩放(非0值) |
| | | if ($value != 0) { |
| | | $value = round($value * $scale, 2); |
| | | } |
| | | } |
| | | // 其他类型(字符串等)保持不变 |
| | | } |
| | | foreach ($data as $l=>$v ){ |
| | | $list[$l]['style']=$v['style']; |
| | | } |
| | | return $this->renderSuccess('',$list); |
| | | } |
| | | |
| | | /** |