SystemGroupData.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/13
  6. */
  7. namespace app\admin\model\system;
  8. use traits\ModelTrait;
  9. use basic\ModelBasic;
  10. /**
  11. * 数据列表 model
  12. * Class SystemGroupData
  13. * @package app\admin\model\system
  14. */
  15. class SystemGroupData extends ModelBasic
  16. {
  17. use ModelTrait;
  18. /**
  19. * 根据where条件获取当前表中的前20条数据
  20. * @param $params
  21. * @return array
  22. */
  23. public static function getList($params){
  24. $model = new self;
  25. if($params['gid'] !== '') $model = $model->where('gid',$params['gid']);
  26. if($params['status'] !== '') $model = $model->where('status',$params['status']);
  27. $model = $model->order('sort desc,id ASC');
  28. return self::page($model,function($item,$key){
  29. $info = json_decode($item->value,true);
  30. foreach ($info as $index => $value) {
  31. if($value["type"] == "checkbox")$info[$index]["value"] = implode(",",$value["value"]);
  32. if($value["type"] == "upload" || $value["type"] == "uploads"){
  33. $html_img = '';
  34. if(is_array($value["value"])){
  35. foreach ($value["value"] as $img) {
  36. $html_img .= '<img class="image" data-image="'.$img.'" width="45" height="45" src="'.$img.'" /><br>';
  37. }
  38. }else{
  39. $html_img = '<img class="image" data-image="'.$value["value"].'" width="45" height="45" src="'.$value["value"].'" />';
  40. }
  41. $info[$index]["value"] = $html_img;
  42. }
  43. }
  44. $item->value = $info;
  45. });
  46. }
  47. /**获得组合数据信息+组合数据列表
  48. * @param $config_name
  49. * @param int $limit
  50. * @return array|bool|false|\PDOStatement|string|\think\Model
  51. */
  52. public static function getGroupData($config_name,$limit = 0)
  53. {
  54. $group = SystemGroup::where('config_name',$config_name)->field('name,info,config_name')->find();
  55. if(!$group) return false;
  56. $group['data'] = self::getAllValue($config_name,$limit);
  57. return $group;
  58. }
  59. /**
  60. * 获取单个值
  61. * @param $config_name
  62. * @param int $limit
  63. * @return mixed
  64. */
  65. public static function getAllValue($config_name,$limit = 0){
  66. $model = new self;
  67. $model->alias('a')->field('a.*,b.config_name')->join('system_group b','a.gid = b.id')->where(array("b.config_name"=>$config_name,"a.status"=>1))->order('sort desc,id ASC');
  68. if($limit > 0) $model->limit($limit);
  69. $data = [];
  70. $result = $model->select();
  71. if(!$result) return $data;
  72. $result = $result->toArray();
  73. foreach ($result as $key => $value) {
  74. $data[$key]["id"] = $value["id"];
  75. $fields = json_decode($value["value"],true);
  76. foreach ($fields as $index => $field) {
  77. if($field['type'] === 'radio') {
  78. $data[$key][$index] = self::getGroupRadioValue($value['gid'],$field["value"]);
  79. }else $data[$key][$index] = $field["value"];
  80. }
  81. }
  82. return $data;
  83. }
  84. public static function tidyList($result)
  85. {
  86. $data = [];
  87. if(!$result) return $data;
  88. foreach ($result as $key => $value) {
  89. $data[$key]["id"] = $value["id"];
  90. $fields = json_decode($value["value"],true);
  91. foreach ($fields as $index => $field) {
  92. $data[$key][$index] = $field['type'] == 'upload' ? (isset($field["value"][0]) ? $field["value"][0]: ''):$field["value"];
  93. }
  94. }
  95. return $data;
  96. }
  97. /**
  98. * 根据id获取当前记录中的数据
  99. * @param $id
  100. * @return mixed
  101. */
  102. public static function getDateValue($id){
  103. $value = self::alias('a')->where(array("id"=>$id))->find();
  104. $data["id"] = $value["id"];
  105. $fields = json_decode($value["value"],true);
  106. foreach ($fields as $index => $field) {
  107. if($field['type'] === 'radio') {
  108. $data[$index] = self::getGroupRadioValue($value['gid'],$field["value"]);
  109. }else $data[$index] = $field["value"];
  110. }
  111. return $data;
  112. }
  113. /**
  114. * TODO radio 根据值获取参数
  115. * @param $id
  116. * @param $value
  117. * @return mixed
  118. */
  119. public static function getGroupRadioValue($id,$value){
  120. $groupData = SystemGroup::getField($id);
  121. foreach ($groupData['fields'] as $key=>&$item){
  122. if($item['type'] == 'radio'){
  123. $params = explode("\n",$item["param"]);
  124. if(is_array($params) && !empty($params)){
  125. foreach ($params as $index => &$v) {
  126. $vl = explode('=>',$v);
  127. if(isset($vl[0]) && isset($vl[1]) && count($vl) && $vl[1] === $value) return $vl[0];
  128. }
  129. }
  130. }
  131. }
  132. return $value;
  133. }
  134. }