DefaultController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace addons\Certificate\backend\controllers;
  3. use addons\Certificate\common\models\Certificate;
  4. use common\enums\StatusEnum;
  5. use common\models\user\UserLevel;
  6. use Yii;
  7. use yii\db\Exception;
  8. use common\controllers\AddonsController;
  9. /**
  10. * 默认控制器
  11. *
  12. * Class DefaultController
  13. * @package addons\Certificate\backend\controllers
  14. */
  15. class DefaultController extends BaseController
  16. {
  17. public $enableCsrfValidation = false;
  18. /**
  19. * 首页
  20. *
  21. * @return string
  22. */
  23. public function actionIndex()
  24. {
  25. $retuest = Yii::$app->request;
  26. if ($retuest->isAjax) {
  27. if ($retuest->isGet) {
  28. $params = Yii::$app->request->get();
  29. // 检查提交的参数
  30. $res = Yii::$app->services->validate->validate($params,[
  31. [['id','name','create_time'], 'safe'],
  32. [['limit'],'default','value' => '15'],
  33. ]);
  34. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  35. $where = [];
  36. if($params['create_time']) $where[] = ['between', 'created_at', strtotime($params['create_time'][0]),strtotime($params['create_time'][1])];
  37. if(!empty($params['name'])) $where[] = ['like', 'name', $params['name']];
  38. $where[] = ['=', 'mall_id', $this->mall_id];
  39. $where[] = ['>=', 'status', StatusEnum::DISABLED];
  40. $order = 'id desc';
  41. $params = array('where' => $where, 'order'=>$order, 'limit' => $params['limit']);
  42. $list = Certificate::listPage($params, false, true);
  43. $this->success('获取成功', $list);
  44. }
  45. } else {
  46. return $this->render($this->action->id);
  47. }
  48. }
  49. /**
  50. * 编辑
  51. * @Author: ltm
  52. * @DateTime: 2022/5/8 0021 11:29
  53. * @Copyright: copyright (c) 2021 广东七件事集团
  54. * @return mixed|string|void
  55. */
  56. public function actionEdit()
  57. {
  58. try {
  59. $request = Yii::$app->request;
  60. if ($request->isAjax) {
  61. // 提交内容
  62. if ($request->isPost) {
  63. $params = Yii::$app->request->post();
  64. $res = Yii::$app->services->validate->validate($params, [
  65. [['name','id','content','status','member_level'], 'safe'],
  66. ]);
  67. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  68. $params = Certificate::exceptNullVal($params);
  69. $params['mall_id'] = $this->mall_id;
  70. if(!empty($params['content']))$params['content'] = json_encode($params['content']);
  71. if(isset($params['status']) && !in_array($params['status'],[StatusEnum::DISABLED,StatusEnum::ENABLED,StatusEnum::DELETE])) throw new Exception("参数错误");;
  72. !isset($params['member_level']) && $params['member_level'] = [];
  73. $res = Certificate::setData($params);
  74. if ($res === false) throw new Exception(Certificate::getStaticError());
  75. return $this->success('操作成功');
  76. }else{
  77. $params = Yii::$app->request->get();
  78. $res = Yii::$app->services->validate->validate($params, [
  79. [['id'], 'safe'],
  80. ]);
  81. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  82. $where = [];
  83. $where[] = ['=' ,'mall_id',$this->mall_id];
  84. $where[] = ['=' ,'id',$params['id']];
  85. $detail = Certificate::getOne($where,true);
  86. $detail['member_level'] = $detail['member_level'] !== '' ? explode(',', $detail['member_level']) : [];
  87. $detail['content'] = json_decode($detail['content']);
  88. $detail['levels'] = UserLevel::getUserLevel([['mall_id' => $this->mall_id], ['status' => StatusEnum::ENABLED]], [], true, 'id,level,name');
  89. $detail['levels'] = array_merge([['level' => 0, 'name' => '普通用户']], $detail['levels']);
  90. return $this->success('操作成功',$detail);
  91. }
  92. }
  93. } catch (\Exception $e) {
  94. return $this->error($e->getMessage(), []);
  95. }
  96. return $this->render($this->action->id);
  97. }
  98. /**
  99. * 等级列表
  100. *
  101. * @return mixed|null
  102. */
  103. public function actionGetLevelList()
  104. {
  105. $levels = UserLevel::getUserLevel([['mall_id' => $this->mall_id], ['status' => StatusEnum::ENABLED]], [], true, 'id,level,name');
  106. return $this->success('success', array_merge([['level' => 0, 'name' => '普通用户']], $levels));
  107. }
  108. }