AdController.php 9.2 KB

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