client_id = $client_id; //应用id
$this->client_secret = $client_secret; // 应用密钥
$this->yly_config = new YlyConfig($this->client_id, $this->client_secret);
$this->access_token = Cache::get('yly_access_token');
//没有access_token时获取
if(empty($this->access_token)){
$this->getToken();
}
}
/**
* Notes:获取access_token
* @return mixed
*/
public function getToken(){
$client = new YlyOauthClient($this->yly_config);
$token = $client->getToken();
$this->access_token = $token->access_token;
Cache::tag('yly_printer')->set('yly_access_token',$this->access_token);
//刷新token、有效期35天(自用型刷新token作用不大)
Cache::tag('yly_printer')->set('yly_refresh_token',$token->refresh_token,35*24*3600);
}
/**
* Notes:刷新access_token
* @return mixed
*/
public function refreshToken(){
$client = new YlyOauthClient($this->yly_config);
$token = $client->refreshToken(Cache::get('yly_refresh_token'));
$this->access_token = $token->access_token;
//重置token
Cache::tag('yly_printer')->set('yly_access_token',$this->access_token);
Cache::tag('yly_printer')->set('yly_refresh_token',$token->refresh_token,35*24*3600);
}
/**
* Notes: 添加打印机
* @param string $machine_code 终端号
* @param string $msign 秘钥
* @param string $print_name 打印机名称
* @return bool|string
*/
public function addPrinter($machine_code,$msign,$print_name){
$print = new PrinterService($this->access_token,$this->yly_config);
$response = $print->addPrinter($machine_code,$msign,$print_name);
return $response;
}
/**
* Notes:删除打印机
* @param sting $machine_code 终端号
* @return bool|string
*/
public function deletePrinter($machine_code){
$print = new PrinterService($this->access_token,$this->yly_config);
$print->deletePrinter($machine_code);
}
/**
* Notes: 设置logo
* @param sting $machine_code 终端号
* @param sting $url logo
*/
public function setIcon($machine_code,$url){
$print = new PrinterService($this->access_token,$this->yly_config);
$print->setIcon($machine_code,$url);
}
/**
* Notes:获取终端状态
* @param sting $machine_code 终端号
*/
public function getPrintStatus($machine_code){
$print = new PrinterService($this->access_token,$this->yly_config);
$response = $print->getPrintStatus($machine_code);
return $response;
}
/**
* Notes:打印机接口
* @param array $machine_list 打印机信息
* @param $order 订单信息
* @return bool|string
*/
public function ylyPrint($printer_list = [] , $order,$template_config){
$print = new PrintService($this->access_token, $this->yly_config);
$order['title'] = $template_config['title'] ?? '';
$order['qr_code'] = $template_config['qr_code_link'] ?? '';
$order['remark'] = $template_config['remark'] ?? '';
foreach ($printer_list as $printer){
if($printer['machine_code']){
$content = "