SystemForm.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\v1\system\form;
  12. use app\controller\admin\AuthController;
  13. use app\services\system\form\SystemFormDataServices;
  14. use app\services\system\form\SystemFormServices;
  15. use think\facade\App;
  16. /**
  17. *
  18. * Class SystemForm
  19. * @package app\controller\admin\v1\system\form
  20. */
  21. class SystemForm extends AuthController
  22. {
  23. /**
  24. * Diy constructor.
  25. * @param App $app
  26. * @param SystemFormServices $services
  27. */
  28. public function __construct(App $app, SystemFormServices $services)
  29. {
  30. parent::__construct($app);
  31. $this->services = $services;
  32. }
  33. /**
  34. * 系统表单列表
  35. * @return mixed
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\DbException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. */
  40. public function index()
  41. {
  42. $where = $this->request->getMore([
  43. ['status', ''],
  44. ['name', ''],
  45. ]);
  46. return $this->success($this->services->getFormList($where));
  47. }
  48. /*
  49. * 所有系统表单
  50. */
  51. public function allSystemForm()
  52. {
  53. $data = $this->services->getFormList([], ['id', 'name']);
  54. return $this->success($data['list'] ?? []);
  55. }
  56. /**
  57. * 获取一条数据
  58. * @param int $id
  59. * @return mixed
  60. */
  61. public function getInfo(int $id)
  62. {
  63. if (!$id) return $this->fail('数据不存在');
  64. [$type] = $this->request->postMore([
  65. ['type', 0],
  66. ], true);
  67. $info = $this->services->get($id);
  68. if ($info) {
  69. $info = $info->toArray();
  70. } else {
  71. return $this->fail('数据不存在');
  72. }
  73. $info['value'] = json_decode($info['value'], true);
  74. if ($type == 1) {//处理表单数据
  75. $value = $info['value'] ?? [];
  76. $info = $this->services->handleForm($value);
  77. }
  78. return $this->success(compact('info'));
  79. }
  80. /**
  81. * 保存资源
  82. * @param int $id
  83. * @return mixed
  84. */
  85. public function save(int $id = 0)
  86. {
  87. $data = $this->request->postMore([
  88. ['name', ''],
  89. ['value', '']
  90. ]);
  91. if (!$data['name']) {
  92. return $this->fail('请输入表单模版名称');
  93. }
  94. if (!$data['value']) {
  95. return $this->fail('请添加表单组件');
  96. }
  97. foreach ($data['value'] as $item) {
  98. $defaultValue = $item['defaultValConfig']['value'] ?? '';
  99. if ($item['name'] == 'texts' && $defaultValue) {//文本类型,验证手机号 身份证 邮箱 数字
  100. $tableList = $item['valConfig']['tabList'] ?? [];
  101. $tableValue = $item['valConfig']['tabVal'] ?? 0;
  102. if ($tableList) {
  103. foreach ($tableList as $type => $verify) {
  104. switch ($tableValue) {
  105. case 0:
  106. break;
  107. case 1://手机号
  108. if (!check_phone($defaultValue)) {
  109. return $this->fail('请输入正确的手机号');
  110. }
  111. break;
  112. case 2://身份证
  113. try {
  114. if (!check_card($defaultValue)) return app('json')->fail('请输入正确的身份证');
  115. } catch (\Throwable $e) {
  116. // return app('json')->fail('请输入正确的身份证');
  117. }
  118. break;
  119. case 3://邮箱
  120. if (!check_mail($defaultValue)) {
  121. return $this->fail('请填写正确的邮箱');
  122. }
  123. break;
  124. case 4://数字
  125. if (!preg_match('/^[0-9]+$/', $defaultValue)) {
  126. return $this->fail('请输入数字');
  127. }
  128. break;
  129. }
  130. }
  131. }
  132. }
  133. }
  134. $value = is_array($data['value']) ? json_encode($data['value']) : $data['value'];
  135. $data['value'] = $value;
  136. $one = $this->services->getOne(['name' => $data['name']]);
  137. if ($one && (($id && $one['id'] != $id) || !$id)) {
  138. return $this->fail('模版名称已经存在');
  139. }
  140. if ($id) {
  141. $info = $this->services->get((int)$id, ['id', 'is_del']);
  142. if (!$info) {
  143. return $this->fail('数据不存在');
  144. }
  145. $data['update_time'] = time();
  146. $res = $this->services->update($id, $data);
  147. if (!$res) $this->fail('修改失败');
  148. } else {
  149. $data['add_time'] = time();
  150. $data['update_time'] = time();
  151. $res = $this->services->save($data);
  152. if (!$res) $this->fail('保存失败');
  153. $id = $res->id;
  154. }
  155. return $this->success($id ? '修改成功' : '保存成功', ['id' => $id]);
  156. }
  157. /**
  158. * 删除模板
  159. * @param $id
  160. * @return mixed
  161. */
  162. public function del($id)
  163. {
  164. $info = $this->services->get((int)$id, ['id', 'is_del']);
  165. if ($info && $info['is_del'] == 0) {
  166. $this->services->update($id, ['is_del' => 1]);
  167. }
  168. return $this->success('删除成功');
  169. }
  170. /**
  171. * 上下架
  172. * @param $id
  173. * @return mixed
  174. */
  175. public function set_show($id, $is_show)
  176. {
  177. $info = $this->services->get((int)$id, ['id']);
  178. if (!$info) {
  179. return $this->fail('数据不存在');
  180. }
  181. $this->services->update($id, ['status' => $is_show, 'update_time' => time()]);
  182. return $this->success('设置成功');
  183. }
  184. /**
  185. * 表单收集数据列表
  186. * @param SystemFormDataServices $systemFormDataServices
  187. * @param $id
  188. * @return mixed
  189. * @throws \think\db\exception\DataNotFoundException
  190. * @throws \think\db\exception\DbException
  191. * @throws \think\db\exception\ModelNotFoundException
  192. */
  193. public function formData(SystemFormDataServices $systemFormDataServices, $id)
  194. {
  195. $where = $this->request->postMore([
  196. ['data', '', '', 'time']
  197. ]);
  198. return $this->success($systemFormDataServices->getFormDataList((int)$id, $where));
  199. }
  200. }