event = $param['event'];
if (!isset($param['order_type'])) {
$this->order = Order::find()->where(['id' => $param['order_id']])->one();
$this->mall_id = $this->order['mall_id'];
$this->order_detail = $this->order->detail;
} else {
$order_type = $param['order_type'];
switch ($order_type){
case "C":
$storeCashierOrder = StoreCashierOrder::find()->where(['id' => $param['order_id']])->one();
$extra = json_decode($storeCashierOrder['extra'],true);
$this->mall_id = $storeCashierOrder['mall_id'];
$order = new Order();
$order->user_id = $storeCashierOrder['user_id'];
$order->mall_id = $storeCashierOrder['mall_id'];
$order->store_id = $storeCashierOrder['store_id'];
$order->order_no = $storeCashierOrder['order_no'];
$order->payment_type = $storeCashierOrder['payment_type'];
$order->pay_money = $storeCashierOrder['pay_money'];
$order->created_at = $storeCashierOrder['created_at'];
$order->remark = $storeCashierOrder['remark'];
$order->original_goods_price = $storeCashierOrder['money'];
$order->seller_remark = '';
$order->score_money = 0.00;
$order->coupon_money = 0.00;
if(!empty($extra)&&is_array($extra)){
if(!empty($extra['deduct_num_score'])){
$order->score_money = $extra['discount_money'];
}
if(!empty($extra['discount'])){
$order->coupon_money = $extra['discount'];
}
}
$order_detail = StoreCashierOrderDetail::lists(['where'=>[['cashier_order_id'=>$storeCashierOrder['id']]]],false);
if(empty($order_detail)) $order_detail = new OrderDetail();
$this->order = $order;
$this->order_detail = $order_detail;
break;
}
}
$params = [
'where' => [['=', 'mall_id', $this->mall_id], ['in', 'name', ['contact_tel', 'web_url', 'quick_map_address']]],
'select' => ['value', 'name']
];
$this->mall_info = array_column(MallSetting::lists($params), 'value', 'name');
return $this;
}
/**
* 设置打印机配置
* @return array
*/
public function getOpenPrinterConfig()
{
$params = [
'select' => [$this->event, 'terminal', 'secret_key', 'api_key', 'mall_id', 'user_id', 'printer_type', 'store_id','mch_id', 'client_id','print_width'],
'where' => [['=', 'mall_id', $this->mall_id], ['status'=>StatusEnum::ENABLED]]
];
$list = Printer::lists($params);
if (empty($list)) return [];
foreach ($list as $k => $val) {
$v = json_decode($val[$this->event], true);
if ($v['is_printer'] == 0) {
unset($list[$k]);
continue;
}
if(!empty($this->order['mch_id'])){
if($this->order['mch_id']!=$val['mch_id']){
continue;
}
}
if(!empty($this->order['store_id'])){
if($this->order['store_id']!=$val['store_id']){
continue;
}
}
$this->config[] = [
'user_id' => $val['user_id'],
'mall_id' => $val['mall_id'],
'client_id' => $val['client_id'],
'machine_code' => $val['terminal'],
'key' => $val['secret_key'],
'client_key' => $val['api_key'],
'time' => $v['num'],
'printer_type' => $val['printer_type'],
'store_id' => $val['store_id'],
'mch_id' => $val['mch_id'],
'template_id' => $v['template_id'],
'print_width' => $val['print_width'],
];
}
}
public function getPrinterTemplate()
{
if(!empty($this->config)){
$template_id = array_column($this->config, 'template_id');
$params = [
'where' => [['=', 'mall_id', $this->mall_id], ['status'=>StatusEnum::ENABLED], ['in', 'id', $template_id]]
];
$this->template_list = array_column(PrinterTemplate::lists($params), null, 'id');
}
}
/**
* 打印
*/
public function print()
{
try {
//判断是商场打印机还是门店打印机
$this->getOpenPrinterConfig();
$this->getPrinterTemplate();
if (empty($this->template_list) || empty($this->config)) {
return;
}
$template = new DefaultTemplate();
$redis = \Yii::$app->redis;
foreach ($this->config as $config) {
if (($config['printer_type'] == 1 && $this->order['store_id'] == 0 && $this->order['mch_id'] == 0) || ($config['printer_type'] == 2 && $this->order['store_id'] == $config['store_id'])|| ($config['printer_type'] == 3 && $this->order['mch_id'] == $config['mch_id'])) {
\Yii::error('===================='.json_encode($config));
$clientId = $config['client_id'];
$machine_code = $config['machine_code'];
$clientSecret = $config['client_key'];
$yilian_config = new YlyConfig($clientId, $clientSecret);
$client = new YlyOauthClient($yilian_config);
$mall_id = $config['mall_id'];
$cacheKey = RedisKeyEnum::suffix('printer_keys', $clientId);
$access_token = $redis->get($cacheKey);
if (empty($access_token)) {
$token = $client->getToken();
$access_token = $token->access_token; //调用API凭证AccessToken 永久有效,请妥善保存.
$redis->set($cacheKey,$access_token,'EX',30*24*3600-1);
}
$machine_code = $config['machine_code']; //机器码
$origin_id = StringHelper::generateOrderNo('p'); //内部订单号(32位以内)
$template_info = $this->template_list[$config['template_id']];
$printData = $this->setOrderPrintData($template_info);
/**文本接口开始**/
$print = new PrintService($access_token, $yilian_config);
$content = $this->getContent($printData,$config['print_width']);
$res = $print->index($machine_code, $content, $origin_id);
if(isset($res->error)&&$res->error!=0){
throw new Exception($res->error_description);
}else{
$result['err'] = 0;
$result['msg'] = '打印成功';
}
}
}
if(!isset($result['err'])){
$result['err'] = 1;
$result['msg'] = '打印错误,请检查打印机配置是否正确';
}
} catch (Exception $e) {
\Yii::error('打印错误,msg=' . $e->getMessage() . ',行数:' . $e->getLine() . $e->getFile());
$result['err'] = 1;
$result['msg'] = '打印错误,msg=' . $e->getMessage() . ',行数:' . $e->getLine() . $e->getFile();
}
return $result;
}
public function getContentOld($data)
{
$content = '';
foreach ($data as $item) {
if (isset($item['show']) && $item['show'] == 0) {
continue;
}
$getter = 'get' . $item['handle'];
if (isset($item['children'])) {
$children = $item['content'];
foreach ($item['children'] as $value) {
if (isset($value['content'])) {
$childGetter = 'get' . $value['handle'];
$children .= $this->$childGetter($value['content']);
}
}
$content .= $this->$getter($children);
} else {
$content .= $this->$getter($item['content']);
}
}
return $content;
}
public function getContent($printData,$print_width)
{
$order_type = '商城订单';
$mall_name = '';
if(!empty($printData->mall_name)){
$mall_name = $printData->mall_name;
}
if(!empty($this->order['mch_id'])){
$order_type = '商户订单';
if(MallAddons::isInstall($this->order['mall_id'],'Mch')){
$mch = Mch::findOne(['id'=>$this->order['mch_id']]);
if(!empty($mch['store_name'])){
$mall_name = $mch['store_name'];
}
}
}
$is_store = false;
if(!empty($this->order['store_id'])){
$order_no = $this->order['order_no'];
if($order_no[0]=='C'){
$order_type = '门店收银台订单';
}else{
$order_type = '门店订单';
}
$store = Store::findOne(['id'=>$this->order['store_id']]);
if(!empty($store['store_name'])){
$is_store = true;
$mall_name = $store['store_name'];
}
}
$content = "
| 名称 | 数量 | "; if(isset($printData->is_show_price)&&$printData->is_show_price) $content .= "单价 | "; $content .= "
| $item->name | $item->num | "; if(isset($printData->is_show_price)&&$printData->is_show_price){ $content .= "$item->unit_price | "; } $content .= "
| [" . $item->attr . "] |