PosterJob.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\jobs;
  12. use app\services\other\PosterServices;
  13. use app\services\other\QrcodeServices;
  14. use app\services\system\attachment\SystemAttachmentServices;
  15. use crmeb\basic\BaseJobs;
  16. use crmeb\services\app\MiniProgramService;
  17. use app\services\other\UploadService;
  18. use crmeb\traits\QueueTrait;
  19. /**
  20. * 海报队列
  21. * Class PosterJob
  22. * @package crmeb\jobs
  23. */
  24. class PosterJob extends BaseJobs
  25. {
  26. use QueueTrait;
  27. /**
  28. * 海报生成
  29. * @param $user
  30. * @param $isSsl
  31. * @return bool
  32. */
  33. public function spreadPoster($user, $isSsl)
  34. {
  35. /** @var SystemAttachmentServices $attachment */
  36. $attachment = app()->make(SystemAttachmentServices::class);
  37. /** @var QrcodeServices $qrcodeService */
  38. $qrcodeService = app()->make(QrcodeServices::class);
  39. $rootPath = app()->getRootPath();
  40. try {
  41. $resRoutine = true;//小程序
  42. $resWap = true;//公众号
  43. $siteUrl = sys_config('site_url');
  44. $routineSpreadBanner = sys_data('routine_spread_banner');
  45. if (!count($routineSpreadBanner)) return false;
  46. //小程序
  47. $name_routine = $user['uid'] . '_' . $user['is_promoter'] . '_user_routine.jpg';
  48. $name_wap = $user['uid'] . '_' . $user['is_promoter'] . '_user_wap.jpg';
  49. $imageInfo_routine = $attachment->getInfo(['name' => $name_routine]);
  50. $imageInfo_wap = $attachment->getInfo(['name' => $name_wap]);
  51. if (isset($imageInfo_routine['att_dir']) && strstr($imageInfo_routine['att_dir'], 'http') !== false && curl_file_exist($imageInfo_routine['att_dir']) === false) {
  52. $imageInfo_routine = null;
  53. $attachment->delete(['name' => $name_routine]);
  54. }
  55. if (isset($imageInfo_wap['att_dir']) && strstr($imageInfo_wap['att_dir'], 'http') !== false && curl_file_exist($imageInfo_wap['att_dir']) === false) {
  56. $imageInfo_wap = null;
  57. $attachment->delete(['name' => $name_wap]);
  58. }
  59. if (!$imageInfo_routine) {
  60. $resForever = $qrcodeService->getForeverQrcode('spread', $user['uid']);
  61. $resCode = MiniProgramService::appCodeUnlimitService($resForever->id, '', 280);
  62. if ($resCode) {
  63. $res = ['res' => $resCode, 'id' => $resForever->id];
  64. } else {
  65. $res = false;
  66. }
  67. if (!$res) return false;
  68. $uploadType = (int)sys_config('upload_type', 1);
  69. $upload = UploadService::init();
  70. $uploadRes = $upload->to('routine/spread/code')->validate()->setAuthThumb(false)->stream($res['res'], $name_routine);
  71. if ($uploadRes === false) {
  72. return false;
  73. }
  74. $imageInfo_routine = $upload->getUploadInfo();
  75. $imageInfo_routine['image_type'] = $uploadType;
  76. $attachment->attachmentAdd($imageInfo_routine['name'], $imageInfo_routine['size'], $imageInfo_routine['type'], $imageInfo_routine['dir'], $imageInfo_routine['thumb_path'], 1, $imageInfo_routine['image_type'], $imageInfo_routine['time'], 2);
  77. $qrcodeService->setQrcodeFind($res['id'], ['status' => 1, 'url_time' => time(), 'qrcode_url' => $imageInfo_routine['dir']]);
  78. $urlCode_routine = $imageInfo_routine['dir'];
  79. } else $urlCode_routine = $imageInfo_routine['att_dir'];
  80. if ($imageInfo_routine['image_type'] == 1) $urlCode_routine = $siteUrl . $urlCode_routine;
  81. if (!$imageInfo_wap) {
  82. $codeUrl = set_http_type($siteUrl . '?spread=' . $user['uid'], $isSsl ? 0 : 1);//二维码链接
  83. $imageInfo_wap = PosterServices::getQRCodePath($codeUrl, $name_wap);
  84. if (is_string($imageInfo_wap)) return false;
  85. $attachment->attachmentAdd($imageInfo_wap['name'], $imageInfo_wap['size'], $imageInfo_wap['type'], $imageInfo_wap['dir'], $imageInfo_wap['thumb_path'], 1, $imageInfo_wap['image_type'], $imageInfo_wap['time'], 2);
  86. $urlCode_wap = $imageInfo_wap['dir'];
  87. } else $urlCode_wap = $imageInfo_wap['att_dir'];
  88. if ($imageInfo_wap['image_type'] == 1) $urlCode_wap = $siteUrl . $urlCode_wap;
  89. $siteUrl = set_http_type($siteUrl, $isSsl ? 0 : 1);
  90. $filelink = [
  91. 'Bold' => 'public\statics\font\Alibaba-PuHuiTi-Regular.otf',
  92. 'Normal' => 'public\statics\font\Alibaba-PuHuiTi-Regular.otf',
  93. ];
  94. if (!file_exists($filelink['Bold'])) return false;
  95. if (!file_exists($filelink['Normal'])) return false;
  96. foreach ($routineSpreadBanner as $key => &$item) {
  97. $posterInfo_routine = '海报生成失败:(';
  98. $config = array(
  99. 'image' => array(
  100. array(
  101. 'url' => $urlCode_routine, //二维码资源
  102. 'stream' => 0,
  103. 'left' => 114,
  104. 'top' => 790,
  105. 'right' => 0,
  106. 'bottom' => 0,
  107. 'width' => 120,
  108. 'height' => 120,
  109. 'opacity' => 100
  110. )
  111. ),
  112. 'text' => array(
  113. array(
  114. 'text' => $user['nickname'],
  115. 'left' => 250,
  116. 'top' => 840,
  117. 'fontPath' => $rootPath . $filelink['Bold'], //字体文件
  118. 'fontSize' => 16, //字号
  119. 'fontColor' => '40,40,40', //字体颜色
  120. 'angle' => 0,
  121. ),
  122. array(
  123. 'text' => '邀请您加入' . sys_config('site_name'),
  124. 'left' => 250,
  125. 'top' => 880,
  126. 'fontPath' => $rootPath . $filelink['Normal'], //字体文件
  127. 'fontSize' => 16, //字号
  128. 'fontColor' => '40,40,40', //字体颜色
  129. 'angle' => 0,
  130. )
  131. ),
  132. 'background' => $item['pic']
  133. );
  134. $resRoutine = $resRoutine && $posterInfo_routine = PosterServices::setSharePoster($config, 'routine/spread/poster', $user['uid'] . '_' . $user['is_promoter'] . '_user_routine_poster_' . $key . '.jpg');
  135. if (!is_array($posterInfo_routine)) return false;
  136. $attachment->attachmentAdd($posterInfo_routine['name'], $posterInfo_routine['size'], $posterInfo_routine['type'], $posterInfo_routine['dir'], $posterInfo_routine['thumb_path'], 1, $posterInfo_routine['image_type'], $posterInfo_routine['time'], 2);
  137. if ($resRoutine) {
  138. if ($posterInfo_routine['image_type'] == 1)
  139. $item['poster'] = $siteUrl . $posterInfo_routine['dir'];
  140. else
  141. $item['poster'] = set_http_type($posterInfo_routine['dir'], $isSsl ? 0 : 1);
  142. $item['poster'] = str_replace('\\', '/', $item['poster']);
  143. }
  144. }
  145. foreach ($routineSpreadBanner as $key => &$item) {
  146. $posterInfo_wap = '海报生成失败:(';
  147. $config = array(
  148. 'image' => array(
  149. array(
  150. 'url' => $urlCode_wap, //二维码资源
  151. 'stream' => 0,
  152. 'left' => 114,
  153. 'top' => 790,
  154. 'right' => 0,
  155. 'bottom' => 0,
  156. 'width' => 120,
  157. 'height' => 120,
  158. 'opacity' => 100
  159. )
  160. ),
  161. 'text' => array(
  162. array(
  163. 'text' => $user['nickname'],
  164. 'left' => 250,
  165. 'top' => 840,
  166. 'fontPath' => $rootPath . $filelink['Bold'], //字体文件
  167. 'fontSize' => 16, //字号
  168. 'fontColor' => '40,40,40', //字体颜色
  169. 'angle' => 0,
  170. ),
  171. array(
  172. 'text' => '邀请您加入' . sys_config('site_name'),
  173. 'left' => 250,
  174. 'top' => 880,
  175. 'fontPath' => $rootPath . $filelink['Normal'], //字体文件
  176. 'fontSize' => 16, //字号
  177. 'fontColor' => '40,40,40', //字体颜色
  178. 'angle' => 0,
  179. )
  180. ),
  181. 'background' => $item['pic']
  182. );
  183. $resWap = $resWap && $posterInfo_wap = PosterServices::setSharePoster($config, 'wap/spread/poster', $user['uid'] . '_' . $user['is_promoter'] . '_user_wap_poster_' . $key . '.jpg');
  184. if (!is_array($posterInfo_wap)) return false;
  185. $attachment->attachmentAdd($posterInfo_wap['name'], $posterInfo_wap['size'], $posterInfo_wap['type'], $posterInfo_wap['dir'], $posterInfo_wap['thumb_path'], 1, $posterInfo_wap['image_type'], $posterInfo_wap['time'], 2);
  186. if ($resWap) {
  187. if ($posterInfo_wap['image_type'] == 1)
  188. $item['wap_poster'] = $siteUrl . $posterInfo_wap['thumb_path'];
  189. else
  190. $item['wap_poster'] = set_http_type($posterInfo_wap['thumb_path'], 1);
  191. }
  192. }
  193. if ($resRoutine && $resWap) return true;
  194. else return false;
  195. } catch (\Exception $e) {
  196. return false;
  197. }
  198. }
  199. }