我们在使用tp5的队列的时候
发现,队列中的代码,如果使用的 config(XXX)来获取配置参数时,全部返回的是 NULL
出现这个问题的原因在于,队列使用了 php-cli的模式,所以它不会找到配置的参数,同样的,如果使用的 common.php中的自定义函数,也会报错,报不到这个函数
参考 https://blog.csdn.net/qq_35422558/article/details/102022422
解决的办法有很多,一个就是把所要用到的配置和函数在 job 的类中重新定义一遍,也就是写死在里面,这种方法当然很low
另一种解决方法是,在job的处理函数中,重新引入配置和函数,如下图
<?php
namespace app\api\jobs;
use app\api\util\nopasspay\NopassPay;
use think\Config;
use think\Db;
use think\queue\Job;
use app\api\util\guzzlerequest\GuzzleRequest;
class ApplyPayJob{
public function fire(Job $job,$data){
if($this->doJobs($data)){
//如果成功,那删除队列中的数据
$job->delete();
}else{
//如果不成功 重新发布
if($job->attempts() > 3){
$order = Db::name("order")->where("order_sn",$data['order_sn'])->find();
unset($order["id"]);
Db::name("order_error")->insert($order);
Db::name("order")->where("order_sn",$data['order_sn'])->delete();
$job->delete();
}else{
$job->release(86400);
}
}
}
public function doJobs($data){
$order_sn = $data["order_sn"];
$order = Db::name("order")->where("order_sn",$order_sn)->find();
return $this->applypayAgain($order,$data);
}
public function applypayAgain($order,$data){
$nopasspay = new NopassPay();
$scene_info = json_encode([
"scene_info"=>[
"start_time"=>date("YmdHis",strtotime($order["ctimes"])),
"deviceid"=>$order["deviceCode"],
"plate_number"=>$order["busNumber"]
]
]);
$nonce_str = $nopasspay->getrandomStr(32);
//得到用户的签约id
$contract = Db::name("contract")->where("uid",$order["uid"])->find();
$config = Config::load(__DIR__."/../config.php");
$nopasspay->key = $config["mininopasspay"]["key"];
require_once(__DIR__."/../common.php");
$data1 = [
"appid"=>$config["mininopasspay"]["appid"],
"mch_id"=>$config["mininopasspay"]["mch_id"],
"sub_mch_id"=>$config["mininopasspay"]["sub_mch_id"],
"nonce_str"=>$nonce_str,
"sign_type"=>"HMAC-SHA256",
"body"=>"公交代扣",
"out_trade_no"=>$order["order_sn"],
"total_fee"=>bcmul($order["pay"],100),
"spbill_create_ip"=>$data["spbill_create_ip"],
"notify_url"=>$config["mydomain"].$config["mininopasspay"]["payapply_notify_url"],
"trade_type"=>"PAP",
"contract_id"=>$contract["contract_id"],
"trade_scene"=>"BUS",
"scene_info"=>$scene_info,
];
$sign = $nopasspay->makeSign($data1,"HMAC-SHA256");
$data1["sign"] = $sign;
$xml = arrayToXml($data1);
$guzzlerequest = new GuzzleRequest();
$result = $guzzlerequest->request("POST",$nopasspay->payApplyUrl,[
'headers' => [
'Content-Type' => 'text/xml; charset=UTF8',
],
"body"=>$xml
]);
$resultArr = xmlToArray($result);
if($resultArr["return_code"] == "SUCCESS" && $resultArr["result_code"] == "SUCCESS"){
$sign = $resultArr["sign"];
unset($resultArr["sign"]);
$attributes = filternone($resultArr);
if($nopasspay->checkSign($attributes,$sign,"HMAC-SHA256")){
//验签成功,
Db::name("order")->where("order_sn",$order["order_sn"])->update([
"is_confirm"=>2
]);
return 1;
}
}
return 0;
}
}
从上面的代码中,我们使用
$contract = Db::name("contract")->where("uid",$order["uid"])->find();
$config = Config::load(__DIR__."/../config.php");
$nopasspay->key = $config["mininopasspay"]["key"];
require_once(__DIR__."/../common.php");
分别 把配置文件 赋值给了一个变量 $config 用 require 引入了 common.php
所以下面的 $config["mininopasspay"]["key"]; 就找到了配置值
并且下的自定义函数 xmlToArray() 以及 arrayToXml()等函数就可以找到了