AlbumPosterController.php 15 KB

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