| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- namespace app\controller\merchant;
- use app\extra\basic\Base;
- use app\extra\service\saas\PrintService;
- use app\extra\weMini\Link;
- use app\middleware\AuthMiddleware;
- use app\model\saas\SaasPrice;
- use app\model\saas\SaasPrintClient;
- use DI\Attribute\Inject;
- use Kkokk\Poster\Facades\Poster;
- use LinFly\Annotation\Route\Controller;
- use LinFly\Annotation\Route\Middleware;
- use LinFly\Annotation\Route\Route;
- use support\Request;
- use support\Response;
- #[Controller(prefix: "/api/mer/prints"),Middleware(AuthMiddleware::class)]
- class Prints extends Base
- {
- #[Inject]
- protected PrintService $service;
- #[Inject]
- protected SaasPrintClient $model;
- #[Route(path: "list",methods: "get")]
- public function getPrintList(Request $request): Response
- {
- try {
- $param = $request->get();
- $param['shop'] = $request->user['agent_id'];
- $list = $this->service->getList($param);
- return successTrans("success.data",pageFormat($list),200);
- } catch (\Throwable $throwable) {
- return error($throwable->getMessage());
- }
- }
- /**
- * 保存配置
- * @param Request $request
- * @return Response
- */
- #[Route(path: "save",methods: "post")]
- public function savePrint(Request $request): Response
- {
- try {
- $param = $this->_valid([
- "id.require" => trans(20010),
- "is_price.require" => trans(20010),
- "paper_size.require" => trans(20010),
- "color.require" => trans(20010),
- "direction.require" => trans(20010),
- "duplex.require" => trans(20010),
- "package.require" => trans(20010),
- "type.require" => trans(20010)
- ],"post");
- if (!is_array($param)) return error($param);
- $printId = $param['id'];
- $is_price = $param['is_price'];
- unset($param['id'],$param['is_price']);
- $print = (new SaasPrintClient)->where("id",$printId)->findOrEmpty();
- if ($print->isEmpty()) return errorTrans("empty.data");
- $print->is_price = $is_price;
- $print->rule = json_encode($param);
- $state = $print->save();
- if (!$state) return errorTrans("error.data");
- return successTrans("success.data");
- } catch (\Throwable $throwable) {
- return error($throwable->getMessage());
- }
- }
- /**
- * 保存配置
- * @param Request $request
- * @return Response
- */
- #[Route(path: "single",methods: "post")]
- public function savePrintSingle(Request $request): Response
- {
- try {
- $param = $request->post();
- if (empty($param['id'])) return errorTrans("empty.require");
- $print = (new SaasPrintClient)->where("id",$param['id'])->findOrEmpty();
- if ($print->isEmpty()) return errorTrans("empty.data");
- if (isset($param['status'])) {
- $param['status'] = $print['status'] == 1 ? 2 : 1;
- }
- $state = $print->save($param);
- if (!$state) return errorTrans("error.data");
- return successTrans("success.data");
- } catch (\Throwable $throwable) {
- return error($throwable->getMessage());
- }
- }
- /**
- * 二维码
- * @param Request $request
- * @return Response
- */
- #[Route(path: "qrcode",methods: "post")]
- public function printQrcode(Request $request): Response
- {
- try {
- $param = $request->post();
- if (empty($param['id'])) return errorTrans("empty.require");
- $print = (new SaasPrintClient)->where("id",$param['id'])->findOrEmpty();
- if ($print->isEmpty()) return errorTrans("empty.data");
- if ($print['status'] <> 1) return errorTrans("error.status-qrcode");
- $qrcodePath = public_path()."/uploads/card/{$print['shop_id']}-print-{$print['code']}.jpg";
- if (!is_file($qrcodePath)) {
- $pathBase = "/uploads/qrcode/".$print['shop_id']."-".$print['code'].".jpg";
- $path = (new Link([
- "appid" => sConf("wechat.mini_appid"),
- "appsecret" => sConf("wechat.mini_secret")
- ]))->createQrcodeWx("/pages/index/index","shop={$print['shop_id']}&code={$print['code']}",$pathBase);
- $bg = base_path()."/resource/img/store-qrcode.jpg";
- $fontPath = base_path()."/resource/font/msyh.ttc";
- if (!is_file($qrcodePath)) {
- Poster::config([
- "path" => $qrcodePath
- ])->buildImDst($bg,425,578)
- // ->buildText(isset($print['shop']['shop_name'])?$print['shop']['shop_name']:'印美自助打印','center',390,18,[0,0,0,1],180,$fontPath)
- // ->buildQr($link,105,160,0,0,217,217)
- // ->buildImage($qrcodeminiPath,105,180,0,0,217,217)
- ->buildImage($path,105,170,0,0,217,247)
- ->getPoster();;
- }
- }
- return successTrans("success.data",['img' => 'data:image/png;base64,'.base64_encode(file_get_contents($qrcodePath))]);
- } catch (\Throwable $throwable) {
- return error($throwable->getMessage());
- }
- }
- #[Route(path: "price",methods: "get")]
- public function getPrintPrice(Request $request): Response
- {
- try {
- $price = (new SaasPrice)->where("shop_id",$request->user['agent_id'])->where("type",1)->field("id,paper_size,duplex,color")->select();
- if ($price->isEmpty()) return errorTrans("empty.data");
- return successTrans("success.data",$price->toArray());
- } catch (\Throwable $throwable) {
- return error($throwable->getMessage());
- }
- }
- }
|