初始化项目:添加后端代码、ThinkPHP框架、前端资源
This commit is contained in:
@@ -0,0 +1,228 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\app\exchange;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use think\Db;
|
||||
use PHPExcel_IOFactory;
|
||||
use PHPExcel;
|
||||
/**
|
||||
* 兑换码列管理
|
||||
*
|
||||
* @icon fa fa-circle-o
|
||||
*/
|
||||
class Code extends Backend
|
||||
{
|
||||
|
||||
/**
|
||||
* Code模型对象
|
||||
* @var \app\admin\model\app\exchange\Code
|
||||
*/
|
||||
protected $model = null;
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = new \app\admin\model\app\exchange\Code;
|
||||
$this->batchModel = new \app\admin\model\app\exchange\Batch;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
|
||||
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
|
||||
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* 查看
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
//当前是否为关联查询
|
||||
$this->relationSearch = true;
|
||||
//设置过滤方法
|
||||
$this->request->filter(['strip_tags', 'trim']);
|
||||
//如果发送的来源是Selectpage,则转发到Selectpage
|
||||
if ($this->request->request('keyField')) {
|
||||
return $this->selectpage();
|
||||
}
|
||||
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
||||
|
||||
$list = $this->model
|
||||
->with(['batch','user'])
|
||||
->where($where)
|
||||
->order($sort, $order)
|
||||
->paginate($limit);
|
||||
|
||||
foreach ($list as &$row){
|
||||
$row->getRelation('user')->visible(\app\admin\model\User::$listShowField);
|
||||
|
||||
$row->link = $this->getLinkUrl($row['code']);
|
||||
}
|
||||
|
||||
|
||||
$result = array("total" => $list->total(), "rows" => $list->items(),'attr'=>[
|
||||
'status'=>$this->model->getStatusList()
|
||||
]);
|
||||
|
||||
return $this->success("获取成功",$result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取订单超时取消地址
|
||||
* @return string
|
||||
*/
|
||||
public function getLinkUrl($code){
|
||||
global $_W;
|
||||
|
||||
return $_W['siteroot'].'/exchange/'.$_W['uniacid'].'/'.$code;
|
||||
}
|
||||
|
||||
protected $exportIndex = 0;
|
||||
|
||||
/**
|
||||
* 导出兑换码
|
||||
* @return void
|
||||
*/
|
||||
public function export(){
|
||||
|
||||
list($where, $sort, $order, $offset, $limit, $page, $alias, $bind,$originWhere) = $this->buildparams();
|
||||
|
||||
$batchId = 0;
|
||||
|
||||
if(!empty($originWhere)){
|
||||
foreach ($originWhere as $item){
|
||||
if(!empty($item) && isset($item[0]) && $item[0] == 'batch_id'){
|
||||
$batchId = $item[2];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$batchInfo = $this->batchModel->where([
|
||||
'id'=>$batchId
|
||||
])->find();
|
||||
|
||||
$excelTitle = '兑换码';
|
||||
|
||||
if($batchInfo){
|
||||
$excelTitle = "批次【{$batchInfo['name']}】兑换码";
|
||||
}
|
||||
|
||||
$objPHPExcel = new \PHPExcel();
|
||||
//设置Excel属性
|
||||
$objPHPExcel->getProperties()
|
||||
->setTitle($excelTitle); //设置标题
|
||||
|
||||
$objPHPExcel->getActiveSheet()->setCellValue('A1', '批次');//可以指定位置
|
||||
$objPHPExcel->getActiveSheet()->setCellValue('B1', '兑换码');
|
||||
$objPHPExcel->getActiveSheet()->setCellValue('C1', '兑换链接');
|
||||
$objPHPExcel->getActiveSheet()->setCellValue('D1', '状态');
|
||||
$objPHPExcel->getActiveSheet()->setCellValue('E1', '兑换用户');
|
||||
$objPHPExcel->getActiveSheet()->setCellValue('F1', '兑换时间');
|
||||
$objPHPExcel->getActiveSheet()->setCellValue('G1', '创建时间');
|
||||
$objPHPExcel->getActiveSheet()->setCellValue('H1', '备注');
|
||||
|
||||
|
||||
// 给表格添加数据
|
||||
$this->model
|
||||
->with(['batch','user'])
|
||||
->where($where)
|
||||
->chunk(500,function($list) use ($objPHPExcel) {
|
||||
if($list){
|
||||
foreach ($list as $key => $item){
|
||||
$objPHPExcel->getActiveSheet()->setCellValue('A'.(2 + $this->exportIndex), $item['batch']['name']);
|
||||
$objPHPExcel->getActiveSheet()->setCellValue('B'.(2 + $this->exportIndex), $item['code']);
|
||||
$objPHPExcel->getActiveSheet()->setCellValue('C'.(2 + $this->exportIndex), $this->getLinkUrl($item['code']));
|
||||
$objPHPExcel->getActiveSheet()->setCellValue('D'.(2 + $this->exportIndex), $item['status'] == 1 ? '未使用' : ($item['status'] == 2 ? '已使用' :'禁用'));
|
||||
$objPHPExcel->getActiveSheet()->setCellValue('E'.(2 + $this->exportIndex), $item['user'] ? (string)$item['user']['username'] : '-');
|
||||
$objPHPExcel->getActiveSheet()->setCellValue('F'.(2 + $this->exportIndex), $item['use_time'] ? date('Y-m-d H:i:s',$item['use_time']) : '-');
|
||||
$objPHPExcel->getActiveSheet()->setCellValue('G'.(2 + $this->exportIndex), date('Y-m-d h:i:s',$item['createtime']));
|
||||
$objPHPExcel->getActiveSheet()->setCellValue('H'.(2 + $this->exportIndex), $item['remark'] ? $item['remark'] : '-');
|
||||
$this->exportIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
},'code.id','desc');
|
||||
//设置字体
|
||||
$objPHPExcel->getActiveSheet()->getStyle('A1:H1')->getFont()->setBold(true)->getColor()->setARGB(\PHPExcel_Style_Color::COLOR_BLACK);
|
||||
//设置水平居中
|
||||
$objPHPExcel->getActiveSheet()->getStyle('A1:H1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
|
||||
//设置背景颜色
|
||||
$objPHPExcel->getActiveSheet()->getStyle('A1:H1')->getFill()->setFillType(\PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setARGB('D9FFC125');
|
||||
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
|
||||
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
|
||||
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(50);
|
||||
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(10);
|
||||
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(25);
|
||||
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(18);
|
||||
$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(18);
|
||||
$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(40);
|
||||
//激活当前表
|
||||
$objPHPExcel->setActiveSheetIndex(0);
|
||||
ob_end_clean();//清除缓冲区,避免乱码
|
||||
|
||||
//弹出提示下载文件
|
||||
header('pragma:public');
|
||||
header("Content-Disposition:attachment;filename={$excelTitle}.xlsx");
|
||||
header('Cache-Control: max-age=0');
|
||||
$objWriter = PHPExcel_IOFactory:: createWriter($objPHPExcel, 'Excel2007');
|
||||
$objWriter->save( 'php://output');
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换状态
|
||||
* @param $ids
|
||||
* @param $status
|
||||
* @return void
|
||||
*/
|
||||
public function status($ids=null,$status=1){
|
||||
if (false === $this->request->isPost()) {
|
||||
$this->error(__("Invalid parameters"));
|
||||
}
|
||||
$ids = $ids ?: $this->request->post("ids");
|
||||
if (empty($ids)) {
|
||||
$this->error(__('Parameter %s can not be empty', 'ids'));
|
||||
}
|
||||
|
||||
$status = $status ?: $this->request->post("status");
|
||||
|
||||
$pk = $this->model->getPk();
|
||||
$adminIds = $this->getDataLimitAdminIds();
|
||||
if (is_array($adminIds)) {
|
||||
$this->model->where($this->dataLimitField, 'in', $adminIds);
|
||||
}
|
||||
$list = $this->model->where($pk, 'in', $ids)->select();
|
||||
|
||||
$count = 0;
|
||||
Db::startTrans();
|
||||
try {
|
||||
foreach ($list as $item) {
|
||||
if($item['status'] == 2){
|
||||
continue;
|
||||
}
|
||||
|
||||
$row = [
|
||||
'status'=>$status
|
||||
];
|
||||
|
||||
$this->model->where([
|
||||
'id'=>$item['id']
|
||||
])->update($row);
|
||||
$count++;
|
||||
}
|
||||
Db::commit();
|
||||
} catch (PDOException|Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
if ($count) {
|
||||
$this->success("操作成功");
|
||||
}
|
||||
$this->error(__('未操作任何数据'));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user