88 lines
2.2 KiB
PHP
88 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
/**
|
|
* 佳博云打印
|
|
* https://dev.poscom.cn/docs
|
|
*/
|
|
class PrintService
|
|
{
|
|
protected $memberCode;
|
|
protected $apiKey;
|
|
|
|
function __construct()
|
|
{
|
|
$this->memberCode = config('postcom.memberCode');
|
|
$this->apiKey = config('postcom.apiKey');
|
|
}
|
|
|
|
/**
|
|
* 通过模板打印
|
|
* https://dev.poscom.cn/openapi?templetPrint
|
|
*
|
|
* @param string $deviceID
|
|
* @param string $templetID
|
|
* @param array $data
|
|
*
|
|
* @return array {code: 0(成功), msg: '错误信息'}
|
|
*/
|
|
public function template($deviceID, $templetID, $data)
|
|
{
|
|
$result = ['code' => 0, 'msg' => ''];
|
|
if (config('app.env') === 'local') {
|
|
return $result;
|
|
}
|
|
$url = 'https://api.poscom.cn/apisc/templetPrint';
|
|
$params = $this->params(['deviceID' => $deviceID]);
|
|
$params['templetID'] = $templetID;
|
|
$params['tData'] = json_encode($data);
|
|
|
|
$response = Http::asForm()->post($url, $params);
|
|
if ($response->successful()) {
|
|
$result = $response->json();
|
|
} else {
|
|
$result['code'] = 1;
|
|
$result['msg'] = '请求失败: ' . $response->status();
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* 构造请求参数
|
|
*
|
|
* @param array $data {deviceID: 终端编号}
|
|
* @return array
|
|
*/
|
|
protected function params($data = [])
|
|
{
|
|
$reqTime = $this->getMillisecond();
|
|
$deviceID = data_get($data, 'deviceID');
|
|
$memberCode = $this->memberCode;
|
|
$msgNo = '';
|
|
$securityCode = md5($memberCode.$deviceID.$msgNo.$reqTime.$this->apiKey);
|
|
return [
|
|
'reqTime' => $reqTime,
|
|
'securityCode' => $securityCode,
|
|
'memberCode' => $memberCode,
|
|
'deviceID' => $deviceID,
|
|
];
|
|
}
|
|
|
|
protected function getMillisecond()
|
|
{
|
|
list($t1, $t2) = explode(' ', microtime());
|
|
return (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000);
|
|
}
|
|
|
|
/**
|
|
* @return PrintService
|
|
*/
|
|
public static function make(...$params)
|
|
{
|
|
return new static(...$params);
|
|
}
|
|
} |