model = new PromotionListsModel; } /** * @title:创建统一查询条件 * @desc: 描述 * @param {int} {type_id} {0} {所属分类} * @param {tinyint} {have_detail} {1} {有无内页图片} * @param {tinyint} {status} {1} {是否开启} * @param {string} {keyword} {} {搜索关键词,可搜索标题|副标题} * @return {*} * @author: 系统开发 * @method: POST * @Date: 2025-02-27 14:40:50 */ private function createWhere() { $data = $this->request->param(); $where = []; $whereOr = []; if(!empty($data['type_id'])){ $where[] = ['type_id','=',$data['type_id']]; } if(!empty($data['have_detail'])){ $where[] = ['have_detail','=',$data['have_detail']]; } if(!empty($data['status'])){ $where[] = ['status','=',$data['status']]; } if(!empty($data['keyword'])){ $keyword = $data['keyword']; $where[] = ['title|subtitle','LIKE',"%$keyword%"]; } return ['where'=>$where,'whereOr'=>$whereOr]; } /** * @title:查询数据列表 * @desc: 描述 * @param {int} {type_id} {0} {所属分类} * @param {tinyint} {have_detail} {1} {有无内页图片} * @param {tinyint} {status} {1} {是否开启} * @param {string} {keyword} {} {搜索关键词,可搜索标题|副标题} * @param {int} {pageNo} {0} {页码} * @param {int} {pageSize} {10} {每页数量} * @return {*} * @author: 系统开发 * @method: POST * @Date: 2025-02-27 14:40:50 */ public function getList(int $pageNo=0,int $pageSize=10) { $whereAry = $this->createWhere(); if(!empty($pageNo)){ $res = $this->model->where($whereAry['where'])->where(function($query)use($whereAry){ $query->whereOr($whereAry['whereOr']); })->with(['TypeId'])->paginate(['page'=>$pageNo,'list_rows'=>$pageSize]); return pageRes(1,'获取成功',$res->total(),$res->items()); }else{ $list = $this->model->where($whereAry['where'])->where(function($query)use($whereAry){ $query->whereOr($whereAry['whereOr']); })->with(['TypeId'])->select(); return res(1,'获取成功',$list); } } /** * @title:选择器数据 * @desc: 描述 * @return {*} * @author: 系统开发 * @method: POST * @Date: 2025-02-27 14:40:50 */ public function getTree($labelField='title') { $fields = ['id',$labelField]; $where = []; $list = $this->model->where($where)->field($fields)->select(); return res(1,"获取成功",$list); } /** * @title:查询信息 * @desc: 描述 * @param {int} {id} {} {信息ID} * @return {*} * @author: 系统开发 * @method: POST * @Date: 2025-02-27 14:40:50 */ public function getInfo(int $id=0) { $info = $this->model->with(['TypeId'])->find($id); return res(1,'获取成功',$info); } /** * @title:新增/编辑信息 * @desc: 描述 * @param {int} {id} {} {主键ID,不传或传0表示新增,大于0表示修改} * @param {int} {type_id} {0} {所属分类} * @param {varchar} {title} {} {标题} * @param {varchar} {subtitle} {} {副标题} * @param {varchar} {detail_img} {} {详情图片} * @param {tinyint} {have_detail} {1} {有无内页图片} * @param {varchar} {inner_img} {} {内页图片} * @param {tinyint} {status} {1} {是否开启} * @return {*} * @author: 系统开发 * @method: POST * @Date: 2025-02-27 14:40:50 */ public function doEdit() { $data = $this->request->param(); $res = $this->model->replace()->save($data); return res(1,'编辑成功'); } /** * @title:删除信息 * @desc: 描述 * @param {int} id {} {信息ID} * @return {*} * @author: 系统开发 * @method: POST * @Date: 2025-02-27 14:40:50 */ public function doDelete(int $id=0) { $info = $this->model->find($id); $info->delete(); return res(1,'删除成功'); } /** * @title:获取可选项 * @desc: 描述 * @return {*} * @author: 系统开发 * @method: POST * @Date: 2025-02-27 14:40:50 */ public function getOptions() { $data = [ 'HaveDetailList'=>PromotionListsModel::HaveDetailList(), 'StatusList'=>PromotionListsModel::StatusList() ]; return res(1,'获取成功',$data); } public function changeStatus(int $id=0) { $info = $this->model->find($id); if(!$info){ return res(2,"未找到此记录"); } $info->status = abs(3 * $info->status - 5); $info->save(); return res(1,"操作成功"); } private $fieldList = [ 'index'=>'序号', 'type_id'=>'所属分类', 'title'=>'标题', 'subtitle'=>'副标题', 'detail_img'=>'详情图片', 'have_detail'=>'有无内页图片', 'inner_img'=>'内页图片', 'status'=>'是否开启', 'create_at'=>'创建时间', 'update_at'=>'更新时间' ]; /** * @title:导出数据 * @desc: 描述 * @param {int} {type_id} {0} {所属分类} * @param {tinyint} {have_detail} {1} {有无内页图片} * @param {tinyint} {status} {1} {是否开启} * @param {string} {keyword} {} {搜索关键词,可搜索标题|副标题} * @return {*} * @author: 系统开发 * @method: POST * @Date: 2025-02-27 14:40:50 */ public function doExport() { $whereAry = $this->createWhere(); $list = $this->model->where($whereAry['where'])->where(function($query)use($whereAry){ $query->whereOr($whereAry['whereOr']); })->with(['TypeId'])->select()->toArray(); $head = array_values($this->fieldList); $body = []; foreach($list as $key=>$item){ $index = $key + 1; foreach($this->fieldList as $k=>$v){ if($k=='index'){ $body[$index][] = $index; }elseif($k=='type_id'){ $body[$index][]=$item['TypeId']?$item['TypeId']['title']:''; }else{ $body[$index][] = isset($item[$k.'_txt'])?(is_array($item[$k.'_txt'])?implode(',',$item[$k.'_txt']):$item[$k.'_txt']):$item[$k]; } } } //创建文件夹 $basepath = 'uploads'.DS.'download'.DS.date('Ymd'); $savepath = public_path().$basepath; if(!is_dir($savepath)){ @mkdir($savepath,0777,true); } //保存文件 $filename = time().GetRandStr().'.xls'; $path = $savepath.DS.$filename; $export_help = new \excel\MultiHeadExcel(); $export_help->export_help($body,$head,$path); //返回路径 $returnpath = WEBURL.DS.$basepath.DS.$filename; slog(1,'导出了宣传详情'); return res(1,'获取成功',['url'=>$returnpath,'name'=>$filename]); } public function __call($name,$arguments) { return res(2,"方法{$name}不存在"); } }