assign('breadcrumb1','积分管理'); $this->assign('breadcrumb2','变动记录'); } /** * 列表页 * * */ public function index(Model $Model,Custom $Custom){ // 接受参数 $code = request('custom_code',''); $phone = request('phone',''); $username = request('username',''); $startTime = request('start_time',''); $endTime = request('end_time',''); // 时间戳 $startTime = $startTime ? Carbon::createFromFormat('Y-m-d',$startTime)->startOfDay()->getTimestamp() : now()->addDays(-7)->startOfDay()->getTimestamp(); $endTime = $endTime ? Carbon::createFromFormat('Y-m-d',$endTime)->endOfDay()->getTimestamp() : now()->endOfDay()->getTimestamp(); // 编码转ID $uid = $Custom->codeToId($code); // 查询条件 $map = []; // 编码ID if( $uid ) $map[] = ['custom.uid','=',$uid]; if( $phone ) $map[] = ['custom.phone','=',$phone]; if( $username ) $map[] = ['custom.username','=',$username]; if( $startTime ) $map[] = ['score_record.pay_time','>=',$startTime]; if( $endTime ) $map[] = ['score_record.pay_time','<=',$endTime]; // 查询数据 $list = $Model->query() ->join('custom','score_record.custom_uid','=','custom.uid') ->where($map) ->orderByDesc('score_record.id') ->select([ 'score_record.id', 'score_record.score', // 变动积分 'score_record.balance', // 变动后剩余 'score_record.buy_type', // 购买类型 'score_record.pay_type', // 支付类型 'score_record.status', // 'custom.uid as custom_code', 'custom.username', 'custom.phone', 'score_record.pay_time', ]) ->paginate(config('page_num',10))->appends(request()->all()); // 循环处理数据 foreach ($list as $key => $value) { // id转编号 $value['custom_code'] = $Custom->idToCode($value['custom_code']); $value['pay_type'] = $Model->getPayType($value['buy_type'],$value['pay_type'],'name'); // 重组 $list[$key] = $value; } // 分配数据 $this->assign('empty', '~~暂无数据'); $this->assign('list',$list); // 加载模板 return $this->fetch(); } /** * 导出表格 * * */ public function down_excel(Model $Model,Custom $Custom){ // 接受参数 $code = request('custom_code',''); $phone = request('phone',''); $username = request('username',''); $startTime = request('start_time',''); $endTime = request('end_time',''); // 时间戳 $startTime = $startTime ? Carbon::createFromFormat('Y-m-d',$startTime)->startOfDay()->getTimestamp() : now()->addDays(-7)->startOfDay()->getTimestamp(); $endTime = $endTime ? Carbon::createFromFormat('Y-m-d',$endTime)->endOfDay()->getTimestamp() : now()->endOfDay()->getTimestamp(); // 编码转ID $uid = $Custom->codeToId($code); // 查询条件 $map = []; // 编码ID if( $uid ) $map[] = ['custom.uid','=',$uid]; if( $phone ) $map[] = ['custom.phone','=',$phone]; if( $username ) $map[] = ['custom.username','=',$username]; if( $startTime ) $map[] = ['score_record.pay_time','>=',$startTime]; if( $endTime ) $map[] = ['score_record.pay_time','<=',$endTime]; // 查询数据 $list = $Model->query() ->join('custom','score_record.custom_uid','=','custom.uid') ->where($map) ->orderByDesc('score_record.id') ->get([ 'score_record.id', 'custom.uid as custom_code', 'custom.username', 'custom.phone', 'score_record.buy_type', // 购买类型 'score_record.pay_type', // 支付类型 'score_record.score', // 变动积分 'score_record.balance', // 变动后剩余 'score_record.pay_time', ])->toArray(); // 循环处理数据 foreach ($list as $key => $value) { // id转编号 $value['custom_code'] = $Custom->idToCode($value['custom_code']); $value['pay_type'] = $Model->getPayType($value['buy_type'],$value['pay_type'],'name'); $value['pay_time'] = date('Y-m-d H:i:s',$value['pay_time']); unset($value['buy_type']); // 重组 $list[$key] = $value; } try { // 去下载 $this->toDown($list); } catch (\Throwable $th) { echo $th->getMessage(); } } /** * 去下载 */ private function toDown($data){ try { $config = [ 'path' =>public_path().'/uploads/' // xlsx文件保存路径 ]; $excel = new \Vtiful\Kernel\Excel($config); $header = [ '记录ID', '客户编码', '客户昵称', '联系方式', '积分来源', '变动积分', '剩余积分', '支付时间', ]; $filePath = $excel->fileName(uniqid().'.xlsx', 'sheet1') ->header($header) ->data($data) ->output(); header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); header('Content-Disposition: attachment;filename="积分记录.xlsx"'); header('Content-Length: '.filesize($filePath)); header('Content-Transfer-Encoding: binary'); header('Cache-Control: must-revalidate'); header('Cache-Control: max-age=0'); header('Pragma: public'); ob_clean(); flush(); if (copy($filePath, 'php://output') === false) { return json_send(['code'=>'error','msg'=>'下载失败']); } @unlink($filePath); return json_send(['code'=>'success','msg'=>'下载成功','path'=>'']); }catch (\Exception $exception) { return json_send(['code'=>'error','msg'=>'下载失败']); } } }