SystemGroupData.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * @author: xaboy<365615158@qq.com>
  4. * @day: 2017/11/13
  5. */
  6. namespace app\models\system;
  7. use crmeb\traits\ModelTrait;
  8. use crmeb\basic\BaseModel;
  9. use app\models\system\SystemGroup as GroupModel;
  10. /**
  11. * 数据列表 model
  12. * Class SystemGroupData
  13. * @package app\models\system
  14. */
  15. class SystemGroupData extends BaseModel
  16. {
  17. /**
  18. * 数据表主键
  19. * @var string
  20. */
  21. protected $pk = 'id';
  22. /**
  23. * 模型名称
  24. * @var string
  25. */
  26. protected $name = 'system_group_data';
  27. use ModelTrait;
  28. /**
  29. * 根据where条件获取当前表中的前20条数据
  30. * @param $params
  31. * @return array
  32. */
  33. public static function getList($params)
  34. {
  35. $model = new self;
  36. if ($params['gid'] !== '') $model = $model->where('gid', $params['gid']);
  37. if ($params['status'] !== '') $model = $model->where('status', $params['status']);
  38. $count = $model->count();
  39. $model = $model->order('sort desc,id ASC');
  40. $list = $model->page((int)$params['page'], (int)$params['limit'])
  41. ->select()
  42. ->each(function ($item) {
  43. $info = json_decode($item->value, true);
  44. foreach ($info as $index => $value) {
  45. if ($value["type"] == "checkbox") $info[$index]["value"] = implode(",", $value["value"]);
  46. // if($value["type"] == "upload" || $value["type"] == "uploads"){
  47. // $html_img = '';
  48. // if(is_array($value["value"])){
  49. // foreach ($value["value"] as $img) {
  50. // $html_img .= '<img class="image" data-image="'.$img.'" width="45" height="45" src="'.$img.'" /><br>';
  51. // }
  52. // }else{
  53. // $html_img = '<img class="image" data-image="'.$value["value"].'" width="45" height="45" src="'.$value["value"].'" />';
  54. // }
  55. // $info[$index]["value"] = $html_img;
  56. // }
  57. }
  58. $item['value'] = $info;
  59. });
  60. foreach ($list as $key => $value) {
  61. foreach ($value['value'] as $k => $v) {
  62. $list[$key][$k] = $v['value'];
  63. }
  64. unset($list[$key]['value']);
  65. }
  66. $type = '';
  67. $Fields = GroupModel::getField($params['gid'])['fields'];
  68. foreach ($Fields as $item) {
  69. if ($item['type'] === 'upload') {
  70. $type = $item['title'];
  71. }
  72. }
  73. return compact('count', 'list', 'type');
  74. }
  75. /**
  76. * 获得组合数据信息+组合数据列表
  77. * @param $config_name
  78. * @param int $limit
  79. * @return array|bool|null|\think\Model
  80. * @throws \think\db\exception\DataNotFoundException
  81. * @throws \think\db\exception\ModelNotFoundException
  82. * @throws \think\exception\DbException
  83. */
  84. public static function getGroupData($config_name, $limit = 0)
  85. {
  86. $group = SystemGroup::where('config_name', $config_name)->field('name,info,config_name')->find();
  87. if (!$group) return false;
  88. $group['data'] = self::getAllValue($config_name, $limit);
  89. return $group;
  90. }
  91. /**
  92. * 获取单个值
  93. * @param $config_name
  94. * @param int $limit
  95. * @return array
  96. * @throws \think\db\exception\DataNotFoundException
  97. * @throws \think\db\exception\ModelNotFoundException
  98. * @throws \think\exception\DbException
  99. */
  100. public static function getAllValue($config_name, $limit = 0)
  101. {
  102. $model = self::alias('a')->field('a.*,b.config_name')->join('system_group b', 'a.gid = b.id')
  103. ->where("b.config_name", $config_name)->where("a.status", 1)
  104. ->order('sort desc,id ASC');
  105. if ($limit > 0) $model->limit($limit);
  106. $data = [];
  107. $result = $model->select();
  108. if (!$result) return $data;
  109. foreach ($result as $key => $value) {
  110. $data[$key]["id"] = $value["id"];
  111. $fields = json_decode($value["value"], true);
  112. foreach ($fields as $index => $field) {
  113. // $data[$key][$index] = $field['type'] == 'upload' ? (isset($field["value"][0]) ? $field["value"][0]: ''):$field["value"];
  114. $data[$key][$index] = $field["value"];
  115. }
  116. }
  117. return $data;
  118. }
  119. /**
  120. * @param $result
  121. * @return array
  122. */
  123. public static function tidyList($result)
  124. {
  125. $data = [];
  126. if (!$result) return $data;
  127. foreach ($result as $key => $value) {
  128. $data[$key]["id"] = $value["id"];
  129. $fields = json_decode($value["value"], true);
  130. foreach ($fields as $index => $field) {
  131. $data[$key][$index] = $field['type'] == 'upload' ? (isset($field["value"][0]) ? $field["value"][0] : '') : $field["value"];
  132. }
  133. }
  134. return $data;
  135. }
  136. /**
  137. * 根据id获取当前记录中的数据
  138. * @param $id
  139. * @return mixed
  140. * @throws \think\db\exception\DataNotFoundException
  141. * @throws \think\db\exception\ModelNotFoundException
  142. * @throws \think\exception\DbException
  143. */
  144. public static function getDateValue($id)
  145. {
  146. $value = self::alias('a')->where(array("id" => $id))->find();
  147. $data["id"] = $value["id"];
  148. $fields = json_decode($value["value"], true);
  149. foreach ($fields as $index => $field) {
  150. $data[$index] = $field["value"];
  151. }
  152. return $data;
  153. }
  154. /**
  155. * 获取列表头
  156. * @param $gid
  157. * @return array
  158. */
  159. public static function getHeader($gid)
  160. {
  161. $data = json_decode(GroupModel::where('id', $gid)->value("fields"), true) ?: [];
  162. foreach ($data as $key => $item) {
  163. if ($item['type'] == 'upload' || $item['type'] == 'uploads') {
  164. $header[$key]['slot'] = $item['title'];
  165. $header[$key]['minWidth'] = 60;
  166. } elseif ($item['title'] == 'url' || $item['title'] == 'wap_url' || $item['title'] == 'link' || $item['title'] == 'wap_link') {
  167. $header[$key]['key'] = $item['title'];
  168. $header[$key]['minWidth'] = 200;
  169. } else {
  170. $header[$key]['key'] = $item['title'];
  171. $header[$key]['minWidth'] = 100;
  172. }
  173. $header[$key]['title'] = $item['name'];
  174. }
  175. array_unshift($header, ['key' => 'id', 'title' => '编号', 'minWidth' => 35]);
  176. array_push($header, ['slot' => 'status', 'title' => '是否显示', 'minWidth' => 80], ['slot' => 'action', 'fixed' => 'right', 'title' => '操作', 'minWidth' => 120]);
  177. return compact('header');
  178. }
  179. }