<?php
namespace app\controller;
use app\controller\Acl;
use app\model\User as Users;
use think\facade\{Db,Filesystem};
use think\exception\ValidateException;
class User extends Acl{
public function record(){
$input=input('post.');
if(existFull($input,['page','limit'])){
$sql=fastSql($input,[
[['name'=>'name|py'],'fullLike'],
['user','fullLike'],
['tel','fullLike'],
['data','fullLike']
]) $sql=frameScope($sql) $sql=sqlAuth('user',$sql) $count = Users::where($sql)->count() $info = Users::with(['frameData','roleData'])->where($sql)->page($input['page'],$input['limit'])->order(['id'=>'desc'])->select() $result=[
'state'=>'success',
'count'=>$count,
'info'=>$info
] }else{
$result=['state'=>'error','info'=>'传入参数不完整!'];
}
return json($result);
}
public function save(){
$input=input('post.');
if(isset($input['id'])){
try {
$input['py']=zhToPy($input['name']) if(empty($input['id'])){
$this->validate($input,'app\validate\User');
}else{
$input['token']="";
if(isset($input['pwd']) && empty($input['pwd'])){
unset($input['pwd']);
}
$this->validate($input,'app\validate\User.update');
}
} catch (ValidateException $e) {
return json(['state'=>'error','info'=>$e->getError()]);
exit;
}
Db::startTrans();
try {
if(empty($input['id'])){
Users::create($input);
pushLog('新增用户[ '.$input['name'].' ]') }else{
Users::update($input);
pushLog('更新用户[ '.$input['name'].' ]') }
Db::commit();
$result=['state'=>'success'];
} catch (\Exception $e) {
Db::rollback();
$result=['state'=>'error','info'=>'内部错误,操作已撤销!'];
}
}else{
$result=['state'=>'error','info'=>'传入参数不完整!'];
}
return json($result);
}
public function get(){
$input=input('post.');
if(existFull($input,['id'])){
$result=[
'state'=>'success',
'info'=>Users::where([['id','=',$input['id']]])->find()
];
}else{
$result=['state'=>'error','info'=>'传入参数不完整!'];
}
return json($result);
}
public function del(){
$input=input('post.');
if(existFull($input,['id'])){
$exist=moreTableFind([
['table'=>'allot','where'=>[['user','=',$input['id']]]],
['table'=>'barter','where'=>[['user','=',$input['id']]]],
['table'=>'bill','where'=>[['user','=',$input['id']]]],
['table'=>'bor','where'=>[['user','=',$input['id']]]],
['table'=>'bre','where'=>[['user','=',$input['id']]]],
['table'=>'buy','where'=>[['user','=',$input['id']]]],
['table'=>'entry','where'=>[['user','=',$input['id']]]],
['table'=>'extry','where'=>[['user','=',$input['id']]]],
['table'=>'ice','where'=>[['user','=',$input['id']]]],
['table'=>'imy','where'=>[['user','=',$input['id']]]],
['table'=>'oce','where'=>[['user','=',$input['id']]]],
['table'=>'omy','where'=>[['user','=',$input['id']]]],
['table'=>'period','where'=>[['user','=',$input['id']]]],
['table'=>'sell','where'=>[['user','=',$input['id']]]],
['table'=>'sor','where'=>[['user','=',$input['id']]]],
['table'=>'sre','where'=>[['user','=',$input['id']]]],
['table'=>'swap','where'=>[['user','=',$input['id']]]],
['table'=>'customer','where'=>[['user','=',$input['id']]]],
['table'=>'often','where'=>[['user','=',$input['id']]]],
['table'=>'record','where'=>[['user','=',$input['id']]]],
['table'=>'supplier','where'=>[['user','=',$input['id']]]],
]);
if(empty($exist)){
$find=Db::name('user')->where([['id','=',$input['id']]])->find();
Db::startTrans();
try {
Db::name('user')->where([['id','=',$input['id']]])->delete();
Db::name('log')->where([['user','=',$input['id']]])->delete();
pushLog('删除用户[ '.$find['name'].' ]')
Db::commit();
$result=['state'=>'success'];
} catch (\Exception $e) {
Db::rollback();
$result=['state'=>'error','info'=>'内部错误,操作已撤销!'];
}
}else{
$result=['state'=>'error','info'=>'存在数据关联,删除失败!'];
}
}else{
$result=['state'=>'error','info'=>'传入参数不完整!'];
}
return json($result);
}
public function upload(){
$file=request()->file('file') if (empty($file)){
$result=['state'=>'error','info'=>'传入数据不完整!'];
}else{
try{
validate(['file'=>['fileSize'=>1*1024*1024,'fileExt'=>'png,gif,jpg,jpeg']])->check(['file'=>$file]);
$fileInfo=Filesystem::disk('upload')->putFile('user', $file, 'uniqid');
$filePath=request()->domain().'/static/upload/'.$fileInfo;
$result=['state'=>'success','info'=>$filePath];
}catch(ValidateException $e) {
$result=['state'=>'error','info'=>$e->getMessage()];
}
}
return json($result);
}
}