FormController.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. <?php
  2. namespace addons\SmartForm\backend\controllers;
  3. use Yii;
  4. use common\controllers\AddonsController;
  5. use common\enums\StatusEnum;
  6. use common\enums\DiyEnum;
  7. use common\helpers\FormatHelper;
  8. use common\enums\RabbitMqEnum;
  9. use common\models\user\User;
  10. use PHPUnit\Util\Exception;
  11. use addons\SmartForm\common\enums\SmartFormEnum;
  12. use addons\SmartForm\common\models\AddonsSmartFormTemplate; # 装修表单
  13. use addons\SmartForm\common\models\AddonsSmartFormTemplateStatistics; # 表单统计
  14. use addons\SmartForm\common\models\AddonsSmartFormDecorationForm; # 表单管理
  15. use addons\SmartForm\common\models\AddonsSmartFormTemplateColumn; #模板字段
  16. use addons\SmartForm\common\models\AddonsSmartFormTemplateUserBehavior; # 用户行为
  17. use addons\SmartForm\common\models\AddonsSmartFormTemplateUserForm;
  18. use addons\SmartForm\common\models\AddonsSmartFormTemplateUserFormInfo;
  19. use common\models\common\Region;
  20. use common\enums\DownloadEnum;
  21. use common\helpers\csv\CsvExportHelper;
  22. use yii\helpers\ArrayHelper;
  23. /**
  24. * 表单管理
  25. *
  26. * Class DefaultController
  27. * @package addons\SmartForm\backend\controllers
  28. */
  29. class FormController extends BaseController
  30. {
  31. public $enableCsrfValidation = false;
  32. /**
  33. * @method actionTemplate
  34. * 获取模板
  35. * @author zqh
  36. * @datetime 2022-05-07T01:58:17+0800
  37. * @return [type] [description]
  38. */
  39. public function actionTemplate()
  40. {
  41. try {
  42. $request = Yii::$app->request;
  43. if ($request->isGet) {
  44. $get = Yii::$app->request->get();
  45. # 检查提交的参数
  46. $res = Yii::$app->services->validate->validate($get,[
  47. [['template_id'], 'required'],
  48. ]);
  49. if($res === false){
  50. return $this->error(Yii::$app->services->validate->getErrorMessage());
  51. }
  52. if(!$get['template_id']){
  53. return $this->error('模板不存在');
  54. }
  55. $where[] = ['=', 'mall_id', $this->mall_id];
  56. $where[] = ['>=', 'status', StatusEnum::DISABLED];
  57. $where[] = ['=', 'id', $get['template_id']];
  58. $info = AddonsSmartFormTemplate::getOne($where, true, [
  59. 'columns'=> function ($query) {
  60. $query->where(['status' => StatusEnum::ENABLED])
  61. ->select('id,smart_form_template_id,smart_form_components_code,smart_form_components_id,title,tips,placeholder,required,limit,option,default,show_default,is_export,content_type')->orderBy('sort asc');
  62. }
  63. ]);
  64. if(!$info){
  65. return $this->error('模板不存在');
  66. }
  67. if($info['expire_time'] !=-1 && $info['expire_time'] < time()){
  68. return $this->error('模板已过期');
  69. }
  70. if($info['columns']){
  71. # 存在模板字段
  72. $columns = $info['columns'];
  73. $classname = new SmartFormEnum();
  74. foreach ($columns as $k => $v) {
  75. if(strpos($v['show_default'],SmartFormEnum::FIRST_CUTTER) !== false){
  76. $info['columns'][$k]['show_default'] = explode(SmartFormEnum::FIRST_CUTTER,$v['show_default']);
  77. }
  78. if(strpos($v['tips'],SmartFormEnum::FIRST_CUTTER) !== false){
  79. $info['columns'][$k]['tips'] = explode(SmartFormEnum::FIRST_CUTTER,$v['tips']);
  80. }
  81. if(strpos($v['option'],SmartFormEnum::FIRST_CUTTER) !== false){
  82. $option = [];
  83. $option = explode(SmartFormEnum::FIRST_CUTTER,$v['option']);
  84. $info['columns'][$k]['option'] = $option;
  85. foreach ($option as $k1 => $v1) {
  86. $info['columns'][$k]['chooseList'][$k1]['title'] = $v1;
  87. }
  88. }
  89. if(strpos($v['default'],SmartFormEnum::DATE_CUTTER) !== false){
  90. $default = [];
  91. $default = explode(SmartFormEnum::DATE_CUTTER,$v['default']);
  92. foreach ($default as $kd => $kv) {
  93. if(strpos($kv,SmartFormEnum::PARAM_CUTTER) !== false){
  94. list($f,$func,$p) = explode(SmartFormEnum::PARAM_CUTTER,$kv);
  95. $default[$kd] = call_user_func(array($classname, $func),$p);
  96. $info['columns'][$k]['show_now_start'][$kd] = 1;
  97. }else{
  98. $default[$kd] = $kv;
  99. $info['columns'][$k]['show_now_start'][$kd] = 0;
  100. }
  101. }
  102. $info['columns'][$k]['default'] = $default;
  103. }else{
  104. if($v['default']){
  105. if(strpos($v['default'],SmartFormEnum::PARAM_CUTTER) !== false){
  106. list($f,$func,$p) = explode(SmartFormEnum::PARAM_CUTTER,$v['default']);
  107. $default = call_user_func(array($classname, $func),$p);
  108. $info['columns'][$k]['show_now_start'] = 1;
  109. $info['columns'][$k]['default'] = $default;
  110. }else{
  111. $default = $v['default'];
  112. $info['columns'][$k]['show_now_start'] = 0;
  113. }
  114. }
  115. }
  116. }
  117. }
  118. # 新增访问次数
  119. $view['user_id'] = $this->user_id;
  120. $view['template_id'] = $get['template_id'];
  121. $view['type'] = 0;
  122. AddonsSmartFormTemplateStatistics::setTplStatistics($view);
  123. $this->success('获取成功', $info);
  124. }
  125. } catch (\Exception $e) {
  126. $exception = FormatHelper::exception($e, "添加模板失败");
  127. Yii::error($exception);
  128. return $this->error($exception);
  129. }
  130. }
  131. /**
  132. * @method actionSubmitForm
  133. * 用户提交表单
  134. * @author zqh
  135. * @datetime 2022-05-07T09:58:57+0800
  136. * @return [type] [description]
  137. */
  138. public function actionSubmitForm()
  139. {
  140. try {
  141. $request = Yii::$app->request;
  142. if ($request->isAjax) {
  143. if ($request->isPost) {
  144. $post = Yii::$app->request->post();
  145. $json = '{"form":{"id":91,"source":0},"column":[{"key":652,"value":"2022-05-08 ~ 2022-05-10"},{"key":653,"value":"[0,1,2]"},{"key":654,"value":"身份证"}]}';
  146. $post = json_decode($json,JSON_UNESCAPED_UNICODE);
  147. $trans = Yii::$app->db->beginTransaction();
  148. $post['mall_id'] = $this->mall_id;
  149. $post['user_id'] = $this->user_id;
  150. # 保存模板
  151. $status = AddonsSmartFormTemplateUserForm::saveUserForm($post);
  152. if(!$status){
  153. Yii::error(AddonsSmartFormTemplate::getStaticError());
  154. return $this->error(AddonsSmartFormTemplate::getStaticError());
  155. }
  156. $trans->commit();
  157. # 填写成功后执行
  158. # 新增访问次数
  159. $view['user_id'] = $this->user_id;
  160. $view['template_id'] = $post['form']['id'];
  161. $view['type'] = 1;
  162. AddonsSmartFormTemplateStatistics::setTplStatistics($view);
  163. return $this->success('操作成功');
  164. }
  165. }
  166. } catch (\Exception $e) {
  167. $trans->rollBack();
  168. $exception = FormatHelper::exception($e, "添加模板失败");
  169. Yii::error($exception);
  170. return $this->error($exception);
  171. }
  172. }
  173. /**
  174. * @method actionIndex
  175. * 首页
  176. * @author zqh
  177. * @datetime 2022-04-27T16:30:11+0800
  178. * @return [type] [description]
  179. */
  180. public function actionIndex()
  181. {
  182. try {
  183. $request = Yii::$app->request;
  184. if ($request->isAjax) {
  185. if ($request->isGet) {
  186. $get = Yii::$app->request->get();
  187. // 检查提交的参数
  188. $res = Yii::$app->services->validate->validate($get,[
  189. [['id','keyword'], 'safe'],
  190. [['limit'],'default','value' => '15'],
  191. ]);
  192. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  193. # 搜索条件
  194. $where = [];
  195. if($get['keyword']) $where[] = ['like', 'title', $get['keyword']];
  196. $where[] = ['>=', 'status', StatusEnum::DISABLED];
  197. $where[] = ['=', 'mall_id', $this->mall_id];
  198. # 排序
  199. $order = 'created_at desc';
  200. # 显示字段
  201. $select = "id,mall_id,diy_page_id,title,header,expire_time,status,created_at";
  202. $params = array('where' => $where, 'order'=>$order, 'select'=>$select, 'limit' => $get['limit']);
  203. # 分页返回
  204. $list = AddonsSmartFormTemplate::listPage($params, false, true);
  205. $this->success('获取成功', $list);
  206. }
  207. } else {
  208. return $this->render($this->action->id,[
  209. ]);
  210. }
  211. } catch (\Exception $e) {
  212. return $this->error($e->getMessage(), []);
  213. }
  214. }
  215. /**
  216. * @method actionAdd
  217. * 新增
  218. * @author zqh
  219. * @datetime 2022-04-27T16:30:05+0800
  220. * @return [type] [description]
  221. */
  222. public function actionAdd()
  223. {
  224. try {
  225. $request = Yii::$app->request;
  226. if ($request->isAjax) {
  227. if ($request->isPost) {
  228. $post = Yii::$app->request->post();
  229. // 检查提交的参数
  230. // $classname = new SmartFormEnum();
  231. // $s = call_user_func(array($classname, 'showTime'),'now');
  232. $json = '{"form":{"id":null,"title":"是的SV","show_heard":0,"first_img":"","time_type":1,"date":"","link_status":0,"jump_type":1,"link":"","qcard_img":""},"toolList":[{"type":"single-link-text","title":"胜多负少","placeholder":"第三方","required":1,"tips":"第三方","components_id":31}]}';
  233. // $post = json_decode($json,true);
  234. $trans = Yii::$app->db->beginTransaction();
  235. try {
  236. $post['mall_id'] = $this->mall_id;
  237. # 保存模板
  238. $status = AddonsSmartFormTemplate::createTemplate($post);
  239. if(!$status){
  240. Yii::error(AddonsSmartFormTemplate::getStaticError());
  241. return $this->error(AddonsSmartFormTemplate::getStaticError());
  242. }
  243. $trans->commit();
  244. return $this->success('操作成功');
  245. } catch (\Exception $e) {
  246. $trans->rollBack();
  247. $exception = FormatHelper::exception($e, "添加模板失败");
  248. Yii::error($exception);
  249. return $this->error($exception);
  250. }
  251. }
  252. }else{
  253. return $this->render($this->action->id,[]);
  254. }
  255. } catch (\Exception $e) {
  256. return $this->error($e->getMessage(), []);
  257. }
  258. // print_r($this->mall_id);exit;
  259. }
  260. /**
  261. * @method actionInfo
  262. * 详情
  263. * @author zqh
  264. * @datetime 2022-04-27T16:30:05+0800
  265. * @return [type] [description]
  266. */
  267. public function actionInfo()
  268. {
  269. /*$s = AddonsSmartFormTemplateUserForm::exportHandle(['download_id'=>7]);
  270. var_dump($s);exit;*/
  271. // try {
  272. $request = Yii::$app->request;
  273. if ($request->isAjax) {
  274. if ($request->isGet) {
  275. $get = Yii::$app->request->get();
  276. // 检查提交的参数
  277. $res = Yii::$app->services->validate->validate($get,[
  278. [['template_id','keyword','date_start','date_end'], 'safe'],
  279. [['limit'],'default','value' => '15'],
  280. ]);
  281. if(!$get['template_id']){
  282. return $this->error('模板不存在', []);
  283. }
  284. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  285. # 获取统计数据
  286. # 今日填写次数-当天填写次数,包含同一个ID用户填写次数
  287. $today_filling_times = AddonsSmartFormTemplateUserBehavior::find()->where(['smart_form_template_id'=>$get['template_id'],'month_day'=>date('Y-m-d'),'behavior_type'=>1])->count('id');
  288. # 今日访问人次-当天访问次数,只访问未填写
  289. $today_view_times = AddonsSmartFormTemplateUserBehavior::find()->where(['smart_form_template_id'=>$get['template_id'],'month_day'=>date('Y-m-d'),'behavior_type'=>0,'is_submit'=>0])->count('id');
  290. # 今日填写人数-当天填写人数,一个ID用户统计一次
  291. $today_filling_people = AddonsSmartFormTemplateUserBehavior::find()
  292. ->select('user_id,month_day')
  293. ->where(['smart_form_template_id'=>$get['template_id'],'month_day'=>date('Y-m-d'),'behavior_type'=>1])
  294. ->groupBy('user_id,month_day')
  295. ->count('user_id');
  296. # 总人数/填写人数x100%=填写率
  297. $today_rate = (string)($today_filling_people > 0 ? ($today_view_times/$today_filling_people) : 0) . '%';
  298. $statistics = AddonsSmartFormTemplateStatistics::getOne([['smart_form_template_id'=>$get['template_id']]], true,[],false,'filling_times,view_times,filling_people');
  299. // print_r($statistics);exit;
  300. if(!$statistics){
  301. $all_rate = '0'. '%';
  302. $statistics['filling_times'] = 0;
  303. $statistics['view_times'] = 0;
  304. $statistics['filling_people'] = 0;
  305. }else{
  306. $all_rate = (string)($statistics['filling_people'] > 0 ? ($statistics['view_times']/$statistics['filling_people']) : 0) . '%';
  307. }
  308. $statistics['today_filling_times'] = $today_filling_times;
  309. $statistics['today_view_times'] = $today_view_times;
  310. $statistics['today_filling_people'] = $today_filling_people;
  311. $statistics['today_rate'] = $today_rate;
  312. $statistics['all_rate'] = $all_rate;
  313. # 获取模板字段
  314. $field = " id,concat('a',`id`) type,smart_form_components_code,title label";
  315. $header = AddonsSmartFormTemplateColumn::find()->select($field)
  316. ->orderBy('sort asc')
  317. ->where(['smart_form_template_id'=>$get['template_id']])->asArray()->all();
  318. # 搜索条件
  319. $where = [];
  320. $where[] = ['=', 'a.smart_form_template_id', $get['template_id']];
  321. if($get['date_start'] && $get['date_end']){
  322. $where[] = ['between', 'a.created_at', strtotime($get['date_start']),strtotime($get['date_end'])];
  323. }
  324. if($get['keyword']){
  325. $user_ids = User::find()->where(['and',['mall_id' => $this->mall_id], ['like', 'nickname', $get['keyword']]])
  326. ->select('id')->asArray()->all();
  327. $user_ids = array_column($user_ids, 'id');
  328. $user_ids = $user_ids ?? [];
  329. $where[] = ['user_id' => $user_ids];
  330. }
  331. $where[] = ['>=', 'a.status', StatusEnum::DISABLED];
  332. # 排序
  333. $order = 'a.created_at desc';
  334. # 显示字段
  335. $select = "a.id,a.user_id,a.smart_form_template_column_id,a.col_value,a.source,a.created_at,a.updated_at";//,c.smart_form_components_id,c.smart_form_components_code,c.col_value
  336. //
  337. $params = array('where' => $where, 'order'=>$order, 'select'=>$select, 'limit' => 10,'alias'=>'a','join'=>[
  338. // ['INNER JOIN', User::tableName() . ' b', "b.id = a.user_id"]
  339. // ,['INNER JOIN', AddonsSmartFormTemplateUserFormInfo::tableName() . ' c', "c.smart_form_template_user_form_id = a.id"]
  340. ]);
  341. # 分页返回
  342. $list = AddonsSmartFormTemplateUserForm::listPage($params, false);
  343. foreach ($list['list'] as $k => $v) {
  344. $users = User::findOne($v["user_id"]);
  345. $nickname = "普通用户";
  346. if(!empty($users)){
  347. $nickname = $users->nickname;
  348. $users = ArrayHelper::toArray($users);
  349. $list['list'][$k]["user"] = $users;
  350. }
  351. $list['list'][$k]["nickname"] = $nickname;
  352. $formInfo = AddonsSmartFormTemplateUserFormInfo::find()->where(["smart_form_template_user_form_id" => $v["id"]])->asArray()->all();
  353. if(!empty($formInfo)){
  354. $list['list'][$k]["formInfo"] = $formInfo;
  355. $template_columns = AddonsSmartFormTemplateColumn::find()
  356. ->where(['and',['in', 'id', array_column($formInfo, 'smart_form_components_id')]])
  357. ->select('id, option')
  358. ->asArray()
  359. ->all();
  360. $template_columns = array_column($template_columns, 'option', 'id');
  361. foreach ($formInfo as $k1 => $v1) {
  362. $v1['option'] = isset($template_columns[$v1['smart_form_components_id']]) ? $template_columns[$v1['smart_form_components_id']] : [];
  363. switch ($v1['smart_form_components_code']) {
  364. case 'id-card':
  365. case 'single-link-text':
  366. $v1['col_value'] = (string)$v1['col_value'] . ' ' ;
  367. break;
  368. case 'upload-photo':
  369. // $col_value = json_decode($v1['col_value'],true);
  370. $v1['col_value'] = explode(',',$v1['col_value']);
  371. break;
  372. case 'city-chose':
  373. # 省市区
  374. // $regin = Region::getOne([['id' => [2,3,4]]], true,[],false,'name');
  375. $regin = Region::find()->select('name')->where(['id' => explode(',',$v1['col_value'])])->asArray()->all();
  376. $regin = implode(" ",array_column($regin,'name'));//Region::getTopStr($v1['col_value']);
  377. $v1['col_value'] = $regin;
  378. break;
  379. case 'checked-tool':
  380. # 多选
  381. $col_value = explode(',',$v1['col_value']);
  382. if(strpos($v1['option'],SmartFormEnum::FIRST_CUTTER) !== false){
  383. $col_option = explode(SmartFormEnum::FIRST_CUTTER,$v1['option']);
  384. }else{
  385. $col_option = (array)$v1['option'];
  386. }
  387. $new_col_option = [];
  388. if(is_array($col_value)){
  389. foreach ($col_value as $ok => $ov) {
  390. if($col_option[$ov]){
  391. $new_col_option[] = $col_option[$ov];
  392. }
  393. }
  394. }else{
  395. $new_col_option[0] = $col_option[0];
  396. }
  397. $v1['col_value'] = implode(',',$new_col_option);
  398. break;
  399. case 'radio-tool':
  400. # 单选
  401. // $col_value = json_decode($v1['col_value'],true);
  402. $col_value = explode(',',$v1['col_value']);
  403. if(strpos($v1['option'],SmartFormEnum::FIRST_CUTTER) !== false){
  404. $col_option = explode(SmartFormEnum::FIRST_CUTTER,$v1['option']);
  405. }else{
  406. $col_option = (array)$v1['option'];
  407. }
  408. $new_col_option = [];
  409. if(is_array($col_value)){
  410. foreach ($col_value as $ok => $ov) {
  411. if($col_option[$ov]){
  412. $new_col_option[] = $col_option[$ov];
  413. }
  414. }
  415. }else{
  416. $new_col_option[0] = $col_option[0];
  417. }
  418. $v1['col_value'] = implode(',',$new_col_option);
  419. break;
  420. case 'drow-box':
  421. # 下拉
  422. // $col_value = json_decode($v1['col_value'],true);
  423. $col_value = explode(',',$v1['col_value']);
  424. if(strpos($v1['option'],SmartFormEnum::FIRST_CUTTER) !== false){
  425. $col_option = explode(SmartFormEnum::FIRST_CUTTER,$v1['option']);
  426. }else{
  427. $col_option = (array)$v1['option'];
  428. }
  429. $new_col_option = [];
  430. if(is_array($col_value)){
  431. foreach ($col_value as $ok => $ov) {
  432. if($col_option[$ov]){
  433. $new_col_option[] = $col_option[$ov];
  434. }
  435. }
  436. }else{
  437. $new_col_option[0] = $col_option[0];
  438. }
  439. $v1['col_value'] = implode(',',$new_col_option);
  440. break;
  441. }
  442. $list['list'][$k]['a'.$v1['smart_form_components_id']] = $v1['col_value'];
  443. }
  444. }
  445. }
  446. $export_list = AddonsSmartFormTemplateUserForm::exportFieldsList($get['template_id']);
  447. $return['statistics'] = $statistics;
  448. $return['header'] = $header;
  449. $return['list'] = $list;
  450. $return['export_list'] = $export_list;
  451. // print_r($return);exit;
  452. $this->success('获取成功', $return);
  453. }
  454. if ($request->post('flag') == 'EXPORT') {
  455. $post = $request->post();
  456. $post['mall_id'] = $this->mall_id;
  457. $post['template_id'] = $post['template_id'];
  458. $post['fields'] = AddonsSmartFormTemplateUserForm::exportFieldsList($post['template_id']);
  459. $info = AddonsSmartFormTemplate::getOne([['id' => $post['template_id']]], true);
  460. $filename = $info['title'] .'-' . date('YmdHis').'_'.rand(10000,99999);
  461. $res = CsvExportHelper::exportDownLoadCenter($this->mall_id,$filename,DownloadEnum::TYPE_AGENT,AddonsSmartFormTemplateUserForm::class,'exportHandle',$post);
  462. if ($res === false) return $this->error('加入导出任务失败'.CsvExportHelper::getStaticError());
  463. return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
  464. }
  465. } else {
  466. return $this->render($this->action->id,[
  467. ]);
  468. }
  469. // } catch (\Exception $e) {
  470. // return $this->error($e->getMessage(), []);
  471. // }
  472. }
  473. /**
  474. * @method editIndex
  475. * 编辑
  476. * @author zqh
  477. * @datetime 2022-04-27T16:30:05+0800
  478. * @return [type] [description]
  479. */
  480. public function actionEdit()
  481. {
  482. try {
  483. $request = Yii::$app->request;
  484. if ($request->isAjax) {
  485. if ($request->isPost) {
  486. # 修改信息
  487. $post = Yii::$app->request->post();
  488. # 判断表单是否已经填写
  489. $hasUse = AddonsSmartFormTemplateStatistics::checkTplUse($post['form']['id']);
  490. $hasUse1 = AddonsSmartFormDecorationForm::checkTplUse($post['form']['id']);
  491. if ($hasUse || $hasUse1) {
  492. return $this->error('表单已使用不能修改', []);
  493. }
  494. // 检查提交的参数
  495. // $classname = new SmartFormEnum();
  496. // $s = call_user_func(array($classname, 'showTime'),'now');
  497. $json = '{"form":{"id":56,"title":"第一次","show_heard":1,"first_img":"header","time_type":0,"date":"2022-05-28 00:00","link_status":1,"jump_type":1,"link":"递四方速递","success_link":"success_link","qcard_img":""},"toolList":[{"components_id":1,"type":"single-link-text","title":"递四方速递","placeholder":"递四方速递","required":1,"tips":"递四方速递"},{"components_id":1,"type":"multi-text","title":"电风扇","placeholder":"电风扇","required":1,"tips":"胜多负少"},{"components_id":1,"type":"id-card","title":"水电费","required":0,"tips":"胜多负少"},{"components_id":1,"type":"drow-box","title":"胜多负少","required":1,"chooseList":[{"title":"选项一第三方"},{"title":"选项二是的发送到"}]},{"components_id":1,"type":"checked-tool","title":"鼎折覆餗","required":1,"chooseList":[{"title":"选项一胜多负少"},{"title":"选项二发生的"},{"title":"鼎折覆餗"}]},{"components_id":1,"type":"radio-tool","title":"水电费","required":0,"chooseList":[{"title":"选项一是的发送到"},{"title":"选项二递四方速递"}]},{"components_id":1,"type":"upload-photo","title":"递四方速递","required":1,"total":3},{"components_id":1,"type":"date-tool","title":"鼎折覆餗","required":1,"tips":"胜多负少","is_show":1,"date":"2022-05-19","show_now_day":0},{"components_id":1,"type":"date-range","title":"第三方","startTitle":"是的发送到","endTitle":"水电站发送到","required":1,"is_start_show":1,"is_end_show":1,"show_now_start":1,"show_now_end":0,"start_date":"","end_date":"2022-05-04"},{"components_id":1,"type":"time-tool","title":"胜多负少","required":1,"tips":"胜多负少","is_show":1,"time":"","show_now_time":1},{"components_id":1,"type":"time-range","title":"","startTitle":"","endTitle":"","required":1,"is_start_show":1,"is_end_show":1,"start_time":"","show_now_start":1,"show_now_end":1,"end_time":""},{"components_id":1,"type":"city-chose","title":"鼎折覆餗","required":1,"city_type":1}]}';
  498. $trans = Yii::$app->db->beginTransaction();
  499. try {
  500. $post['mall_id'] = $this->mall_id;
  501. # 保存模板
  502. $status = AddonsSmartFormTemplate::createTemplate($post);
  503. if(!$status){
  504. Yii::error(AddonsSmartFormTemplate::getStaticError());
  505. return $this->error(AddonsSmartFormTemplate::getStaticError());
  506. }
  507. $trans->commit();
  508. return $this->success('操作成功');
  509. } catch (\Exception $e) {
  510. $trans->rollBack();
  511. $exception = FormatHelper::exception($e, "添加模板失败");
  512. Yii::error($exception);
  513. return $this->error($exception);
  514. }
  515. }else{
  516. # 获取信息
  517. $get = Yii::$app->request->get();
  518. $template_id = $get['template_id'];
  519. $info = AddonsSmartFormTemplate::getTplInfo($template_id);
  520. if(!$info){
  521. Yii::error(AddonsSmartFormTemplate::getStaticError());
  522. return $this->error(AddonsSmartFormTemplate::getStaticError());
  523. }
  524. return $this->success('获取成功', $info);
  525. }
  526. }else{
  527. return $this->render('edit',[
  528. ]);
  529. }
  530. } catch (\Exception $e) {
  531. return $this->error($e->getMessage(), []);
  532. }
  533. }
  534. /**
  535. * @method share
  536. * 分享
  537. * @author zqh
  538. * @datetime 2022-04-27T16:32:09+0800
  539. * @return [type] [description]
  540. */
  541. public function actionShare()
  542. {
  543. try {
  544. $request = Yii::$app->request;
  545. if ($request->isAjax) {
  546. if ($request->isPost) {
  547. $post = Yii::$app->request->post();
  548. if(!$post['template_id']){
  549. return $this->error('模板不存在', []);
  550. }
  551. $template_id = $post['template_id'];
  552. $h5_url = Yii::$app->services->setting->get('system.h5_url') ?? '';
  553. $sign = $this->mall['mall_sign'];
  554. if ($h5_url) {
  555. $share_url = $h5_url .'/#'. DiyEnum::SMART_FORM_PAGE . '?sign=' . $sign . '&template_id=' . $template_id;
  556. }else{
  557. $share_url = '';
  558. }
  559. return $this->success('操作成功',$share_url);
  560. }
  561. }
  562. } catch (\Exception $e) {
  563. return $this->error($e->getMessage(), []);
  564. }
  565. }
  566. /**
  567. * @method copy
  568. * 复制
  569. * @author zqh
  570. * @datetime 2022-04-27T16:32:09+0800
  571. * @return [type] [description]
  572. */
  573. public function actionCopy()
  574. {
  575. try {
  576. $request = Yii::$app->request;
  577. if ($request->isAjax) {
  578. if ($request->isPost) {
  579. $post = Yii::$app->request->post();
  580. $template_id = $post['template_id'];
  581. $trans = Yii::$app->db->beginTransaction();
  582. $s = AddonsSmartFormTemplate::copyTpl($template_id);
  583. if(!$s){
  584. $trans->rollBack();
  585. Yii::error(AddonsSmartFormTemplate::getStaticError());
  586. return $this->error(AddonsSmartFormTemplate::getStaticError());
  587. }
  588. $trans->commit();
  589. return $this->success('操作成功');
  590. }
  591. }
  592. } catch (\Exception $e) {
  593. return $this->error($e->getMessage(), []);
  594. }
  595. }
  596. public function actionDeleteInfo()
  597. {
  598. try {
  599. $request = Yii::$app->request;
  600. if ($request->isAjax) {
  601. if (\Yii::$app->request->isPost) {
  602. $params = \Yii::$app->request->post();
  603. $res = Yii::$app->services->validate->validate($params, [
  604. [['id'], 'required'],
  605. ]);
  606. # 删除 用户提交表单
  607. $model2 = AddonsSmartFormTemplateUserForm::findOne(['id'=>$params['id']]);
  608. if($model2){
  609. $s2 = AddonsSmartFormTemplateUserForm::updateAll(['status'=>StatusEnum::DELETE],['id'=>$params['id']]);
  610. }
  611. if(!$s2){
  612. return $this->error('删除失败!', []);
  613. }
  614. return $this->success('操作成功!');
  615. }
  616. }
  617. } catch (\Exception $e) {
  618. return $this->error($e->getMessage(), []);
  619. }
  620. }
  621. /**
  622. * @method actionDelete
  623. * 删除
  624. * @author zqh
  625. * @datetime 2022-04-27T16:35:31+0800
  626. * @return [type] [description]
  627. */
  628. public function actionDelete()
  629. {
  630. try {
  631. $request = Yii::$app->request;
  632. if ($request->isAjax) {
  633. if ($request->isPost) {
  634. $params = \Yii::$app->request->post();
  635. $res = Yii::$app->services->validate->validate($params, [
  636. [['id'], 'required'],
  637. ]);
  638. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  639. $params['mall_id'] = $this->mall_id;
  640. $params['status'] = StatusEnum::DELETE;
  641. $res = AddonsSmartFormTemplate::del($params);
  642. if ($res === false) {
  643. return $this->error(AddonsSmartFormTemplate::getStaticError());
  644. }
  645. return $this->success('操作成功');
  646. }
  647. }
  648. } catch (\Exception $e) {
  649. return $this->error($e->getMessage(), []);
  650. }
  651. }
  652. /**
  653. * @return mixed|null
  654. */
  655. public function actionSmsKeys()
  656. {
  657. return $this->success('获取成功', SmartFormEnum::SMS_KEYS);
  658. }
  659. }