易联云打印机 php

首先加载composer

 

composer require yly-openapi/yly-openapi-sdk:v1.0.1


然后根据业务去设置打印



<?php
/*
Create By 2021/9/16 - 10:56 - Delimma
To overcome difficulties!
*/

namespace app\common\controller\lib;


use App\Api\PrintService;
use app\common\model\Order;
use app\common\model\UserCardHand;
use App\Config\YlyConfig;
use App\Oauth\YlyOauthClient;
use Exception;
use think\Cache;

class YlyPrint
{
    public function print_yly($order_num)
    {
        $config = new YlyConfig('1086274038','6622bd7f2907dd48997d8906c804b92f');
        $client = new YlyOauthClient($config);
        try {
            $token = Cache::get('access_token');
            if ($token){
                $token = json_decode($token);
            }
            if (!$token){
                $token = $client->getToken();
                Cache::set('access_token',json_encode($token),864000);
            }
        } catch (Exception $e) {
            throw new \think\Exception('获取或更新access_token的次数,已超过最大限制! ');
            echo $e->getMessage() . "\n";
            print_r(json_decode($e->getMessage(), true));
            return;
        }

        $access_token = $token->access_token;           //调用API凭证AccessToken 永久有效,请妥善保存.
        $refresh_token = $token->refresh_token;         //刷新AccessToken凭证 失效时间35天
        $expires_in = $token->expires_in;               //自有型应用可忽略此回调参数, AccessToken失效时间30天
        $machine_code = '4004561590';                             //机器码
        $origin_id = time();                                //内部订单号(32位以内)

        $order_info = Order::where(['order_id'=>$order_num])->find() ?? [];
        if ($order_info) $order_info = $order_info->toArray();
        $order_detail = json_decode($order_info['order_detail'],true);

        $pay_type = '';// 支付状态文字
        switch ($order_info['pay_type']){
            case 'xj':$pay_type='现金支付';break;
            case 'zfb':$pay_type='支付宝支付';break;
            case 'wx':$pay_type='微信支付';break;
            case 'zl':$pay_type='租赁';break;
        }

        $order_cost_price = 0;




        /**文本接口开始**/
        $print = new PrintService($access_token, $config);
//58mm排版 排版指令详情请看 http://doc2.10ss.net/332006
        $content = "<FS2><center>十二毛</center></FS2>";
        $content .= str_repeat('.', 32);
        $content .= "<FS2><center>--$pay_type--</center></FS2>";
        $content .= "订单时间:". date("Y-m-d H:i") . "\n";
        $content .= "订单编号:$order_num\n";
        $content .= str_repeat('*', 14) . "商品" . str_repeat("*", 14);
        $content .= "<table>";
        foreach ($order_detail as $k =>$v){
            $content .= "\n";
            $order_cost_price = $order_cost_price+$v['goods']['price'];
            $content .= "<tr><td>".$v['goods']['title'].$v['goods_sku_value']['value']."</td><td>x1</td><td>".$v['goods']['price']."</td></tr>";
            $content .= "<tr><td colspan='3'>货号:".$v['goods']['art_no']."</td></tr>";
            $content .= "\n";
        }

        $discount = 0; //折扣
        $content .= "</table>";
        $content .= str_repeat('.', 32);
        $user_card_id = $order_info['user_card_id'];
        if ($user_card_id ) {
            $user_card = UserCardHand::find($user_card_id);
            $user_card_discount = $user_card->sale_discount *100 ."%";
            $content .= "会员卡:$user_card->titile\n";
            if ($pay_type!='租赁'){
                $content .= "会员卡折扣:$user_card_discount\n";
            }

        }

        if ($order_info['discount'] != 1){
            $hand_discount = $order_info['discount']*100 ."%";
            $content .= "折上折:".$hand_discount."\n";
        }

        if ($order_info['zero'] != 0){
            $content .= "抹零:¥".$order_info['zero']."\n";
        }
        if ($pay_type!='租赁'){
            $discount = $order_cost_price - $order_info['money'];
            $content .= "小计:¥$order_cost_price\n";
            $content .= "折扣:¥$discount \n";
            $content .= str_repeat('*', 32);
            $content .= "订单总价:¥".$order_info['money']." \n";
        }

        $content .= "<FS2><center>************</center></FS2>";

        try{
//            $res = $print->index($machine_code, "<FS2><center>共享衣橱</center></FS2>", $origin_id);
            $res = $print->index($machine_code, $content, $origin_id);
        }catch (Exception $e) {
            throw new \think\Exception('打印失败');
        }

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苗先生的PHP记录

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值