CardSecretUseController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace addons\CardSecret\backend\controllers;
  3. use addons\CardSecret\common\models\CardSecret;
  4. use addons\CardSecret\common\models\CardSecretUse;
  5. use addons\CardSecret\common\models\Cate;
  6. use common\enums\StatusEnum;
  7. use common\models\user\User;
  8. use Yii;
  9. use common\controllers\AddonsController;
  10. use yii\db\Exception;
  11. use yii\helpers\Json;
  12. /**
  13. * 默认控制器
  14. *
  15. * Class DefaultController
  16. * @package addons\CardSecret\backend\controllers
  17. */
  18. class CardSecretUseController 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->isGet) {
  31. $get = Yii::$app->request->get();
  32. // 检查提交的参数
  33. $res = Yii::$app->services->validate->validate($get,[
  34. [['user_id','mobile','cate_id','user_keyword','name','use_status','create_time'], 'safe'],
  35. [['limit'],'default','value' => '15'],
  36. ]);
  37. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  38. $where = [];
  39. if (!empty($get['user_keyword'])) {
  40. $user_ids = User::find()->select("id")->where(['mall_id' => $this->mall_id])->andWhere(['like', 'mobile', $get['user_keyword'] . '%', false])->asArray()->column();
  41. $user_name = User::find()->select("id")->where(['mall_id' => $this->mall_id])->andWhere(['like', 'nickname', $get['user_keyword'] . '%', false])->asArray()->column();
  42. $user_ids = $user_ids + $user_name;
  43. if (empty($user_ids)) $this->success('查无此用户');
  44. $where[] = ['in', 'user_id', $user_ids];
  45. }
  46. if($get['create_time']) $where[] = ['between', 'created_at', strtotime($get['create_time'][0]),strtotime($get['create_time'][1])];
  47. $with = ['user','cate','cardSecret'];
  48. if(!empty($get['name'])) {
  49. $card_ids = CardSecret::find()->select("id")->where(['mall_id' => $this->mall_id])->andWhere(['like', 'name', $get['name'] . '%', false])->asArray()->column();
  50. $where[] = ['in', 'card_secret_id', $card_ids];
  51. }
  52. if(!empty($get['use_status'])) $where[] = ['=', 'use_status', $get['use_status']];
  53. if(!empty($get['user_id'])) $where[] = ['=', 'user_id', $get['user_id']];
  54. if(!empty($get['cate_id'])) $where[] = ['=', 'cate_id', $get['cate_id']];
  55. $where[] = ['=', 'mall_id', $this->mall_id];
  56. $where[] = ['>=', 'status', StatusEnum::DISABLED];
  57. $order = 'id desc';
  58. $params = array('where' => $where,'with' => $with, 'order'=>$order, 'limit' => $get['limit']);
  59. $list = CardSecretUse::listPage($params, false, true);
  60. //获取分类
  61. $list['cate'] = Cate::getCateMap(1,$this->mall_id);
  62. $this->success('获取成功', $list);
  63. }
  64. } else {
  65. return $this->render($this->action->id);
  66. }
  67. }
  68. /**
  69. * 等级编辑
  70. * @Author: ltm
  71. * @DateTime: 2021/10/21 0021 11:29
  72. * @Copyright: copyright (c) 2021 广东七件事集团
  73. * @return mixed|string|void
  74. */
  75. public function actionEdit()
  76. {
  77. try {
  78. $request = Yii::$app->request;
  79. if ($request->isAjax) {
  80. // 提交等级内容
  81. if ($request->isPost) {
  82. $params = Yii::$app->request->post();
  83. $res = Yii::$app->services->validate->validate($params, [
  84. [['name','id','status'], 'safe'],
  85. ]);
  86. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  87. $params = Cate::exceptNullVal($params);
  88. if(!empty($params['id'])) $params['updated_at'] = time();//更新
  89. $params['create_at'] = time();
  90. if(!$params['name']) unset($params['name']);
  91. $params['mall_id'] = $this->mall_id;
  92. if(isset($params['status']) && !in_array($params['status'],[StatusEnum::DISABLED,StatusEnum::ENABLED,StatusEnum::DELETE])) throw new Exception("参数错误");;
  93. $res = Cate::setData($params);
  94. if ($res === false) throw new Exception(MaterialCate::getStaticError());
  95. return $this->success('操作成功');
  96. }
  97. }
  98. } catch (\Exception $e) {
  99. return $this->error($e->getMessage(), []);
  100. }
  101. return $this->render($this->action->id);
  102. }
  103. }