SystemGroupData.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. namespace app\adminapi\controller\v1\setting;
  3. use app\adminapi\controller\AuthController;
  4. use think\Request;
  5. use crmeb\services\{
  6. FormBuilder as Form, UtilService as Util
  7. };
  8. use think\facade\Route as Url;
  9. use app\models\system\{
  10. SystemGroup as GroupModel, SystemGroupData as GroupDataModel, SystemGroupDataMerchant as GroupDataMerchantModel
  11. };
  12. /**
  13. * 数据管理
  14. * Class SystemGroupData
  15. * @package app\adminapi\controller\v1\setting
  16. */
  17. class SystemGroupData extends AuthController
  18. {
  19. /**
  20. * 获取数据列表头
  21. * @return mixed
  22. */
  23. public function header()
  24. {
  25. $gid = $this->request->param('gid');
  26. if (!$gid) return $this->fail('参数错误');
  27. $header = GroupDataModel::getHeader($gid);
  28. return $this->success($header);
  29. }
  30. /**
  31. * 显示资源列表
  32. *
  33. * @return \think\Response
  34. */
  35. public function index()
  36. {
  37. $where = Util::getMore([
  38. ['gid', 0],
  39. ['page', 1],
  40. ['limit', 15],
  41. ['status', ''],
  42. ], $this->request);
  43. $mer_id = $this->merId ?: '';
  44. $list = GroupDataMerchantModel::getList($where, $mer_id);
  45. return $this->success($list);
  46. }
  47. /**
  48. * 显示创建资源表单页.
  49. *
  50. * @return \think\Response
  51. */
  52. public function create()
  53. {
  54. $gid = $this->request->param('gid');
  55. $Fields = GroupModel::getField($gid);
  56. $f = array();
  57. $f[] = Form::hidden('gid', $gid);
  58. foreach ($Fields["fields"] as $key => $value) {
  59. $info = [];
  60. if (isset($value["param"])) {
  61. $value["param"] = str_replace("\r\n", "\n", $value["param"]);//防止不兼容
  62. $params = explode("\n", $value["param"]);
  63. if (is_array($params) && !empty($params)) {
  64. foreach ($params as $index => $v) {
  65. $vl = explode('=>', $v);
  66. if (isset($vl[0]) && isset($vl[1])) {
  67. $info[$index]["value"] = $vl[0];
  68. $info[$index]["label"] = $vl[1];
  69. }
  70. }
  71. }
  72. }
  73. switch ($value["type"]) {
  74. case 'input':
  75. $f[] = Form::input($value["title"], $value["name"]);
  76. break;
  77. case 'textarea':
  78. $f[] = Form::input($value["title"], $value["name"])->type('textarea')->placeholder($value['param']);
  79. break;
  80. case 'radio':
  81. $f[] = Form::radio($value["title"], $value["name"], $info[0]["value"] ?? '')->options($info);
  82. break;
  83. case 'checkbox':
  84. $f[] = Form::checkbox($value["title"], $value["name"], $info[0] ?? '')->options($info);
  85. break;
  86. case 'select':
  87. $f[] = Form::select($value["title"], $value["name"], $info[0] ?? '')->options($info)->multiple(false);
  88. break;
  89. case 'upload':
  90. $f[] = Form::frameImageOne($value["title"], $value["name"], Url::buildUrl('admin/widget.images/index', array('fodder' => $value["title"], 'big' => 1)))->icon('ios-image')->width('60%')->height('435px');
  91. break;
  92. case 'uploads':
  93. $f[] = Form::frameImages($value["title"], $value["name"], Url::buildUrl('admin/widget.images/index', array('fodder' => $value["title"], 'big' => 1)))->maxLength(5)->icon('ios-images')->width('60%')->height('435px')->spin(0);
  94. break;
  95. default:
  96. $f[] = Form::input($value["title"], $value["name"]);
  97. break;
  98. }
  99. }
  100. $f[] = Form::number('sort', '排序', 1);
  101. $f[] = Form::radio('status', '状态', 1)->options([['value' => 1, 'label' => '显示'], ['value' => 2, 'label' => '隐藏']]);
  102. return $this->makePostForm('添加数据', $f, Url::buildUrl('/setting/group_data'), 'POST');
  103. }
  104. /**
  105. * 保存新建的资源
  106. *
  107. * @param \think\Request $request
  108. * @return \think\Response
  109. */
  110. public function save(Request $request)
  111. {
  112. $params = request()->post();
  113. $Fields = GroupModel::getField($params['gid']);
  114. foreach ($params as $key => $param) {
  115. foreach ($Fields['fields'] as $index => $field) {
  116. if ($key == $field["title"]) {
  117. // if($param == "" || count($param) == 0)
  118. if ($param == "")
  119. return $this->fail($field["name"] . "不能为空!");
  120. else {
  121. $value[$key]["type"] = $field["type"];
  122. $value[$key]["value"] = $param;
  123. }
  124. }
  125. }
  126. }
  127. $data = array("gid" => $params['gid'], "add_time" => time(), "value" => json_encode($value), "sort" => $params["sort"], "status" => $params["status"]);
  128. $data['mer_id'] = $this->merId ?: '';
  129. GroupDataMerchantModel::create($data);
  130. return $this->success('添加数据成功!');
  131. }
  132. /**
  133. * 显示指定的资源
  134. *
  135. * @param int $id
  136. * @return \think\Response
  137. */
  138. public function read($id)
  139. {
  140. //
  141. }
  142. /**
  143. * 显示编辑资源表单页.
  144. *
  145. * @param int $id
  146. * @return \think\Response
  147. */
  148. public function edit($id)
  149. {
  150. $gid = $this->request->param('gid');
  151. $GroupData = GroupDataMerchantModel::get($id);
  152. $GroupDataValue = json_decode($GroupData["value"], true);
  153. $Fields = GroupModel::getField($gid);
  154. $f = array();
  155. $f[] = Form::hidden('gid', $gid);
  156. if (!isset($Fields['fields'])) return $this->fail('数据解析失败!');
  157. foreach ($Fields['fields'] as $key => $value) {
  158. $info = [];
  159. if (isset($value["param"])) {
  160. $value["param"] = str_replace("\r\n", "\n", $value["param"]);//防止不兼容
  161. $params = explode("\n", $value["param"]);
  162. if (is_array($params) && !empty($params)) {
  163. foreach ($params as $index => $v) {
  164. $vl = explode('=>', $v);
  165. if (isset($vl[0]) && isset($vl[1])) {
  166. $info[$index]["value"] = $vl[0];
  167. $info[$index]["label"] = $vl[1];
  168. }
  169. }
  170. }
  171. }
  172. $fvalue = isset($GroupDataValue[$value['title']]['value']) ? $GroupDataValue[$value['title']]['value'] : '';
  173. switch ($value['type']) {
  174. case 'input':
  175. $f[] = Form::input($value['title'], $value['name'], $fvalue);
  176. break;
  177. case 'textarea':
  178. $f[] = Form::input($value['title'], $value['name'], $fvalue)->type('textarea');
  179. break;
  180. case 'radio':
  181. $f[] = Form::radio($value['title'], $value['name'], $fvalue)->options($info);
  182. break;
  183. case 'checkbox':
  184. $f[] = Form::checkbox($value['title'], $value['name'], $fvalue)->options($info);
  185. break;
  186. case 'upload':
  187. if (!empty($fvalue)) {
  188. $image = is_string($fvalue) ? $fvalue : $fvalue[0];
  189. } else {
  190. $image = '';
  191. }
  192. $f[] = Form::frameImageOne($value['title'], $value['name'], Url::buildUrl('admin/widget.images/index', array('fodder' => $value['title'], 'big' => 1)), $image)->icon('ios-image')->width('60%')->height('435px');
  193. break;
  194. case 'uploads':
  195. $images = !empty($fvalue) ? $fvalue : [];
  196. $f[] = Form::frameImages($value['title'], $value['name'], Url::buildUrl('admin/widget.images/index', array('fodder' => $value['title'], 'big' => 1)), $images)->maxLength(5)->icon('ios-images')->width('60%')->height('435px')->spin(0);
  197. break;
  198. case 'select':
  199. $f[] = Form::select($value['title'], $value['name'], $fvalue)->setOptions($info);
  200. break;
  201. default:
  202. $f[] = Form::input($value['title'], $value['name'], $fvalue);
  203. break;
  204. }
  205. }
  206. $f[] = Form::number('sort', '排序', $GroupData["sort"]);
  207. $f[] = Form::radio('status', '状态', $GroupData["status"])->options([['value' => 1, 'label' => '显示'], ['value' => 0, 'label' => '隐藏']]);
  208. return $this->makePostForm('编辑数据', $f, Url::buildUrl('/setting/group_data/' . $id), 'PUT');
  209. }
  210. /**
  211. * 保存更新的资源
  212. *
  213. * @param \think\Request $request
  214. * @param int $id
  215. * @return \think\Response
  216. */
  217. public function update(Request $request, $id)
  218. {
  219. $GroupData = GroupDataMerchantModel::get($id);
  220. $Fields = GroupModel::getField($GroupData["gid"]);
  221. $params = request()->post();
  222. foreach ($params as $key => $param) {
  223. foreach ($Fields['fields'] as $index => $field) {
  224. if ($key == $field["title"]) {
  225. if (trim($param) == '')
  226. return $this->fail($field["name"] . "不能为空!");
  227. else {
  228. $value[$key]["type"] = $field["type"];
  229. $value[$key]["value"] = $param;
  230. }
  231. }
  232. }
  233. }
  234. $data = array("value" => json_encode($value), "sort" => $params["sort"], "status" => $params["status"]);
  235. $data['mer_id'] = $this->merId ?: '';
  236. GroupDataMerchantModel::edit($data, $id);
  237. return $this->success('修改成功!');
  238. }
  239. /**
  240. * 删除指定资源
  241. *
  242. * @param int $id
  243. * @return \think\Response
  244. */
  245. public function delete($id)
  246. {
  247. if (!GroupDataMerchantModel::merSet($this->merId)->del($id))
  248. return $this->fail(GroupDataMerchantModel::getErrorInfo('删除失败,请稍候再试!'));
  249. else
  250. return $this->success('删除成功!');
  251. }
  252. /**
  253. * 修改状态
  254. * @param $id
  255. * @param $status
  256. * @return mixed
  257. */
  258. public function set_status($id, $status)
  259. {
  260. if ($status == '' || $id == 0) return $this->fail('参数错误');
  261. GroupDataMerchantModel::merSet($this->merId)->where(['id' => $id])->update(['status' => $status]);
  262. return $this->success($status == 0 ? '隐藏成功' : '显示成功');
  263. }
  264. }