326 lines
12 KiB
PHP
326 lines
12 KiB
PHP
<?php
|
|
|
|
namespace app\admin\controller\user;
|
|
|
|
use app\common\controller\Backend;
|
|
use app\common\library\Auth;
|
|
use app\common\model\user\Oauth;
|
|
use PHPExcel_IOFactory;
|
|
use think\Db;
|
|
/**
|
|
* 会员管理
|
|
*
|
|
* @icon fa fa-user
|
|
*/
|
|
class User extends Backend
|
|
{
|
|
|
|
protected $relationSearch = false;
|
|
protected $searchFields = 'id,username,nickname';
|
|
|
|
/**
|
|
* @var \app\admin\model\User
|
|
*/
|
|
protected $model = null;
|
|
|
|
public function _initialize()
|
|
{
|
|
parent::_initialize();
|
|
$this->model = new \app\admin\model\User;
|
|
}
|
|
|
|
/**
|
|
* 查看
|
|
*/
|
|
public function index()
|
|
{
|
|
//设置过滤方法
|
|
$this->request->filter(['strip_tags', 'trim']);
|
|
|
|
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
|
|
|
|
// 第一步:先查出当前页的 ID 列表(轻量级、快)
|
|
$offset = max(0, $offset); // 保证 offset 不小于 0
|
|
$idList = $this->model
|
|
->where($where)
|
|
->order($sort, $order)
|
|
->limit($offset, $limit)
|
|
->column('id'); // 获取 ID 数组
|
|
|
|
if (empty($idList)) {
|
|
$this->success("ok", ["total" => 0, "rows" => [],
|
|
"attr" => [
|
|
"platform" => $this->model->getPlatformList()
|
|
]]);
|
|
}
|
|
|
|
// 第二步:通过 ID 查详情,保持顺序
|
|
$list = $this->model
|
|
->whereIn('id', $idList)
|
|
->orderRaw("FIELD(id," . implode(',', $idList) . ")")
|
|
->select();
|
|
|
|
// 字段处理
|
|
foreach ($list as $v) {
|
|
$v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname);
|
|
$v->hidden(['password', 'salt']);
|
|
}
|
|
|
|
// 总数单独计算(可考虑缓存)
|
|
$total = $this->model->where($where)->count();
|
|
|
|
// 返回结果
|
|
$result = [
|
|
"total" => $total,
|
|
"rows" => $list,
|
|
"attr" => [
|
|
"platform" => $this->model->getPlatformList()
|
|
]
|
|
];
|
|
|
|
$this->success("ok", $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getUserInfo($ids){
|
|
|
|
$row = $this->model->get($ids);
|
|
|
|
if (!$row) {
|
|
$this->error(__('No Results were found'));
|
|
}
|
|
|
|
return $this->success("",$row);
|
|
}
|
|
/**
|
|
* 添加
|
|
*/
|
|
public function add()
|
|
{
|
|
if (false === $this->request->isPost()) {
|
|
return $this->view->fetch();
|
|
}
|
|
$params = $this->request->post('row/a');
|
|
if (empty($params)) {
|
|
$this->error(__('Parameter %s can not be empty', ''));
|
|
}
|
|
$params = $this->preExcludeFields($params);
|
|
|
|
if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
|
|
$params[$this->dataLimitField] = $this->auth->id;
|
|
}
|
|
$result = false;
|
|
$userAuth = new \app\common\library\Auth();
|
|
//判断是否已有用户注册
|
|
|
|
try {
|
|
$ret = $userAuth->register($params['mobile'],$params['password'],'',$params['mobile'],[
|
|
'uniacid'=>UNIACID,
|
|
'platform'=>'H5'
|
|
],true);
|
|
|
|
if(!$ret){
|
|
$this->error($userAuth->getError());
|
|
}
|
|
} catch (ValidateException|PDOException|Exception $e) {
|
|
|
|
$this->error($e->getMessage());
|
|
}
|
|
$this->success("创建成功");
|
|
}
|
|
|
|
/**
|
|
* 编辑
|
|
*/
|
|
public function edit($ids = null)
|
|
{
|
|
$row = $this->model->get($ids);
|
|
$this->modelValidate = true;
|
|
if (!$row) {
|
|
$this->error(__('No Results were found'));
|
|
}
|
|
return parent::edit($ids);
|
|
}
|
|
|
|
|
|
protected $exportIndex = 0;
|
|
/**
|
|
* 导出用户
|
|
* @return void
|
|
*/
|
|
public function export(){
|
|
|
|
list($where, $sort, $order, $offset, $limit, $page, $alias, $bind) = $this->buildparams();
|
|
|
|
$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', '累计消费金额');
|
|
$objPHPExcel->getActiveSheet()->setCellValue('I1', '最近消费时间');
|
|
$objPHPExcel->getActiveSheet()->setCellValue('J1', '注册IP');
|
|
$objPHPExcel->getActiveSheet()->setCellValue('K1', '最近登录IP');
|
|
$objPHPExcel->getActiveSheet()->setCellValue('L1', '注册时间');
|
|
$objPHPExcel->getActiveSheet()->setCellValue('M1', '最近登录时间');
|
|
$objPHPExcel->getActiveSheet()->setCellValue('N1', '来源');
|
|
|
|
$orderModel = new \app\admin\model\order\Order;
|
|
|
|
|
|
// 给表格添加数据
|
|
$this->model
|
|
->where($where)
|
|
->chunk(10,function($list) use ($objPHPExcel,$orderModel) {
|
|
if($list){
|
|
|
|
foreach ($list as $key => $item){
|
|
$orderData = $orderModel->getTransactionStatistic($item['id']);
|
|
$objPHPExcel->getActiveSheet()->setCellValue('A'.(2 + $this->exportIndex), $item['username']);
|
|
$objPHPExcel->getActiveSheet()->setCellValue('B'.(2 + $this->exportIndex), $item['nickname']);
|
|
$objPHPExcel->getActiveSheet()->setCellValue('C'.(2 + $this->exportIndex), $item['mobile']);
|
|
$objPHPExcel->getActiveSheet()->setCellValue('D'.(2 + $this->exportIndex), $item['score']);
|
|
$objPHPExcel->getActiveSheet()->setCellValue('E'.(2 + $this->exportIndex), $item['money']);
|
|
$objPHPExcel->getActiveSheet()->setCellValue('F'.(2 + $this->exportIndex), $item['status'] == 'normal' ? '正常' : '禁用');
|
|
$objPHPExcel->getActiveSheet()->setCellValue('G'.(2 + $this->exportIndex), $orderData['pay_count']);
|
|
$objPHPExcel->getActiveSheet()->setCellValue('H'.(2 + $this->exportIndex), $orderData['pay_price']);
|
|
$objPHPExcel->getActiveSheet()->setCellValue('I'.(2 + $this->exportIndex), $orderData['last_pay_time'] ? date('Y-m-d H:i:s',$orderData['last_pay_time']) : '-');
|
|
$objPHPExcel->getActiveSheet()->setCellValue('J'.(2 + $this->exportIndex), $item['joinip']);
|
|
$objPHPExcel->getActiveSheet()->setCellValue('K'.(2 + $this->exportIndex), $item['loginip']);
|
|
$objPHPExcel->getActiveSheet()->setCellValue('L'.(2 + $this->exportIndex), $item['jointime'] ? date('Y-m-d H:i:s',$item['jointime']) : '-');
|
|
$objPHPExcel->getActiveSheet()->setCellValue('M'.(2 + $this->exportIndex), $item['logintime'] ? date('Y-m-d H:i:s',$item['logintime']) : '-');
|
|
$objPHPExcel->getActiveSheet()->setCellValue('N'.(2 + $this->exportIndex), $item['platform'] ? __($item['platform']) : '默认渠道');
|
|
$this->exportIndex++;
|
|
}
|
|
}
|
|
|
|
},'id','desc');
|
|
|
|
|
|
//设置字体
|
|
$objPHPExcel->getActiveSheet()->getStyle('A1:N1')->getFont()->setBold(true)->getColor()->setARGB(\PHPExcel_Style_Color::COLOR_BLACK);
|
|
//设置水平居中
|
|
$objPHPExcel->getActiveSheet()->getStyle('A1:N1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
|
|
//设置背景颜色
|
|
$objPHPExcel->getActiveSheet()->getStyle('A1:N1')->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(12);
|
|
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(10);
|
|
$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(12);
|
|
$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(12);
|
|
$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(18);
|
|
$objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(16);
|
|
$objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(16);
|
|
$objPHPExcel->getActiveSheet()->getColumnDimension('L')->setWidth(18);
|
|
$objPHPExcel->getActiveSheet()->getColumnDimension('M')->setWidth(18);
|
|
$objPHPExcel->getActiveSheet()->getColumnDimension('N')->setWidth(12);
|
|
//激活当前表
|
|
$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 = PHPExcel_Writer_Excel2007($objPHPExcel, 'Excel2007');
|
|
|
|
$objWriter->save( 'php://output');
|
|
}
|
|
|
|
/**
|
|
* 切换可用状态
|
|
* @return void
|
|
*/
|
|
public function changeStatus($id){
|
|
$row = $this->model->get($id);
|
|
if (!$row) {
|
|
$this->error(__('No Results were found'));
|
|
}
|
|
|
|
$row->allowField(true)->save([
|
|
'status'=>$row['status'] == 'normal' ? 'hidden' : 'normal'
|
|
]);
|
|
|
|
return $this->success("操作成功");
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取用户信息 pophover组件调用
|
|
* @param $id
|
|
* @return void
|
|
*/
|
|
public function detail($id = null){
|
|
$id = input('id');
|
|
|
|
if(!$id){
|
|
$this->error("参数异常");
|
|
}
|
|
$row = $this->model->field([
|
|
'avatar','id','nickname','username','mobile','score','money','platform'
|
|
])->find($id);
|
|
|
|
if(!$row){
|
|
$this->error("获取失败",$row);
|
|
}
|
|
|
|
if(!$row->avatar){
|
|
$row->avatar = letter_avatar($row->nickname);
|
|
}else{
|
|
$row->avatar = cdnurl($row->avatar);
|
|
}
|
|
|
|
|
|
$row->platform = __($row->platform);
|
|
$this->success("获取成功",$row);
|
|
}
|
|
|
|
/**
|
|
* 删除
|
|
*/
|
|
public function del($ids = "")
|
|
{
|
|
if (!$this->request->isPost()) {
|
|
$this->error(__("Invalid parameters"));
|
|
}
|
|
$ids = $ids ? $ids : $this->request->post("ids");
|
|
$list = $this->model->where('id', 'in', $ids)->select();
|
|
|
|
$count = 0;
|
|
Db::startTrans();
|
|
try {
|
|
foreach ($list as $item) {
|
|
Oauth::where([
|
|
'user_id'=>$item->id
|
|
])->delete();
|
|
|
|
$count += $item->delete();
|
|
}
|
|
Db::commit();
|
|
} catch (PDOException|Exception $e) {
|
|
Db::rollback();
|
|
$this->error($e->getMessage());
|
|
}
|
|
if ($count) {
|
|
$this->success("操作成功");
|
|
}
|
|
$this->error(__('No rows were deleted'));
|
|
|
|
}
|
|
|
|
}
|