Prints.php 6.0 KB

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