DefaultController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php
  2. namespace addons\SmartForm\api\modules\v1\controllers;
  3. use Yii;
  4. use api\controllers\OnAuthController;
  5. use common\enums\StatusEnum;
  6. use common\components\sensitivewords\SensitiveWords;
  7. use addons\SmartForm\common\enums\SmartFormEnum;
  8. use common\helpers\FormatHelper;
  9. use common\models\user\User;
  10. use common\models\user\UserFollow;
  11. use yii\db\Exception;
  12. use function EasyWeChat\Kernel\data_to_array;
  13. use addons\SmartForm\common\models\AddonsSmartFormTemplate; # 装修表单
  14. use addons\SmartForm\common\models\AddonsSmartFormTemplateStatistics; # 装修表单
  15. use addons\SmartForm\common\models\AddonsSmartFormTemplateUserForm; # 装修表单
  16. use common\models\user\UserAddress; # 用户地址
  17. use addons\SmartForm\common\models\AddonsSmartFormSetting; # 配置
  18. use addons\SmartForm\common\models\Region;
  19. /**
  20. * 默认控制器
  21. *
  22. * Class DefaultController
  23. * @package addons\SmartForm\api\modules\v1\controllers
  24. */
  25. class DefaultController extends OnAuthController
  26. {
  27. public $modelClass = '';
  28. /**
  29. * 不用进行登录验证的方法
  30. *
  31. * 例如: ['index', 'update', 'create', 'view', 'delete']
  32. * 默认全部需要验证
  33. *
  34. * @var array
  35. */
  36. protected $authOptional = ['index'];
  37. /**
  38. * 首页
  39. *
  40. * @return string
  41. */
  42. public function actionIndex()
  43. {
  44. return 'Hello world';
  45. }
  46. /**
  47. * @method actionTemplate
  48. * 获取模板
  49. * @author zqh
  50. * @datetime 2022-05-07T01:58:17+0800
  51. * @return [type] [description]
  52. */
  53. public function actionTemplate()
  54. {
  55. try {
  56. $request = Yii::$app->request;
  57. if ($request->isGet) {
  58. $get = Yii::$app->request->get();
  59. # 检查提交的参数
  60. $res = Yii::$app->services->validate->validate($get,[
  61. [['template_id'], 'required'],
  62. [['diy_page_id'], 'required'],
  63. ]);
  64. if($res === false){
  65. return $this->error(Yii::$app->services->validate->getErrorMessage());
  66. }
  67. if(!$get['template_id']){
  68. return $this->error('模板不存在');
  69. }
  70. $where[] = ['=', 'mall_id', $this->mall_id];
  71. $where[] = ['>=', 'status', StatusEnum::DISABLED];
  72. $where[] = ['=', 'id', $get['template_id']];
  73. $info = AddonsSmartFormTemplate::getOne($where, true, [
  74. 'columns'=> function ($query) {
  75. $query->where(['status' => StatusEnum::ENABLED])
  76. ->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');
  77. }
  78. ]);
  79. if(!$info){
  80. return $this->error('模板不存在');
  81. }
  82. if($info['expire_time'] !=-1 && $info['expire_time'] < time()){
  83. return $this->error('模板已过期');
  84. }
  85. if($info['columns']){
  86. # 存在模板字段
  87. $columns = $info['columns'];
  88. $classname = new SmartFormEnum();
  89. foreach ($columns as $k => $v) {
  90. if(strpos($v['show_default'],SmartFormEnum::FIRST_CUTTER) !== false){
  91. $info['columns'][$k]['show_default'] = explode(SmartFormEnum::FIRST_CUTTER,$v['show_default']);
  92. }
  93. if(strpos($v['tips'],SmartFormEnum::FIRST_CUTTER) !== false){
  94. $info['columns'][$k]['tips'] = explode(SmartFormEnum::FIRST_CUTTER,$v['tips']);
  95. }
  96. if(strpos($v['option'],SmartFormEnum::FIRST_CUTTER) !== false){
  97. $option = [];
  98. $option = explode(SmartFormEnum::FIRST_CUTTER,$v['option']);
  99. $info['columns'][$k]['option'] = $option;
  100. foreach ($option as $k1 => $v1) {
  101. $info['columns'][$k]['chooseList'][$k1]['title'] = $v1;
  102. }
  103. }
  104. if(strpos($v['default'],SmartFormEnum::DATE_CUTTER) !== false){
  105. $default = [];
  106. $default = explode(SmartFormEnum::DATE_CUTTER,$v['default']);
  107. foreach ($default as $kd => $kv) {
  108. if(strpos($kv,SmartFormEnum::PARAM_CUTTER) !== false){
  109. list($f,$func,$p) = explode(SmartFormEnum::PARAM_CUTTER,$kv);
  110. $default[$kd] = call_user_func(array($classname, $func),$p);
  111. $info['columns'][$k]['show_now_start'][$kd] = 1;
  112. }else{
  113. $default[$kd] = $kv;
  114. $info['columns'][$k]['show_now_start'][$kd] = 0;
  115. }
  116. }
  117. $info['columns'][$k]['default'] = $default;
  118. }else{
  119. if($v['default']){
  120. if(strpos($v['default'],SmartFormEnum::PARAM_CUTTER) !== false){
  121. list($f,$func,$p) = explode(SmartFormEnum::PARAM_CUTTER,$v['default']);
  122. $default = call_user_func(array($classname, $func),$p);
  123. $info['columns'][$k]['show_now_start'] = 1;
  124. $info['columns'][$k]['default'] = $default;
  125. }else{
  126. $default = $v['default'];
  127. $info['columns'][$k]['show_now_start'] = 0;
  128. }
  129. }
  130. }
  131. }
  132. }
  133. # 新增访问次数
  134. $view['user_id'] = $this->user_id;
  135. $view['template_id'] = $get['template_id'];
  136. $view['diy_page_id'] = $get['diy_page_id'];
  137. $view['type'] = 0;
  138. AddonsSmartFormTemplateStatistics::setTplStatistics($view);
  139. $this->success('获取成功', $info);
  140. }
  141. } catch (\Exception $e) {
  142. // $trans->rollBack();
  143. $exception = FormatHelper::exception($e, "添加模板失败");
  144. Yii::error($exception);
  145. return $this->error($exception);
  146. }
  147. }
  148. /**
  149. * @method actionStatisticsView
  150. * 统计阅读
  151. * @author zqh
  152. * @datetime 2022-05-09T23:57:11+0800
  153. * @return [type] [description]
  154. */
  155. public function actionStatisticsView()
  156. {
  157. try {
  158. $request = Yii::$app->request;
  159. if ($request->isPost) {
  160. $post = Yii::$app->request->post();
  161. # 新增访问次数
  162. $view['user_id'] = $this->user_id;
  163. $view['template_id'] = $post['template_id'] ?? 0;
  164. $view['diy_page_id'] = $post['diy_page_id'] ?? 0;
  165. $view['type'] = 0;
  166. $s = AddonsSmartFormTemplateStatistics::setTplStatistics($view);
  167. if(!$s){
  168. Yii::error(AddonsSmartFormTemplateStatistics::getStaticError());
  169. return $this->error(AddonsSmartFormTemplateStatistics::getStaticError());
  170. }
  171. return $this->success('操作成功');
  172. }
  173. } catch (\Exception $e) {
  174. // $trans->rollBack();
  175. $exception = FormatHelper::exception($e, "添加模板失败");
  176. Yii::error($exception);
  177. return $this->error($exception);
  178. }
  179. }
  180. /**
  181. * @method actionSubmitForm
  182. * 用户提交表单
  183. * @author zqh
  184. * @datetime 2022-05-07T09:58:57+0800
  185. * @return [type] [description]
  186. */
  187. public function actionSubmitForm()
  188. {
  189. try {
  190. $request = Yii::$app->request;
  191. if ($request->isPost) {
  192. $post = Yii::$app->request->post();
  193. $json = '{"form":{"id":90,"diy_page_id":1,"source":0},"column":[{"key":649,"value":"张三"},{"key":650,"value":"备注"},{"key":651,"value":"备注"}]}';
  194. // $post = json_decode($json,JSON_UNESCAPED_UNICODE);
  195. $trans = Yii::$app->db->beginTransaction();
  196. $post['mall_id'] = $this->mall_id;
  197. $post['user_id'] = $this->user_id;
  198. # 保存模板
  199. $status = AddonsSmartFormTemplateUserForm::saveUserForm($post);
  200. if(!$status){
  201. Yii::error(AddonsSmartFormTemplate::getStaticError());
  202. return $this->error(AddonsSmartFormTemplate::getStaticError());
  203. }
  204. $trans->commit();
  205. # 填写成功后执行
  206. # 新增访问次数
  207. $view['user_id'] = $this->user_id;
  208. $view['template_id'] = $post['form']['id'];
  209. $view['diy_page_id'] = $post['form']['diy_page_id'];
  210. $view['type'] = 1;
  211. AddonsSmartFormTemplateStatistics::setTplStatistics($view);
  212. return $this->success('操作成功', $status);
  213. }
  214. } catch (\Exception $e) {
  215. $trans->rollBack();
  216. $exception = FormatHelper::exception($e, "添加模板失败");
  217. Yii::error($exception);
  218. return $this->error($exception);
  219. }
  220. }
  221. /**
  222. * @method actionUserInfo
  223. * 获取用户信息
  224. * @author zqh
  225. * @datetime 2022-05-31T10:26:56+0800
  226. * @return [type] [description]
  227. */
  228. public function actionUserInfo()
  229. {
  230. try {
  231. $request = Yii::$app->request;
  232. if ($request->isGet) {
  233. $get = Yii::$app->request->get();
  234. # 检查提交的参数
  235. /*$res = Yii::$app->services->validate->validate($get,[
  236. [['template_id'], 'required'],
  237. [['diy_page_id'], 'required'],
  238. ]);
  239. if($res === false){
  240. return $this->error(Yii::$app->services->validate->getErrorMessage());
  241. }
  242. if(!$get['template_id']){
  243. return $this->error('模板不存在');
  244. }*/
  245. # 用户地址
  246. $address = Region::getUserAddress($this->user_id);
  247. # 用户信息
  248. $where = [];
  249. $where[] = ['mall_id' => $this->mall_id];
  250. $where[] = ['id' => $this->user_id];
  251. $userInfo = User::getUser($where);
  252. $return['address'] = $address;
  253. $return['name'] = $userInfo['nickname'];
  254. $return['mobile'] = $userInfo['mobile'];
  255. $this->success('获取成功', $return);
  256. }
  257. } catch (\Exception $e) {
  258. $exception = FormatHelper::exception($e, "获取失败");
  259. Yii::error($exception);
  260. return $this->error($exception);
  261. }
  262. }
  263. /**
  264. * @method actionSetting
  265. * 获取配置
  266. * @author zqh
  267. * @datetime 2022-06-01T00:45:17+0800
  268. * @return [type] [description]
  269. */
  270. public function actionSetting()
  271. {
  272. try {
  273. $request = Yii::$app->request;
  274. if ($request->isGet) {
  275. $where['mall_id'] = $this->mall_id;
  276. $where['status'] = StatusEnum::ENABLED;
  277. $find = AddonsSmartFormSetting::findOne($where,true);
  278. return $this->success('获取成功', $find);
  279. }
  280. } catch (\Exception $e) {
  281. $exception = FormatHelper::exception($e, "获取失败");
  282. Yii::error($exception);
  283. return $this->error($exception);
  284. }
  285. }
  286. }