DefaultController.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace addons\Sudoku\backend\controllers;
  3. use addons\Coupon\common\models\AddonsCoupon;
  4. use addons\Coupon\common\models\AddonsCouponUserRelate;
  5. use addons\Sudoku\common\models\AddonsSudoku;
  6. use common\enums\StatusEnum;
  7. use common\models\common\PlatformSetting;
  8. use common\models\user\User;
  9. use Yii;
  10. use common\controllers\AddonsController;
  11. use yii\db\Exception;
  12. /**
  13. * 默认控制器
  14. *
  15. * Class DefaultController
  16. * @package addons\Sudoku\backend\controllers
  17. */
  18. class DefaultController extends BaseController
  19. {
  20. public $enableCsrfValidation = false;
  21. /**
  22. * 首页
  23. *
  24. * @return string
  25. */
  26. public function actionIndex()
  27. {
  28. $retuest = Yii::$app->request;
  29. if ($retuest->isAjax) {
  30. if ($retuest->isPost) {
  31. $post = Yii::$app->request->post();
  32. // 检查提交的参数
  33. $res = Yii::$app->services->validate->validate($post,[
  34. [['list'], 'safe'],
  35. ]);
  36. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  37. $t = \Yii::$app->db->beginTransaction();
  38. try {
  39. $prize_ids = [];
  40. $params['where'][] = ['mall_id'=>$this->mall_id];
  41. $findData = AddonsSudoku::lists($params,true);
  42. if($findData) foreach ($findData as $v) $prize_ids[$v['prize_id']] = $v['id'];
  43. $post_type_list = array_column($post['list'],'type');
  44. if(!in_array(5,$post_type_list)){
  45. throw new \Exception('8个奖品至少选中一个谢谢参与');
  46. }
  47. foreach ($post['list'] as $k => $v){
  48. $v['mall_id'] = $this->mall_id;
  49. $v['created_at'] = time();
  50. if($prize_ids) $v['id'] = $prize_ids[$v['prize_id']];
  51. $res = AddonsSudoku::setData($v);
  52. if(!$res){
  53. throw new \Exception(AddonsSudoku::getStaticError());
  54. }
  55. }
  56. $t->commit();
  57. return $this->success('操作成功');
  58. }catch (\Exception $e) {
  59. $t->rollBack();
  60. return $this->success('操作失败,error:'.$e->getMessage());
  61. return false;
  62. }
  63. }
  64. if ($retuest->isGet) {
  65. $params['where'][] = ['mall_id'=>$this->mall_id];
  66. $params['order'] = 'id asc';
  67. $params['with'] = ['goods'];
  68. $list = AddonsSudoku::lists($params,true);
  69. $coupon_params['where'][] = ['mall_id' => $this->mall_id];
  70. $coupon_params['where'][] = ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]];
  71. $coupon_params['order'] = 'sort asc,id desc';
  72. $coupon = AddonsCoupon::lists($coupon_params,true);
  73. $get_system_setting = PlatformSetting::getConfByGroup('system', true);
  74. $api_url = $get_system_setting['api_url']['value'];
  75. return $this->success('操作成功', compact('list','coupon','api_url'));
  76. }
  77. } else {
  78. return $this->render($this->action->id);
  79. }
  80. }
  81. public function actionLottery(){
  82. }
  83. }