Adv.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /**
  3. * lemocms
  4. * ============================================================================
  5. * 版权所有 2018-2027 lemocms,并保留所有权利。
  6. * 网站地址: https://www.lemocms.com
  7. * ----------------------------------------------------------------------------
  8. * 采用最新Thinkphp6实现
  9. * ============================================================================
  10. * Author: yuege
  11. * Date: 2019/8/2
  12. */
  13. namespace app\admin\controller;
  14. use app\common\controller\Backend;
  15. use app\common\model\AdvPosition;
  16. use think\facade\Db;
  17. use think\facade\Request;
  18. use think\facade\View;
  19. use app\common\model\Adv as AdvModel;
  20. class Adv extends Backend {
  21. public function initialize(){
  22. parent::initialize();
  23. }
  24. /*-----------------------广告管理----------------------*/
  25. // 广告列表
  26. public function index()
  27. {
  28. if(Request::isPost()){
  29. $keys = Request::post('keys','','trim');
  30. $page = Request::post('page') ? Request::post('page') : 1;
  31. $list=Db::name('adv')->alias('a')
  32. ->join('adv_position ap','a.pid = ap.id','left')
  33. ->field('a.*,ap.position_name,ap.position_desc')
  34. ->where('a.ad_name','like','%'.$keys.'%')
  35. ->order('a.sort desc,a.id desc')
  36. ->paginate(['list_rows' => $this->pageSize, 'page' => $page])
  37. ->toArray();
  38. return $result = ['code'=>0,'msg'=>lang('get info success'),'data'=>$list['data'],'count'=>$list['total']];
  39. }
  40. return View::fetch();
  41. }
  42. // 广告添加
  43. public function add()
  44. {
  45. if (Request::isPost()) {
  46. $data = Request::post();
  47. try{
  48. $this->validate($data, 'Adv');
  49. }catch (\Exception $e){
  50. $this->error($e->getMessage());
  51. }
  52. if($data['time']){
  53. $time = explode(' - ',$data['time']);
  54. $data['start_time'] = strtotime($time[0]);
  55. $data['end_time'] = strtotime($time[1]);
  56. }else{
  57. $data['start_time'] = '';
  58. $data['end_time'] = '';
  59. }
  60. //添加
  61. $result = AdvModel::create($data);
  62. if ($result) {
  63. $this->success(lang('add success'), url('index'));
  64. } else {
  65. $this->error(lang('add fail'));
  66. }
  67. } else {
  68. $info = '';
  69. $posGroup = AdvPosition::where('status', 1)->select();
  70. $view = [
  71. 'info' =>$info,
  72. 'posGroup' => $posGroup,
  73. 'title' => lang('add'),
  74. ];
  75. View::assign($view);
  76. return View::fetch();
  77. }
  78. }
  79. /**
  80. * 广告修改
  81. */
  82. public function edit()
  83. {
  84. if (Request::isPost()) {
  85. $data = Request::post();
  86. try{
  87. $this->validate($data, 'Adv');
  88. }catch (\Exception $e){
  89. $this->error($e->getMessage());
  90. }
  91. AdvModel::update($data);
  92. $this->success(lang('edit success'), url('index'));
  93. } else {
  94. $id = Request::param('id');
  95. if ($id) {
  96. $posGroup = AdvPosition::where('status', 1)->select();
  97. $info = AdvModel::find($id);
  98. $info['time'] = date('Y-m-d',$info['start_time']).' - '.date('Y-m-d',$info['end_time']);
  99. $view = [
  100. 'info' => $info,
  101. 'posGroup' => $posGroup,
  102. 'title' => '编辑',
  103. ];
  104. View::assign($view);
  105. return View::fetch('add');
  106. }
  107. }
  108. }
  109. // 广告删除
  110. public function delete()
  111. {
  112. $id = Request::post('id');
  113. AdvModel::destroy($id);
  114. $this->success(lang('delete success'));
  115. }
  116. // 广告状态修改
  117. public function state()
  118. {
  119. if (Request::isPost()) {
  120. $id = Request::post('id');
  121. if (empty($id)) {
  122. $this->error('id'.lang('not exist'));
  123. }
  124. $adv = AdvModel::find($id);
  125. $status = $adv['status'] == 1 ? 0 : 1;
  126. $adv->status = $status;
  127. $adv->save();
  128. $this->success(lang('edit success'));
  129. }
  130. }
  131. /*-----------------------广告位置管理----------------------*/
  132. // 广告位置管理
  133. public function pos()
  134. {
  135. if(Request::isPost()){
  136. //条件筛选
  137. $keys = Request::param('keys');
  138. //查出所有数据
  139. $list = AdvPosition::where('position_name','like','%'.$keys.'%')
  140. ->order('id desc')
  141. ->paginate(
  142. $this->pageSize, false,
  143. ['query' => Request::param()]
  144. )->toArray();
  145. return $result = ['code'=>0,'msg'=>lang('get info success'),'data'=>$list['data'],'count'=>$list['total']];
  146. }
  147. return View::fetch();
  148. }
  149. // 广告位置添加
  150. public function posAdd()
  151. {
  152. if (Request::isPost()) {
  153. $data = Request::post();
  154. try {
  155. $this->validate($data, 'AdvPosition');
  156. } catch (\Exception $e) {
  157. $this->error($e->getMessage());
  158. }
  159. $result = AdvPosition::create($data);
  160. if ($result) {
  161. $this->success(lang('add success'), url('pos'));
  162. } else {
  163. $this->error(lang('add fail'));
  164. }
  165. } else {
  166. $view = [
  167. 'info' => null,
  168. 'title' => lang('add')
  169. ];
  170. View::assign($view);
  171. return View::fetch('pos_add');
  172. }
  173. }
  174. // 广告位置修改
  175. public function posEdit()
  176. {
  177. if (Request::isPost()) {
  178. $data = Request::post();
  179. try{
  180. $this->validate($data, 'AdvPosition');
  181. }catch (\Exception $e){
  182. $this->error($e->getMessage());
  183. }
  184. $where['id'] = $data['id'];
  185. $res = AdvPosition::update($data, $where);
  186. if($res){
  187. $this->success(lang('edit success'), url('pos'));
  188. }else{
  189. $this->error(lang('edit fail'));
  190. }
  191. } else {
  192. $id = Request::param('id');
  193. $info = AdvPosition::find(['id' => $id]);
  194. $view = [
  195. 'info' => $info,
  196. 'title' => lang('edit')
  197. ];
  198. View::assign($view);
  199. return View::fetch('pos_add');
  200. }
  201. }
  202. // 广告位置状态修改
  203. public function posState()
  204. {
  205. if (Request::isPost()) {
  206. $id = Request::param('id');
  207. $info = AdvPosition::find($id);
  208. $info->status = $info['status'] == 1 ? 0 : 1;
  209. $info->save();
  210. $this->success(lang('edit success'));
  211. }
  212. }
  213. // 广告位置删除
  214. public function posDel()
  215. {
  216. $id = Request::post('id');
  217. AdvPosition::destroy($id);
  218. $this->success(lang('delete success'));
  219. }
  220. }