初始化项目:添加后端代码、ThinkPHP框架、前端资源
This commit is contained in:
@@ -0,0 +1,199 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\app\coupon;
|
||||
use think\Db;
|
||||
use app\common\controller\Backend;
|
||||
use app\admin\model\app\coupon\Coupon as CouponModel;
|
||||
use app\admin\model\app\coupon\CouponUser as CouponUserModel;
|
||||
use app\common\traits\app\CouponSend;
|
||||
use app\admin\model\app\coupon\CouponGoods;
|
||||
use PHPExcel_IOFactory;
|
||||
/**
|
||||
* 领券用户
|
||||
*/
|
||||
class User extends Backend
|
||||
{
|
||||
protected $model = null;
|
||||
|
||||
use CouponSend;
|
||||
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->model = new CouponUserModel;
|
||||
}
|
||||
|
||||
public function index(){
|
||||
|
||||
|
||||
//当前是否为关联查询
|
||||
$this->relationSearch = true;
|
||||
//设置过滤方法
|
||||
$this->request->filter(['strip_tags', 'trim']);
|
||||
|
||||
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
||||
|
||||
$list = $this->model
|
||||
->with(['user', 'order'])
|
||||
->where($where)
|
||||
->order($sort, $order)
|
||||
->paginate($limit);
|
||||
|
||||
foreach ($list as $row) {
|
||||
}
|
||||
|
||||
$result = array("total" => $list->total(), "rows" => $list->items(),'attr'=>[
|
||||
'statusList'=>$this->model->statusList(),
|
||||
'getTypeList'=>$this->model->getTypeList(),
|
||||
]);
|
||||
|
||||
return $this->success("获取成功",$result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换状态
|
||||
* @param $ids
|
||||
* @param $status
|
||||
* @return void
|
||||
*/
|
||||
public function ban($ids=null){
|
||||
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'));
|
||||
}
|
||||
|
||||
$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) {
|
||||
$row = [
|
||||
'status'=>2
|
||||
];
|
||||
$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(__('未操作任何数据'));
|
||||
}
|
||||
|
||||
protected $exportIndex = 0;
|
||||
/**
|
||||
* 导出领取用户
|
||||
* @return void
|
||||
*/
|
||||
public function export(){
|
||||
|
||||
//当前是否为关联查询
|
||||
// $this->relationSearch = true;
|
||||
//设置过滤方法
|
||||
$this->request->filter(['strip_tags', 'trim']);
|
||||
|
||||
$excelTitle = '领/用券用户【'.date('Y-m-d h:i',time()).'】';
|
||||
|
||||
|
||||
$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', '用券时间');
|
||||
|
||||
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
||||
|
||||
// 给表格添加数据
|
||||
$this->model
|
||||
->with(['user', 'order'])
|
||||
->where($where)
|
||||
->chunk(10,function($list) use ($objPHPExcel) {
|
||||
if($list){
|
||||
foreach ($list as $key => $item){
|
||||
$objPHPExcel->getActiveSheet()->setCellValue('A'.(2 + $this->exportIndex), $item['user']['username']);
|
||||
$objPHPExcel->getActiveSheet()->setCellValue('B'.(2 + $this->exportIndex), $item['user']['nickname']);
|
||||
$objPHPExcel->getActiveSheet()->setCellValue('C'.(2 + $this->exportIndex), $item['user']['mobile']);
|
||||
$objPHPExcel->getActiveSheet()->setCellValue('D'.(2 + $this->exportIndex), $item['get_type_text']);
|
||||
$objPHPExcel->getActiveSheet()->setCellValue('E'.(2 + $this->exportIndex), $item['createtime'] ? date('Y-m-d H:i:s',$item['createtime']) : '-');
|
||||
$objPHPExcel->getActiveSheet()->setCellValue('F'.(2 + $this->exportIndex), $item['status_text']);
|
||||
$objPHPExcel->getActiveSheet()->setCellValue('G'.(2 + $this->exportIndex), $item['use_order_id'] ? $item['use_order_id'] : '-');
|
||||
$objPHPExcel->getActiveSheet()->setCellValue('H'.(2 + $this->exportIndex), $item['use_time'] ? date('Y-m-d H:i:s',$item['use_time']) : '-');
|
||||
$this->exportIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
},$this->model->getTable().'.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(20);
|
||||
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
|
||||
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(12);
|
||||
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
|
||||
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(10);
|
||||
$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20);
|
||||
$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(20);
|
||||
//激活当前表
|
||||
$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 $id
|
||||
* @return void
|
||||
*/
|
||||
public function send()
|
||||
{
|
||||
$user_ids = $this->request->post('user_ids/a');
|
||||
$coupon_id = $this->request->post('coupon_id');
|
||||
$num = $this->request->post('num',1);
|
||||
|
||||
if(!$num){
|
||||
$this->error("请输入正确的发放数量");
|
||||
}
|
||||
|
||||
$users = \app\admin\model\User::whereIn('id', $user_ids)->select();
|
||||
|
||||
Db::transaction(function () use ($users, $coupon_id,$num) {
|
||||
$this->manualSend($users, $coupon_id,$num);
|
||||
});
|
||||
|
||||
$this->success('发放成功');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user