PromotionLists.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. namespace app\admin\controller\promotion;
  3. /**
  4. * @title : 宣传详情 管理控制器
  5. * @desc :
  6. * @Author : 系统开发
  7. * @Date : 2025-02-27 14:40:50
  8. * @icon fa fa-leaf
  9. */
  10. use app\admin\controller\Base;
  11. use app\common\model\promotion\PromotionLists as PromotionListsModel;
  12. class PromotionLists extends Base
  13. {
  14. protected $model = null;
  15. protected $noNeedLogin = ['getOptions'];
  16. protected $noNeedAuth = [];
  17. public function initialize()
  18. {
  19. parent::initialize();
  20. $this->model = new PromotionListsModel;
  21. }
  22. /**
  23. * @title:创建统一查询条件
  24. * @desc: 描述
  25. * @param {int} {type_id} {0} {所属分类}
  26. * @param {tinyint} {have_detail} {1} {有无内页图片}
  27. * @param {tinyint} {status} {1} {是否开启}
  28. * @param {string} {keyword} {} {搜索关键词,可搜索标题|副标题}
  29. * @return {*}
  30. * @author: 系统开发
  31. * @method: POST
  32. * @Date: 2025-02-27 14:40:50
  33. */
  34. private function createWhere()
  35. {
  36. $data = $this->request->param();
  37. $where = [];
  38. $whereOr = [];
  39. if(!empty($data['type_id'])){
  40. $where[] = ['type_id','=',$data['type_id']];
  41. }
  42. if(!empty($data['have_detail'])){
  43. $where[] = ['have_detail','=',$data['have_detail']];
  44. }
  45. if(!empty($data['status'])){
  46. $where[] = ['status','=',$data['status']];
  47. }
  48. if(!empty($data['keyword'])){
  49. $keyword = $data['keyword'];
  50. $where[] = ['title|subtitle','LIKE',"%$keyword%"];
  51. }
  52. return ['where'=>$where,'whereOr'=>$whereOr];
  53. }
  54. /**
  55. * @title:查询数据列表
  56. * @desc: 描述
  57. * @param {int} {type_id} {0} {所属分类}
  58. * @param {tinyint} {have_detail} {1} {有无内页图片}
  59. * @param {tinyint} {status} {1} {是否开启}
  60. * @param {string} {keyword} {} {搜索关键词,可搜索标题|副标题}
  61. * @param {int} {pageNo} {0} {页码}
  62. * @param {int} {pageSize} {10} {每页数量}
  63. * @return {*}
  64. * @author: 系统开发
  65. * @method: POST
  66. * @Date: 2025-02-27 14:40:50
  67. */
  68. public function getList(int $pageNo=0,int $pageSize=10)
  69. {
  70. $whereAry = $this->createWhere();
  71. if(!empty($pageNo)){
  72. $res = $this->model->where($whereAry['where'])->where(function($query)use($whereAry){
  73. $query->whereOr($whereAry['whereOr']);
  74. })->with(['TypeId'])->paginate(['page'=>$pageNo,'list_rows'=>$pageSize]);
  75. return pageRes(1,'获取成功',$res->total(),$res->items());
  76. }else{
  77. $list = $this->model->where($whereAry['where'])->where(function($query)use($whereAry){
  78. $query->whereOr($whereAry['whereOr']);
  79. })->with(['TypeId'])->select();
  80. return res(1,'获取成功',$list);
  81. }
  82. }
  83. /**
  84. * @title:选择器数据
  85. * @desc: 描述
  86. * @return {*}
  87. * @author: 系统开发
  88. * @method: POST
  89. * @Date: 2025-02-27 14:40:50
  90. */
  91. public function getTree($labelField='title')
  92. {
  93. $fields = ['id',$labelField];
  94. $where = [];
  95. $list = $this->model->where($where)->field($fields)->select();
  96. return res(1,"获取成功",$list);
  97. }
  98. /**
  99. * @title:查询信息
  100. * @desc: 描述
  101. * @param {int} {id} {} {信息ID}
  102. * @return {*}
  103. * @author: 系统开发
  104. * @method: POST
  105. * @Date: 2025-02-27 14:40:50
  106. */
  107. public function getInfo(int $id=0)
  108. {
  109. $info = $this->model->with(['TypeId'])->find($id);
  110. return res(1,'获取成功',$info);
  111. }
  112. /**
  113. * @title:新增/编辑信息
  114. * @desc: 描述
  115. * @param {int} {id} {} {主键ID,不传或传0表示新增,大于0表示修改}
  116. * @param {int} {type_id} {0} {所属分类}
  117. * @param {varchar} {title} {} {标题}
  118. * @param {varchar} {subtitle} {} {副标题}
  119. * @param {varchar} {detail_img} {} {详情图片}
  120. * @param {tinyint} {have_detail} {1} {有无内页图片}
  121. * @param {varchar} {inner_img} {} {内页图片}
  122. * @param {tinyint} {status} {1} {是否开启}
  123. * @return {*}
  124. * @author: 系统开发
  125. * @method: POST
  126. * @Date: 2025-02-27 14:40:50
  127. */
  128. public function doEdit()
  129. {
  130. $data = $this->request->param();
  131. $res = $this->model->replace()->save($data);
  132. return res(1,'编辑成功');
  133. }
  134. /**
  135. * @title:删除信息
  136. * @desc: 描述
  137. * @param {int} id {} {信息ID}
  138. * @return {*}
  139. * @author: 系统开发
  140. * @method: POST
  141. * @Date: 2025-02-27 14:40:50
  142. */
  143. public function doDelete(int $id=0)
  144. {
  145. $info = $this->model->find($id);
  146. $info->delete();
  147. return res(1,'删除成功');
  148. }
  149. /**
  150. * @title:获取可选项
  151. * @desc: 描述
  152. * @return {*}
  153. * @author: 系统开发
  154. * @method: POST
  155. * @Date: 2025-02-27 14:40:50
  156. */
  157. public function getOptions()
  158. {
  159. $data = [
  160. 'HaveDetailList'=>PromotionListsModel::HaveDetailList(),
  161. 'StatusList'=>PromotionListsModel::StatusList()
  162. ];
  163. return res(1,'获取成功',$data);
  164. }
  165. public function changeStatus(int $id=0)
  166. {
  167. $info = $this->model->find($id);
  168. if(!$info){
  169. return res(2,"未找到此记录");
  170. }
  171. $info->status = abs(3 * $info->status - 5);
  172. $info->save();
  173. return res(1,"操作成功");
  174. }
  175. private $fieldList = [
  176. 'index'=>'序号',
  177. 'type_id'=>'所属分类',
  178. 'title'=>'标题',
  179. 'subtitle'=>'副标题',
  180. 'detail_img'=>'详情图片',
  181. 'have_detail'=>'有无内页图片',
  182. 'inner_img'=>'内页图片',
  183. 'status'=>'是否开启',
  184. 'create_at'=>'创建时间',
  185. 'update_at'=>'更新时间'
  186. ];
  187. /**
  188. * @title:导出数据
  189. * @desc: 描述
  190. * @param {int} {type_id} {0} {所属分类}
  191. * @param {tinyint} {have_detail} {1} {有无内页图片}
  192. * @param {tinyint} {status} {1} {是否开启}
  193. * @param {string} {keyword} {} {搜索关键词,可搜索标题|副标题}
  194. * @return {*}
  195. * @author: 系统开发
  196. * @method: POST
  197. * @Date: 2025-02-27 14:40:50
  198. */
  199. public function doExport()
  200. {
  201. $whereAry = $this->createWhere();
  202. $list = $this->model->where($whereAry['where'])->where(function($query)use($whereAry){
  203. $query->whereOr($whereAry['whereOr']);
  204. })->with(['TypeId'])->select()->toArray();
  205. $head = array_values($this->fieldList);
  206. $body = [];
  207. foreach($list as $key=>$item){
  208. $index = $key + 1;
  209. foreach($this->fieldList as $k=>$v){
  210. if($k=='index'){
  211. $body[$index][] = $index;
  212. }elseif($k=='type_id'){
  213. $body[$index][]=$item['TypeId']?$item['TypeId']['title']:'';
  214. }else{
  215. $body[$index][] = isset($item[$k.'_txt'])?(is_array($item[$k.'_txt'])?implode(',',$item[$k.'_txt']):$item[$k.'_txt']):$item[$k];
  216. }
  217. }
  218. }
  219. //创建文件夹
  220. $basepath = 'uploads'.DS.'download'.DS.date('Ymd');
  221. $savepath = public_path().$basepath;
  222. if(!is_dir($savepath)){
  223. @mkdir($savepath,0777,true);
  224. }
  225. //保存文件
  226. $filename = time().GetRandStr().'.xls';
  227. $path = $savepath.DS.$filename;
  228. $export_help = new \excel\MultiHeadExcel();
  229. $export_help->export_help($body,$head,$path);
  230. //返回路径
  231. $returnpath = WEBURL.DS.$basepath.DS.$filename;
  232. slog(1,'导出了宣传详情');
  233. return res(1,'获取成功',['url'=>$returnpath,'name'=>$filename]);
  234. }
  235. public function __call($name,$arguments)
  236. {
  237. return res(2,"方法{$name}不存在");
  238. }
  239. }