<?php
namespace app\controller ;
use app\controller\Acl;
use app\model\Code as Codes;
use think\facade\{Db,Filesystem};
use think\exception\ValidateException;
class Code extends Acl {
public function record(){
$input=input('post.');
$sql=fastSql($input,[
['name','fullLike'],
['info','fullLike'],
['type','fullDec1'],
['data','fullLike']
]) $count = Codes::where($sql)->count() $info = Codes::where($sql)->append(['extension'])->page($input['page'],$input['limit'])->order(['id'=>'desc'])->select() $result=[
'state'=>'success',
'count'=>$count,
'info'=>$info
] return json($result);
}
public function save(){
$input=input('post.');
if(isset($input['id'])){
try {
empty($input['id'])?$this->validate($input,'app\validate\Code'):$this->validate($input,'app\validate\Code.update');
} catch (ValidateException $e) {
return json(['state'=>'error','info'=>$e->getError()]);
exit;
}
Db::startTrans();
try {
if(empty($input['id'])){
Codes::create($input);
pushLog('新增条码[ '.$input['name'].' ]') }else{
Codes::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'=>Codes::where([['id','=',$input['id']]])->find()
];
}else{
$result=['state'=>'error','info'=>'传入参数不完整!'];
}
return json($result);
}
public function del(){
$input=input('post.');
if(existFull($input,['parm']) && is_array($input['parm'])){
$data=Db::name('code')->where([['id','in',$input['parm']]])->order(['id'=>'desc'])->select()->toArray();
Db::startTrans();
try {
Db::name('code')->where([['id','in',$input['parm']]])->delete();
Db::name('log')->insert(['time'=>time(),'user'=>getUserID(),'info'=>'删除条码信息[ '.implode(' | ',array_column($data,'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 import(){
delOverdueFile('static.upload.xlsx') $file=request()->file('file') if(empty($file)){
$result=['state'=>'error','info'=>'传入数据不完整!'];
}else{
try{
validate(['file'=>['fileSize'=>2*1024*1024,'fileExt'=>'xlsx']])->check(['file'=>$file]);
$fileInfo = Filesystem::disk('upload')->putFile('xlsx', $file, 'uniqid');
$filePath = pathChange('static.upload').$fileInfo;
$data=getXlsx($filePath);
unset($data[1]) $sql=[] foreach ($data as $dataKey=>$dataVo) {
$record=[
'name'=>$dataVo['A'],
'info'=>$dataVo['B'],
'type'=>$dataVo['C']=="条形码"?0:1,
'data'=>$dataVo['D']
];
try {
$this->validate($record,'app\validate\Code');
$sql[]=$record } catch (ValidateException $e) {
return json(['state'=>'error','info'=>'模板文件第[ '.$dataKey.' ]行'.$e->getError()]);
exit;
}
}
$code = new Codes;
$code->saveAll($sql);
$result=['state'=>'success','info'=>'成功导入'.count($sql).'行条码数据'];
}catch(ValidateException $e) {
$result=['state'=>'error','info'=>$e->getMessage()] }
}
return json($result);
}
public function exports(){
$input=input('get.');
if(existFull($input,['parm']) && is_array($input['parm'])){
$info=Codes::where([['id','in',$input['parm']]])->append(['extension'])->order(['id'=>'desc'])->select()->toArray() foreach ($info as $infoKey=>$infoVo) {
if($infoVo['type']==0){
$info[$infoKey]['img']=[
'type'=>'img',
'info'=>txm($infoVo['info'],false)
];
}else if($infoVo['type']==1){
$info[$infoKey]['img']=[
'type'=>'img',
'info'=>ewm($infoVo['info'],false)
];
}else{
exit("Error");
}
}
$field=[
'name'=>'条码名称',
'info'=>'条码内容',
'extension|type'=>'条码类型',
'img'=>'条码图像',
'data'=>'备注信息'
];
$excel=[] $excel[]=['type'=>'title','info'=>'条码信息'];
$thead=array_values($field) $tbody=[] foreach ($info as $infoVo) {
$rowData=[];
foreach (array_keys($field) as $fieldVo) {
$rowData[]=arraySeek($infoVo,$fieldVo) }
$tbody[]=$rowData }
$excel[]=['type'=>'table','info'=>['thead'=>$thead,'tbody'=>$tbody]] $excel[]=['type'=>'node','info'=>['总数:'.count($info)]];
pushLog('导出条码信息') buildExcel('条码信息',$excel);
}else{
return json(['state'=>'error','info'=>'传入数据不完整!']);
}
}
public function view(){
$input=input('get.');
if(existFull($input,['text','type']) && in_array($input['type'],['txm','ewm'])){
if($input['type']=='txm'){
txm($input['text']);
}else if($input['type']=='ewm'){
ewm($input['text']);
}else{
exit('error');
}
}else{
return json(['state'=>'error','info'=>'传入参数不完整!']);
}
}
}