Form.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\form\model;
  11. use app\model\BaseModel;
  12. use think\facade\Cache;
  13. /**
  14. * 系统表单
  15. */
  16. class Form extends BaseModel
  17. {
  18. public $form_type = [
  19. 'order' => [
  20. 'type' => 'order',
  21. 'name' => '统一下单',
  22. 'preview_img' => 'addon/form/shop/view/public/img/preview/order.png',
  23. 'head_img' => 'addon/form/shop/view/public/img/head/order.png'
  24. ],
  25. 'goods' => [
  26. 'type' => 'goods',
  27. 'name' => '商品表单',
  28. 'preview_img' => 'addon/form/shop/view/public/img/preview/goods.png',
  29. 'head_img' => 'addon/form/shop/view/public/img/head/goods.png'
  30. ],
  31. 'custom' => [
  32. 'type' => 'custom',
  33. 'name' => '自定义表单',
  34. 'preview_img' => 'addon/form/shop/view/public/img/preview/custom.png',
  35. 'head_img' => 'addon/form/shop/view/public/img/head/custom.png'
  36. ]
  37. ];
  38. /**
  39. * 表单组件
  40. */
  41. const FORM_COMPONENT = [
  42. 'ONE_LINE_TEXT' => [
  43. 'name' => 'ONE_LINE_TEXT',
  44. 'title' => '单行文本',
  45. 'type' => 'SYSTEM',
  46. 'controller' => 'Text',
  47. 'value' => [
  48. 'title' => '单行文本框',
  49. 'placeholder' => '请输入提示语',
  50. 'default' => '',
  51. 'required' => true
  52. ],
  53. 'sort' => '10000',
  54. 'support_diy_view' => '',
  55. 'max_count' => 0,
  56. 'is_delete' => 0,
  57. 'icon' => 'addon/form/shop/view/public/img/icon/on_line_text.png',
  58. 'icon_selected' => 'addon/form/shop/view/public/img/icon/on_line_text_selected.png'
  59. ],
  60. 'MANY_LINE_TEXT' => [
  61. 'name' => 'MANY_LINE_TEXT',
  62. 'title' => '多行文本',
  63. 'type' => 'SYSTEM',
  64. 'controller' => 'Textarea',
  65. 'value' => [
  66. 'title' => '多行文本',
  67. 'placeholder' => '请输入提示语',
  68. 'default' => '',
  69. 'required' => true
  70. ],
  71. 'sort' => '10000',
  72. 'support_diy_view' => '',
  73. 'max_count' => 0,
  74. 'is_delete' => 0,
  75. 'icon' => 'addon/form/shop/view/public/img/icon/many_line_text.png',
  76. 'icon_selected' => 'addon/form/shop/view/public/img/icon/many_line_text_selected.png'
  77. ],
  78. 'ID_CARD' => [
  79. 'name' => 'ID_CARD',
  80. 'title' => '身份证号码',
  81. 'type' => 'SYSTEM',
  82. 'controller' => 'Text',
  83. 'value' => [
  84. 'title' => '身份证号码',
  85. 'placeholder' => '请输入身份证号码',
  86. 'required' => true
  87. ],
  88. 'sort' => '10000',
  89. 'support_diy_view' => '',
  90. 'max_count' => 0,
  91. 'is_delete' => 0,
  92. 'icon' => 'addon/form/shop/view/public/img/icon/id_card.png',
  93. 'icon_selected' => 'addon/form/shop/view/public/img/icon/id_card_selected.png'
  94. ],
  95. 'SELECT' => [
  96. 'name' => 'SELECT',
  97. 'title' => '下拉框',
  98. 'type' => 'SYSTEM',
  99. 'controller' => 'Select',
  100. 'value' => [
  101. 'title' => '下拉框',
  102. 'options' => [
  103. '选项一',
  104. '选项二'
  105. ],
  106. 'required' => true
  107. ],
  108. 'sort' => '10000',
  109. 'support_diy_view' => '',
  110. 'max_count' => 0,
  111. 'is_delete' => 0,
  112. 'icon' => 'addon/form/shop/view/public/img/icon/select.png',
  113. 'icon_selected' => 'addon/form/shop/view/public/img/icon/select_selected.png'
  114. ],
  115. 'CHECKBOX' => [
  116. 'name' => 'CHECKBOX',
  117. 'title' => '多选框',
  118. 'type' => 'SYSTEM',
  119. 'controller' => 'Checkbox',
  120. 'value' => [
  121. 'title' => '多选框',
  122. 'options' => [
  123. '选项一',
  124. '选项二'
  125. ],
  126. 'required' => true
  127. ],
  128. 'sort' => '10000',
  129. 'support_diy_view' => '',
  130. 'max_count' => 0,
  131. 'is_delete' => 0,
  132. 'icon' => 'addon/form/shop/view/public/img/icon/checkbox.png',
  133. 'icon_selected' => 'addon/form/shop/view/public/img/icon/checkbox_selected.png'
  134. ],
  135. 'RADIO' => [
  136. 'name' => 'RADIO',
  137. 'title' => '单选框',
  138. 'type' => 'SYSTEM',
  139. 'controller' => 'Radio',
  140. 'value' => [
  141. 'title' => '单选框',
  142. 'options' => [
  143. '选项一',
  144. '选项二'
  145. ],
  146. 'required' => true
  147. ],
  148. 'sort' => '10000',
  149. 'support_diy_view' => '',
  150. 'max_count' => 0,
  151. 'is_delete' => 0,
  152. 'icon' => 'addon/form/shop/view/public/img/icon/radio.png',
  153. 'icon_selected' => 'addon/form/shop/view/public/img/icon/radio_selected.png'
  154. ],
  155. 'IMG' => [
  156. 'name' => 'IMG',
  157. 'title' => '图片',
  158. 'type' => 'SYSTEM',
  159. 'controller' => 'Img',
  160. 'value' => [
  161. 'title' => '图片',
  162. 'max_count' => 3,
  163. 'required' => true
  164. ],
  165. 'sort' => '10000',
  166. 'support_diy_view' => '',
  167. 'max_count' => 0,
  168. 'is_delete' => 0,
  169. 'icon' => 'addon/form/shop/view/public/img/icon/img.png',
  170. 'icon_selected' => 'addon/form/shop/view/public/img/icon/img_selected.png'
  171. ],
  172. 'DATE' => [
  173. 'name' => 'DATE',
  174. 'title' => '日期',
  175. 'type' => 'SYSTEM',
  176. 'controller' => 'Date',
  177. 'value' => [
  178. 'title' => '日期',
  179. 'placeholder' => '请输入提示语',
  180. 'is_show_default' => true,
  181. 'is_current' => true,
  182. 'default' => '',
  183. 'required' => true
  184. ],
  185. 'sort' => '10000',
  186. 'support_diy_view' => '',
  187. 'max_count' => 0,
  188. 'is_delete' => 0,
  189. 'icon' => 'addon/form/shop/view/public/img/icon/date.png',
  190. 'icon_selected' => 'addon/form/shop/view/public/img/icon/date_selected.png'
  191. ],
  192. 'DATE_LIMIT' => [
  193. 'name' => 'DATE_LIMIT',
  194. 'title' => '日期范围',
  195. 'type' => 'SYSTEM',
  196. 'controller' => 'Datelimit',
  197. 'value' => [
  198. 'title' => '日期范围',
  199. 'placeholder_start' => '请输入起始日期提示语',
  200. 'placeholder_end' => '请输入结束日期提示语',
  201. 'is_show_default_start' => true,
  202. 'is_show_default_end' => true,
  203. 'is_current_start' => true,
  204. 'is_current_end' => true,
  205. 'default_start' => '',
  206. 'default_end' => '',
  207. 'required' => true
  208. ],
  209. 'sort' => '10000',
  210. 'support_diy_view' => '',
  211. 'max_count' => 0,
  212. 'is_delete' => 0,
  213. 'icon' => 'addon/form/shop/view/public/img/icon/date_limit.png',
  214. 'icon_selected' => 'addon/form/shop/view/public/img/icon/date_limit_selected.png'
  215. ],
  216. 'TIME' => [
  217. 'name' => 'TIME',
  218. 'title' => '时间',
  219. 'type' => 'SYSTEM',
  220. 'controller' => 'Time',
  221. 'value' => [
  222. 'title' => '时间',
  223. 'placeholder' => '请输入提示语',
  224. 'is_show_default' => true,
  225. 'is_current' => true,
  226. 'default' => '',
  227. 'required' => true
  228. ],
  229. 'sort' => '10000',
  230. 'support_diy_view' => '',
  231. 'max_count' => 0,
  232. 'is_delete' => 0,
  233. 'icon' => 'addon/form/shop/view/public/img/icon/time.png',
  234. 'icon_selected' => 'addon/form/shop/view/public/img/icon/time_selected.png'
  235. ],
  236. 'TIME_LIMIT' => [
  237. 'name' => 'TIME_LIMIT',
  238. 'title' => '时间范围',
  239. 'type' => 'SYSTEM',
  240. 'controller' => 'Timelimit',
  241. 'value' => [
  242. 'title' => '时间范围',
  243. 'placeholder_start' => '请输入起始时间提示语',
  244. 'placeholder_end' => '请输入结束时间提示语',
  245. 'is_show_default_start' => true,
  246. 'is_show_default_end' => true,
  247. 'is_current_start' => true,
  248. 'is_current_end' => true,
  249. 'default_start' => '',
  250. 'default_end' => '',
  251. 'required' => true
  252. ],
  253. 'sort' => '10000',
  254. 'support_diy_view' => '',
  255. 'max_count' => 0,
  256. 'is_delete' => 0,
  257. 'icon' => 'addon/form/shop/view/public/img/icon/time_limit.png',
  258. 'icon_selected' => 'addon/form/shop/view/public/img/icon/time_limit_selected.png'
  259. ],
  260. 'CITY' => [
  261. 'name' => 'CITY',
  262. 'title' => '城市',
  263. 'type' => 'SYSTEM',
  264. 'controller' => 'City',
  265. 'value' => [
  266. 'title' => '城市',
  267. 'placeholder' => '请输入提示语',
  268. 'default_type' => 1,
  269. 'required' => true
  270. ],
  271. 'sort' => '10000',
  272. 'support_diy_view' => '',
  273. 'max_count' => 0,
  274. 'is_delete' => 0,
  275. 'icon' => 'addon/form/shop/view/public/img/icon/city.png',
  276. 'icon_selected' => 'addon/form/shop/view/public/img/icon/city_selected.png'
  277. ],
  278. 'MOBILE' => [
  279. 'name' => 'MOBILE',
  280. 'title' => '手机号码',
  281. 'type' => 'SYSTEM',
  282. 'controller' => 'Text',
  283. 'value' => [
  284. 'title' => '手机号码',
  285. 'placeholder' => '请输入手机号码',
  286. 'required' => true
  287. ],
  288. 'sort' => '10000',
  289. 'support_diy_view' => '',
  290. 'max_count' => 0,
  291. 'is_delete' => 0,
  292. 'icon' => 'addon/form/shop/view/public/img/icon/mobile.png',
  293. 'icon_selected' => 'addon/form/shop/view/public/img/icon/mobile_selected.png'
  294. ],
  295. ];
  296. /**
  297. * 添加自定义模板
  298. * @param $data
  299. * @return array
  300. */
  301. public function addForm($data)
  302. {
  303. $res = model('form')->add($data);
  304. if ($res) {
  305. Cache::tag("form")->clear();
  306. return $this->success($res);
  307. } else {
  308. return $this->error($res);
  309. }
  310. }
  311. /**
  312. * 修改自定义模板
  313. * @param array $data
  314. * @param array $condition
  315. * @return array
  316. */
  317. public function editForm($data, $condition)
  318. {
  319. $res = model('form')->update($data, $condition);
  320. if ($res) {
  321. Cache::tag("form")->clear();
  322. return $this->success($res);
  323. } else {
  324. return $this->error($res);
  325. }
  326. }
  327. /**
  328. * 获取自定义模板信息
  329. * @param array $condition
  330. * @param string $field
  331. * @return array
  332. */
  333. public function getFormInfo($condition = [], $field = '*', $alias = 'a', $join = [])
  334. {
  335. $data = json_encode([ $condition, $field, $alias, $join ]);
  336. $cache = Cache::get("form_getFormInfo_" . $data);
  337. if (!empty($cache)) {
  338. return $this->success($cache);
  339. }
  340. $info = model('form')->getInfo($condition, $field, $alias, $join);
  341. Cache::tag("form")->set("form_getFormInfo_" . $data, $info);
  342. return $this->success($info);
  343. }
  344. /**
  345. * 删除表单
  346. * @param array $condition
  347. * @return array
  348. */
  349. public function deleteForm($id, $site_id)
  350. {
  351. model('form')->startTrans();
  352. $res = model('form')->delete([ [ 'id', '=', $id ], [ 'site_id', '=', $site_id ] ]);
  353. if ($res) {
  354. model('form_data')->delete([ [ 'form_id', '=', $id ], [ 'site_id', '=', $site_id ] ]);
  355. model('form')->commit();
  356. return $this->success($res);
  357. } else {
  358. model('form')->rollback();
  359. return $this->error($res);
  360. }
  361. }
  362. /**
  363. * 获取表单分页列表
  364. * @param array $condition
  365. * @param int $page
  366. * @param int $page_size
  367. * @param string $order
  368. * @param string $field
  369. * @return array
  370. */
  371. public function getFormPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = 'id desc', $field = '*')
  372. {
  373. $list = model('form')->pageList($condition, $field, $order, $page, $page_size);
  374. return $this->success($list);
  375. }
  376. /**
  377. * 获取表单列表
  378. * @param array $condition
  379. * @param string $order
  380. * @param string $field
  381. * @return array
  382. */
  383. public function getFormList($condition = [], $order = 'id desc', $field = '*')
  384. {
  385. $list = model('form')->getList($condition, $field, $order);
  386. return $this->success($list);
  387. }
  388. /**
  389. * 查询订单数据并导出
  390. * @param $condition
  391. */
  392. public function export($param)
  393. {
  394. $form_info = model('form')->getInfo([ [ 'id', '=', $param[ 'id' ] ], [ 'site_id', '=', $param[ 'site_id' ] ] ], 'form_name,id,json_data');
  395. if (empty($form_info)) return $this->error('', '未获取到表单信息');
  396. $form_info[ 'json_data' ] = json_decode($form_info[ 'json_data' ], true);
  397. $form_data_list = model('form_data')->getList([ [ 'fd.form_id', '=', $form_info[ 'id' ] ] ], 'fd.form_data,fd.create_time,m.nickname', 'id desc', 'fd', [ [ 'member m', 'm.member_id = fd.member_id', 'left' ] ]);
  398. if (empty($form_data_list)) return $this->error('', '表单没有可导出的数据');
  399. $phpExcel = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
  400. $phpExcel->getProperties()->setTitle("表单数据");
  401. $phpExcel->getProperties()->setSubject("表单数据");
  402. $sheet = $phpExcel->getActiveSheet();
  403. // 创建表头
  404. foreach ($form_info[ 'json_data' ] as $k => $form_item) {
  405. $sheet->getCellByColumnAndRow(( $k + 1 ), 1)->setValue($form_item[ 'value' ][ 'title' ]);
  406. }
  407. $sheet->getCellByColumnAndRow(( count($form_info[ 'json_data' ]) + 1 ), 1)->setValue('会员');
  408. $sheet->getCellByColumnAndRow(( count($form_info[ 'json_data' ]) + 2 ), 1)->setValue('填写时间');
  409. // 填充数据
  410. foreach ($form_data_list as $data_k => $data_item) {
  411. foreach ($form_info[ 'json_data' ] as $k => $form_item) {
  412. $form_data = array_column(json_decode($data_item[ 'form_data' ], true), null, 'id');
  413. if (!empty($form_data[ $form_item[ 'id' ] ])) {
  414. $sheet->getCellByColumnAndRow(( $k + 1 ), ( $data_k + 2 ))->setValue($form_data[ $form_item[ 'id' ] ] ? $form_data[ $form_item[ 'id' ] ][ 'val' ] : '');
  415. }
  416. }
  417. $sheet->getCellByColumnAndRow(( count($form_info[ 'json_data' ]) + 1 ), ( $data_k + 2 ))->setValue($data_item[ 'nickname' ] ? $data_item[ 'nickname' ] : '');
  418. $sheet->getCellByColumnAndRow(( count($form_info[ 'json_data' ]) + 2 ), ( $data_k + 2 ))->setValue(time_to_date($data_item[ 'create_time' ]));
  419. }
  420. $objWriter = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($phpExcel, 'Xlsx');
  421. $file = date('Y年m月d日-' . $form_info[ 'form_name' ] . '数据导出', time()) . '.xlsx';
  422. $objWriter->save($file);
  423. header("Content-type:application/octet-stream");
  424. $filename = basename($file);
  425. header("Content-Disposition:attachment;filename = " . $filename);
  426. header("Accept-ranges:bytes");
  427. header("Accept-length:" . filesize($file));
  428. readfile($file);
  429. unlink($file);
  430. exit;
  431. }
  432. /**
  433. * 添加表单数据
  434. * @param $param
  435. */
  436. public function addFormData($param)
  437. {
  438. $info = model('form')->getInfo([ [ 'id', '=', $param[ 'form_id' ] ], [ 'site_id', '=', $param[ 'site_id' ] ] ]);
  439. if (empty($info)) return $this->error('', '该表单不存在');
  440. if (!$info[ 'is_use' ]) return $this->error('', '该表单未启用');
  441. $id = model('form_data')->add([
  442. 'site_id' => $param[ 'site_id' ],
  443. 'form_id' => $param[ 'form_id' ],
  444. 'member_id' => $param[ 'member_id' ],
  445. 'relation_id' => $param[ 'relation_id' ],
  446. 'create_time' => time(),
  447. 'form_data' => json_encode($param[ 'form_data' ]),
  448. 'scene' => $param[ 'scene' ]
  449. ]);
  450. return $this->success($id);
  451. }
  452. public function urlQrcode($page, $qrcode_param, $site_id)
  453. {
  454. $params = [
  455. 'site_id' => $site_id,
  456. 'data' => $qrcode_param,
  457. 'page' => $page,
  458. 'promotion_type' => '',
  459. 'h5_path' => $page . '?id=' . $qrcode_param[ 'id' ],
  460. 'qrcode_path' => 'upload/qrcode/form',
  461. 'qrcode_name' => 'form_qrcode_' . $qrcode_param[ 'id' ] . '_' . $site_id
  462. ];
  463. $solitaire = event('ExtensionInformation', $params, true);
  464. return $this->success($solitaire);
  465. }
  466. }