AdController.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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 common\helpers\Cache;
  12. use common\helpers\Form;
  13. use common\helpers\Log;
  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. $lid = Yii::$app->request->get('lid');
  55. // 如果admin不是超管,只允许查询自己关联的国家
  56. $admin = Admin::findOne(Yii::$app->user->id);
  57. $roleId = $admin->ROLE_ID;
  58. if ($roleId == \Yii::$app->params['superAdminRoleId']) {
  59. $countries = Countries::find()->asArray()->all();
  60. } else {
  61. // 关联国家
  62. $countries = Countries::find()
  63. ->select('COU.ID, COU.CODE, COU.NAME')
  64. ->from(['COU' => Countries::tableName()])
  65. ->join('INNER JOIN', AdminCountry::tableName() . ' AS ADL', 'ADL.COUNTRY_ID = COU.ID')
  66. ->where(['ADL.ADMIN_ID' => $admin->ID])
  67. ->asArray()
  68. ->all();
  69. }
  70. $filter = $this->filterCondition([
  71. 'TITLE' => 'TITLE',
  72. 'COUNTRY_ID' => 'COUNTRY_ID',
  73. ]);
  74. $condition = $filter['condition'];
  75. $params = $filter['params'];
  76. $condition .= ' AND AD.LID=:LID';
  77. $params[':LID']=$lid;
  78. $countryIds = array_column($countries, 'ID');
  79. if (!empty($countryIds)) {
  80. $placeholders = [];
  81. foreach ($countryIds as $key => $id) {
  82. $paramName = ':countryId_' . $key;
  83. $placeholders[] = $paramName;
  84. $params[$paramName] = $id;
  85. }
  86. $condition .= ' AND AD.COUNTRY_ID IN (' . implode(',', $placeholders) . ')';
  87. }
  88. $data = Ad::lists($condition, $params, [
  89. 'select' => 'AD.*,ADC.NAME AS COUNTRY_NAME, ADC.CODE AS COUNTRY_CODE',
  90. 'from' => Ad::tableName().' AS AD',
  91. 'join' => [
  92. ['INNER JOIN', Countries::tableName() . ' AS ADC', 'ADC.ID=AD.COUNTRY_ID'],
  93. ],
  94. 'orderBy' => 'AD.STATUS DESC,AD.SORT DESC,AD.CREATED_AT ASC',
  95. ]);
  96. $data['allLocation'] = AdLocation::getAllLocation();
  97. $data['countries'] = $countries;
  98. $data['allArticle'] = Article::find()
  99. ->from(['ART' => Article::tableName()])
  100. ->select('ART.ID,ART.TITLE')
  101. ->andFilterWhere(['ART.COUNTRY_ID' => array_column($countries, 'ID')])
  102. ->asArray()
  103. ->all();
  104. return static::notice($data);
  105. }
  106. /**
  107. * 添加
  108. * @return mixed
  109. * @throws \yii\web\HttpException
  110. */
  111. public function actionAdd(){
  112. if(Yii::$app->request->isPost) {
  113. return parent::edit(AdForm::class, Yii::t('ctx', 'AdAddedSuccessfully'), null, null, null, function($formModel, $result){
  114. // 添加操作日志
  115. // Log::adminHandle('添加广告:'.$result->TITLE);
  116. });
  117. }
  118. // 获取全部分类
  119. $allLocation = AdLocation::getAllLocation();
  120. $allArticle = Article::findAllAsArray();
  121. return static::notice(['allLocation'=>$allLocation, 'allArticle' => $allArticle]);
  122. }
  123. /**
  124. * 编辑
  125. * @return mixed
  126. * @throws \yii\web\HttpException
  127. */
  128. public function actionEdit(){
  129. $id = Yii::$app->request->get('id');
  130. if(Yii::$app->request->isPost) {
  131. return parent::edit(AdForm::class, Yii::t('ctx', 'EditAdSuccessfully'), null, null, null, function($formModel, $result){
  132. // 添加操作日志
  133. // Log::adminHandle('编辑广告:'.$result->TITLE);
  134. });
  135. }
  136. $oneData = Ad::findOneAsArray(['ID'=>$id]);
  137. // 获取全部分类
  138. $allLocation = AdLocation::getAllLocation();
  139. $allArticle = Article::findAllAsArray();
  140. return static::notice(['oneData'=>$oneData, 'allLocation'=>$allLocation, 'allArticle' => $allArticle]);
  141. }
  142. /**
  143. * 删除
  144. * @return mixed
  145. * @throws \yii\db\Exception
  146. * @throws \yii\web\HttpException
  147. */
  148. public function actionAdDelete(){
  149. $adForm = new AdForm();
  150. $result = static::delete(Ad::class, function ($selected) use ($adForm) {
  151. $adForm->beforeDelete($selected);
  152. }, function ($selected) use ($adForm) {
  153. $adForm->delete($selected);
  154. }, true);
  155. return $result;
  156. }
  157. /**
  158. * 隐藏
  159. * @return mixed
  160. * @throws \yii\db\Exception
  161. * @throws \yii\web\HttpException
  162. */
  163. public function actionAdHide(){
  164. $adForm = new AdForm();
  165. $result = static::hide(Ad::class, 'hide', function ($selected) use ($adForm) {
  166. }, function ($selected) use ($adForm) {
  167. }, true);
  168. return $result;
  169. }
  170. /**
  171. * 取消隐藏
  172. * @return mixed
  173. * @throws \yii\db\Exception
  174. * @throws \yii\web\HttpException
  175. */
  176. public function actionAdUnHide(){
  177. $adForm = new AdForm();
  178. $result = static::hide(Ad::class, 'un-hide', function ($selected) use ($adForm) {
  179. }, function ($selected) use ($adForm) {
  180. }, true);
  181. return $result;
  182. }
  183. /**
  184. * 排序
  185. * @return mixed
  186. * @throws \yii\web\HttpException
  187. */
  188. public function actionSort(){
  189. if(Yii::$app->request->get('id')){
  190. $formModel = new AdForm();
  191. $formModel->scenario = 'sort';
  192. if($formModel->load(Yii::$app->request->get(), '') && $formModel->sortTo()){
  193. return static::notice(Yii::t('ctx', 'successfully'));
  194. } else {
  195. return static::notice(Yii::t('ctx', 'failed'), 400);
  196. }
  197. }
  198. }
  199. /**
  200. * 状态
  201. * @return mixed
  202. * @throws \yii\web\HttpException
  203. */
  204. public function actionStatus(){
  205. if(Yii::$app->request->get('id')){
  206. $formModel = new AdForm();
  207. $formModel->scenario = 'status';
  208. if($formModel->load(Yii::$app->request->get(), '') && $formModel->statusTo()){
  209. return static::notice(Yii::t('ctx', 'successfully'));
  210. } else {
  211. return static::notice(Yii::t('ctx', 'failed'), 400);
  212. }
  213. }
  214. }
  215. /**
  216. * 上传图片
  217. * @return mixed
  218. * @throws \yii\base\Exception
  219. * @throws \yii\db\Exception
  220. * @throws \yii\web\HttpException
  221. */
  222. public function actionUpload(){
  223. if(\Yii::$app->request->isPost){
  224. $formModel = new UploadForm();
  225. $formModel->scenario = 'ad';
  226. $formModel->file = UploadedFile::getInstanceByName('file');
  227. $formModel->token = \Yii::$app->request->request('uploadToken');;
  228. if($formModel->file && $uploader = $formModel->upload()){
  229. return static::notice($uploader->URL);
  230. } else {
  231. return static::notice(Form::formatErrorsForApi($formModel->getErrors()), 400);
  232. }
  233. } else {
  234. $token = Cache::setUploadToken();
  235. return static::notice($token);
  236. }
  237. }
  238. }