Postertemplate.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\fenxiao\shop\controller;
  11. use addon\fenxiao\model\PosterTemplate as PosterTemplateModel;
  12. use app\model\system\Site;
  13. use app\model\upload\Upload;
  14. use app\shop\controller\BaseShop;
  15. use extend\Poster as PosterExtend;
  16. /**
  17. * 海报模板 控制器
  18. */
  19. class Postertemplate extends BaseShop
  20. {
  21. /**
  22. * 海报模板列表
  23. * @return mixed
  24. */
  25. public function lists()
  26. {
  27. if (request()->isAjax()) {
  28. $page_index = input('page', 1);
  29. $page_size = input('page_size', PAGE_LIST_ROWS);
  30. $search_text = input('search_text', '');
  31. $condition = [ [ 'site_id', '=', $this->site_id ] ];
  32. $condition[] = [ 'template_type', '=', 'fenxiao' ];
  33. if (!empty($search_text)) {
  34. $condition[] = [ 'poster_name', 'like', '%' . $search_text . '%' ];
  35. }
  36. $poster_template_model = new PosterTemplateModel();
  37. $res = $poster_template_model->getPosterTemplatePageList($condition, $page_index, $page_size);
  38. return $res;
  39. } else {
  40. $this->forthMenu();
  41. return $this->fetch('poster_template/lists');
  42. }
  43. }
  44. /**
  45. * 添加海报模板
  46. * @return mixed
  47. */
  48. public function addPosterTemplate()
  49. {
  50. $poster_template_model = new PosterTemplateModel();
  51. if (request()->isAjax()) {
  52. $default_template = PosterTemplateModel::DEFAULT_TEMPLATE;
  53. $add_data = [];
  54. $template_json = [];
  55. foreach ($default_template as $field => $field_value) {
  56. if ($field == 'template_json') {
  57. foreach ($default_template[ 'template_json' ] as $json_field => $json_field_value) {
  58. $template_json[ $json_field ] = input($json_field, $json_field_value);
  59. }
  60. } else {
  61. $add_data[ $field ] = input($field, $field_value);
  62. }
  63. }
  64. unset($add_data[ 'template_id' ]);
  65. $add_data[ 'site_id' ] = $this->site_id;
  66. $add_data[ 'template_json' ] = json_encode($template_json, true);
  67. return $poster_template_model->addPosterTemplate($add_data);
  68. } else {
  69. //模板信息
  70. $muban_id = input('muban_id', 0);
  71. $muban_info = $poster_template_model->getMubanInfo([ [ 'muban_id', '=', $muban_id ] ])[ 'data' ];
  72. if (!empty($muban_info)) {
  73. $template_info = $muban_info;
  74. $template_info[ 'template_json' ] = json_decode($template_info[ 'template_json' ], true);
  75. $template_info[ 'template_id' ] = 0;
  76. } else {
  77. $template_info = PosterTemplateModel::DEFAULT_TEMPLATE;
  78. }
  79. $template_info[ 'template_json' ] = $poster_template_model->correctTemplateJsonData($template_info[ 'template_json' ])[ 'data' ];
  80. $template_info = array_merge($template_info, $template_info[ 'template_json' ]);
  81. unset($template_info[ 'template_json' ]);
  82. $this->assign('template_info', $template_info);
  83. //站点信息
  84. $site_model = new Site();
  85. $site_info = $site_model->getSiteInfo([ [ "site_id", "=", $this->site_id ] ], 'site_name,logo,logo_square')[ 'data' ];
  86. $this->assign('site_data', $site_info);
  87. return $this->fetch('poster_template/add');
  88. }
  89. }
  90. /**
  91. * 添加海报模板
  92. * @return mixed
  93. */
  94. public function editPosterTemplate()
  95. {
  96. $template_id = input('template_id', '');
  97. $poster_template_model = new PosterTemplateModel();
  98. if (request()->isAjax()) {
  99. $default_template = PosterTemplateModel::DEFAULT_TEMPLATE;
  100. $edit_data = [];
  101. $template_json = [];
  102. foreach ($default_template as $field => $field_value) {
  103. if ($field == 'template_json') {
  104. foreach ($default_template[ 'template_json' ] as $json_field => $json_field_value) {
  105. $template_json[ $json_field ] = input($json_field, $json_field_value);
  106. }
  107. } else {
  108. $edit_data[ $field ] = input($field, $field_value);
  109. }
  110. }
  111. unset($edit_data[ 'template_id' ]);
  112. $edit_data[ 'site_id' ] = $this->site_id;
  113. $edit_data[ 'template_json' ] = json_encode($template_json, true);
  114. return $poster_data = $poster_template_model->editPosterTemplate($edit_data, [
  115. [ 'template_id', '=', $template_id ],
  116. [ 'site_id', '=', $this->site_id ]
  117. ]);
  118. } else {
  119. //模板信息
  120. $template_info = $poster_template_model->getPosterTemplateInfo([
  121. [ 'site_id', '=', $this->site_id ],
  122. [ 'template_id', '=', $template_id ],
  123. ], '*')[ 'data' ];
  124. if (empty($template_info)) $this->error('模板信息有误');
  125. $template_info[ 'template_json' ] = json_decode($template_info[ 'template_json' ], true);
  126. $template_info[ 'template_json' ] = $poster_template_model->correctTemplateJsonData($template_info[ 'template_json' ])[ 'data' ];
  127. $template_info = array_merge($template_info, $template_info[ 'template_json' ]);
  128. unset($template_info[ 'template_json' ]);
  129. $this->assign('template_info', $template_info);
  130. //站点信息
  131. $site_model = new Site();
  132. $site_info = $site_model->getSiteInfo([ [ "site_id", "=", $this->site_id ] ], 'site_name,logo,logo_square');
  133. $this->assign('site_data', $site_info[ 'data' ]);
  134. return $this->fetch('poster_template/add');
  135. }
  136. }
  137. /**
  138. * 获取海报模板预览
  139. * @return mixed
  140. */
  141. public function posterTemplateDetail()
  142. {
  143. $template_id = input('template_id', '');
  144. $condition = [
  145. [ 'template_id', '=', $template_id ],
  146. [ 'site_id', '=', $this->site_id ]
  147. ];
  148. $poster_template_model = new PosterTemplateModel();
  149. $poster_data = $poster_template_model->getPosterTemplateInfo($condition);
  150. if (empty($poster_data[ 'data' ])) {
  151. return $poster_template_model->error(null, '海报数据有误');
  152. }
  153. $poster_data[ 'data' ][ 'template_json' ] = json_decode($poster_data[ 'data' ][ 'template_json' ], true);
  154. $poster_data[ 'data' ][ 'template_json' ] = $poster_template_model->correctTemplateJsonData($poster_data[ 'data' ][ 'template_json' ])[ 'data' ];
  155. $site_model = new Site();
  156. $where = array (
  157. [ "site_id", "=", $this->site_id ]
  158. );
  159. $site_info = $site_model->getSiteInfo($where);
  160. $poster_width = 720;
  161. $poster_height = 1280;
  162. $poster = new PosterExtend($poster_width, $poster_height);
  163. $ground = [
  164. [
  165. 'action' => 'setBackground', // 设背景色
  166. 'data' => [ 255, 255, 255 ]
  167. ],
  168. ];
  169. $fontRate = 0.725; // 20px 等于 14.5磅,换算比率 1px = 0.725磅
  170. $option = [
  171. [
  172. 'action' => 'imageCopy', // 写入二维码
  173. 'data' => [
  174. getUrl() . '/public/static/img/caner_erweima.png',
  175. $poster_data[ 'data' ][ 'qrcode_left' ] * 2,
  176. $poster_data[ 'data' ][ 'qrcode_top' ] * 2,
  177. $poster_data[ 'data' ][ 'qrcode_width' ] * 2,
  178. $poster_data[ 'data' ][ 'qrcode_height' ] * 2,
  179. 'square',
  180. 0,
  181. 1
  182. ]
  183. ],
  184. [
  185. 'action' => 'imageText', // 写入分享语
  186. 'data' => [
  187. $poster_data[ 'data' ][ 'template_json' ][ 'share_content' ],
  188. $poster_data[ 'data' ][ 'template_json' ][ 'share_content_font_size' ] * $fontRate * 2,
  189. is_array($poster_data[ 'data' ][ 'template_json' ][ 'share_content_color' ]) ? $poster_data[ 'data' ][ 'template_json' ][ 'share_content_color' ] : hex2rgb($poster_data[ 'data' ][ 'template_json' ][ 'share_content_color' ]),
  190. $poster_data[ 'data' ][ 'template_json' ][ 'share_content_left' ] * 2,
  191. ( $poster_data[ 'data' ][ 'template_json' ][ 'share_content_top' ] ) * 2,
  192. $poster_data[ 'data' ][ 'template_json' ][ 'share_content_width' ] * 2,
  193. 1
  194. ]
  195. ]
  196. ];
  197. $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' ]);
  198. $member_option = [
  199. [
  200. 'action' => 'imageCopy', // 写入用户头像
  201. 'data' => [
  202. getUrl() . '/public/static/img/default_img/head.png',
  203. $poster_data[ 'data' ][ 'template_json' ][ 'headimg_left' ] * 2,
  204. $poster_data[ 'data' ][ 'template_json' ][ 'headimg_top' ] * 2,
  205. $poster_data[ 'data' ][ 'template_json' ][ 'headimg_width' ] * 2,
  206. $poster_data[ 'data' ][ 'template_json' ][ 'headimg_height' ] * 2,
  207. !empty($poster_data[ 'data' ][ 'template_json' ][ 'headimg_shape' ]) ? $poster_data[ 'data' ][ 'template_json' ][ 'headimg_shape' ] : 'square',
  208. 0,
  209. $poster_data[ 'data' ][ 'template_json' ][ 'headimg_is_show' ]
  210. ]
  211. ],
  212. [
  213. 'action' => 'imageText', // 写入分享人昵称
  214. 'data' => [
  215. '用户昵称',
  216. $poster_data[ 'data' ][ 'template_json' ][ 'nickname_font_size' ] * $fontRate * 2,
  217. $nickname_color,
  218. $poster_data[ 'data' ][ 'template_json' ][ 'nickname_left' ] * 2,
  219. ( $poster_data[ 'data' ][ 'template_json' ][ 'nickname_top' ] ) * 2,
  220. $poster_data[ 'data' ][ 'template_json' ][ 'nickname_width' ] * 2,
  221. $poster_data[ 'data' ][ 'template_json' ][ 'nickname_height' ] * 2,
  222. 0,
  223. $poster_data[ 'data' ][ 'template_json' ][ 'nickname_is_show' ]
  224. ]
  225. ],
  226. ];
  227. list($width, $height, $type, $attr) = getimagesize(img($poster_data[ 'data' ][ 'background' ]));
  228. $height = 720 * $height / $width;
  229. $back_ground = [
  230. [
  231. 'action' => 'imageCopy', // 写入背景图
  232. 'data' => [
  233. img($poster_data[ 'data' ][ 'background' ]),
  234. 0,
  235. 0,
  236. 720,
  237. $height,
  238. 'square',
  239. 0,
  240. 1
  241. ]
  242. ],
  243. ];
  244. $option = array_merge($ground, $back_ground, $option, $member_option);
  245. $option_res = $poster->create($option);
  246. if (is_array($option_res)) return $option_res;
  247. $pic_name = rand(10000, 99999);
  248. $res = $option_res->jpeg('upload/poster/goods', 'fenxiao' . $pic_name);
  249. if ($res[ 'code' ] < 0) {
  250. return $res;
  251. }
  252. $upload = new Upload($this->site_id);
  253. $cloud_res = $upload->fileCloud($res[ 'data' ][ 'path' ]);
  254. return $cloud_res;
  255. }
  256. /**
  257. * 删除海报模板
  258. * @return mixed
  259. */
  260. public function delPosterTemplate()
  261. {
  262. if (request()->isAjax()) {
  263. $template_ids = input('template_ids', '');
  264. $condition = [
  265. [ 'template_id', 'in', $template_ids ],
  266. [ 'site_id', '=', $this->site_id ],
  267. ];
  268. $poster_template_model = new PosterTemplateModel();
  269. $res = $poster_template_model->deletePosterTemplate($condition);
  270. return $res;
  271. }
  272. }
  273. /**
  274. * 编辑模板状态啊
  275. * @return array
  276. */
  277. public function editstatus()
  278. {
  279. if (request()->isAjax()) {
  280. $template_id = input('template_id', 0);
  281. $template_status = input('template_status', 0);
  282. $condition = [
  283. [ 'template_id', 'in', $template_id ],
  284. [ 'site_id', '=', $this->site_id ]
  285. ];
  286. $data = [ 'template_status' => $template_status ];
  287. $poster_template_model = new PosterTemplateModel();
  288. $res = $poster_template_model->editPosterTemplate($data, $condition);
  289. return $res;
  290. }
  291. }
  292. }