DefaultController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. namespace addons\Material\backend\controllers;
  3. use addons\Material\common\enums\MaterialEnum;
  4. use common\components\sensitivewords\SensitiveWords;
  5. use addons\Material\common\models\Material;
  6. use addons\Material\common\models\MaterialCate;
  7. use common\enums\StatusEnum;
  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\Material\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->isGet) {
  31. $post = Yii::$app->request->get();
  32. // 检查提交的参数
  33. $res = Yii::$app->services->validate->validate($post,[
  34. [['id','level','cate_id', 'second_cate_id'], 'integer'],
  35. [['id','keyword','check_status','date_end','date_start','mobile','user_id','user_name','describe'], 'safe'],
  36. [['limit'],'default','value' => '15'],
  37. ]);
  38. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  39. $where = [];
  40. // 根据搜索手机号查询用户id
  41. if (!empty($post['mobile'])) {
  42. $user_ids = User::find()->select("id")->where(['mall_id' => $this->mall_id])->andWhere(['like', 'mobile', $post['mobile'] . '%', false])->asArray()->column();
  43. if (empty($user_ids)) $this->success('查无此用户');
  44. $where[] = ['in', 'user_id', $user_ids];
  45. }
  46. // 根据搜索会员昵称查询用户id
  47. if (!empty($post['user_name'])) {
  48. $user_ids = User::find()->select("id")->where(['mall_id' => $this->mall_id])->andWhere(['like', 'nickname', $post['user_name'] . '%', false])->asArray()->column();
  49. $where[] = ['or',['in', 'user_id', $user_ids],['like','user_name',$post['user_name']]];
  50. }
  51. if($post['keyword']) $where[] = ['=', 'user_name', $post['keyword']];
  52. if(!empty($post['describe'])) $where[] = ['like', 'describe', trim($post['describe'])];
  53. if($post['user_id']) $where[] = ['=', 'user_id', $post['user_id']];
  54. if($post['cate_id']) $where[] = ['=', 'cate_id', $post['cate_id']];
  55. if($post['second_cate_id']) $where[] = ['=', 'second_cate_id', $post['second_cate_id']];
  56. if($post['date_start']&&$post['date_end']) $where[] = ['between', 'created_at', strtotime($post['date_start']),strtotime($post['date_end'])];
  57. $where[] = ['=', 'mall_id', $this->mall_id];
  58. if(isset($post['check_status']) && in_array($post['check_status'],[StatusEnum::ENABLED,StatusEnum::DISABLED,'2'])) {
  59. $where[] = ['=', 'check_status', $post['check_status']];
  60. } else {
  61. $where[] = ['=', 'check_status', StatusEnum::ENABLED];
  62. }
  63. $where[] = ['>=','status',StatusEnum::DISABLED];
  64. $order = 'sort,created_at desc';
  65. $params = array('where' => $where, 'order'=>$order, 'limit' => $post['limit'],'with' => ['cate','goods','user','secondCate']);
  66. $list = Material::listPage($params, false, true);
  67. // 获取分类
  68. $list['cates'] = MaterialCate::getList($this->mall_id);
  69. $this->success('获取成功', $list);
  70. }
  71. } else {
  72. return $this->render($this->action->id);
  73. }
  74. }
  75. public function actionList()
  76. {
  77. $retuest = Yii::$app->request;
  78. if ($retuest->isAjax) {
  79. if ($retuest->isGet) {
  80. $post = Yii::$app->request->get();
  81. // 检查提交的参数
  82. $res = Yii::$app->services->validate->validate($post,[
  83. [['id','level'], 'integer'],
  84. [['id','keyword','check_status','date_end','date_start','mobile','user_id','user_name'], 'safe'],
  85. [['limit'],'default','value' => '15'],
  86. ]);
  87. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  88. $where = [];
  89. // 根据搜索手机号查询用户id
  90. if (!empty($post['mobile'])) {
  91. $user_ids = User::find()->select("id")->where(['mall_id' => $this->mall_id])->andWhere(['like', 'mobile', $post['mobile'] . '%', false])->asArray()->column();
  92. if (empty($user_ids)) $this->success('查无此用户');
  93. $where[] = ['in', 'user_id', $user_ids];
  94. }
  95. // 根据搜索会员昵称查询用户id
  96. if (!empty($post['user_name'])) {
  97. $user_ids = User::find()->select("id")->where(['mall_id' => $this->mall_id])->andWhere(['like', 'nickname', $post['user_name'] . '%', false])->asArray()->column();
  98. $where[] = ['or',['in', 'user_id', $user_ids],['like','user_name',$post['user_name']]];
  99. }
  100. if($post['keyword']) $where[] = ['=', 'user_name', $post['keyword']];
  101. if($post['user_id']) $where[] = ['=', 'user_id', $post['user_id']];
  102. if($post['date_start']&&$post['date_end']) $where[] = ['between', 'created_at', strtotime($post['date_start']),strtotime($post['date_end'])];
  103. $where[] = ['=', 'mall_id', $this->mall_id];
  104. if(isset($post['check_status']) && in_array($post['check_status'],[StatusEnum::ENABLED,StatusEnum::DISABLED,'2'])) {
  105. $where[] = ['=', 'check_status', $post['check_status']];
  106. } else {
  107. $where[] = ['=', 'check_status', StatusEnum::ENABLED];
  108. }
  109. $where[] = ['>=','status',StatusEnum::DISABLED];
  110. $order = 'sort,created_at desc';
  111. $params = array('where' => $where, 'order'=>$order, 'limit' => $post['limit'],'with' => ['cate','goods','user','secondCate']);
  112. $list = Material::listPage($params, false, true);
  113. $this->success('获取成功', $list);
  114. }
  115. } else {
  116. return $this->render($this->action->id);
  117. }
  118. }
  119. /**
  120. * 编辑
  121. * @Author: ltm
  122. * @DateTime: 2021/12/13 0021 18:29
  123. * @Copyright: copyright (c) 2021 广东七件事集团
  124. * @return mixed|string|void
  125. */
  126. public function actionEdit()
  127. {
  128. try {
  129. $request = Yii::$app->request;
  130. if ($request->isAjax) {
  131. // 提交等级内容
  132. if ($request->isGet) {
  133. $params = Yii::$app->request->get();
  134. $res = Yii::$app->services->validate->validate($params, [
  135. [['id'], 'safe'],
  136. ]);
  137. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  138. $detail = Material::getOne([['id' => $params['id']]], 1, ['goods']);
  139. if(!empty($detail['goods_id'])) $detail['goods_id'] = json_decode($detail['goods_id']);
  140. $detail && $detail['pic_url'] = !empty($detail['pic_url']) ? json_decode($detail['pic_url']) : [];
  141. return $this->success('操作成功', compact('detail'));
  142. }
  143. //提交等级内容
  144. if ($request->isPost) {
  145. $params = Yii::$app->request->post();
  146. $is_type = $params['type'] == 1;
  147. if($params['type'] == 1){ //禁用操作和删除操作和审核操作
  148. $res = Yii::$app->services->validate->validate($params, [
  149. [['id','status','check_status','remark'],'safe']
  150. ]);
  151. }else{
  152. $res = Yii::$app->services->validate->validate($params, [
  153. [['cate_id','describe','download_type'], 'required'],
  154. [['second_cate_id','pic_url','id','virtual_download_num','goods_id','thumb_url','status','video_url'],'safe']
  155. ]);
  156. }
  157. $params = Material::exceptNullVal($params);
  158. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  159. $params['mall_id'] = $this->mall_id;
  160. if($params['id']) $params['updated_at'] = time();else $params['create_at'] = time();//更新时间
  161. //删除操作
  162. if(isset($params['status']) && in_array($params['status'],[StatusEnum::DISABLED,StatusEnum::ENABLED,StatusEnum::DELETE])) {
  163. $params['status'] = $params['status'];
  164. }
  165. $where[] = ['=', 'mall_id', $this->mall_id];
  166. $where[] = ['>', 'status', StatusEnum::DISABLED];
  167. //审核操作
  168. if($params['check_status']) {
  169. $params['check_status'] = $params['check_status'];
  170. $params['status'] = StatusEnum::ENABLED;
  171. }
  172. if($params['check_status']) $params['check_at'] = time();
  173. if(!empty($params['pic_url'])) $params['pic_url'] = json_encode($params['pic_url']);
  174. //验证标题内容是否有敏感词汇
  175. $config = \Yii::$app->services->setting->addons->get($this->mall_id, MaterialEnum::ADDONS_NAME, 'basic');
  176. if ($config['is_filter'] == '1') {
  177. $common = new SensitiveWords();
  178. $textStatus = $common->getTextStatus($params['describe'],$this->mall_id);
  179. if ($textStatus['status'] != 0) {
  180. return $this->error('敏感词过滤报错:'.$textStatus['msg']);
  181. }
  182. };
  183. if (!$is_type) {
  184. $params['user_name'] = $this->mall['name'] ?? ''; // 默认获取商家名称
  185. if (!empty($config['user_name'])) {
  186. // 独立配置了获取配置名称
  187. $params['user_name'] = $config['user_name'];
  188. }
  189. $mallLogo = Yii::$app->services->setting->mall->get($this->mall_id, 'basic.logo');
  190. if(empty($mallLogo)) $mallLogo = \Yii::$app->request->hostInfo.'/resources/img/admin/mall-logo.png';
  191. $params['user_avatar_url'] = $mallLogo ?? '';
  192. if (!empty($config['user_avatar_url'])) {
  193. $params['user_avatar_url'] = $config['user_avatar_url'];
  194. }
  195. }
  196. $res = Material::setData($params);
  197. if ($res === false) throw new Exception(MaterialCate::getStaticCodeError()['error']);
  198. return $this->success('操作成功');
  199. }
  200. }
  201. } catch (\Exception $e) {
  202. return $this->error($e->getMessage(), []);
  203. }
  204. return $this->render($this->action->id);
  205. }
  206. /**
  207. * 审核
  208. * @Author: ltm
  209. * @DateTime: 2021/12/13 0021 18:29
  210. * @Copyright: copyright (c) 2021 广东七件事集团
  211. * @return mixed|string|void
  212. */
  213. public function actionCheck(){
  214. return $this->render($this->action->id);
  215. }
  216. /**
  217. * 获取分类
  218. *
  219. * @return mixed
  220. */
  221. public function actionGetCateList()
  222. {
  223. $params['where'][] = ['=', 'mall_id', $this->mall_id];
  224. $params['where'][] = ['>=', 'status', StatusEnum::DISABLED];
  225. $params['where'][] = ['=', 'parent_id', 0];
  226. $params['with'] = ['children' => function ($query) {
  227. $query->select('id, id as value, name as label,parent_id');
  228. }];
  229. $params['select'] = 'id, id as value, name as label';
  230. $materil_cate_list = MaterialCate::lists($params, 1);
  231. return $this->success('ok', $materil_cate_list);
  232. }
  233. }