Prints.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace app\controller\merchant;
  3. use app\extra\basic\Base;
  4. use app\extra\service\saas\PrintService;
  5. use app\extra\weMini\Link;
  6. use app\middleware\AuthMiddleware;
  7. use app\model\saas\SaasPrintClient;
  8. use DI\Attribute\Inject;
  9. use Kkokk\Poster\Facades\Poster;
  10. use LinFly\Annotation\Route\Controller;
  11. use LinFly\Annotation\Route\Middleware;
  12. use LinFly\Annotation\Route\Route;
  13. use support\Request;
  14. use support\Response;
  15. #[Controller(prefix: "/api/mer/prints"),Middleware(AuthMiddleware::class)]
  16. class Prints extends Base
  17. {
  18. #[Inject]
  19. protected PrintService $service;
  20. #[Inject]
  21. protected SaasPrintClient $model;
  22. #[Route(path: "list",methods: "get")]
  23. public function getPrintList(Request $request): Response
  24. {
  25. try {
  26. $param = $request->get();
  27. $list = $this->service->getList($param);
  28. return successTrans("success.data",pageFormat($list),200);
  29. } catch (\Throwable $throwable) {
  30. return error($throwable->getMessage());
  31. }
  32. }
  33. /**
  34. * 保存配置
  35. * @param Request $request
  36. * @return Response
  37. */
  38. #[Route(path: "save",methods: "post")]
  39. public function savePrint(Request $request): Response
  40. {
  41. try {
  42. $param = $this->_valid([
  43. "id.require" => trans(20010),
  44. "is_price.require" => trans(20010),
  45. "paper_size.require" => trans(20010),
  46. "color.require" => trans(20010),
  47. "direction.require" => trans(20010),
  48. "duplex.require" => trans(20010),
  49. "package.require" => trans(20010),
  50. "type.require" => trans(20010)
  51. ],"post");
  52. if (!is_array($param)) return error($param);
  53. $printId = $param['id'];
  54. $is_price = $param['is_price'];
  55. unset($param['id'],$param['is_price']);
  56. $print = (new SaasPrintClient)->where("id",$printId)->findOrEmpty();
  57. if ($print->isEmpty()) return errorTrans("empty.data");
  58. $print->is_price = $is_price;
  59. $print->rule = json_encode($param);
  60. $state = $print->save();
  61. if (!$state) return errorTrans("error.data");
  62. return successTrans("success.data");
  63. } catch (\Throwable $throwable) {
  64. return error($throwable->getMessage());
  65. }
  66. }
  67. /**
  68. * 保存配置
  69. * @param Request $request
  70. * @return Response
  71. */
  72. #[Route(path: "single",methods: "post")]
  73. public function savePrintSingle(Request $request): Response
  74. {
  75. try {
  76. $param = $request->post();
  77. if (empty($param['id'])) return errorTrans("empty.require");
  78. $print = (new SaasPrintClient)->where("id",$param['id'])->findOrEmpty();
  79. if ($print->isEmpty()) return errorTrans("empty.data");
  80. if (isset($param['status'])) {
  81. $param['status'] = $print['status'] == 1 ? 2 : 1;
  82. }
  83. $state = $print->save($param);
  84. if (!$state) return errorTrans("error.data");
  85. return successTrans("success.data");
  86. } catch (\Throwable $throwable) {
  87. return error($throwable->getMessage());
  88. }
  89. }
  90. /**
  91. * 二维码
  92. * @param Request $request
  93. * @return Response
  94. */
  95. #[Route(path: "qrcode",methods: "post")]
  96. public function printQrcode(Request $request): Response
  97. {
  98. try {
  99. $param = $request->post();
  100. if (empty($param['id'])) return errorTrans("empty.require");
  101. $print = (new SaasPrintClient)->where("id",$param['id'])->findOrEmpty();
  102. if ($print->isEmpty()) return errorTrans("empty.data");
  103. $qrcodePath = public_path()."/uploads/card/{$print['shop_id']}-print-{$print['code']}.jpg";
  104. if (!is_file($qrcodePath)) {
  105. $pathBase = "/uploads/qrcode/".$print['shop_id']."-".$print['code'].".jpg";
  106. $path = (new Link([
  107. "appid" => sConf("wechat.mini_appid"),
  108. "appsecret" => sConf("wechat.mini_secret")
  109. ]))->createQrcodeWx("/pages/index/index","shop={$print['shop_id']}&code={$print['code']}",$pathBase);
  110. $bg = base_path()."/resource/img/store-qrcode.jpg";
  111. $fontPath = base_path()."/resource/font/msyh.ttc";
  112. if (!is_file($qrcodePath)) {
  113. Poster::config([
  114. "path" => $qrcodePath
  115. ])->buildImDst($bg,425,578)
  116. // ->buildText(isset($print['shop']['shop_name'])?$print['shop']['shop_name']:'印美自助打印','center',390,18,[0,0,0,1],180,$fontPath)
  117. // ->buildQr($link,105,160,0,0,217,217)
  118. // ->buildImage($qrcodeminiPath,105,180,0,0,217,217)
  119. ->buildImage($path,105,170,0,0,217,247)
  120. ->getPoster();;
  121. }
  122. }
  123. return successTrans("success.data",['img' => 'data:image/png;base64,'.base64_encode(file_get_contents($qrcodePath))]);
  124. } catch (\Throwable $throwable) {
  125. return error($throwable->getMessage());
  126. }
  127. }
  128. }