<?php
namespace app \index \controller ;
use think\Hook;
use think\Request;
use app\index\controller\Acl;
use app\index\model\Code as Codes;
use app\index\controller\Formfield;
class Code extends Acl {
public function main(){
return $this->fetch();
}
public function code_list(){
$input=input('post.');
if(isset_full($input,'page') && isset_full($input,'limit')){
$sql=get_sql($input,[
'name'=>'full_name_py_link',
'code'=>'full_like',
'type'=>'full_dec_1'
],'code') $count = Codes::where ($sql)->count() $arr = Codes::where($sql)->page($input['page'],$input['limit'])->order('id desc')->select() $resule=[
'code'=>0,
'msg'=>'获取成功',
'count'=>$count,
'data'=>$arr
] }else{
$resule=['state'=>'error','info'=>'传入参数不完整!'];
}
return json($resule);
}
public function set_code(){
$input=input('post.');
if(isset($input['id'])){
if(empty($input['id'])){
$vali = $this->validate($input,'code');
if($vali===true){
$input['py']=zh2py($input['name']) $create_info=Codes::create(syn_sql($input,'code'));
Hook::listen('create_code',$create_info) push_log('新增条码信息[ '.$create_info['name'].' ]') $resule=['state'=>'success'];
}else{
$resule=['state'=>'error','info'=>$vali];
}
}else{
$vali = $this->validate($input,'code.update');
if($vali===true){
$input['py']=zh2py($input['name']) $update_info=Codes::update(syn_sql($input,'code'));
Hook::listen('update_code',$update_info) push_log('更新条码信息[ '.$update_info['name'].' ]') $resule=['state'=>'success'];
}else{
$resule=['state'=>'error','info'=>$vali];
}
}
}else{
$resule=['state'=>'error','info'=>'传入参数不完整!'];
}
return json($resule);
}
public function get_code(){
$input=input('post.');
if(isset_full($input,'id')){
$resule=Codes::where(['id'=>$input['id']])->find();
}else{
$resule=['state'=>'error','info'=>'传入参数不完整!'];
}
return json($resule);
}
public function del_code(){
$input=input('post.');
if(isset_full($input,'arr') && is_array($input['arr'])){
$info=db('code')->where(['id'=>['in',$input['arr']]])->select() foreach ($info as $info_vo) {
push_log('删除条码信息[ '.$info_vo['name'].' ]') Hook::listen('del_code',$info_vo['id']) }
Codes::where(['id'=>['in',$input['arr']]])->delete();
$resule=['state'=>'success'];
}else{
$resule=['state'=>'error','info'=>'传入参数不完整!'];
}
return json($resule);
}
public function export_code(){
$input=input('get.');
$sql=get_sql($input,[
'name'=>'full_name_py_link',
'code'=>'full_like',
'type'=>'full_dec_1'
],'code') $arr = Codes::where($sql)->order('id desc')->select() foreach ($arr as $key=>$vo) {
if($vo['type']['nod']=='0'){
$arr[$key]['img']=txm($vo['code'],false);
}elseif($vo['type']['nod']=='1'){
$arr[$key]['img']=ewm($vo['code'],false);
}
}
$formfield=get_formfield('code_export','array') $excel=[] array_push($excel,['type'=>'title','info'=>'条码列表']);
$table_cell=[] foreach ($formfield as $formfield_vo) {
$table_cell[$formfield_vo['key']]=$formfield_vo['text'];
}
$table_data=[] foreach ($arr as $arr_vo) {
$row_data=[];
foreach ($formfield as $formfield_vo) {
$val='nod_initial' foreach (explode('|',$formfield_vo['data']) as $source) {
$val=$val=='nod_initial'?$arr_vo[$source]:(isset($val[$source])?$val[$source]:'');
}
if($formfield_vo['key']=='img'){
$row_data[$formfield_vo['key']]=[
'type'=>'img',
'info'=>$val
] }else{
$row_data[$formfield_vo['key']]=$val }
}
array_push($table_data,$row_data) }
array_push($excel,['type'=>'table','info'=>['cell'=>$table_cell,'data'=>$table_data]]) push_log('导出条码信息') export_excel('条码列表',$excel);
}
public function import_code(Request $request){
$file=$request->file('file') if (empty($file)){
$resule=['state'=>'error','info'=>'传入数据不完整!'];
}else{
$nod=$file->validate (['ext'=>'xlsx'])->rule ('uniqid')->move (ROOT_PATH.'skin'.DS.'upload'.DS.'xlsx') if($nod){
$path=ROOT_PATH .'skin'.DS .'upload'.DS .'xlsx'.DS.$nod->getSaveName();
$arr=get_xlsx($path);
unset($arr[1]) $create_sql=[] $type_arr=['0'=>'条形码','1'=>'二维码'];
foreach ($arr as $key=>$vo) {
$sql=[] $sql['name']=$vo['A'];
$sql['py']=zh2py($vo['A']) $sql['code']=$vo['B'];
$type_search=array_search($vo['C'],$type_arr);
if($type_search!==false){
$sql['type']=$type_search;
}else{
return json(['state'=>'error','info'=>'模板文件第[ '.$key.' ]行条码类型可选项为[条形码|二维码]']);
}
$sql['data']=$vo['D'];
$vali = $this->validate($sql,'code');
if($vali===true){
push_log('导入条码信息[ '.$sql['name'].' ]') array_push($create_sql,$sql) }else{
return json(['state'=>'error','info'=>'模板文件第[ '.$key.' ]行'.$vali]);
}
}
$insert_count=db('code')->insertAll($create_sql);
$resule=['state'=>'success','info'=>$insert_count];
}else{
$resule=['state'=>'error','info'=>$file->getError()];
}
}
return json($resule);
}
public function view(){
$input=input('get.');
if(isset_full($input,'text') && isset_full($input,'type') && in_array($input['type'],['txm','ewm'])){
if($input['type']=='txm'){
txm($input['text']);
}else if($input['type']=='ewm'){
ewm($input['text']);
}
}else{
return json(['state'=>'error','info'=>'传入参数不完整!']);
}
}
}