MaterialController.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace addons\BusinessCard\backend\controllers;
  3. use addons\BusinessCard\common\enums\CardEnums;
  4. use addons\BusinessCard\common\models\BusinessCardMaterials;
  5. use addons\BusinessCard\common\models\BusinessCardMaterialsCate;
  6. use common\enums\StatusEnum;
  7. use common\models\user\User;
  8. use Yii;
  9. class MaterialController extends BaseController
  10. {
  11. public $enableCsrfValidation = false;
  12. public function actionIndex()
  13. {
  14. if (Yii::$app->request->isAjax && Yii::$app->request->isGet) {
  15. $get = Yii::$app->request->get();
  16. $where = [['=', 'mall_id', $this->mall_id], ['<>', 'status' , StatusEnum::DELETE]];
  17. if (isset($get['id']) && $get['id']) {
  18. $where[] = ['=', 'id', $get['id']];
  19. }
  20. if (isset($get['user_id']) && $get['user_id']) {
  21. $where[] = ['=', 'user_id', $get['user_id']];
  22. }
  23. if (isset($get['ext_type']) && $get['ext_type']) {
  24. $where[] = ['=', 'ext_type', $get['ext_type']];
  25. }
  26. if (isset($get['cate_id']) && $get['cate_id']) {
  27. $where[] = ['=', 'cate_id', $get['cate_id']];
  28. }
  29. if (isset($get['title']) && $get['title']) {
  30. $where[] = ['like', 'title', '%' . trim($get['title']) . '%', false];
  31. }
  32. // 昵称、手机号
  33. if (isset($get['nickname']) && $get['nickname']) {
  34. $user_ids = User::find()
  35. ->orWhere(['like', 'nickname', "%" . $get['nickname'] . "%", false])
  36. ->orWhere(['like', 'mobile', "%" . $get['nickname'] . "%", false])
  37. ->select('id')
  38. ->asArray()
  39. ->column();
  40. $where[] = ['in', 'user_id', $user_ids];
  41. }
  42. $params = [
  43. 'limit' => 10,
  44. 'where' => $where,
  45. 'with' => ['cate', 'user'],
  46. 'order' => 'created_at desc'
  47. ];
  48. $list = BusinessCardMaterials::listPage($params);
  49. $list['material_ext_type'] = CardEnums::getMaterialExtType();
  50. return $this->success('操作成功', $list);
  51. } else {
  52. return $this->render('index');
  53. }
  54. }
  55. // 处理
  56. public function actionHandle()
  57. {
  58. if (Yii::$app->request->isAjax && Yii::$app->request->isPost) {
  59. $params = Yii::$app->request->post();
  60. $res = Yii::$app->services->validate->validate($params, [
  61. [['id', 'status', 'is_show'], 'integer'],
  62. ]);
  63. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  64. $params = array_filter($params, function ($val) {
  65. return $val != null;
  66. });
  67. $params['mall_id'] = $this->mall_id;
  68. $res = BusinessCardMaterials::setData($params);
  69. if ($res == false) return $this->error("操作失败: " . BusinessCardMaterials::getStaticError());
  70. return $this->success("操作成功");
  71. }
  72. }
  73. // 获取素材分类
  74. public function actionCateList()
  75. {
  76. $list = BusinessCardMaterialsCate::find()
  77. ->select(['id', 'name'])
  78. ->andWhere(['<>', 'status', StatusEnum::DELETE])
  79. ->andWhere(['=', 'mall_id', $this->mall_id])
  80. ->asArray()
  81. ->all();
  82. return $this->success('操作成功', $list);
  83. }
  84. // 编辑
  85. public function actionEdit()
  86. {
  87. if (Yii::$app->request->isAjax && Yii::$app->request->isPost) {
  88. $params = Yii::$app->request->post();
  89. $res = Yii::$app->services->validate->validate($params, [
  90. [['id', 'user_id', 'is_show', 'cate_id', 'status'], 'integer'],
  91. [['title', 'intro', 'ext_type', 'share_title', 'share_desc', 'cover_img', 'material_link'], 'string'],
  92. [['material_cont'], 'safe'],
  93. ]);
  94. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  95. $params = array_filter($params, function ($val) {
  96. return $val != null;
  97. });
  98. $params['mall_id'] = $this->mall_id;
  99. $params['status'] = isset($params['status']) ? $params['status'] : StatusEnum::ENABLED;
  100. $res = BusinessCardMaterials::setData($params);
  101. if ($res == false) return $this->error("操作失败: " . BusinessCardMaterials::getStaticError());
  102. return $this->success('操作成功', []);
  103. } else {
  104. return $this->render('edit');
  105. }
  106. }
  107. // 获取视频格式
  108. public function actionExtType() {
  109. $list = CardEnums::getMaterialExtType();
  110. return $this->success('操作成功', $list);
  111. }
  112. public function actionInfo()
  113. {
  114. if (Yii::$app->request->isAjax && Yii::$app->request->isGet) {
  115. $id = Yii::$app->request->get('id');
  116. $info = BusinessCardMaterials::getOne([
  117. ['=', 'id', $id],
  118. ['=', 'mall_id', $this->mall_id]
  119. ], true);
  120. $is_null = $info ? 0 : 1;
  121. return $this->success('操作成功', compact('info', 'is_null'));
  122. }
  123. }
  124. }