liufumingren e8605149c0 初始化
2026-08-06 17:04:35 +08:00

134 lines
4.8 KiB
PHP

<?php
namespace app\api\controller;
use app\common\controller\Api;
use app\common\model\User;
use think\Hook;
use fast\Random;
use fast\Http;
use think\Log;
class Prints extends Api
{
protected $noNeedLogin = '';
protected $noNeedRight = '*';
public function _initialize()
{
parent::_initialize();
$this->redis = new \Redis();
$this->redis->connect('127.0.0.1', 6379);
$this->redis->auth('qwe2019@');
$this->redis->select(11);
$this->PrintComputerModel = new \app\common\model\wms_new\PrintComputer;
}
public function getbindInfo(){
$prams = $this->request->get();
$query_code = $prams["code"]?$prams["code"]:null;
if(!$query_code){
$this->error("参数错误");
}
$code = $this->redis->get("bind:".$query_code);
if(!$code){
$this->error("已过期,请重新扫码","bind:".$code);
}
$wms_account_id = $this->auth->wms_account_id;
$StoreModel = new \app\common\model\wms_new\Store;
$AccountStoreEmployeModel = new \app\common\model\wms_new\AccountStoreEmploye;
$stores = $AccountStoreEmployeModel->alias("AccountStoreEmploye")->field("AccountStoreEmploye.store_id as store_id,store.company_name as company_name")->where(["AccountStoreEmploye.account_id"=>$wms_account_id,"AccountStoreEmploye.deleted_at"=>null,"AccountStoreEmploye.status"=>1,"store.project"=>"warehouse"])->join("wms_store store","store.id = AccountStoreEmploye.store_id")->select();
//$StoreModel->where()->select();
$uuid = Random::uuid();
$code = json_decode($code);
$code->stores = $stores;
$this->redis->set("pbind:".$uuid,json_encode($code),400);
$this->success('查询成功',["userid"=>$wms_account_id,"stores"=>$stores,"code"=>$uuid]);
}
public function bindPrintComputer(){
$params = $this->request->post();
$code = isset($params["code"]) ? $params["code"] : null;
$computerName = isset($params["computerName"]) ? $params["computerName"] : null;
$warehouse_id = isset($params["warehouse_id"]) ? $params["warehouse_id"] : null;
// 参数校验
if(!$code || !$computerName || !$warehouse_id){
Log::error("绑定打印电脑参数错误");
$this->error("参数错误");
}
$code_data = $this->redis->get("pbind:".$code);
if(!$code_data){
Log::error("绑定码已过期");
$this->error("已过期,请重新扫码绑定", ["restart"=>1]);
}
$code_data = json_decode($code_data, true);
// 验证仓库是否在允许列表中
$inWarehouseList = false;
foreach($code_data["stores"] as $r){
if($r["store_id"] == $warehouse_id){
$inWarehouseList = true;
break;
}
}
if(!$inWarehouseList){
Log::error("仓库不在绑定码允许列表中");
$this->error("参数错误");
}
// 创建打印电脑记录
$data = $this->PrintComputerModel->create([
"warehouse_id" => $warehouse_id,
"name" => $computerName,
"status" => 0,
"is_recording" => 0
]);
$insert_id = $data->id ?? 0;
if(!$insert_id){
Log::error("打印电脑记录插入失败");
$this->error("插入失败", $data);
}
// 请求远程服务绑定
$url = "http://printer.wms.gztbyc.com/auth/bind?timestamp=".time();
$body = [
"roomId" => $code_data["uniqueId"],
"warehouse" => $warehouse_id,
"computerId" => $insert_id
];
$body = http_build_query($body);
$options = [
CURLOPT_HTTPHEADER => [
'Content-Type: application/x-www-form-urlencoded',
'Accept: application/json'
]
];
Log::error("远程绑定接口请求数据".$body);
$req = Http::sendRequest($url, $body, 'POST', $options);
if($req["ret"]){
$msg = $req["msg"];
$msg = json_decode($msg, true);
if($msg["status"] == "fail"){
$this->PrintComputerModel->where(["id" => $insert_id])->delete();
Log::error("远程绑定接口返回失败");
$this->error($msg["message"]);
} else {
Log::info("打印电脑绑定成功");
$this->success("绑定成功");
}
} else {
$this->PrintComputerModel->where(["id" => $insert_id])->delete();
Log::error("远程绑定请求失败");
$this->error("请求失败");
}
}
}