dggjimai/application/admin/controller/UserController.php

215 lines
7.2 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\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[] = ['integral','>=',$param['integral_start']];
}
if(isset($param['integral_end']) && $param['integral_end']){
$where[] = ['integral','<=',$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 equipmentadd(Request $request,Order $omodel)
{
if ($request->isPost()) {
$param = $request->param();
$goods_ids=$param['goods_id'];
if(count($goods_ids)==0){
return error('请选择设备信息');
}
$field_data = array();
foreach ($goods_ids as $key => $value) {
if($param['goods_id'][$key][0] && $param['number'][$key][0] && $param['end_time'][$key][0]){
$goods_info=Goods::where(array('id'=>$param['goods_id'][$key][0]))->find()->toArray();
$field_data[] = [
'order_no'=>get_order_sn(),
'goods_id' => $param['goods_id'][$key][0],
'number' => $param['number'][$key][0],
'end_time' => strtotime($param['end_time'][$key][0]),
'user_id' => $param['id'],
'goods_name' => $goods_info['name'],
'attr' => $goods_info['attr'],
'order_price' => $goods_info['price']*$param['number'][$key][0],
'create_time'=>time(),
];
}
}
if(count($field_data)==0){
return error('请检查设备信息是否填写完整');
}
$result = $omodel->insertAll($field_data);
$url = URL_BACK;
if (isset($param['_create']) && $param['_create'] == 1) {
$url = URL_RELOAD;
}
return $result ? success('添加成功', $url) : error();
}
$this->assign([
'goods_list' => Goods::all(),
]);
return $this->fetch();
}
}