request; if ($request->isGet) { $get = Yii::$app->request->get(); # 检查提交的参数 $res = Yii::$app->services->validate->validate($get,[ [['template_id'], 'required'], ]); if($res === false){ return $this->error(Yii::$app->services->validate->getErrorMessage()); } if(!$get['template_id']){ return $this->error('模板不存在'); } $where[] = ['=', 'mall_id', $this->mall_id]; $where[] = ['>=', 'status', StatusEnum::DISABLED]; $where[] = ['=', 'id', $get['template_id']]; $info = AddonsSmartFormTemplate::getOne($where, true, [ 'columns'=> function ($query) { $query->where(['status' => StatusEnum::ENABLED]) ->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'); } ]); if(!$info){ return $this->error('模板不存在'); } if($info['expire_time'] !=-1 && $info['expire_time'] < time()){ return $this->error('模板已过期'); } if($info['columns']){ # 存在模板字段 $columns = $info['columns']; $classname = new SmartFormEnum(); foreach ($columns as $k => $v) { if(strpos($v['show_default'],SmartFormEnum::FIRST_CUTTER) !== false){ $info['columns'][$k]['show_default'] = explode(SmartFormEnum::FIRST_CUTTER,$v['show_default']); } if(strpos($v['tips'],SmartFormEnum::FIRST_CUTTER) !== false){ $info['columns'][$k]['tips'] = explode(SmartFormEnum::FIRST_CUTTER,$v['tips']); } if(strpos($v['option'],SmartFormEnum::FIRST_CUTTER) !== false){ $option = []; $option = explode(SmartFormEnum::FIRST_CUTTER,$v['option']); $info['columns'][$k]['option'] = $option; foreach ($option as $k1 => $v1) { $info['columns'][$k]['chooseList'][$k1]['title'] = $v1; } } if(strpos($v['default'],SmartFormEnum::DATE_CUTTER) !== false){ $default = []; $default = explode(SmartFormEnum::DATE_CUTTER,$v['default']); foreach ($default as $kd => $kv) { if(strpos($kv,SmartFormEnum::PARAM_CUTTER) !== false){ list($f,$func,$p) = explode(SmartFormEnum::PARAM_CUTTER,$kv); $default[$kd] = call_user_func(array($classname, $func),$p); $info['columns'][$k]['show_now_start'][$kd] = 1; }else{ $default[$kd] = $kv; $info['columns'][$k]['show_now_start'][$kd] = 0; } } $info['columns'][$k]['default'] = $default; }else{ if($v['default']){ if(strpos($v['default'],SmartFormEnum::PARAM_CUTTER) !== false){ list($f,$func,$p) = explode(SmartFormEnum::PARAM_CUTTER,$v['default']); $default = call_user_func(array($classname, $func),$p); $info['columns'][$k]['show_now_start'] = 1; $info['columns'][$k]['default'] = $default; }else{ $default = $v['default']; $info['columns'][$k]['show_now_start'] = 0; } } } } } # 新增访问次数 $view['user_id'] = $this->user_id; $view['template_id'] = $get['template_id']; $view['type'] = 0; AddonsSmartFormTemplateStatistics::setTplStatistics($view); $this->success('获取成功', $info); } } catch (\Exception $e) { $exception = FormatHelper::exception($e, "添加模板失败"); Yii::error($exception); return $this->error($exception); } } /** * @method actionSubmitForm * 用户提交表单 * @author zqh * @datetime 2022-05-07T09:58:57+0800 * @return [type] [description] */ public function actionSubmitForm() { try { $request = Yii::$app->request; if ($request->isAjax) { if ($request->isPost) { $post = Yii::$app->request->post(); $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":"身份证"}]}'; $post = json_decode($json,JSON_UNESCAPED_UNICODE); $trans = Yii::$app->db->beginTransaction(); $post['mall_id'] = $this->mall_id; $post['user_id'] = $this->user_id; # 保存模板 $status = AddonsSmartFormTemplateUserForm::saveUserForm($post); if(!$status){ Yii::error(AddonsSmartFormTemplate::getStaticError()); return $this->error(AddonsSmartFormTemplate::getStaticError()); } $trans->commit(); # 填写成功后执行 # 新增访问次数 $view['user_id'] = $this->user_id; $view['template_id'] = $post['form']['id']; $view['type'] = 1; AddonsSmartFormTemplateStatistics::setTplStatistics($view); return $this->success('操作成功'); } } } catch (\Exception $e) { $trans->rollBack(); $exception = FormatHelper::exception($e, "添加模板失败"); Yii::error($exception); return $this->error($exception); } } /** * @method actionIndex * 首页 * @author zqh * @datetime 2022-04-27T16:30:11+0800 * @return [type] [description] */ public function actionIndex() { try { $request = Yii::$app->request; if ($request->isAjax) { if ($request->isGet) { $get = Yii::$app->request->get(); // 检查提交的参数 $res = Yii::$app->services->validate->validate($get,[ [['id','keyword'], 'safe'], [['limit'],'default','value' => '15'], ]); if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); # 搜索条件 $where = []; if($get['keyword']) $where[] = ['like', 'title', $get['keyword']]; $where[] = ['>=', 'status', StatusEnum::DISABLED]; $where[] = ['=', 'mall_id', $this->mall_id]; # 排序 $order = 'created_at desc'; # 显示字段 $select = "id,mall_id,diy_page_id,title,header,expire_time,status,created_at"; $params = array('where' => $where, 'order'=>$order, 'select'=>$select, 'limit' => $get['limit']); # 分页返回 $list = AddonsSmartFormTemplate::listPage($params, false, true); $this->success('获取成功', $list); } } else { return $this->render($this->action->id,[ ]); } } catch (\Exception $e) { return $this->error($e->getMessage(), []); } } /** * @method actionAdd * 新增 * @author zqh * @datetime 2022-04-27T16:30:05+0800 * @return [type] [description] */ public function actionAdd() { try { $request = Yii::$app->request; if ($request->isAjax) { if ($request->isPost) { $post = Yii::$app->request->post(); // 检查提交的参数 // $classname = new SmartFormEnum(); // $s = call_user_func(array($classname, 'showTime'),'now'); $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}]}'; // $post = json_decode($json,true); $trans = Yii::$app->db->beginTransaction(); try { $post['mall_id'] = $this->mall_id; # 保存模板 $status = AddonsSmartFormTemplate::createTemplate($post); if(!$status){ Yii::error(AddonsSmartFormTemplate::getStaticError()); return $this->error(AddonsSmartFormTemplate::getStaticError()); } $trans->commit(); return $this->success('操作成功'); } catch (\Exception $e) { $trans->rollBack(); $exception = FormatHelper::exception($e, "添加模板失败"); Yii::error($exception); return $this->error($exception); } } }else{ return $this->render($this->action->id,[]); } } catch (\Exception $e) { return $this->error($e->getMessage(), []); } // print_r($this->mall_id);exit; } /** * @method actionInfo * 详情 * @author zqh * @datetime 2022-04-27T16:30:05+0800 * @return [type] [description] */ public function actionInfo() { /*$s = AddonsSmartFormTemplateUserForm::exportHandle(['download_id'=>7]); var_dump($s);exit;*/ // try { $request = Yii::$app->request; if ($request->isAjax) { if ($request->isGet) { $get = Yii::$app->request->get(); // 检查提交的参数 $res = Yii::$app->services->validate->validate($get,[ [['template_id','keyword','date_start','date_end'], 'safe'], [['limit'],'default','value' => '15'], ]); if(!$get['template_id']){ return $this->error('模板不存在', []); } if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); # 获取统计数据 # 今日填写次数-当天填写次数,包含同一个ID用户填写次数 $today_filling_times = AddonsSmartFormTemplateUserBehavior::find()->where(['smart_form_template_id'=>$get['template_id'],'month_day'=>date('Y-m-d'),'behavior_type'=>1])->count('id'); # 今日访问人次-当天访问次数,只访问未填写 $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'); # 今日填写人数-当天填写人数,一个ID用户统计一次 $today_filling_people = AddonsSmartFormTemplateUserBehavior::find() ->select('user_id,month_day') ->where(['smart_form_template_id'=>$get['template_id'],'month_day'=>date('Y-m-d'),'behavior_type'=>1]) ->groupBy('user_id,month_day') ->count('user_id'); # 总人数/填写人数x100%=填写率 $today_rate = (string)($today_filling_people > 0 ? ($today_view_times/$today_filling_people) : 0) . '%'; $statistics = AddonsSmartFormTemplateStatistics::getOne([['smart_form_template_id'=>$get['template_id']]], true,[],false,'filling_times,view_times,filling_people'); // print_r($statistics);exit; if(!$statistics){ $all_rate = '0'. '%'; $statistics['filling_times'] = 0; $statistics['view_times'] = 0; $statistics['filling_people'] = 0; }else{ $all_rate = (string)($statistics['filling_people'] > 0 ? ($statistics['view_times']/$statistics['filling_people']) : 0) . '%'; } $statistics['today_filling_times'] = $today_filling_times; $statistics['today_view_times'] = $today_view_times; $statistics['today_filling_people'] = $today_filling_people; $statistics['today_rate'] = $today_rate; $statistics['all_rate'] = $all_rate; # 获取模板字段 $field = " id,concat('a',`id`) type,smart_form_components_code,title label"; $header = AddonsSmartFormTemplateColumn::find()->select($field) ->orderBy('sort asc') ->where(['smart_form_template_id'=>$get['template_id']])->asArray()->all(); # 搜索条件 $where = []; $where[] = ['=', 'a.smart_form_template_id', $get['template_id']]; if($get['date_start'] && $get['date_end']){ $where[] = ['between', 'a.created_at', strtotime($get['date_start']),strtotime($get['date_end'])]; } if($get['keyword']){ $user_ids = User::find()->where(['and',['mall_id' => $this->mall_id], ['like', 'nickname', $get['keyword']]]) ->select('id')->asArray()->all(); $user_ids = array_column($user_ids, 'id'); $user_ids = $user_ids ?? []; $where[] = ['user_id' => $user_ids]; } $where[] = ['>=', 'a.status', StatusEnum::DISABLED]; # 排序 $order = 'a.created_at desc'; # 显示字段 $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 // $params = array('where' => $where, 'order'=>$order, 'select'=>$select, 'limit' => 10,'alias'=>'a','join'=>[ // ['INNER JOIN', User::tableName() . ' b', "b.id = a.user_id"] // ,['INNER JOIN', AddonsSmartFormTemplateUserFormInfo::tableName() . ' c', "c.smart_form_template_user_form_id = a.id"] ]); # 分页返回 $list = AddonsSmartFormTemplateUserForm::listPage($params, false); foreach ($list['list'] as $k => $v) { $users = User::findOne($v["user_id"]); $nickname = "普通用户"; if(!empty($users)){ $nickname = $users->nickname; $users = ArrayHelper::toArray($users); $list['list'][$k]["user"] = $users; } $list['list'][$k]["nickname"] = $nickname; $formInfo = AddonsSmartFormTemplateUserFormInfo::find()->where(["smart_form_template_user_form_id" => $v["id"]])->asArray()->all(); if(!empty($formInfo)){ $list['list'][$k]["formInfo"] = $formInfo; $template_columns = AddonsSmartFormTemplateColumn::find() ->where(['and',['in', 'id', array_column($formInfo, 'smart_form_components_id')]]) ->select('id, option') ->asArray() ->all(); $template_columns = array_column($template_columns, 'option', 'id'); foreach ($formInfo as $k1 => $v1) { $v1['option'] = isset($template_columns[$v1['smart_form_components_id']]) ? $template_columns[$v1['smart_form_components_id']] : []; switch ($v1['smart_form_components_code']) { case 'id-card': case 'single-link-text': $v1['col_value'] = (string)$v1['col_value'] . ' ' ; break; case 'upload-photo': // $col_value = json_decode($v1['col_value'],true); $v1['col_value'] = explode(',',$v1['col_value']); break; case 'city-chose': # 省市区 // $regin = Region::getOne([['id' => [2,3,4]]], true,[],false,'name'); $regin = Region::find()->select('name')->where(['id' => explode(',',$v1['col_value'])])->asArray()->all(); $regin = implode(" ",array_column($regin,'name'));//Region::getTopStr($v1['col_value']); $v1['col_value'] = $regin; break; case 'checked-tool': # 多选 $col_value = explode(',',$v1['col_value']); if(strpos($v1['option'],SmartFormEnum::FIRST_CUTTER) !== false){ $col_option = explode(SmartFormEnum::FIRST_CUTTER,$v1['option']); }else{ $col_option = (array)$v1['option']; } $new_col_option = []; if(is_array($col_value)){ foreach ($col_value as $ok => $ov) { if($col_option[$ov]){ $new_col_option[] = $col_option[$ov]; } } }else{ $new_col_option[0] = $col_option[0]; } $v1['col_value'] = implode(',',$new_col_option); break; case 'radio-tool': # 单选 // $col_value = json_decode($v1['col_value'],true); $col_value = explode(',',$v1['col_value']); if(strpos($v1['option'],SmartFormEnum::FIRST_CUTTER) !== false){ $col_option = explode(SmartFormEnum::FIRST_CUTTER,$v1['option']); }else{ $col_option = (array)$v1['option']; } $new_col_option = []; if(is_array($col_value)){ foreach ($col_value as $ok => $ov) { if($col_option[$ov]){ $new_col_option[] = $col_option[$ov]; } } }else{ $new_col_option[0] = $col_option[0]; } $v1['col_value'] = implode(',',$new_col_option); break; case 'drow-box': # 下拉 // $col_value = json_decode($v1['col_value'],true); $col_value = explode(',',$v1['col_value']); if(strpos($v1['option'],SmartFormEnum::FIRST_CUTTER) !== false){ $col_option = explode(SmartFormEnum::FIRST_CUTTER,$v1['option']); }else{ $col_option = (array)$v1['option']; } $new_col_option = []; if(is_array($col_value)){ foreach ($col_value as $ok => $ov) { if($col_option[$ov]){ $new_col_option[] = $col_option[$ov]; } } }else{ $new_col_option[0] = $col_option[0]; } $v1['col_value'] = implode(',',$new_col_option); break; } $list['list'][$k]['a'.$v1['smart_form_components_id']] = $v1['col_value']; } } } $export_list = AddonsSmartFormTemplateUserForm::exportFieldsList($get['template_id']); $return['statistics'] = $statistics; $return['header'] = $header; $return['list'] = $list; $return['export_list'] = $export_list; // print_r($return);exit; $this->success('获取成功', $return); } if ($request->post('flag') == 'EXPORT') { $post = $request->post(); $post['mall_id'] = $this->mall_id; $post['template_id'] = $post['template_id']; $post['fields'] = AddonsSmartFormTemplateUserForm::exportFieldsList($post['template_id']); $info = AddonsSmartFormTemplate::getOne([['id' => $post['template_id']]], true); $filename = $info['title'] .'-' . date('YmdHis').'_'.rand(10000,99999); $res = CsvExportHelper::exportDownLoadCenter($this->mall_id,$filename,DownloadEnum::TYPE_AGENT,AddonsSmartFormTemplateUserForm::class,'exportHandle',$post); if ($res === false) return $this->error('加入导出任务失败'.CsvExportHelper::getStaticError()); return $this->success('添加导出任务成功,任务完成后请在下载中心进行下载!'); } } else { return $this->render($this->action->id,[ ]); } // } catch (\Exception $e) { // return $this->error($e->getMessage(), []); // } } /** * @method editIndex * 编辑 * @author zqh * @datetime 2022-04-27T16:30:05+0800 * @return [type] [description] */ public function actionEdit() { try { $request = Yii::$app->request; if ($request->isAjax) { if ($request->isPost) { # 修改信息 $post = Yii::$app->request->post(); # 判断表单是否已经填写 $hasUse = AddonsSmartFormTemplateStatistics::checkTplUse($post['form']['id']); $hasUse1 = AddonsSmartFormDecorationForm::checkTplUse($post['form']['id']); if ($hasUse || $hasUse1) { return $this->error('表单已使用不能修改', []); } // 检查提交的参数 // $classname = new SmartFormEnum(); // $s = call_user_func(array($classname, 'showTime'),'now'); $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}]}'; $trans = Yii::$app->db->beginTransaction(); try { $post['mall_id'] = $this->mall_id; # 保存模板 $status = AddonsSmartFormTemplate::createTemplate($post); if(!$status){ Yii::error(AddonsSmartFormTemplate::getStaticError()); return $this->error(AddonsSmartFormTemplate::getStaticError()); } $trans->commit(); return $this->success('操作成功'); } catch (\Exception $e) { $trans->rollBack(); $exception = FormatHelper::exception($e, "添加模板失败"); Yii::error($exception); return $this->error($exception); } }else{ # 获取信息 $get = Yii::$app->request->get(); $template_id = $get['template_id']; $info = AddonsSmartFormTemplate::getTplInfo($template_id); if(!$info){ Yii::error(AddonsSmartFormTemplate::getStaticError()); return $this->error(AddonsSmartFormTemplate::getStaticError()); } return $this->success('获取成功', $info); } }else{ return $this->render('edit',[ ]); } } catch (\Exception $e) { return $this->error($e->getMessage(), []); } } /** * @method share * 分享 * @author zqh * @datetime 2022-04-27T16:32:09+0800 * @return [type] [description] */ public function actionShare() { try { $request = Yii::$app->request; if ($request->isAjax) { if ($request->isPost) { $post = Yii::$app->request->post(); if(!$post['template_id']){ return $this->error('模板不存在', []); } $template_id = $post['template_id']; $h5_url = Yii::$app->services->setting->get('system.h5_url') ?? ''; $sign = $this->mall['mall_sign']; if ($h5_url) { $share_url = $h5_url .'/#'. DiyEnum::SMART_FORM_PAGE . '?sign=' . $sign . '&template_id=' . $template_id; }else{ $share_url = ''; } return $this->success('操作成功',$share_url); } } } catch (\Exception $e) { return $this->error($e->getMessage(), []); } } /** * @method copy * 复制 * @author zqh * @datetime 2022-04-27T16:32:09+0800 * @return [type] [description] */ public function actionCopy() { try { $request = Yii::$app->request; if ($request->isAjax) { if ($request->isPost) { $post = Yii::$app->request->post(); $template_id = $post['template_id']; $trans = Yii::$app->db->beginTransaction(); $s = AddonsSmartFormTemplate::copyTpl($template_id); if(!$s){ $trans->rollBack(); Yii::error(AddonsSmartFormTemplate::getStaticError()); return $this->error(AddonsSmartFormTemplate::getStaticError()); } $trans->commit(); return $this->success('操作成功'); } } } catch (\Exception $e) { return $this->error($e->getMessage(), []); } } public function actionDeleteInfo() { try { $request = Yii::$app->request; if ($request->isAjax) { if (\Yii::$app->request->isPost) { $params = \Yii::$app->request->post(); $res = Yii::$app->services->validate->validate($params, [ [['id'], 'required'], ]); # 删除 用户提交表单 $model2 = AddonsSmartFormTemplateUserForm::findOne(['id'=>$params['id']]); if($model2){ $s2 = AddonsSmartFormTemplateUserForm::updateAll(['status'=>StatusEnum::DELETE],['id'=>$params['id']]); } if(!$s2){ return $this->error('删除失败!', []); } return $this->success('操作成功!'); } } } catch (\Exception $e) { return $this->error($e->getMessage(), []); } } /** * @method actionDelete * 删除 * @author zqh * @datetime 2022-04-27T16:35:31+0800 * @return [type] [description] */ public function actionDelete() { try { $request = Yii::$app->request; if ($request->isAjax) { if ($request->isPost) { $params = \Yii::$app->request->post(); $res = Yii::$app->services->validate->validate($params, [ [['id'], 'required'], ]); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $params['mall_id'] = $this->mall_id; $params['status'] = StatusEnum::DELETE; $res = AddonsSmartFormTemplate::del($params); if ($res === false) { return $this->error(AddonsSmartFormTemplate::getStaticError()); } return $this->success('操作成功'); } } } catch (\Exception $e) { return $this->error($e->getMessage(), []); } } /** * @return mixed|null */ public function actionSmsKeys() { return $this->success('获取成功', SmartFormEnum::SMS_KEYS); } }