Poster.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. <?php
  2. // +---------------------------------------------------------------------+
  3. // | NiuCloud | [ WE CAN DO IT JUST NiuCloud ]  |
  4. // +---------------------------------------------------------------------+
  5. // | Copy right 2019-2029 www.niucloud.com  |
  6. // +---------------------------------------------------------------------+
  7. // | Author | NiuCloud <niucloud@outlook.com>  |
  8. // +---------------------------------------------------------------------+
  9. // | Repository | https://github.com/niucloud/framework.git  |
  10. // +---------------------------------------------------------------------+
  11. namespace addon\fenxiao\shop\controller;
  12. use addon\fenxiao\model\Poster as PosterModel;
  13. use app\model\system\Site;
  14. use app\model\upload\Upload;
  15. use app\shop\controller\BaseShop;
  16. use extend\Poster as PosterExtend;
  17. use think\facade\Cache;
  18. /**
  19. * 海报模板 控制器
  20. */
  21. class Poster extends BaseShop
  22. {
  23. public function __construct()
  24. {
  25. //执行父类构造函数
  26. parent::__construct();
  27. }
  28. /**
  29. * 海报模板列表
  30. * @return mixed
  31. */
  32. public function list()
  33. {
  34. if (request()->isAjax()) {
  35. $page_index = input('page', 1);
  36. $page_size = input('page_size', PAGE_LIST_ROWS);
  37. $search_text = input('search_text', '');
  38. $condition = [['site_id', '=', $this->site_id]];
  39. if(!empty($search_text)){
  40. $condition[] = ['poster_name','like', '%' . $search_text . '%'];
  41. }
  42. $condition[] = ['template_type', '=', 'fenxiao'];
  43. $poster_template_model = new PosterModel();
  44. $res = $poster_template_model->getPosterTemplatePageList($condition, $page_index, $page_size);
  45. return $res;
  46. } else {
  47. return $this->fetch('poster/lists');
  48. }
  49. }
  50. /**
  51. * 添加海报模板
  52. * @return mixed
  53. */
  54. public function add()
  55. {
  56. $template_id = input('template_id', '');
  57. $poster_template_model = new PosterModel();
  58. if (request()->isAjax()) {
  59. $input = input('param.poster_detail');
  60. if(empty(input('template_id'))){
  61. //数据
  62. $data = [
  63. 'poster_name' => input('poster_detail.poster_name', ''),
  64. 'background' => input('poster_detail.background', ''),
  65. 'qrcode_width' => input('poster_detail.qrcode_width', ''),
  66. 'qrcode_height' => input('poster_detail.qrcode_height', ''),
  67. 'qrcode_top' => input('poster_detail.qrcode_top', ''),
  68. 'qrcode_left' => input('poster_detail.qrcode_left', ''),
  69. 'template_type' => input('poster_detail.template_type',''),
  70. 'create_time' => time(),
  71. 'site_id' => $this->site_id
  72. ];
  73. $goods_template = [
  74. 'headimg_is_show' => 1,
  75. 'headimg_shape' => input('poster_detail.headimg_shape', ''),
  76. 'nickname_is_show' => 1,
  77. 'nickname_font_size' => input('poster_detail.nickname_font_size', ''),
  78. 'nickname_color' => input('poster_detail.nickname_color', ''),
  79. 'headimg_width' => input('poster_detail.headimg_width', ''),
  80. 'headimg_height' => input('poster_detail.headimg_height', ''),
  81. 'headimg_top' => input('poster_detail.headimg_top', ''),
  82. 'headimg_left' => input('poster_detail.headimg_left', ''),
  83. 'nickname_width' => input('poster_detail.nickname_width', ''),
  84. 'nickname_height' => input('poster_detail.nickname_height', ''),
  85. 'nickname_top' => input('poster_detail.nickname_top', ''),
  86. 'nickname_left' => input('poster_detail.nickname_left', ''),
  87. 'poster_text' => input('poster_detail.poster_text', ''),
  88. 'text_content_size' => input('poster_detail.text_content_size', ''),
  89. 'text_content_color' => input('poster_detail.text_content_color', ''),
  90. 'text_content_top' => input('poster_detail.text_content_top', ''),
  91. 'text_content_left' => input('poster_detail.text_content_left', ''),
  92. ];
  93. $data['template_json'] = json_encode($goods_template,true);
  94. $poster_data = $poster_template_model->addPosterTemplate($data);
  95. }else{
  96. //数据
  97. $data = [
  98. 'poster_name' => input('poster_detail.poster_name', ''),
  99. 'background' => input('poster_detail.background', ''),
  100. 'qrcode_width' => input('poster_detail.qrcode_width', ''),
  101. 'qrcode_height' => input('poster_detail.qrcode_height', ''),
  102. 'qrcode_top' => input('poster_detail.qrcode_top', ''),
  103. 'qrcode_left' => input('poster_detail.qrcode_left', ''),
  104. 'template_type' => input('poster_detail.template_type',''),
  105. 'site_id' => $this->site_id
  106. ];
  107. $goods_template = [
  108. 'headimg_is_show' => 1,
  109. 'headimg_shape' => input('poster_detail.headimg_shape', ''),
  110. 'nickname_is_show' => 1,
  111. 'nickname_font_size' => input('poster_detail.nickname_font_size', ''),
  112. 'nickname_color' => input('poster_detail.nickname_color', ''),
  113. 'headimg_width' => input('poster_detail.headimg_width', ''),
  114. 'headimg_height' => input('poster_detail.headimg_height', ''),
  115. 'headimg_top' => input('poster_detail.headimg_top', ''),
  116. 'headimg_left' => input('poster_detail.headimg_left', ''),
  117. 'nickname_width' => input('poster_detail.nickname_width', ''),
  118. 'nickname_height' => input('poster_detail.nickname_height', ''),
  119. 'nickname_top' => input('poster_detail.nickname_top', ''),
  120. 'nickname_left' => input('poster_detail.nickname_left', ''),
  121. 'poster_text' => input('poster_detail.poster_text', ''),
  122. 'text_content_size' => input('poster_detail.text_content_size', ''),
  123. 'text_content_color' => input('poster_detail.text_content_color', ''),
  124. 'text_content_top' => input('poster_detail.text_content_top', ''),
  125. 'text_content_left' => input('poster_detail.text_content_left', ''),
  126. ];
  127. $data['template_json'] = json_encode($goods_template,true);
  128. $poster_data = $poster_template_model->editdPosterTemplate($data,[['template_id','=',input('template_id')],['site_id','=',$this->site_id]]);
  129. }
  130. return $poster_data;
  131. } else {
  132. if (!empty($template_id)){
  133. $condition = [
  134. ['template_id', '=', 1],
  135. ['site_id', '=', $this->site_id]
  136. ];
  137. $res = $poster_template_model->getPosterTemplateInfo($condition);
  138. $res['data']['json_array'] = json_decode($res['data']['template_json'],true);
  139. $res['data']['json_array']['qrcode_type'] = $res['data']['qrcode_type'] ?? '';
  140. $res['data']['json_array']['qrcode_width'] = $res['data']['qrcode_width'] ?? 80;
  141. $res['data']['json_array']['qrcode_height'] = $res['data']['qrcode_height'] ?? 80;
  142. $res['data']['json_array']['qrcode_top'] = $res['data']['qrcode_top'] ?? 540;
  143. $res['data']['json_array']['qrcode_left'] = $res['data']['qrcode_left'] ?? 260;
  144. $res['data']['json_array']['template_type'] = $res['data']['template_type'] ?? 'goods';
  145. $this->assign('template_data', $res['data']);
  146. }
  147. $site_model = new Site();
  148. $where = array (
  149. [ "site_id", "=", $this->site_id ]
  150. );
  151. $site_info = $site_model->getSiteInfo($where,'site_name,logo');
  152. $this->assign('site_data',$site_info['data']);
  153. return $this->fetch('poster/add');
  154. }
  155. }
  156. /**
  157. * 添加海报模板
  158. * @return mixed
  159. */
  160. public function edit()
  161. {
  162. $template_id = input('template_id', '');
  163. $poster_template_model = new PosterModel();
  164. if (request()->isAjax()) {
  165. $input = input('param.poster_detail');
  166. //数据
  167. $data = [
  168. 'poster_name' => input('poster_detail.poster_name', ''),
  169. 'background' => input('poster_detail.background', ''),
  170. 'qrcode_width' => input('poster_detail.qrcode_width', ''),
  171. 'qrcode_height' => input('poster_detail.qrcode_height', ''),
  172. 'qrcode_top' => input('poster_detail.qrcode_top', ''),
  173. 'qrcode_left' => input('poster_detail.qrcode_left', ''),
  174. 'template_type' => input('poster_detail.template_type',''),
  175. 'site_id' => $this->site_id
  176. ];
  177. $goods_template = [
  178. 'headimg_is_show' => 1,
  179. 'headimg_shape' => input('poster_detail.headimg_shape', ''),
  180. 'nickname_is_show' => 1,
  181. 'nickname_font_size' => input('poster_detail.nickname_font_size', ''),
  182. 'nickname_color' => input('poster_detail.nickname_color', ''),
  183. 'headimg_width' => input('poster_detail.headimg_width', ''),
  184. 'headimg_height' => input('poster_detail.headimg_height', ''),
  185. 'headimg_top' => input('poster_detail.headimg_top', ''),
  186. 'headimg_left' => input('poster_detail.headimg_left', ''),
  187. 'nickname_width' => input('poster_detail.nickname_width', ''),
  188. 'nickname_height' => input('poster_detail.nickname_height', ''),
  189. 'nickname_top' => input('poster_detail.nickname_top', ''),
  190. 'nickname_left' => input('poster_detail.nickname_left', ''),
  191. 'poster_text' => input('poster_detail.poster_text', ''),
  192. 'text_content_size' => input('poster_detail.text_content_size', ''),
  193. 'text_content_color' => input('poster_detail.text_content_color', ''),
  194. 'text_content_top' => input('poster_detail.text_content_top', ''),
  195. 'text_content_left' => input('poster_detail.text_content_left', ''),
  196. ];
  197. $data['template_json'] = json_encode($goods_template,true);
  198. $poster_data = $poster_template_model->editdPosterTemplate($data,[['template_id','=',input('template_id')],['site_id','=',$this->site_id]]);
  199. return $poster_data;
  200. } else {
  201. if (!empty($template_id)){
  202. $condition = [
  203. ['template_id', '=', $template_id],
  204. ['site_id', '=', $this->site_id]
  205. ];
  206. $res = $poster_template_model->getPosterTemplateInfo($condition);
  207. $res['data']['json_array'] = json_decode($res['data']['template_json'],true);
  208. $res['data']['json_array']['qrcode_type'] = $res['data']['qrcode_type'] ?? '';
  209. $res['data']['json_array']['qrcode_width'] = $res['data']['qrcode_width'] ?? 80;
  210. $res['data']['json_array']['qrcode_height'] = $res['data']['qrcode_height'] ?? 80;
  211. $res['data']['json_array']['qrcode_top'] = $res['data']['qrcode_top'] ?? 540;
  212. $res['data']['json_array']['qrcode_left'] = $res['data']['qrcode_left'] ?? 260;
  213. $res['data']['json_array']['template_type'] = $res['data']['template_type'] ?? 'goods';
  214. $this->assign('template_data', $res['data']);
  215. }
  216. $site_model = new Site();
  217. $where = array (
  218. [ "site_id", "=", $this->site_id ]
  219. );
  220. $site_info = $site_model->getSiteInfo($where,'site_name,logo');
  221. $this->assign('site_data',$site_info['data']);
  222. return $this->fetch('poster/edit');
  223. }
  224. }
  225. /**
  226. * 获取海报模板预览
  227. * @return mixed
  228. */
  229. public function posterTemplateDetail()
  230. {
  231. $template_id = input('template_id', '');
  232. $condition = [
  233. ['template_id', '=', $template_id],
  234. ['site_id', '=', $this->site_id]
  235. ];
  236. $poster_template_model = new PosterModel();
  237. $poster_data = $poster_template_model->getPosterTemplateInfo($condition);
  238. if(!empty($poster_data['data'])){
  239. $poster_data['data']['template_json'] = json_decode($poster_data['data']['template_json'],true);
  240. $site_model = new Site();
  241. $where = array (
  242. [ "site_id", "=", $this->site_id ]
  243. );
  244. $site_info = $site_model->getSiteInfo($where);
  245. $poster_width = 720;
  246. $poster_height = 1280;
  247. $poster = new PosterExtend($poster_width, $poster_height);
  248. $ground = [
  249. [
  250. 'action' => 'setBackground', // 设背景色
  251. 'data' => [255, 255, 255]
  252. ],
  253. ];
  254. $fontRate = 0.725; // 20px 等于 14.5磅,换算比率 1px = 0.725磅
  255. $option = [
  256. [
  257. 'action' => 'imageCopy', // 写入二维码
  258. 'data' => [
  259. getUrl().'/public/static/img/caner_erweima.png',
  260. $poster_data['data']['qrcode_left']*2,
  261. $poster_data['data']['qrcode_top']*2,
  262. $poster_data['data']['qrcode_width']*2,
  263. $poster_data['data']['qrcode_height']*2,
  264. 'square',
  265. 0,
  266. 1
  267. ]
  268. ],
  269. [
  270. 'action' => 'imageText', // 写入分享语
  271. 'data' => [
  272. $poster_data['data']['template_json']['poster_text'],
  273. 18,
  274. [141, 141, 141],
  275. $poster_data['data']['template_json']['text_content_left']*2,
  276. ($poster_data['data']['template_json']['text_content_top'])*2,
  277. 440,
  278. 1
  279. ]
  280. ]
  281. ];
  282. $nickname_color = is_array($poster_data['data']['template_json']['nickname_color']) ? $poster_data['data']['template_json']['nickname_color'] : hex2rgb($poster_data['data']['template_json']['nickname_color']);
  283. $member_option = [
  284. [
  285. 'action' => 'imageCopy', // 写入用户头像
  286. 'data' => [
  287. getUrl().'/app/shop/view/public/img/default_headimg.png',
  288. $poster_data['data']['template_json']['headimg_left']*2,
  289. $poster_data['data']['template_json']['headimg_top']*2,
  290. $poster_data['data']['template_json']['headimg_width']*2,
  291. $poster_data['data']['template_json']['headimg_height']*2,
  292. !empty($poster_data['data']['template_json']['headimg_shape'])?$poster_data['data']['template_json']['headimg_shape']:'square',
  293. 0,
  294. $poster_data['data']['template_json']['headimg_is_show']
  295. ]
  296. ],
  297. [
  298. 'action' => 'imageText', // 写入分享人昵称
  299. 'data' => [
  300. '用户昵称',
  301. $poster_data['data']['template_json']['nickname_font_size'] * $fontRate*2,
  302. $nickname_color,
  303. $poster_data['data']['template_json']['nickname_left']*2,
  304. ($poster_data['data']['template_json']['nickname_top'])*2,
  305. $poster_data['data']['template_json']['nickname_width']*2,
  306. $poster_data['data']['template_json']['nickname_height']*2,
  307. 0,
  308. $poster_data['data']['template_json']['nickname_is_show']
  309. ]
  310. ],
  311. ];
  312. list($width,$height,$type,$attr)=getimagesize(img($poster_data['data']['background']));
  313. $height = 720*$height/$width;
  314. $back_ground = [
  315. [
  316. 'action' => 'imageCopy', // 写入背景图
  317. 'data' => [
  318. img($poster_data['data']['background']),
  319. 0,
  320. 0,
  321. 720,
  322. $height,
  323. 'square',
  324. 0,
  325. 1
  326. ]
  327. ],
  328. ];
  329. $option = array_merge($ground,$back_ground,$option, $member_option);
  330. $option_res = $poster->create($option);
  331. if (is_array($option_res)) return $option_res;
  332. $pic_name = rand(10000, 99999);
  333. $res = $option_res->jpeg('upload/poster/goods', 'fenxiao'.$pic_name);
  334. if ($res['code'] == 0) {
  335. $upload = new Upload($this->site_id);
  336. $cloud_res = $upload->fileCloud($res['data']['path']);
  337. if ($cloud_res['code'] >= 0) {
  338. return $cloud_res[ 'data' ];
  339. } else {
  340. return $this->error();
  341. }
  342. }
  343. }else{
  344. return '参数不能为空';
  345. }
  346. }
  347. public function getMubanInfo(){
  348. $muban_id = input('muban_id',0);
  349. $muban_model = new PosterModel();
  350. $muban_info = $muban_model->getMubanInfo([['muban_id','=',$muban_id]]);
  351. $site_model = new Site();
  352. $where = array (
  353. [ "site_id", "=", $this->site_id ]
  354. );
  355. $site_info = $site_model->getSiteInfo($where,'site_name,logo');
  356. $muban_info['data']['site_data'] = $site_info['data'];
  357. return $muban_info;
  358. }
  359. /**
  360. * 删除海报模板
  361. * @return mixed
  362. */
  363. public function delPosterTemplate()
  364. {
  365. if (request()->isAjax()) {
  366. $template_ids = input('template_ids', '');
  367. $condition = [
  368. ['template_id', 'in', $template_ids],
  369. ['site_id', '=', $this->site_id]
  370. ];
  371. $poster_template_model = new PosterModel();
  372. $res = $poster_template_model->deletePosterTemplate($condition);
  373. return $res;
  374. }
  375. }
  376. /**
  377. * 编辑模板状态啊
  378. * @return array
  379. */
  380. public function editstatus()
  381. {
  382. if (request()->isAjax()) {
  383. $template_id = input('template_id', 0);
  384. $template_status = input('template_status', 0);
  385. $condition = [
  386. ['template_id', 'in', $template_id],
  387. ['site_id', '=', $this->site_id]
  388. ];
  389. $data = ['template_status' => $template_status];
  390. $poster_template_model = new PosterModel();
  391. $res = $poster_template_model->editdPosterTemplate($data, $condition);
  392. return $res;
  393. }
  394. }
  395. /**
  396. * 获取海报模板
  397. * @return mixed
  398. */
  399. public function getMubanList(){
  400. return 1;
  401. }
  402. }