ThinkPHP对接易联云打印

引入composer包

composer require yly-openapi/yly-openapi-sdk

php 复制代码
<?php

namespace app\common\library;

use app\admin\model\yp\Order;
use App\Api\PrintService;
use App\Config\YlyConfig;
use App\Oauth\YlyOauthClient;
use think\Cache;
use think\Config;

class Yly
{

    /**
     * 易联云打印订单
     * @param $security_info
     * @return int
     * @throws \think\Exception
     */
    public function print_yly($security_info)
    {
        //获取配置中的应用id和密匙
        $config = new YlyConfig(Config::get('site.clientId'), Config::get('site.clientSecret'));
        $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天         //机器码
        $origin_id = time();                                //内部订单号(32位以内)
        /**文本接口开始**/
        $print = new PrintService($access_token, $config);
        //58mm排版 排版指令详情请看 http://doc2.10ss.net/332006
        $content = '';//
        $content .= "<FS2><center>金蛋到家</center></FS2>";
        $content .= "时间:".date('Y-m-d H:i:s', $security_info->createtime) . "\n";
        $content .= "单号:". $security_info->order_no . "\n";
        $content .= "姓名:". $security_info->name . "\n";
        $content .= "电话:". $security_info->phone . "\n";
        $content .= "城市:". $security_info->city . "\n";
        $content .= "地址:". $security_info->address . "\n";
        $content .= "商品:------------". "\n";
        foreach ($security_info->item as $key=>$value){
            $content .= $value['goods_title']."*".$value['num']."\n";
        }
        $content .= "-----------------". "\n";
        $content .= "备注:". $security_info->remarks . "\n";
        $content .= "金额:". $security_info->goods_money . "\n";
        $youhui = $security_info->goods_money-$security_info->actual_money;
        if($youhui >0){
            $content .= "优惠:". $youhui . "\n";
        }
        $content .= "实付:". $security_info->actual_money . "\n";
        try {
            //获取配置中的设备号
            $res = $print->index(Config::get('site.machineCode'), $content, $origin_id);
        } catch (Exception $e) {
            throw new \think\Exception('打印失败');
            return 0;
        }
        //打印状态升级成已打印
        Order::where('id',$security_info->id)->where('print_status','0')->update(['print_status'=>'1']);
        return 1;
    }

}

如上,传入一个订单信息,根据订单信息定义打印文本

易联云文档:指令集 · yilianyun-open-api · 看云

相关推荐
2401_857439696 小时前
SSM 架构下 Vue 电脑测评系统:为电脑性能评估赋能
开发语言·php
远游客07137 小时前
centos stream 8下载安装遇到的坑
linux·服务器·centos
LIKEYYLL9 小时前
GNU Octave:特性、使用案例、工具箱、环境与界面
服务器·gnu
云云32110 小时前
搭建云手机平台的技术要求?
服务器·线性代数·安全·智能手机·矩阵
云云32110 小时前
云手机有哪些用途?云手机选择推荐
服务器·线性代数·安全·智能手机·矩阵
CircleMouse10 小时前
Centos7, 使用yum工具,出现 Could not resolve host: mirrorlist.centos.org
linux·运维·服务器·centos
木子Linux11 小时前
【Linux打怪升级记 | 问题01】安装Linux系统忘记设置时区怎么办?3个方法教你回到东八区
linux·运维·服务器·centos·云计算
不惑_11 小时前
小白入门 · 腾讯云轻量服务器部署 Hadoop 3.3.6
服务器·hadoop·腾讯云
阿甘知识库12 小时前
宝塔面板跨服务器数据同步教程:双机备份零停机
android·运维·服务器·备份·同步·宝塔面板·建站
Smile灬凉城66612 小时前
反序列化为啥可以利用加号绕过php正则匹配
开发语言·php