Nessuna descrizione
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FieldLogic.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /**
  3. * 易优CMS
  4. * ============================================================================
  5. * 版权所有 2016-2028 海口快推科技有限公司,并保留所有权利。
  6. * 网站地址: http://www.eyoucms.com
  7. * ----------------------------------------------------------------------------
  8. * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
  9. * ============================================================================
  10. * Author: 小虎哥 <1105415366@qq.com>
  11. * Date: 2018-4-3
  12. */
  13. namespace app\home\logic;
  14. use think\Model;
  15. use think\Db;
  16. /**
  17. * 字段逻辑定义
  18. * Class CatsLogic
  19. * @package home\Logic
  20. */
  21. class FieldLogic extends Model
  22. {
  23. /**
  24. * 查询解析模型数据用以页面展示
  25. * @param array $data 表数据
  26. * @param intval $channel_id 模型ID
  27. * @param array $batch 是否批量列表
  28. * @author 小虎哥 by 2018-7-25
  29. */
  30. public function getChannelFieldList($data, $channel_id = '', $batch = false, $is_minipro = false)
  31. {
  32. if (!empty($data) && !empty($channel_id)) {
  33. /*获取模型对应的附加表字段信息*/
  34. $map = array(
  35. 'channel_id' => $channel_id,
  36. );
  37. $fieldInfo = model('Channelfield')->getListByWhere($map, '*', 'name');
  38. /*--end*/
  39. $data = $this->handleAddonFieldList($data, $fieldInfo, $batch, $is_minipro);
  40. } else {
  41. $data = array();
  42. }
  43. return $data;
  44. }
  45. /**
  46. * 查询解析单个数据表的数据用以页面展示
  47. * @param array $data 表数据
  48. * @param intval $channel_id 模型ID
  49. * @param array $batch 是否批量列表
  50. * @author 小虎哥 by 2018-7-25
  51. */
  52. public function getTableFieldList($data, $channel_id = '', $batch = false)
  53. {
  54. if (!empty($data) && !empty($channel_id)) {
  55. /*获取自定义表字段信息*/
  56. $map = array(
  57. 'channel_id' => $channel_id,
  58. );
  59. $fieldInfo = model('Channelfield')->getListByWhere($map, '*', 'name');
  60. /*--end*/
  61. $data = $this->handleAddonFieldList($data, $fieldInfo, $batch);
  62. } else {
  63. $data = array();
  64. }
  65. return $data;
  66. }
  67. /**
  68. * 处理自定义字段的值
  69. * @param array $data 表数据
  70. * @param array $fieldInfo 自定义字段集合
  71. * @param array $batch 是否批量列表
  72. * @author 小虎哥 by 2018-7-25
  73. */
  74. public function handleAddonFieldList($data, $fieldInfo, $batch = false, $is_minipro = false)
  75. {
  76. if (false !== $batch) {
  77. return $this->handleBatchAddonFieldList($data, $fieldInfo);
  78. }
  79. if (!empty($data) && !empty($fieldInfo)) {
  80. static $city_list = null;
  81. static $province_list = null;
  82. static $area_list = null;
  83. $baseTag = new \think\template\taglib\api\Base;
  84. foreach ($data as $key => $val) {
  85. $dtype = !empty($fieldInfo[$key]) ? $fieldInfo[$key]['dtype'] : '';
  86. if (empty($dtype)) {
  87. continue;
  88. }
  89. $dfvalue_unit = !empty($fieldInfo[$key]) ? $fieldInfo[$key]['dfvalue_unit'] : '';
  90. switch ($dtype) {
  91. case 'int':
  92. case 'float':
  93. case 'text':
  94. {
  95. if (preg_match('/^(\/[\/\w]+)?(\/public\/static\/|\/uploads\/)(.*)\.(\w+)$/i', $val)) {
  96. $val = handle_subdir_pic($val);
  97. }
  98. !empty($dfvalue_unit) && $data[$key.'_unit'] = $dfvalue_unit;
  99. break;
  100. }
  101. case 'imgs':
  102. {
  103. if (!is_array($val)) {
  104. $eyou_imgupload_list = @unserialize($val);
  105. if (false === $eyou_imgupload_list) {
  106. $eyou_imgupload_list = [];
  107. $eyou_imgupload_data = explode(',', $val);
  108. foreach ($eyou_imgupload_data as $k1 => $v1) {
  109. if (!empty($v1)) {
  110. $eyou_imgupload_list[$k1] = [
  111. 'image_url' => (true === $is_minipro) ? $baseTag->get_default_pic($v1) : handle_subdir_pic($v1),
  112. 'intro' => '',
  113. ];
  114. }
  115. }
  116. }
  117. } else {
  118. $eyou_imgupload_list = [];
  119. $eyou_imgupload_data = $val;
  120. foreach ($eyou_imgupload_data as $k1 => $v1) {
  121. if (!empty($v1['image_url'])) {
  122. $v1['image_url'] = (true === $is_minipro) ? $baseTag->get_default_pic($v1['image_url']) : handle_subdir_pic($v1['image_url']);
  123. isset($v1['intro']) && $v1['intro'] = htmlspecialchars_decode($v1['intro']);
  124. $eyou_imgupload_list[$k1] = $v1;
  125. }
  126. }
  127. }
  128. !empty($eyou_imgupload_list) && $eyou_imgupload_list = array_values($eyou_imgupload_list);
  129. $val = $eyou_imgupload_list;
  130. break;
  131. }
  132. case 'img':
  133. {
  134. $val = (true === $is_minipro) ? $baseTag->get_default_pic($val) : handle_subdir_pic($val);
  135. break;
  136. }
  137. case 'media':
  138. {
  139. $val = handle_subdir_pic($val,'media');
  140. break;
  141. }
  142. case 'file':
  143. {
  144. $val = handle_subdir_pic($val);
  145. break;
  146. }
  147. case 'checkbox':
  148. case 'files':
  149. {
  150. if (!is_array($val)) {
  151. $val = !empty($val) ? explode(',', $val) : array();
  152. }
  153. foreach ($val as $k1 => $v1) {
  154. $val[$k1] = handle_subdir_pic($v1);
  155. }
  156. break;
  157. }
  158. case 'htmltext':
  159. {
  160. $val = htmlspecialchars_decode($val);
  161. /*追加指定内嵌样式到编辑器内容的img标签,兼容图片自动适应页面*/
  162. $titleNew = !empty($data['title']) ? $data['title'] : '';
  163. $val = img_style_wh($val, $titleNew);
  164. /*--end*/
  165. /*支持子目录*/
  166. $val = handle_subdir_pic($val, 'html');
  167. /*--end*/
  168. if (true === $is_minipro) {
  169. $val = $baseTag->html_httpimgurl($val);
  170. }
  171. break;
  172. }
  173. case 'decimal':
  174. {
  175. $val = floatval($val);
  176. // $val = number_format($val,'2','.',',');
  177. break;
  178. }
  179. case 'region':
  180. {
  181. // 先在默认值里寻找是否存在对应区域ID的名称
  182. $dfvalue = !empty($fieldInfo[$key]['dfvalue']) ? $fieldInfo[$key]['dfvalue'] : '';
  183. if (!empty($dfvalue)) {
  184. $dfvalue_tmp = unserialize($dfvalue);
  185. $region_ids = !empty($dfvalue_tmp['region_ids']) ? explode(',', $dfvalue_tmp['region_ids']) : [];
  186. if (!empty($region_ids)) {
  187. $arr_index = array_search($val, $region_ids);
  188. if (false !== $arr_index && 0 <= $arr_index) {
  189. $dfvalue_tmp['region_names'] = str_replace(',', ',', $dfvalue_tmp['region_names']);
  190. $region_names = explode(',', $dfvalue_tmp['region_names']);
  191. $val = $region_names[$arr_index];
  192. }
  193. }
  194. }
  195. // 默认值里不存在,则去区域表里获取
  196. if (!empty($val) && preg_match('/^([\d\,]+)$/i', $val)) {
  197. $val_new = '';
  198. $val_arr = explode(',', $val);
  199. foreach ($val_arr as $_k => $_v) {
  200. null === $city_list && $city_list = get_city_list();
  201. if (!empty($city_list[$_v])) {
  202. $val_new .= $city_list[$_v]['name'];
  203. } else {
  204. null === $province_list && $province_list = get_province_list();
  205. if (!empty($province_list[$_v])) {
  206. $val_new .= $province_list[$_v]['name'];
  207. } else {
  208. null === $area_list && $area_list = get_area_list();
  209. $val_new .= !empty($area_list[$_v]) ? $area_list[$_v]['name'] : '';
  210. }
  211. }
  212. }
  213. $val = $val_new;
  214. }
  215. break;
  216. }
  217. default:
  218. {
  219. /*支持子目录*/
  220. if (is_string($val)) {
  221. $val = handle_subdir_pic($val, 'html');
  222. $val = handle_subdir_pic($val);
  223. }
  224. /*--end*/
  225. break;
  226. }
  227. }
  228. $data[$key] = $val;
  229. }
  230. //移动端详情
  231. if (isMobile() && isset($data['content']) && !empty($data['content_ey_m'])) {
  232. $data['content'] = $data['content_ey_m'];
  233. }
  234. }
  235. return $data;
  236. }
  237. /**
  238. * 列表批量处理自定义字段的值
  239. * @param array $data 表数据
  240. * @param array $fieldInfo 自定义字段集合
  241. * @author 小虎哥 by 2018-7-25
  242. */
  243. public function handleBatchAddonFieldList($data, $fieldInfo)
  244. {
  245. if (!empty($data) && !empty($fieldInfo)) {
  246. foreach ($data as $key => $subdata) {
  247. $data[$key] = $this->handleAddonFieldList($subdata, $fieldInfo);
  248. }
  249. }
  250. return $data;
  251. }
  252. }