RenovationController.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. <?php
  2. namespace addons\SmartForm\backend\controllers;
  3. use Yii;
  4. use common\enums\DiyEnum;
  5. use common\enums\StatusEnum;
  6. use common\controllers\AddonsController;
  7. use common\models\diy\DiyPage;
  8. use common\models\user\User;
  9. use addons\SmartForm\common\enums\SmartFormEnum;
  10. use addons\SmartForm\common\models\AddonsSmartFormTemplate; # 装修表单
  11. use addons\SmartForm\common\models\AddonsSmartFormTemplateStatistics; # 表单统计
  12. use addons\SmartForm\common\models\AddonsSmartFormDecorationForm; # 表单管理
  13. use addons\SmartForm\common\models\AddonsSmartFormTemplateColumn; #模板字段
  14. use addons\SmartForm\common\models\AddonsSmartFormTemplateUserBehavior; # 用户行为
  15. use addons\SmartForm\common\models\AddonsSmartFormTemplateUserForm;
  16. use addons\SmartForm\common\models\AddonsSmartFormTemplateUserFormInfo;
  17. use common\helpers\ArrayHelper;
  18. use common\models\common\Region;
  19. /**
  20. * 表单管理
  21. *
  22. * Class DefaultController
  23. * @package addons\SmartForm\backend\controllers
  24. */
  25. class RenovationController extends BaseController
  26. {
  27. public $enableCsrfValidation = false;
  28. /**
  29. * 首页
  30. *
  31. * @return string
  32. */
  33. public function actionIndex()
  34. {
  35. try {
  36. $request = Yii::$app->request;
  37. if ($request->isAjax) {
  38. if ($request->isGet) {
  39. $get = Yii::$app->request->get();
  40. // 检查提交的参数
  41. $res = Yii::$app->services->validate->validate($get,[
  42. [['id','keyword'], 'safe'],
  43. [['limit'],'default','value' => '15'],
  44. ]);
  45. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  46. # 搜索条件
  47. $where = [];
  48. // if($get['keyword']) $where[] = ['like', 'a.title', $get['keyword']];
  49. // $where[] = ['>=', 'a.status', StatusEnum::DISABLED];
  50. $where[] = ['>=', 'b.status', StatusEnum::DISABLED];
  51. $where[] = ['=', 'mall_id', $this->mall_id];
  52. # 排序
  53. $order = 'b.created_at desc';
  54. # 显示字段
  55. $select = "b.smart_form_template_id,b.id,b.mall_id,b.diy_page_id";
  56. $params = array('where' => $where, 'order'=>$order, 'select'=>$select, 'limit' => $get['limit'],'alias'=>'b'
  57. ,
  58. /*'join'=>[
  59. ['INNER JOIN', AddonsSmartFormTemplate::tableName() . ' a', "b.smart_form_template_id = a.id AND b.mall_id = a.mall_id"],
  60. ['INNER JOIN', DiyPage::tableName() . ' c', "c.id = b.diy_page_id"]
  61. ]*/
  62. );
  63. # 分页返回
  64. // $list = AddonsSmartFormTemplate::listPage($params, false, true);
  65. $list = AddonsSmartFormDecorationForm::listPage($params, false, true);
  66. if(!empty($list['list'])){
  67. foreach ($list['list'] as $k => $v) {
  68. $formTemplate = AddonsSmartFormTemplate::getOne([['id' => $v['smart_form_template_id'],'mall_id' => $v['mall_id'],'status' => StatusEnum::ENABLED]], true);
  69. $list['list'][$k]['title'] = $formTemplate['title'];
  70. $list['list'][$k]['header'] = $formTemplate['header'];
  71. $list['list'][$k]['expire_time'] = $formTemplate['expire_time'];
  72. $list['list'][$k]['status'] = $formTemplate['status'];
  73. $list['list'][$k]['created_at'] = $formTemplate['created_at'];
  74. $diy_page = DiyPage::getOne([['id'=>$v['diy_page_id']]], true);
  75. $list['list'][$k]['diy_page_title'] = $diy_page['title'];
  76. }
  77. }
  78. $this->success('获取成功', $list);
  79. }
  80. } else {
  81. return $this->render($this->action->id,[
  82. ]);
  83. }
  84. } catch (\Exception $e) {
  85. return $this->error($e->getMessage(), []);
  86. }
  87. }
  88. /**
  89. * @method actionInfo
  90. * 详情
  91. * @author zqh
  92. * @datetime 2022-04-27T16:30:05+0800
  93. * @return [type] [description]
  94. */
  95. public function actionInfo()
  96. {
  97. /*$s = AddonsSmartFormTemplateUserForm::exportHandle(['download_id'=>7]);
  98. var_dump($s);exit;*/
  99. try {
  100. $request = Yii::$app->request;
  101. if ($request->isAjax) {
  102. if ($request->isGet) {
  103. $get = Yii::$app->request->get();
  104. // 检查提交的参数
  105. $res = Yii::$app->services->validate->validate($get,[
  106. [['diy_page_id','template_id','keyword','date_start','date_end'], 'safe'],
  107. [['limit'],'default','value' => '15'],
  108. ]);
  109. if(!$get['template_id']){
  110. return $this->error('模板不存在', []);
  111. }
  112. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  113. # 获取统计数据
  114. $where1['smart_form_template_id'] = $get['template_id'];
  115. $where1['diy_page_id'] = $get['diy_page_id'];
  116. $where1['month_day'] = date('Y-m-d');
  117. $where1['behavior_type'] = 1;
  118. # 今日填写次数-当天填写次数,包含同一个ID用户填写次数
  119. $today_filling_times = AddonsSmartFormTemplateUserBehavior::find()->where($where1)->count('id');
  120. # 今日访问人次-当天访问次数,只访问未填写
  121. $where2['smart_form_template_id'] = $get['template_id'];
  122. $where2['diy_page_id'] = $get['diy_page_id'];
  123. $where2['month_day'] = date('Y-m-d');
  124. $where2['behavior_type'] = 0;
  125. $where2['is_submit'] = 0;
  126. $today_view_times = AddonsSmartFormTemplateUserBehavior::find()->where($where2)->count('id');
  127. # 今日填写人数-当天填写人数,一个ID用户统计一次
  128. $where3['smart_form_template_id'] = $get['template_id'];
  129. $where3['diy_page_id'] = $get['diy_page_id'];
  130. $where3['month_day'] = date('Y-m-d');
  131. $where3['behavior_type'] = 1;
  132. $today_filling_people = AddonsSmartFormTemplateUserBehavior::find()
  133. ->select('user_id,month_day')
  134. ->where($where3)
  135. ->groupBy('user_id,month_day')
  136. ->count('user_id');
  137. // print_r($today_filling_people);exit;
  138. # 总人数/填写人数x100%=填写率
  139. $today_rate = (string)($today_filling_people > 0 ? ($today_view_times/$today_filling_people) : 0) . '%';
  140. # 获取统计数据
  141. $whereAll1['smart_form_template_id'] = $get['template_id'];
  142. $whereAll1['diy_page_id'] = $get['diy_page_id'];
  143. $whereAll1['behavior_type'] = 1;
  144. # 总填写次数- 填写次数,包含同一个ID用户填写次数
  145. $all_filling_times = AddonsSmartFormTemplateUserBehavior::find()->where($whereAll1)->count('id');
  146. # 总访问人次- 访问次数,只访问未填写
  147. $whereAll2['smart_form_template_id'] = $get['template_id'];
  148. $whereAll2['diy_page_id'] = $get['diy_page_id'];
  149. $whereAll2['behavior_type'] = 0;
  150. $whereAll2['is_submit'] = 0;
  151. $all_view_times = AddonsSmartFormTemplateUserBehavior::find()->where($whereAll2)->count('id');
  152. # 总填写人数- 填写人数,一个ID用户统计一次
  153. $whereAll3['smart_form_template_id'] = $get['template_id'];
  154. $whereAll3['diy_page_id'] = $get['diy_page_id'];
  155. $whereAll3['behavior_type'] = 1;
  156. $all_filling_people = AddonsSmartFormTemplateUserBehavior::find()
  157. ->select('user_id')
  158. ->where($whereAll3)
  159. ->groupBy('user_id')
  160. ->count('user_id');
  161. // print_r($all_filling_people);exit;
  162. $all_rate = (string)($all_filling_people > 0 ? ($all_view_times/$all_filling_people) : 0) . '%';
  163. $statistics['filling_times'] = $all_filling_times;
  164. $statistics['view_times'] = $all_view_times;
  165. $statistics['filling_people'] = $all_filling_people;
  166. $statistics['all_rate'] = $all_rate;
  167. $statistics['today_filling_times'] = $today_filling_times;
  168. $statistics['today_view_times'] = $today_view_times;
  169. $statistics['today_filling_people'] = $today_filling_people;
  170. $statistics['today_rate'] = $today_rate;
  171. $statistics['all_rate'] = $all_rate;
  172. # 获取模板字段
  173. $field = " id,concat('a',`id`) type,smart_form_components_code,title label";
  174. $header = AddonsSmartFormTemplateColumn::find()->select($field)
  175. ->orderBy('sort asc')
  176. ->where(['smart_form_template_id'=>$get['template_id']])->asArray()->all();
  177. # 搜索条件
  178. $where = [];
  179. $where[] = ['=', 'a.smart_form_template_id', $get['template_id']];
  180. if($get['date_start'] && $get['date_end']){
  181. $where[] = ['between', 'a.created_at', strtotime($get['date_start']),strtotime($get['date_end'])];
  182. }
  183. // if($get['keyword']) $where[] = ['like', 'b.nickname', $get['keyword']];
  184. if($get['keyword']){
  185. $user_ids = User::find()
  186. ->where(['and', ['like', 'nickname', $get['keyword']]])
  187. ->select('id')
  188. ->asArray()
  189. ->all();
  190. $where[] = ['in', 'a.user_id', array_column($user_ids, 'id')];
  191. }
  192. $where[] = ['>=', 'a.status', StatusEnum::DISABLED];
  193. $where[] = ['=', 'a.diy_page_id', $get['diy_page_id']];
  194. # 排序
  195. $order = 'a.created_at desc';
  196. # 显示字段
  197. $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
  198. // $with = [
  199. // 'user'
  200. // ,'formInfo' => function($query){
  201. // $query->alias('a')->select('a.*,b.option')->join('inner join',AddonsSmartFormTemplateColumn::tableName() . 'b', "b.id = a.smart_form_components_id");
  202. // }
  203. // ];
  204. // $params = array('where' => $where, 'order'=>$order, 'select'=>$select, 'limit' => 10, 'with' => $with,'alias'=>'a','join'=>[
  205. // ['INNER JOIN', User::tableName() . ' b', "b.id = a.user_id"]
  206. // ]);
  207. $params = array('where' => $where, 'order'=>$order, 'select'=>$select, 'limit' => 10, 'alias'=>'a');
  208. # 分页返回
  209. $list = AddonsSmartFormTemplateUserForm::listPage($params, false);
  210. foreach ($list['list'] as $k => $v) {
  211. $users = User::findOne($v["user_id"]);
  212. $nickname = "普通用户";
  213. if(!empty($users)){
  214. $nickname = $users->nickname;
  215. $users = ArrayHelper::toArray($users);
  216. $list['list'][$k]["user"] = $users;
  217. }
  218. $list['list'][$k]["nickname"] = $nickname;
  219. $v['formInfo'] = AddonsSmartFormTemplateUserFormInfo::find()->where(["smart_form_template_user_form_id" => $v["id"]])->asArray()->all();
  220. if(!$v['formInfo']){
  221. $v['formInfo'] = [];
  222. }
  223. $template_columns = AddonsSmartFormTemplateColumn::find()
  224. ->where(['and',['in', 'id', array_column($v['formInfo'], 'smart_form_components_id')]])
  225. ->select('id, option')
  226. ->asArray()
  227. ->all();
  228. $template_columns = array_column($template_columns, 'option', 'id');
  229. foreach ($v['formInfo'] as $k1 => $v1) {
  230. $v1['option'] = isset($template_columns[$v1['smart_form_components_id']]) ? $template_columns[$v1['smart_form_components_id']] : [];
  231. switch ($v1['smart_form_components_code']) {
  232. case 'id-card':
  233. case 'single-link-text':
  234. // $col_value = json_decode($v1['col_value'],true);
  235. $v1['col_value'] = (string)$v1['col_value'] . ' ' ;
  236. break;
  237. case 'upload-photo':
  238. // $col_value = json_decode($v1['col_value'],true);
  239. $v1['col_value'] = explode(',',$v1['col_value']);
  240. break;
  241. case 'city-chose':
  242. # 省市区
  243. // $regin = Region::getOne([['id' => [2,3,4]]], true,[],false,'name');
  244. $regin = Region::find()->select('name')->where(['id' => explode(',',$v1['col_value'])])->asArray()->all();
  245. $regin = implode(" ",array_column($regin,'name'));//Region::getTopStr($v1['col_value']);
  246. $v1['col_value'] = $regin;
  247. break;
  248. case 'checked-tool':
  249. # 多选
  250. $col_value = explode(',',$v1['col_value']);
  251. if(strpos($v1['option'],SmartFormEnum::FIRST_CUTTER) !== false){
  252. $col_option = explode(SmartFormEnum::FIRST_CUTTER,$v1['option']);
  253. }else{
  254. $col_option = (array)$v1['option'];
  255. }
  256. $new_col_option = [];
  257. if(is_array($col_value)){
  258. foreach ($col_value as $ok => $ov) {
  259. if($col_option[$ov]){
  260. $new_col_option[] = $col_option[$ov];
  261. }
  262. }
  263. }else{
  264. $new_col_option[0] = $col_option[0];
  265. }
  266. $v1['col_value'] = implode(',',$new_col_option);
  267. break;
  268. case 'radio-tool':
  269. # 单选
  270. // $col_value = json_decode($v1['col_value'],true);
  271. $col_value = explode(',',$v1['col_value']);
  272. if(strpos($v1['option'],SmartFormEnum::FIRST_CUTTER) !== false){
  273. $col_option = explode(SmartFormEnum::FIRST_CUTTER,$v1['option']);
  274. }else{
  275. $col_option = (array)$v1['option'];
  276. }
  277. $new_col_option = [];
  278. if(is_array($col_value)){
  279. foreach ($col_value as $ok => $ov) {
  280. if($col_option[$ov]){
  281. $new_col_option[] = $col_option[$ov];
  282. }
  283. }
  284. }else{
  285. $new_col_option[0] = $col_option[0];
  286. }
  287. $v1['col_value'] = implode(',',$new_col_option);
  288. break;
  289. case 'drow-box':
  290. # 下拉
  291. // $col_value = json_decode($v1['col_value'],true);
  292. $col_value = explode(',',$v1['col_value']);
  293. if(strpos($v1['option'],SmartFormEnum::FIRST_CUTTER) !== false){
  294. $col_option = explode(SmartFormEnum::FIRST_CUTTER,$v1['option']);
  295. }else{
  296. $col_option = (array)$v1['option'];
  297. }
  298. $new_col_option = [];
  299. if(is_array($col_value)){
  300. foreach ($col_value as $ok => $ov) {
  301. if($col_option[$ov]){
  302. $new_col_option[] = $col_option[$ov];
  303. }
  304. }
  305. }else{
  306. $new_col_option[0] = $col_option[0];
  307. }
  308. $v1['col_value'] = implode(',',$new_col_option);
  309. break;
  310. }
  311. $list['list'][$k]['a'.$v1['smart_form_components_id']] = $v1['col_value'];
  312. }
  313. }
  314. $export_list = AddonsSmartFormTemplateUserForm::exportFieldsList($get['template_id']);
  315. $this->success('获取成功', compact('statistics','header','list','export_list'));
  316. }
  317. if ($request->post('flag') == 'EXPORT') {
  318. $post = $request->post();
  319. $post['mall_id'] = $this->mall_id;
  320. $post['template_id'] = $post['template_id'];
  321. $post['fields'] = AddonsSmartFormTemplateUserForm::exportFieldsList($post['template_id']);
  322. $info = AddonsSmartFormTemplate::getOne([['id' => $post['template_id']]], true);
  323. $filename = $info['title'] .'-' . date('YmdHis').'_'.rand(10000,99999);
  324. $res = CsvExportHelper::exportDownLoadCenter($this->mall_id,$filename,DownloadEnum::TYPE_AGENT,AddonsSmartFormTemplateUserForm::class,'exportHandle',$post);
  325. if ($res === false) return $this->error('加入导出任务失败'.CsvExportHelper::getStaticError());
  326. return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!');
  327. }
  328. } else {
  329. return $this->render($this->action->id,[
  330. ]);
  331. }
  332. } catch (\Exception $e) {
  333. return $this->error($e->getMessage(), []);
  334. }
  335. }
  336. /**
  337. * @method editIndex
  338. * 编辑
  339. * @author zqh
  340. * @datetime 2022-04-27T16:30:05+0800
  341. * @return [type] [description]
  342. */
  343. public function actionEdit()
  344. {
  345. return $this->render('edit',[
  346. ]);
  347. }
  348. /**
  349. * @method share
  350. * 分享
  351. * @author zqh
  352. * @datetime 2022-04-27T16:32:09+0800
  353. * @return [type] [description]
  354. */
  355. public function actionShare()
  356. {
  357. try {
  358. $request = Yii::$app->request;
  359. if ($request->isAjax) {
  360. if ($request->isPost) {
  361. $post = Yii::$app->request->post();
  362. if(!$post['id']){
  363. return $this->error('模板不存在', []);
  364. }
  365. # 获取信息
  366. $info = AddonsSmartFormDecorationForm::findOne(['status' => StatusEnum::ENABLED,'id'=>$post['id']]);
  367. $h5_url = Yii::$app->services->setting->get('system.h5_url') ?? '';
  368. $sign = $this->mall['mall_sign'];
  369. $diy_page = DiyPage::getOne([['id'=>$info->diy_page_id]], true);
  370. $share_url = '';
  371. if ($diy_page['is_home_page'] == 1) {
  372. $share_url = $h5_url .'/#'. DiyEnum::HOME_PAGE_PATH . '?sign=' . $sign. '&id=' . $diy_page['id'];
  373. } elseif ($diy_page['template'] == DiyEnum::USER_CENTER) {
  374. $share_url = $h5_url .'/#'. DiyEnum::USER_CENTER_PATH . '?sign=' . $sign. '&id=' . $diy_page['id'];
  375. } else {
  376. $share_url = $h5_url .'/#'. DiyEnum::CUSTOM_PAGE_PATH . '?sign=' . $sign . '&id=' . $diy_page['id'];
  377. }
  378. return $this->success('操作成功',$share_url);
  379. }
  380. }
  381. } catch (\Exception $e) {
  382. return $this->error($e->getMessage(), []);
  383. }
  384. }
  385. /**
  386. * @method actionDelete
  387. * 删除
  388. * @author zqh
  389. * @datetime 2022-04-27T16:35:31+0800
  390. * @return [type] [description]
  391. */
  392. public function actionDelete()
  393. {
  394. try {
  395. $request = Yii::$app->request;
  396. if ($request->isAjax) {
  397. if ($request->isPost) {
  398. $params = $request->post();
  399. $res = Yii::$app->services->validate->validate($params, [
  400. [['id'], 'required'],
  401. ]);
  402. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  403. $status = AddonsSmartFormDecorationForm::del($params);
  404. if(!$status){
  405. Yii::error(AddonsSmartFormDecorationForm::getStaticError());
  406. return $this->error(AddonsSmartFormDecorationForm::getStaticError());
  407. }
  408. return $this->success('操作成功');
  409. /*
  410. # 获取信息
  411. $info = AddonsSmartFormDecorationForm::findOne(['status' => StatusEnum::ENABLED,'id'=>$params['id']]);
  412. if(!$info){
  413. return $this->success('无需删除', []);
  414. }
  415. // $trans = Yii::$app->db->beginTransaction();
  416. # 删除装修
  417. // $diyInfo = DiyPage::getOne([['id'=>$info['diy_page_id']]], true);
  418. $diyInfo = DiyPage::findOne(['id' => $info['diy_page_id']]);
  419. if($diyInfo['content']){
  420. $content = json_decode($diyInfo['content'],true);
  421. $has = false;
  422. foreach ($content as $k => $v) {
  423. if($v['funcListItem']['type']=='smart-form'){
  424. unset($content[$k]);
  425. $has = true;
  426. }
  427. }
  428. if($has){
  429. $diyInfo['updated_at'] = time();
  430. $diyInfo['content'] = json_encode($content,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
  431. $s1 = $diyInfo->save();
  432. }else{
  433. $s1 = true;
  434. }
  435. }else{
  436. $s1 = true;
  437. }
  438. if(!$s1){
  439. return $this->error('移除装修表单失败');
  440. }
  441. $info->status = StatusEnum::DELETE;
  442. $info->updated_at = time();
  443. $s2 = $info->save();
  444. if(!$s2){
  445. return $this->error('移除表单失败');
  446. }
  447. return $this->success('操作成功!');
  448. */
  449. }
  450. }
  451. } catch (\Exception $e) {
  452. return $this->error($e->getMessage(), []);
  453. }
  454. }
  455. }