AlbumPosterController.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: 思维定制
  5. * Date: 2019/3/4
  6. * Time: 15:12
  7. */
  8. namespace App\Http\Controllers\Api\V1;
  9. use App\Models\AlbumAgentModel;
  10. use App\Models\AlbumManufacturerModel;
  11. use App\Models\AlbumPosterModel;
  12. use App\Models\AlbumUserModel;
  13. use App\Models\AlbumWatchRecord;
  14. use App\Services\Base\ErrorCode;
  15. use EasyWeChat\Factory;
  16. use Grafika\Color;
  17. use Grafika\Grafika;
  18. use Validator, Response,Auth;
  19. use Illuminate\Http\Request;
  20. class AlbumPosterController extends Controller
  21. {
  22. /**
  23. * @api {post} /api/album_post/info 获取海报数据(info)
  24. * @apiDescription 获取海报数据(info)
  25. * @apiGroup Album_Post
  26. * @apiPermission 需要登录
  27. * @apiVersion 0.1.0
  28. * @apiParam {int} [store_id] 商户id
  29. * @apiSuccessExample {json} Success-Response:
  30. * HTTP/1.1 200 OK
  31. * {
  32. * "status": true,
  33. * "status_code": 0,
  34. * "message": "",
  35. * "data": [
  36. * 'posters':'asdawd', //海报
  37. * 'words':'asdawd', //话术
  38. * 'introduce':'222', //介绍
  39. * 'share':'xxx' //分享图片
  40. * 'phone':'xxx' //电话
  41. * 'username':'xxx' //姓名
  42. * 'title':'xxx' //分享标题
  43. * 'qrcode':'xxx' //二维码
  44. * ]
  45. * }
  46. * @apiErrorExample {json} Error-Response:
  47. * HTTP/1.1 400 Bad Request
  48. * {
  49. * "state": false,
  50. * "code": 1000,
  51. * "message": "传入参数不正确",
  52. * "data": null or []
  53. * }
  54. * 可能出现的错误代码:
  55. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  56. */
  57. public function posterInfo(Request $request)
  58. {
  59. $userAuth = Auth('api')->user();
  60. $validator = Validator::make($request->all(), [
  61. 'store_id' => 'required',
  62. ], [
  63. 'store_id.required' => '店铺信息未知',
  64. ]);
  65. if ($validator->fails()) {
  66. return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS, '');
  67. }
  68. $store_id = $request->input('store_id');
  69. $info = AlbumPosterModel::where('store_id', $store_id)->get();
  70. $WeChatApp = AlbumManufacturerModel::where('store_id', $store_id)->first();
  71. if ($userAuth->is_dealer != 1) {
  72. if ($userAuth->up_agent_id == 0) {
  73. $name = $WeChatApp->name;
  74. } else {
  75. $agent_check = AlbumAgentModel::where([
  76. ['store_id',$store_id],
  77. ['id',$userAuth->up_agent_id]
  78. ])->first();
  79. $name = $agent_check->realname;
  80. }
  81. } else {
  82. $agent_check = AlbumAgentModel::where([
  83. ['store_id',$store_id],
  84. ['user_id',$userAuth->id]
  85. ])->first();
  86. $name = $agent_check->realname;
  87. }
  88. $datas['info'] = $info;
  89. $datas['username'] = $name;
  90. $datas['avatar'] = $userAuth->avatar;
  91. $datas['share'] = $WeChatApp->share_image;
  92. $datas['title'] = $WeChatApp->share_title;
  93. $datas['personalQrcode'] = $this->createPosterPersonal($WeChatApp, $store_id);
  94. $datas['qrcode'] = $this->createPoster($userAuth, $store_id);
  95. return $this->api(compact('datas'));
  96. }
  97. /**
  98. * @api {post} /api/album_post/download 下载海报(download)
  99. * @apiDescription 生成海报(download)
  100. * @apiGroup Album_Post
  101. * @apiPermission 需要登录
  102. * @apiVersion 0.1.0
  103. * @apiParam {int} [store_id] 商户id
  104. * @apiParam {int} [poster_id] 海报id
  105. * @apiSuccessExample {json} Success-Response:
  106. * HTTP/1.1 200 OK
  107. * {
  108. * "status": true,
  109. * "status_code": 0,
  110. * "message": "",
  111. * "data": []
  112. * }
  113. * @apiErrorExample {json} Error-Response:
  114. * HTTP/1.1 400 Bad Request
  115. * {
  116. * "state": false,
  117. * "code": 1000,
  118. * "message": "传入参数不正确",
  119. * "data": null or []
  120. * }
  121. * 可能出现的错误代码:
  122. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  123. */
  124. public function posterDownload(Request $request)
  125. {
  126. $userAuth = Auth('api')->user();
  127. $validator = Validator::make($request->all(), [
  128. 'store_id' => 'required',
  129. 'poster_id' => 'required',
  130. ], [
  131. 'store_id.required' => '店铺信息未知',
  132. 'poster_id.required' => '缺少图片链接'
  133. ]);
  134. if ($validator->fails()) {
  135. return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS, '');
  136. }
  137. $data = $request->input();
  138. if ($userAuth->up_agent_id != 0) {
  139. $add_record['agent_id'] = $userAuth->up_agent_id;
  140. $add_record['open_id'] = $userAuth->open_id;
  141. $add_record['action'] = 9;
  142. $add_record['store_id'] = $data['store_id'];
  143. $add_record['detail'] = '保存了图片';
  144. $user_agent = AlbumAgentModel::where('id', $userAuth->up_agent_id)->first();
  145. $user_agent->share_times++;
  146. $user_agent->save();
  147. AlbumWatchRecord::create($add_record);
  148. }
  149. AlbumPosterModel::where([['id', $data['poster_id']],['store_id', $data['store_id']]])->increment('downloadNum');
  150. return $this->api([]);
  151. }
  152. Private function createPosterPersonal($WeChatApp, $store_id)
  153. {
  154. $userAuth = Auth('api')->user();
  155. if (file_exists(public_path() . '/download/' . $userAuth->id . '_personal.png')) {
  156. unlink(public_path() . '/download/' . $userAuth->id . '_personal.png');
  157. }
  158. if ($userAuth->is_dealer == 1) {
  159. $user_agent = AlbumAgentModel::where([['user_id',$userAuth->id],['status',1]])->first();
  160. $agent_id = $user_agent['id'];
  161. } else {
  162. $agent_id = $userAuth->up_agent_id == 0 ? 0 : $userAuth->up_agent_id;
  163. }
  164. if ($userAuth->up_agent_id != 0) {
  165. $add_record['agent_id'] = $userAuth->up_agent_id;
  166. $add_record['open_id'] = $userAuth->open_id;
  167. $add_record['action'] = 9;
  168. $add_record['store_id'] = $store_id;
  169. $add_record['detail'] = '保存了图片';
  170. $user_agent = AlbumAgentModel::where('id', $userAuth->up_agent_id)->first();
  171. $user_agent->share_times++;
  172. $user_agent->save();
  173. AlbumWatchRecord::create($add_record);
  174. }
  175. $editor = Grafika::createEditor();
  176. $editor->open($image_poster, public_path() . '/base/poster/img/poster_canvas.png');
  177. $editor->open($image_poduct, str_replace(env('CDN_URL'), public_path(), $WeChatApp->circleOfFriends));
  178. $editor->resizeExactWidth($image_poduct, 440);
  179. $editor->resizeExactHeight($image_poduct, 440);
  180. $editor->blend($image_poster, $image_poduct, 'normal', 1, 'top-left', 10, 10);
  181. $font_bold = public_path() . '/base/poster/font/msyhbd.ttc';
  182. $font = public_path() . '/base/poster/font/msyh.ttc';
  183. for ($i = 0; $i <= 1; $i++) {
  184. $len = mb_strlen($WeChatApp->introduce);
  185. if ($len <= 17) {
  186. $editor->text($image_poster, $WeChatApp->introduce, 19, 10, 460, new Color("#000000"), $font_bold, 0);
  187. break;
  188. }
  189. if ($i == 0) {
  190. $text = mb_substr($WeChatApp->introduce, $i * 17, 17);
  191. } else {
  192. $text = mb_substr($WeChatApp->introduce, $i * 17, ($len - 17) > 10 ? 10 : ($len - 17));
  193. }
  194. $editor->text($image_poster, $text, 19, 10, 460 + 50 * $i, new Color("#000000"), $font_bold, 0);
  195. }
  196. $editor->open($image_icon, public_path() . '/base/poster/img/phone.png');
  197. $editor->resizeExactWidth($image_icon, 40);
  198. $editor->resizeExactHeight($image_icon, 40);
  199. $editor->blend($image_poster, $image_icon, 'normal', 1, 'top-left', 10, 570);
  200. if ($userAuth->is_dealer != 1) {
  201. if ($userAuth->up_agent_id == 0) {
  202. $name = $WeChatApp->name;
  203. $phone = $WeChatApp->phone;
  204. } else {
  205. $agent_check = AlbumAgentModel::where([
  206. ['store_id',$store_id],
  207. ['id',$userAuth->up_agent_id]
  208. ])->first();
  209. $name = $agent_check->realname;
  210. $phone = $agent_check->phone;
  211. }
  212. } else {
  213. $agent_check = AlbumAgentModel::where([
  214. ['store_id',$store_id],
  215. ['user_id',$userAuth->id]
  216. ])->first();
  217. $name = $agent_check->realname;
  218. $phone = $agent_check->phone;
  219. }
  220. $editor->text($image_poster, $phone, 20, 55, 577, new Color("#eb7a48"), $font, 0);
  221. $editor->text($image_poster, '长按识别二维码 展示家具画册', 16, 10, 650, new Color("#666666"), $font, 0);
  222. if ($agent_id == 0) {
  223. $editor->open($image_qrcode, str_replace(env('CDN_URL'), public_path(), $WeChatApp->qrcode));
  224. $editor->resizeExactWidth($image_qrcode, 140);
  225. $editor->resizeExactHeight($image_qrcode, 140);
  226. } else {
  227. $file = $this->getQrCodeEasy($WeChatApp->xyx_id, $WeChatApp->xyx_secret, $agent_id, $store_id, $userAuth->id);
  228. if (!$file) {
  229. return $this->error(0, '参数错误,请检查配置', []);
  230. }
  231. $editor->open($image_qrcode, $file);
  232. $editor->resizeExactWidth($image_qrcode, 140);
  233. $editor->resizeExactHeight($image_qrcode, 140);
  234. }
  235. $editor->blend($image_poster, $image_qrcode, 'normal', 1, 'top-left', 310, 500);
  236. $editor->open($image_avatar, public_path() . '/base/poster/avatar/' . $store_id . "/$userAuth->id.jpg");
  237. $editor->resizeExactWidth($image_avatar, 64);
  238. $editor->resizeExactHeight($image_avatar, 64);
  239. $editor->blend($image_poster, $image_avatar, 'normal', 1, 'top-left', 348, 538);
  240. $editor->text($image_poster, $name, 16, 380 - (22 * (mb_strlen($name) / 2)), 650, new Color("#666666"), $font, 0);
  241. $editor->save($image_poster, public_path() . '/download/' . $userAuth->id . '_personal.png');
  242. $real_url = env('CDN_URL') . '/download/' . $userAuth->id . '_personal.png';
  243. return $real_url;
  244. }
  245. private function getQrCode($appId, $appSecret, $store_id)
  246. {
  247. $access = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appId&secret=$appSecret";
  248. $res_access = $this->curlGet($access);
  249. $res_access = json_decode($res_access, true);
  250. if (isset($res_access['access_token'])) {
  251. $ACCESS_TOKEN = $res_access['access_token'];
  252. $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $ACCESS_TOKEN;
  253. $data = array();
  254. $data['scene'] = "scene";//自定义信息,可以填写诸如识别用户身份的字段,注意用中文时的情况
  255. $data['page'] = "pages/index/index";//扫描后对应的path
  256. $data['width'] = 800;//自定义的尺寸
  257. $data['auto_color'] = false;//是否自定义颜色
  258. $color = array(
  259. "r" => "0",
  260. "g" => "0",
  261. "b" => "0",
  262. );
  263. $data['line_color'] = $color;//自定义的颜色值
  264. //dd($data,$url);
  265. $data = json_encode($data);
  266. $this->getHttpArray($url, $data, $store_id);
  267. return true;
  268. } else {
  269. \Log::error($res_access);
  270. return false;
  271. }
  272. }
  273. /**
  274. * 获取小程序码 EasyWeChat
  275. * @param $appId string
  276. * @param $appSecret string
  277. * @param $agent_id int
  278. * @param $store_id int
  279. * @param $user_id int
  280. * @return bool|string
  281. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  282. */
  283. private function getQrCodeEasy($appId, $appSecret, $agent_id, $store_id, $user_id)
  284. {
  285. $config = [
  286. 'app_id' => $appId,
  287. 'secret' => $appSecret,
  288. 'response_type' => 'array',
  289. ];
  290. $app = Factory::miniProgram($config);
  291. $response = $app->app_code->get('pages/index/index?agentid=' . $agent_id, [
  292. 'width' => 140,
  293. ]);
  294. if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
  295. if (!file_exists(public_path() . '/base/poster/QrCode/' . $store_id)) {
  296. mkdir(public_path() . '/base/poster/QrCode/' . $store_id, 0755, true);
  297. }
  298. $filename = $response->saveAs(public_path() . '/base/poster/QrCode/' . $store_id . "/", "$user_id.png");
  299. if ($filename) {
  300. return public_path() . "/base/poster/QrCode/" . $store_id . "/$user_id.png";
  301. } else {
  302. return false;
  303. }
  304. } else {
  305. return false;
  306. }
  307. }
  308. private function createPoster($userAuth, $store_id)
  309. {
  310. if ($userAuth->is_dealer == 1) {
  311. $user_agent = AlbumAgentModel::where([['user_id',$userAuth->id],['status',1]])->first();
  312. $agent_id = $user_agent['id'];
  313. } else {
  314. $agent_id = $userAuth->up_agent_id == 0 ? 0 : $userAuth->up_agent_id;
  315. }
  316. if (file_exists(public_path() . '/download/' . $userAuth->id . '.png')) {
  317. unlink(public_path() . '/download/' . $userAuth->id . '.png');
  318. }
  319. if ($userAuth->up_agent_id != 0) {
  320. $add_record['agent_id'] = $userAuth->up_agent_id;
  321. $add_record['open_id'] = $userAuth->open_id;
  322. $add_record['action'] = 9;
  323. $add_record['store_id'] = $store_id;
  324. $add_record['detail'] = '保存了图片';
  325. $user_agent = AlbumAgentModel::where('id', $userAuth->up_agent_id)->first();
  326. $user_agent->share_times++;
  327. $user_agent->save();
  328. //$album = new AlbumController();
  329. //$album->sendLogsMessage($data['store_id'], $agent->open_id, 9, $userAuth->username, $agent->g_open_id);
  330. AlbumWatchRecord::create($add_record);
  331. }
  332. $editor = Grafika::createEditor();
  333. $WeChatApp = AlbumManufacturerModel::where('store_id', $store_id)->first();
  334. if ($agent_id == 0) {
  335. $editor->open($image_qrcode, str_replace(env('CDN_URL'), public_path(), $WeChatApp->qrcode));
  336. $editor->resizeExactWidth($image_qrcode, 430);
  337. $editor->resizeExactHeight($image_qrcode, 430);
  338. } else {
  339. $file = $this->getQrCodeEasy($WeChatApp->xyx_id, $WeChatApp->xyx_secret, $agent_id, $store_id, $userAuth->id);
  340. if (!$file) {
  341. return $this->error(0, '参数错误,请检查配置', []);
  342. }
  343. $editor->open($image_qrcode, $file);
  344. $editor->resizeExactWidth($image_qrcode, 430);
  345. $editor->resizeExactHeight($image_qrcode, 430);
  346. }
  347. $editor->open($image_avatar, public_path() . '/base/poster/avatar/' . $store_id . "/$userAuth->id.jpg");
  348. $editor->resizeExactWidth($image_avatar, 190);
  349. $editor->resizeExactHeight($image_avatar, 190);
  350. $editor->blend($image_qrcode, $image_avatar, 'normal', 1, 'top-left', 120, 120);
  351. $editor->save($image_qrcode, public_path() . '/download/' . $userAuth->id . '.png');
  352. $real_url = env('CDN_URL') . '/download/' . $userAuth->id . '.png';
  353. return $this->api(compact('real_url'));
  354. }
  355. /**
  356. * @api {post} /api/album_post/del 删除海报(del) 已废弃
  357. * @apiDescription 删除海报(del)
  358. * @apiGroup Album_Post
  359. * @apiPermission 需要登录
  360. * @apiVersion 0.1.0
  361. * @apiParam {string} [url] 图片
  362. * @apiSuccessExample {json} Success-Response:
  363. * HTTP/1.1 200 OK
  364. * {
  365. * "status": true,
  366. * "status_code": 0,
  367. * }
  368. * @apiErrorExample {json} Error-Response:
  369. * HTTP/1.1 400 Bad Request
  370. * {
  371. * "state": false,
  372. * "code": 1000,
  373. * "message": "传入参数不正确",
  374. * "data": null or []
  375. * }
  376. * 可能出现的错误代码:
  377. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  378. */
  379. public function posterDel(Request $request)
  380. {
  381. $validator = Validator::make($request->all(), [
  382. 'url' => 'required',
  383. ], [
  384. 'url' => '缺少图片链接'
  385. ]);
  386. if ($validator->fails()) {
  387. return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS, '');
  388. }
  389. $url = $request->input('url');
  390. $real_url = str_replace(env('CDN_URL'), public_path(), $url);
  391. if (file_exists($real_url)) {
  392. unlink($real_url);
  393. }
  394. return $this->api([], 0);
  395. }
  396. private function curlGet($url)
  397. {
  398. $ch = curl_init();
  399. curl_setopt($ch, CURLOPT_URL, $url);
  400. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  401. curl_setopt($ch, CURLOPT_HEADER, 0);
  402. $output = curl_exec($ch);
  403. curl_close($ch);
  404. return $output;
  405. }
  406. private function getHttpArray($url, $post_data, $store_id)
  407. {
  408. $ch = curl_init();
  409. curl_setopt($ch, CURLOPT_URL, $url);
  410. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  411. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //没有这个会自动输出,不用print_r()也会在后面多个1
  412. curl_setopt($ch, CURLOPT_POST, 1);
  413. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  414. $output = curl_exec($ch);
  415. file_put_contents(public_path() . '/upload/QrCode/' . $store_id . '.png', $output . PHP_EOL, FILE_APPEND);
  416. curl_close($ch);
  417. //$out = json_decode($output);
  418. return true;
  419. }
  420. }