AdController.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: leo
  5. * Date: 2018/2/24
  6. * Time: 下午12:48
  7. */
  8. namespace backendApi\modules\v1\controllers;
  9. use backendApi\modules\v1\models\Admin;
  10. use backendApi\modules\v1\models\AdminCountry;
  11. use backendApi\modules\v1\models\lists\ad\AdIndexList;
  12. use common\helpers\Cache;
  13. use common\helpers\Form;
  14. use common\helpers\LoggerTool;
  15. use common\models\Ad;
  16. use common\models\AdLocation;
  17. use common\models\Article;
  18. use common\models\Countries;
  19. use common\models\forms\AdForm;
  20. use common\models\forms\UploadForm;
  21. use Yii;
  22. use yii\web\UploadedFile;
  23. class AdController extends BaseController
  24. {
  25. public $modelClass = Ad::class;
  26. public function actions() {
  27. return parent::actions(); // TODO: Change the autogenerated stub
  28. }
  29. /**
  30. * 广告位列表
  31. * @return mixed
  32. * @throws \yii\web\HttpException
  33. */
  34. public function actionLocation(){
  35. $condition = '';
  36. $params = [];
  37. $data = AdLocation::lists($condition, $params, [
  38. 'select' => 'ADL.*,ADMC.ADMIN_NAME CREATE_ADMIN_NAME,ADMU.ADMIN_NAME UPDATE_ADMIN_NAME',
  39. 'from' => AdLocation::tableName().' AS ADL',
  40. 'join' => [
  41. ['LEFT JOIN', Admin::tableName() . ' AS ADMC', 'ADMC.ID=ADL.CREATE_ADMIN'],
  42. ['LEFT JOIN', Admin::tableName() . ' AS ADMU', 'ADMU.ID=ADL.UPDATE_ADMIN'],
  43. ],
  44. 'orderBy' => 'ADL.CREATED_AT ASC',
  45. ]);
  46. return static::notice($data);
  47. }
  48. /**
  49. * 列表
  50. * @return mixed
  51. * @throws \yii\web\HttpException
  52. */
  53. public function actionList()
  54. {
  55. // 获取位置ID
  56. $lid = Yii::$app->request->get('lid', 0);
  57. if (empty($lid)) {
  58. throw new \yii\web\HttpException(400, Yii::t('ctx', 'paramError'));
  59. }
  60. // 如果admin不是超管,只允许查询自己关联的国家
  61. $admin = Admin::findOne(Yii::$app->user->id);
  62. $roleId = $admin->ROLE_ID;
  63. if ($roleId == \Yii::$app->params['superAdminRoleId']) {
  64. $countries = Countries::find()->asArray()->all();
  65. } else {
  66. // 关联国家
  67. $countries = Countries::find()
  68. ->select('COU.ID, COU.CODE, COU.NAME')
  69. ->from(['COU' => Countries::tableName()])
  70. ->join('INNER JOIN', AdminCountry::tableName() . ' AS ADL', 'ADL.COUNTRY_ID = COU.ID')
  71. ->where(['ADL.ADMIN_ID' => $admin->ID])
  72. ->asArray()
  73. ->all();
  74. }
  75. $filter = $this->filterCondition([
  76. 'AD.TITLE' => 'TITLE',
  77. 'AD.COUNTRY_ID' => 'COUNTRY_ID',
  78. 'AD.STATUS' => 'STATUS',
  79. ]);
  80. $condition = $filter['condition'];
  81. $params = $filter['params'];
  82. $condition .= ' AND AD.LID=:LID';
  83. $params[':LID'] = $lid;
  84. $countryIds = array_column($countries, 'ID');
  85. if (!empty($countryIds)) {
  86. $placeholders = [];
  87. foreach ($countryIds as $key => $id) {
  88. $paramName = ':countryId_' . $key;
  89. $placeholders[] = $paramName;
  90. $params[$paramName] = $id;
  91. }
  92. $condition .= ' AND AD.COUNTRY_ID IN (' . implode(',', $placeholders) . ')';
  93. }
  94. $obj = new AdIndexList();
  95. // 直接获取完整的数据,包括columnsShow和filterTypes
  96. $data = $obj->getList(['condition' => $condition, 'params' => $params]);
  97. LoggerTool::error($data);
  98. // 添加额外数据
  99. $data['allLocation'] = AdLocation::getAllLocation();
  100. $data['countries'] = $countries;
  101. $data['allArticle'] = Article::find()
  102. ->from(['ART' => Article::tableName()])
  103. ->select('ART.ID,ART.TITLE')
  104. ->andFilterWhere(['ART.COUNTRY_ID' => array_column($countries, 'ID')])
  105. ->asArray()
  106. ->all();
  107. return static::notice($data);
  108. }
  109. /**
  110. * 添加
  111. * @return mixed
  112. * @throws \yii\web\HttpException
  113. */
  114. public function actionAdd(){
  115. if(Yii::$app->request->isPost) {
  116. return parent::edit(AdForm::class, Yii::t('ctx', 'AdAddedSuccessfully'), null, null, null, function($formModel, $result){
  117. // 添加操作日志
  118. // Log::adminHandle('添加广告:'.$result->TITLE);
  119. });
  120. }
  121. // 获取全部分类
  122. $allLocation = AdLocation::getAllLocation();
  123. $allArticle = Article::findAllAsArray();
  124. return static::notice(['allLocation'=>$allLocation, 'allArticle' => $allArticle]);
  125. }
  126. /**
  127. * 编辑
  128. * @return mixed
  129. * @throws \yii\web\HttpException
  130. */
  131. public function actionEdit(){
  132. $id = Yii::$app->request->get('id');
  133. if(Yii::$app->request->isPost) {
  134. return parent::edit(AdForm::class, Yii::t('ctx', 'EditAdSuccessfully'), null, null, null, function($formModel, $result){
  135. // 添加操作日志
  136. // Log::adminHandle('编辑广告:'.$result->TITLE);
  137. });
  138. }
  139. $oneData = Ad::findOneAsArray(['ID'=>$id]);
  140. // 获取全部分类
  141. $allLocation = AdLocation::getAllLocation();
  142. // 关联文章
  143. $allArticle = Article::find()
  144. ->from(['ART' => Article::tableName()])
  145. ->select('ART.ID,ART.TITLE')
  146. ->andFilterWhere(['ART.COUNTRY_ID' => $oneData['COUNTRY_ID']])
  147. ->asArray()
  148. ->all();
  149. return static::notice(['oneData'=>$oneData, 'allLocation'=>$allLocation, 'allArticle' => $allArticle]);
  150. }
  151. /**
  152. * 删除
  153. * @return mixed
  154. * @throws \yii\db\Exception
  155. * @throws \yii\web\HttpException
  156. */
  157. public function actionAdDelete(){
  158. $adForm = new AdForm();
  159. $result = static::delete(Ad::class, function ($selected) use ($adForm) {
  160. $adForm->beforeDelete($selected);
  161. }, function ($selected) use ($adForm) {
  162. $adForm->delete($selected);
  163. }, true);
  164. return $result;
  165. }
  166. /**
  167. * 隐藏
  168. * @return mixed
  169. * @throws \yii\db\Exception
  170. * @throws \yii\web\HttpException
  171. */
  172. public function actionAdHide(){
  173. $adForm = new AdForm();
  174. $result = static::hide(Ad::class, 'hide', function ($selected) use ($adForm) {
  175. }, function ($selected) use ($adForm) {
  176. }, true);
  177. return $result;
  178. }
  179. /**
  180. * 取消隐藏
  181. * @return mixed
  182. * @throws \yii\db\Exception
  183. * @throws \yii\web\HttpException
  184. */
  185. public function actionAdUnHide(){
  186. $adForm = new AdForm();
  187. $result = static::hide(Ad::class, 'un-hide', function ($selected) use ($adForm) {
  188. }, function ($selected) use ($adForm) {
  189. }, true);
  190. return $result;
  191. }
  192. /**
  193. * 排序
  194. * @return mixed
  195. * @throws \yii\web\HttpException
  196. */
  197. public function actionSort(){
  198. if(Yii::$app->request->get('id')){
  199. $formModel = new AdForm();
  200. $formModel->scenario = 'sort';
  201. if($formModel->load(Yii::$app->request->get(), '') && $formModel->sortTo()){
  202. return static::notice(Yii::t('ctx', 'successfully'));
  203. } else {
  204. return static::notice(Yii::t('ctx', 'failed'), 400);
  205. }
  206. }
  207. }
  208. /**
  209. * 状态
  210. * @return mixed
  211. * @throws \yii\web\HttpException
  212. */
  213. public function actionStatus(){
  214. if(Yii::$app->request->get('id')){
  215. $formModel = new AdForm();
  216. $formModel->scenario = 'status';
  217. if($formModel->load(Yii::$app->request->get(), '') && $formModel->statusTo()){
  218. return static::notice(Yii::t('ctx', 'successfully'));
  219. } else {
  220. return static::notice(Yii::t('ctx', 'failed'), 400);
  221. }
  222. }
  223. }
  224. /**
  225. * 上传图片
  226. * @return mixed
  227. * @throws \yii\base\Exception
  228. * @throws \yii\db\Exception
  229. * @throws \yii\web\HttpException
  230. */
  231. public function actionUpload(){
  232. if(\Yii::$app->request->isPost){
  233. $formModel = new UploadForm();
  234. $formModel->scenario = 'ad';
  235. $formModel->file = UploadedFile::getInstanceByName('file');
  236. $formModel->token = \Yii::$app->request->request('uploadToken');;
  237. if($formModel->file && $uploader = $formModel->upload()){
  238. return static::notice($uploader->URL);
  239. } else {
  240. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  241. }
  242. } else {
  243. $token = Cache::setUploadToken();
  244. return static::notice($token);
  245. }
  246. }
  247. public function actionCountry()
  248. {
  249. $admin = Admin::findOne(Yii::$app->user->id);
  250. $roleId = $admin->ROLE_ID;
  251. if ($roleId == \Yii::$app->params['superAdminRoleId']) {
  252. $countries = Countries::find()->asArray()->all();
  253. } else {
  254. // 关联国家
  255. $countries = Countries::find()
  256. ->select('COU.ID, COU.CODE, COU.NAME')
  257. ->from(['COU' => Countries::tableName()])
  258. ->join('INNER JOIN', AdminCountry::tableName() . ' AS ADL', 'ADL.COUNTRY_ID = COU.ID')
  259. ->where(['ADL.ADMIN_ID' => $admin->ID])
  260. ->asArray()
  261. ->all();
  262. }
  263. return static::notice($countries);
  264. }
  265. }