getAccessToken($printer['client_id']);
if (!empty($token)) {
return $token;
}
// 没有则调用接口去获取
$client = new YlyOauthClient($config);
$token = $client->getToken();
$accessToken = $token->access_token;
// 自有应用模式access_token无失效时间,接口访问频次:10次/日
(new YlyPrinterCache())->setAccessToken($printer['client_id'], $accessToken, 20 * 24 * 3600);
return $accessToken;
}
/**
* @notes 获取打印模板
* @param $printer
* @return array
* @author Tab
* @date 2021/11/16 19:37
*/
public function tempalte($printer)
{
$withoutField = 'create_time, update_time, delete_time';
return PrinterTemplate::withoutField($withoutField)->findOrEmpty($printer['template_id'])->toArray();
}
/**
* @notes 开始打印
* @param $order
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author Tab
* @date 2021/11/16 19:37
*/
public function startPrint($order, $scene = PrinterEnum::ORDER_PRIINT)
{
// 打印机状态
$status = false;
// 打印机列表
$printerLists = Printer::where('status', YesNoEnum::YES)->select()->toArray();
foreach ($printerLists as $printer) {
$response = (new YlyPrinterService())->getPrintStatus($printer);
if (! in_array($response->body->state, [ 1, 2 ])) {
continue;
}
$status = true;
if ($scene == PrinterEnum::ORDER_PAY && !$printer['auto_print']) {
// 订单支付但未开启打印跳过
continue;
}
$template = $this->tempalte($printer);
$this->singlePrint($printer, $order, $template);
}
return $status;
}
/**
* @notes 添加打印机
* @param $params
* @return mixed
* @author Tab
* @date 2021/11/16 19:37
*/
public function addPrinter($params)
{
$print = $this->getPrinter($params);
$response = $print->addPrinter($params['machine_code'],$params['private_key'],$params['name']);
return $response;
}
/**
* @notes 删除打印机
* @param $params
* @author Tab
* @date 2021/11/16 17:32
*/
public function deletePrinter($params)
{
$print = $this->getPrinter($params);
$print->deletePrinter($params['machine_code']);
}
/**
* @notes 获取打印机状态
* @param $params
* @return mixed
* @author Tab
* @date 2021/11/16 18:04
*/
public function getPrintStatus($params){
$print = $this->getPrinter($params);
$response = $print->getPrintStatus($params['machine_code']);
return $response;
}
/**
* @notes 获取打印机接口
* @param $params
* @return PrinterService
* @author Tab
* @date 2021/11/16 18:52
*/
public function getPrinter($params)
{
$config = new YlyConfig($params['client_id'], $params['client_secret']);
$accessToken = $this->getAccessToken($params, $config);
return new PrinterService($accessToken, $config);
}
/**
* @notes 获取打印接口
* @param $params
* @return PrintService
* @author Tab
* @date 2021/11/16 18:53
*/
public function getPrint($params)
{
$config = new YlyConfig($params['client_id'], $params['client_secret']);
$accessToken = $this->getAccessToken($params, $config);
return new PrintService($accessToken, $config);
}
/**
* @notes 单次打印
* @param $printer
* @param $order
* @param $template
* @author Tab
* @date 2021/11/16 18:53
*/
public function singlePrint($printer, $order, $template)
{
$shopName = ConfigService::get('shop', 'name');
$print = $this->getPrint($printer);
$content = "
| 商品信息 | 数量 | 单价 |
| ".mb_substr($goods['goods_snap']->goods_name, 0, 6)." | "; $content .= "".$goods['goods_num']." | "; switch ($order['order_type']) { // 拼团 秒杀 砍价 使用实际价格 case OrderEnum::TEAM_ORDER: case OrderEnum::SECKILL_ORDER: case OrderEnum::BARGAIN_ORDER: $content .= "¥".$goods['goods_price']." | ¥".$goods['original_price']." | "; $all_original_money += $goods['original_price'] * $goods['goods_num']; break; } } $content .= '