AlbumPosterController.php 13 KB

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