dggjimai/application/admin/controller/UserController.php

214 lines
6.8 KiB
PHP
Executable File

<?php
/**
* 用户控制器
*/
namespace app\admin\controller;
use think\Request;
use app\common\model\User;
use app\common\model\Order;
use app\common\model\Goods;
use app\common\model\AccountLog;
use app\common\validate\UserValidate;
use app\common\validate\OrderValidate;
class UserController extends Controller
{
//列表
public function index(Request $request, User $model)
{
$param = $request->param();
$where = [];
//查询
if(isset($param['keyword']) && $param['keyword']){
$where[] = [$param['keyword_type'],'like','%'.$param['keyword'].'%'];
}
//等级查询
if(isset($param['level']) && $param['level'] != ''){
$where[] = ['user_level_id','=',$param['level']];
}
//提现积分
if(isset($param['integral_start']) && $param['integral_start']){
$where[] = ['user_money','>=',$param['integral_start']];
}
if(isset($param['integral_end']) && $param['integral_end']){
$where[] = ['user_money','<=',$param['integral_end']];
}
//注册时间
if(isset($param['start_time']) && $param['start_time']!=''){
$where[] = ['create_time','>=',strtotime($param['start_time'])];
}
if(isset($param['end_time']) && $param['end_time']!=''){
$where[] = ['create_time','<=',strtotime($param['end_time'])];
}
$model = $model->where($where)->scope('where', $param);
$data = $model->paginate($this->admin['per_page'], false, ['query' => $request->get()]);
foreach ($data as $key => $value) {
// 上级推荐人
$value['first_leader_info'] = User::getUserInfo($value['first_leader']);
//推荐下级人数
$value['fans'] = User::where([
['first_leader|second_leader', '=', $value['id']]
])->count();
//设备数量
$value['sb_num'] = Order::where([
['user_id', '=', $value['id']]
])->count();
}
//关键词,排序等赋值
$this->assign($request->get());
$this->assign([
'data' => $data,
'page' => $data->render(),
'total' => $data->total(),
]);
return $this->fetch();
}
//添加
public function add(Request $request, User $model, UserValidate $validate)
{
if ($request->isPost()) {
$param = $request->param();
$validate_result = $validate->scene('add')->check($param);
if (!$validate_result) {
return error($validate->getError());
}
//处理头像上传
$attachment_avatar = new \app\common\model\Attachment;
$file_avatar = $attachment_avatar->upload('avatar');
if ($file_avatar) {
$param['avatar'] = $file_avatar->url;
} else {
return error($attachment_avatar->getError());
}
$result = $model::create($param);
$url = URL_BACK;
if (isset($param['_create']) && $param['_create'] == 1) {
$url = URL_RELOAD;
}
return $result ? success('添加成功', $url) : error();
}
return $this->fetch();
}
//修改
public function edit($id, Request $request, User $model, UserValidate $validate)
{
$data = $model::get($id);
if ($request->isPost()) {
$param = $request->param();
$validate_result = $validate->scene('edit')->check($param);
if (!$validate_result) {
return error($validate->getError());
}
//处理头像上传
if (!empty($_FILES['avatar']['name'])) {
$attachment_avatar = new \app\common\model\Attachment;
$file_avatar = $attachment_avatar->upload('avatar');
if ($file_avatar) {
$param['avatar'] = $file_avatar->url;
}
}
$result = $data->save($param);
return $result ? success() : error();
}
$this->assign([
'data' => $data,
]);
return $this->fetch('add');
}
//删除
public function del($id, User $model)
{
if (count($model->noDeletionId) > 0) {
if (is_array($id)) {
if (array_intersect($model->noDeletionId, $id)) {
return error('ID为' . implode(',', $model->noDeletionId) . '的数据无法删除');
}
} else if (in_array($id, $model->noDeletionId)) {
return error('ID为' . $id . '的数据无法删除');
}
}
if ($model->softDelete) {
$result = $model->whereIn('id', $id)->useSoftDelete('delete_time', time())->delete();
} else {
$result = $model->whereIn('id', $id)->delete();
}
return $result ? success('操作成功', URL_RELOAD) : error();
}
//启用
public function enable($id, User $model)
{
$result = $model->whereIn('id', $id)->update(['status' => 1]);
return $result ? success('操作成功', URL_RELOAD) : error();
}
//禁用
public function disable($id, User $model)
{
$result = $model->whereIn('id', $id)->update(['status' => 0]);
return $result ? success('操作成功', URL_RELOAD) : error();
}
//账户调整
public function adjust_account($id, Request $request, User $model)
{
$data = $model::get($id);
if ($request->isPost()) {
$post_data = $request->param();
User::startTrans();
try {
$number = $post_data['money'];
$change_type = 1;
$money_sql = User::raw('user_money + ' . $post_data['money']);
if ($post_data['money_handle'] == 0) {
$change_type = 2;
$money_sql = User::raw('user_money - ' . $post_data['money']);
}
$update_data['user_money'] = $money_sql;
User::where('id', $post_data['id'])->update($update_data);
//账户变动记录
AccountLog::AccountRecord(
$post_data['id'],
$post_data['money'],
$change_type,
100,
$post_data['money_remark']
);
User::commit();
} catch (Exception $e) {
User::rollback();
return error($e->getMessage());
}
return success('调整成功');
}
$this->assign([
'info' => $data,
]);
return $this->fetch();
}
}