| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- <?php
- namespace addons\SmartForm\api\modules\v1\controllers;
- use Yii;
- use api\controllers\OnAuthController;
- use common\enums\StatusEnum;
- use common\components\sensitivewords\SensitiveWords;
- use addons\SmartForm\common\enums\SmartFormEnum;
- use common\helpers\FormatHelper;
- use common\models\user\User;
- use common\models\user\UserFollow;
- use yii\db\Exception;
- use function EasyWeChat\Kernel\data_to_array;
- use addons\SmartForm\common\models\AddonsSmartFormTemplate; # 装修表单
- use addons\SmartForm\common\models\AddonsSmartFormTemplateStatistics; # 装修表单
- use addons\SmartForm\common\models\AddonsSmartFormTemplateUserForm; # 装修表单
- use common\models\user\UserAddress; # 用户地址
- use addons\SmartForm\common\models\AddonsSmartFormSetting; # 配置
- use addons\SmartForm\common\models\Region;
- /**
- * 默认控制器
- *
- * Class DefaultController
- * @package addons\SmartForm\api\modules\v1\controllers
- */
- class DefaultController extends OnAuthController
- {
- public $modelClass = '';
- /**
- * 不用进行登录验证的方法
- *
- * 例如: ['index', 'update', 'create', 'view', 'delete']
- * 默认全部需要验证
- *
- * @var array
- */
- protected $authOptional = ['index'];
- /**
- * 首页
- *
- * @return string
- */
- public function actionIndex()
- {
- return 'Hello world';
- }
- /**
- * @method actionTemplate
- * 获取模板
- * @author zqh
- * @datetime 2022-05-07T01:58:17+0800
- * @return [type] [description]
- */
- public function actionTemplate()
- {
- try {
- $request = Yii::$app->request;
- if ($request->isGet) {
- $get = Yii::$app->request->get();
-
- # 检查提交的参数
- $res = Yii::$app->services->validate->validate($get,[
- [['template_id'], 'required'],
- [['diy_page_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['diy_page_id'] = $get['diy_page_id'];
- $view['type'] = 0;
- AddonsSmartFormTemplateStatistics::setTplStatistics($view);
- $this->success('获取成功', $info);
- }
- } catch (\Exception $e) {
- // $trans->rollBack();
- $exception = FormatHelper::exception($e, "添加模板失败");
- Yii::error($exception);
- return $this->error($exception);
-
- }
- }
- /**
- * @method actionStatisticsView
- * 统计阅读
- * @author zqh
- * @datetime 2022-05-09T23:57:11+0800
- * @return [type] [description]
- */
- public function actionStatisticsView()
- {
- try {
- $request = Yii::$app->request;
- if ($request->isPost) {
- $post = Yii::$app->request->post();
- # 新增访问次数
- $view['user_id'] = $this->user_id;
- $view['template_id'] = $post['template_id'] ?? 0;
- $view['diy_page_id'] = $post['diy_page_id'] ?? 0;
- $view['type'] = 0;
- $s = AddonsSmartFormTemplateStatistics::setTplStatistics($view);
- if(!$s){
- Yii::error(AddonsSmartFormTemplateStatistics::getStaticError());
- return $this->error(AddonsSmartFormTemplateStatistics::getStaticError());
- }
- return $this->success('操作成功');
- }
- } catch (\Exception $e) {
- // $trans->rollBack();
- $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->isPost) {
- $post = Yii::$app->request->post();
- $json = '{"form":{"id":90,"diy_page_id":1,"source":0},"column":[{"key":649,"value":"张三"},{"key":650,"value":"备注"},{"key":651,"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['diy_page_id'] = $post['form']['diy_page_id'];
- $view['type'] = 1;
- AddonsSmartFormTemplateStatistics::setTplStatistics($view);
- return $this->success('操作成功', $status);
- }
- } catch (\Exception $e) {
- $trans->rollBack();
- $exception = FormatHelper::exception($e, "添加模板失败");
- Yii::error($exception);
- return $this->error($exception);
-
- }
- }
- /**
- * @method actionUserInfo
- * 获取用户信息
- * @author zqh
- * @datetime 2022-05-31T10:26:56+0800
- * @return [type] [description]
- */
- public function actionUserInfo()
- {
- try {
- $request = Yii::$app->request;
- if ($request->isGet) {
- $get = Yii::$app->request->get();
-
- # 检查提交的参数
- /*$res = Yii::$app->services->validate->validate($get,[
- [['template_id'], 'required'],
- [['diy_page_id'], 'required'],
- ]);
- if($res === false){
- return $this->error(Yii::$app->services->validate->getErrorMessage());
- }
- if(!$get['template_id']){
- return $this->error('模板不存在');
- }*/
- # 用户地址
- $address = Region::getUserAddress($this->user_id);
- # 用户信息
- $where = [];
- $where[] = ['mall_id' => $this->mall_id];
- $where[] = ['id' => $this->user_id];
- $userInfo = User::getUser($where);
- $return['address'] = $address;
- $return['name'] = $userInfo['nickname'];
- $return['mobile'] = $userInfo['mobile'];
- $this->success('获取成功', $return);
- }
- } catch (\Exception $e) {
-
- $exception = FormatHelper::exception($e, "获取失败");
- Yii::error($exception);
- return $this->error($exception);
-
- }
- }
- /**
- * @method actionSetting
- * 获取配置
- * @author zqh
- * @datetime 2022-06-01T00:45:17+0800
- * @return [type] [description]
- */
- public function actionSetting()
- {
- try {
- $request = Yii::$app->request;
- if ($request->isGet) {
- $where['mall_id'] = $this->mall_id;
- $where['status'] = StatusEnum::ENABLED;
- $find = AddonsSmartFormSetting::findOne($where,true);
- return $this->success('获取成功', $find);
- }
-
- } catch (\Exception $e) {
-
- $exception = FormatHelper::exception($e, "获取失败");
- Yii::error($exception);
- return $this->error($exception);
-
- }
- }
- }
|