FormGoodsController.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace addons\SmartForm\backend\controllers;
  3. use addons\SmartForm\common\models\SmartFormTemplateGoods;
  4. use common\enums\StatusEnum;
  5. use common\helpers\FormatHelper;
  6. use addons\SmartForm\common\models\AddonsSmartFormTemplate; # 装修表单
  7. use Yii;
  8. /**
  9. * 表单商品管理
  10. *
  11. * Class FormGoodsController
  12. * @package addons\SmartForm\backend\controllers
  13. */
  14. class FormGoodsController extends BaseController
  15. {
  16. public $enableCsrfValidation = false;
  17. /**
  18. * 获取商品关联表单信息
  19. * @return mixed|void
  20. */
  21. public function actionInfo()
  22. {
  23. try {
  24. $get = Yii::$app->request->get();
  25. $res = Yii::$app->services->validate->validate($get, [
  26. [['goods_id'], 'required'],
  27. ]);
  28. if ($res === false) {
  29. return $this->error(Yii::$app->services->validate->getErrorMessage());
  30. }
  31. $where[] = ['=', 'mall_id', $this->mall_id];
  32. $where[] = ['>=', 'status', StatusEnum::DISABLED];
  33. $where[] = ['=', 'goods_id', $get['goods_id']];
  34. $with = ['template'];
  35. $info = SmartFormTemplateGoods::getOne($where, true,$with);
  36. if (!$info) {
  37. return $this->error('商品未关联表单');
  38. }
  39. //$template = AddonsSmartFormTemplate::findOne(['id' => $info['template_id']]);
  40. //$info['template'] = $template->toArray();
  41. $this->success('获取成功', $info);
  42. } catch (\Exception $e) {
  43. $exception = FormatHelper::exception($e, "获取表单商品失败");
  44. Yii::error($exception);
  45. return $this->error($exception);
  46. }
  47. }
  48. /**
  49. * 保存商品关联表单
  50. * @return mixed|void
  51. */
  52. public function actionEdit()
  53. {
  54. try {
  55. $request = Yii::$app->request;
  56. if ($request->isPost) {
  57. $post = Yii::$app->request->post('form');
  58. $post = json_decode($post,true);
  59. $res = Yii::$app->services->validate->validate($post, [
  60. [['goods_id'], 'required'],
  61. [['status', 'template_id', 'goods_id'], 'integer'],
  62. ]);
  63. if ($res === false) {
  64. return $this->error(Yii::$app->services->validate->getErrorMessage());
  65. }
  66. $post['mall_id'] = $this->mall_id;
  67. SmartFormTemplateGoods::setData($post);
  68. return $this->success('操作成功');
  69. }
  70. } catch (\Exception $e) {
  71. $exception = FormatHelper::exception($e, "保存表单商品失败");
  72. Yii::error($exception);
  73. return $this->error($exception);
  74. }
  75. }
  76. }