Product.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. use think\Response;
  6. /**
  7. * 商品
  8. */
  9. class Product extends Api
  10. {
  11. protected $noNeedLogin = [''];
  12. // 无需鉴权的接口,*表示全部
  13. protected $noNeedRight = ['*'];
  14. //生成我的视频海报
  15. //生成邀请码二维码图片
  16. public function inviteimage($product_id,$introcode) {
  17. $params['text'] = config('h5_url') . '/#/pages/home/detail?id='.$product_id.'&introcode=' . $introcode;
  18. $qrcode_service = \addons\qrcode\library\Service::qrcode($params);
  19. // $mimetype = 'image/png';
  20. // $response = Response::create()->header("Content-Type", $mimetype);
  21. // 直接显示二维码
  22. // header('Content-Type: ' . $qrcode_service->getContentType());
  23. // $response->content($qrcode_service->writeString());
  24. $qrcodePath = ROOT_PATH . 'public/uploads/qrcode/';
  25. if (!is_dir($qrcodePath)) {
  26. @mkdir($qrcodePath);
  27. }
  28. if (is_really_writable($qrcodePath)) {
  29. $filename = md5(implode('', $params)) . '.png';
  30. $filePath = $qrcodePath . $filename;
  31. $qrcode_service->writeFile($filePath);
  32. }
  33. return '/uploads/qrcode/' . $filename;
  34. }
  35. //生成视频分享海报
  36. public function shareposter() {
  37. $product_id = input('product_id', 0);
  38. if (!$product_id) {
  39. $this->error('参数缺失');
  40. }
  41. $id = \addons\unishop\extend\Hashids::decodeHex($product_id);
  42. $info = Db::name('unishop_product')->where('id',$id)->find();
  43. if (!$info) {
  44. $this->error('商品不存在');
  45. }
  46. $info = info_domain_image($info,['image']);
  47. $inviteimage = $this->inviteimage($product_id,$this->auth->introcode);
  48. $data = [
  49. [
  50. "left"=> "0px",
  51. "top"=> "0px",
  52. "type"=> "img",
  53. "width"=> "320px",
  54. "height"=> "320px",
  55. "src"=> $info['image'],//"https://metavision.oss-cn-hongkong.aliyuncs.com/uploads/20220615/f00cb545deb4c4e7296f444239d83e84.jpg"
  56. ],
  57. [
  58. "left"=> "10px",
  59. "top"=> "330px",
  60. "type"=> "img",
  61. "width"=> "100px",
  62. "height"=> "100px",
  63. "src"=> httpurllocal('/assets/img/posterlogo.png')//"https://metavision.oss-cn-hongkong.aliyuncs.com/uploads/20220615/f00cb545deb4c4e7296f444239d83e84.jpg"
  64. ],
  65. [
  66. "left"=> "5px",
  67. "top"=> "440px",
  68. "type"=> "nickname",
  69. "width"=> "166px",
  70. "height"=> "38px",
  71. "size"=> "11px",
  72. "color"=> "#333333",
  73. "content" => (iconv_strlen($info['title'], 'utf-8') <= 12 ? $info['title'] : mb_substr($info['title'], 0, 12) )
  74. ],
  75. [
  76. "left"=> "10px",
  77. "top"=> "460px",
  78. "type"=> "nickname",
  79. "width"=> "166px",
  80. "height"=> "38px",
  81. "size"=> "11px",
  82. "color"=> "#ff0000",
  83. "content" => '¥'.$info['sales_price'],
  84. ],
  85. [
  86. "left"=> "10px",
  87. "top"=> "480px",
  88. "type"=> "nickname",
  89. "width"=> "166px",
  90. "height"=> "38px",
  91. "size"=> "11px",
  92. "color"=> "#333333",
  93. "content" => (iconv_strlen($this->auth->nickname, 'utf-8') <= 8 ? $this->auth->nickname : mb_substr($this->auth->nickname, 0, 8))
  94. ],
  95. [
  96. "left"=> "200px",
  97. "top"=> "330px",
  98. "type"=> "img",
  99. "width"=> "110px",
  100. "height"=> "110px",
  101. "src"=> httpurllocal($inviteimage)//"https://metavision.oss-cn-hongkong.aliyuncs.com/uploads/20220615/f00cb545deb4c4e7296f444239d83e84.jpg"
  102. ],
  103. [
  104. "left"=> "205px",
  105. "top"=> "445px",
  106. "type"=> "nickname",
  107. "width"=> "166px",
  108. "height"=> "38px",
  109. "size"=> "9px",
  110. "color"=> "#666666",
  111. "content" => '长按扫码查看详情',
  112. ],
  113. ];
  114. $data = json_encode($data, 320);
  115. $poster = [
  116. 'id' => 1,
  117. 'title' => '测试',
  118. 'waittext' => '您的专属海报正在拼命生成中,请等待片刻...',
  119. 'bg_image' => '/assets/img/posterproductbg.png',
  120. 'data' => $data,
  121. 'status' => 'normal',
  122. 'weigh' => 0,
  123. 'createtime' => 1653993709,
  124. 'updatetime' => 1653994259,
  125. ];
  126. $image = new \addons\poster\library\Image();
  127. $imgurl = $image->createPosterImage($poster, $this->auth->getUser());
  128. if (!$imgurl) {
  129. $this->error('生成海报出错');
  130. }
  131. $imgurl = $_SERVER["REQUEST_SCHEME"]."://".$_SERVER["HTTP_HOST"] . '/' . $imgurl;
  132. //echo '<html><body><img src="'.$imgurl.'"></body></html>';
  133. $this->success('', $imgurl);
  134. }
  135. }