当前位置:   article > 正文

ThinkPHP对接易联云打印

ThinkPHP对接易联云打印

引入composer包

composer require yly-openapi/yly-openapi-sdk

  1. <?php
  2. namespace app\common\library;
  3. use app\admin\model\yp\Order;
  4. use App\Api\PrintService;
  5. use App\Config\YlyConfig;
  6. use App\Oauth\YlyOauthClient;
  7. use think\Cache;
  8. use think\Config;
  9. class Yly
  10. {
  11. /**
  12. * 易联云打印订单
  13. * @param $security_info
  14. * @return int
  15. * @throws \think\Exception
  16. */
  17. public function print_yly($security_info)
  18. {
  19. //获取配置中的应用id和密匙
  20. $config = new YlyConfig(Config::get('site.clientId'), Config::get('site.clientSecret'));
  21. $client = new YlyOauthClient($config);
  22. try {
  23. $token = Cache::get('access_token');
  24. if ($token) {
  25. $token = json_decode($token);
  26. }
  27. if (!$token) {
  28. $token = $client->getToken();
  29. Cache::set('access_token', json_encode($token), 864000);
  30. }
  31. } catch (Exception $e) {
  32. throw new \think\Exception('获取或更新access_token的次数,已超过最大限制! ');
  33. echo $e->getMessage() . "\n";
  34. print_r(json_decode($e->getMessage(), true));
  35. return;
  36. }
  37. $access_token = $token->access_token; //调用API凭证AccessToken 永久有效,请妥善保存.
  38. $refresh_token = $token->refresh_token; //刷新AccessToken凭证 失效时间35天
  39. $expires_in = $token->expires_in; //自有型应用可忽略此回调参数, AccessToken失效时间30天 //机器码
  40. $origin_id = time(); //内部订单号(32位以内)
  41. /**文本接口开始**/
  42. $print = new PrintService($access_token, $config);
  43. //58mm排版 排版指令详情请看 http://doc2.10ss.net/332006
  44. $content = '';//
  45. $content .= "<FS2><center>金蛋到家</center></FS2>";
  46. $content .= "时间:".date('Y-m-d H:i:s', $security_info->createtime) . "\n";
  47. $content .= "单号:". $security_info->order_no . "\n";
  48. $content .= "姓名:". $security_info->name . "\n";
  49. $content .= "电话:". $security_info->phone . "\n";
  50. $content .= "城市:". $security_info->city . "\n";
  51. $content .= "地址:". $security_info->address . "\n";
  52. $content .= "商品:------------". "\n";
  53. foreach ($security_info->item as $key=>$value){
  54. $content .= $value['goods_title']."*".$value['num']."\n";
  55. }
  56. $content .= "-----------------". "\n";
  57. $content .= "备注:". $security_info->remarks . "\n";
  58. $content .= "金额:". $security_info->goods_money . "\n";
  59. $youhui = $security_info->goods_money-$security_info->actual_money;
  60. if($youhui >0){
  61. $content .= "优惠:". $youhui . "\n";
  62. }
  63. $content .= "实付:". $security_info->actual_money . "\n";
  64. try {
  65. //获取配置中的设备号
  66. $res = $print->index(Config::get('site.machineCode'), $content, $origin_id);
  67. } catch (Exception $e) {
  68. throw new \think\Exception('打印失败');
  69. return 0;
  70. }
  71. //打印状态升级成已打印
  72. Order::where('id',$security_info->id)->where('print_status','0')->update(['print_status'=>'1']);
  73. return 1;
  74. }
  75. }

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

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/907400
推荐阅读
相关标签
  

闽ICP备14008679号