Form.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\admin\system\form;
  12. use app\common\repositories\store\StoreActivityRelatedRepository;
  13. use app\common\repositories\system\form\FormRepository;
  14. use app\common\repositories\system\notice\SystemNoticeRepository;
  15. use app\validate\admin\SystemNoticeValidate;
  16. use crmeb\basic\BaseController;
  17. use crmeb\services\ExcelService;
  18. use think\App;
  19. use think\Exception;
  20. use think\exception\ValidateException;
  21. /**
  22. * 系统表单
  23. */
  24. class Form extends BaseController
  25. {
  26. /**
  27. * @var FormRepository
  28. */
  29. protected $repository;
  30. /**
  31. * SystemNotice constructor.
  32. * @param App $app
  33. * @param FormRepository $repository
  34. */
  35. public function __construct(App $app, FormRepository $repository)
  36. {
  37. parent::__construct($app);
  38. $this->repository = $repository;
  39. }
  40. /**
  41. * 列表
  42. * @return \think\response\Json
  43. * @author Qinii
  44. */
  45. public function lst()
  46. {
  47. $where = $this->request->params(['keyword', 'date']);
  48. [$page, $limit] = $this->getPage();
  49. $where['is_del'] = 0;
  50. $where['mer_id'] = $this->request->merId();
  51. return app('json')->success($this->repository->getList($where, $page, $limit));
  52. }
  53. /**
  54. * 详情
  55. * @param $id
  56. * @return \think\response\Json
  57. * @author Qinii
  58. */
  59. public function detail($id)
  60. {
  61. $data = $this->repository->get($id);
  62. if (!$data || $data['mer_id'] != $this->request->merId())
  63. return app('json')->fail('数据不存在');
  64. return app('json')->success($data);
  65. }
  66. /**
  67. * 添加
  68. * @return \think\response\Json
  69. * @author Qinii
  70. */
  71. public function create()
  72. {
  73. $data = $this->checkParams();
  74. $this->repository->create($data);
  75. return app('json')->success('添加成功');
  76. }
  77. /**
  78. * 编辑
  79. * @param $id
  80. * @return \think\response\Json
  81. * @author Qinii
  82. */
  83. public function update($id)
  84. {
  85. if (!$this->repository->merHas($this->request->merId(), $id))
  86. return app('json')->fail('数据不存在');
  87. $data = $this->checkParams();
  88. $data['update_time'] = date('Y-m-d H:i:s', time());
  89. $this->repository->update($id, $data);
  90. return app('json')->success('编辑成功');
  91. }
  92. /**
  93. * 删除
  94. * @param $id
  95. * @return \think\response\Json
  96. * @author Qinii
  97. */
  98. public function delete($id)
  99. {
  100. $mer_id = $this->request->merId();
  101. if (!$this->repository->merHas($mer_id, $id))
  102. return app('json')->fail('数据不存在');
  103. $this->repository->delete($id, $mer_id);
  104. return app('json')->success('删除成功');
  105. }
  106. /**
  107. * 字段验证
  108. * @return array
  109. * @author Qinii
  110. * @day 2023/10/7
  111. */
  112. public function checkParams()
  113. {
  114. $data = $this->request->params(['name', 'value', ['status', 1]]);
  115. $data['mer_id'] = $this->request->merId();
  116. if (!$data['value']) throw new ValidateException('请选择表单组件');
  117. $i = 0;
  118. foreach ($data['value'] as &$value) {
  119. if (!isset($value['key']) || !$value['key']) {
  120. $value['key'] = $value['name'] . '_' . time() . $i;
  121. }
  122. try {
  123. $keys[] = [
  124. 'key' => $value['key'],
  125. 'label' => $value['titleConfig']['value'],
  126. 'val' => $value['titleShow']['val'],
  127. 'type' => $value['name']
  128. ];
  129. } catch (Exception $exception) {
  130. throw new ValidateException('表单组件异常');
  131. }
  132. $i++;
  133. }
  134. $data['name'] = $data['name'] ?: '系统表单';
  135. $data['form_keys'] = json_encode($keys, JSON_UNESCAPED_UNICODE);
  136. $data['value'] = json_encode($data['value'], JSON_UNESCAPED_UNICODE);
  137. return $data;
  138. }
  139. /**
  140. * 筛选列表
  141. * @return \think\response\Json
  142. * @author Qinii
  143. * @day 2023/10/9
  144. */
  145. public function select()
  146. {
  147. $where = [
  148. 'status' => 1,
  149. 'mer_id' => $this->request->merId(),
  150. 'is_del' => 0,
  151. ];
  152. $data = $this->repository->search($where)->field('form_id,name')->select();
  153. return app('json')->success($data);
  154. }
  155. /**
  156. * 表单的结构类型获取
  157. * @param $id
  158. * @return \think\response\Json
  159. * @author Qinii
  160. * @day 2023/10/9
  161. */
  162. public function info($id)
  163. {
  164. $mer_id = $this->request->param('mer_id', 0);
  165. if (empty($mer_id)) {
  166. $mer_id = $this->request->merId();
  167. }
  168. $data = $this->repository->getSearch(['form_id' => $id, 'mer_id' => $mer_id])->value('form_keys');
  169. if (!$data)
  170. return app('json')->success([]);
  171. return app('json')->success(json_decode($data));
  172. }
  173. /**
  174. * 用户提交的表单记录信息
  175. * @param $id
  176. * @param StoreActivityRelatedRepository $storeActivityRelatedRepository
  177. * @return \think\response\Json
  178. * @author Qinii
  179. * @day 2023/10/9
  180. */
  181. public function formUserList($id, StoreActivityRelatedRepository $storeActivityRelatedRepository)
  182. {
  183. [$page, $limit] = $this->getPage();
  184. $where['create_time'] = $this->request->param('date', '');
  185. $where['link_id'] = $id;
  186. $where['activity_type'] = $storeActivityRelatedRepository::ACTIVITY_TYPE_FORM;
  187. $header = $this->repository->getFormKeys($id, $this->request->merId());
  188. $data = $storeActivityRelatedRepository->getList($where, $page, $limit);
  189. $data['header'] = json_decode($header['form']);
  190. return app('json')->success($data);
  191. }
  192. /**
  193. * 导出用户提交的信息
  194. * @param StoreActivityRelatedRepository $storeActivityRelatedRepository
  195. * @return \think\response\Json
  196. * @author Qinii
  197. * @day 2023/10/9
  198. */
  199. public function excel(StoreActivityRelatedRepository $storeActivityRelatedRepository)
  200. {
  201. [$page, $limit] = $this->getPage();
  202. $where['link_id'] = $this->request->param('id', 0);
  203. $where['create_time'] = $this->request->param('date', 0);
  204. if (!$where['link_id']) return app('json')->fail('请选择表单');
  205. $where['activity_type'] = $storeActivityRelatedRepository::ACTIVITY_TYPE_FORM;
  206. $data = $this->repository->excel($where, $page, $limit, $this->request->merId());
  207. return app('json')->success($data);
  208. }
  209. }