AlbumPosterController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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\AlbumManufacturerModel;
  10. use App\Models\AlbumPosterModel;
  11. use App\Services\Base\ErrorCode;
  12. use Grafika\Grafika;
  13. use Validator, Response,Auth;
  14. use Illuminate\Http\Request;
  15. class AlbumPosterController extends Controller
  16. {
  17. /**
  18. * @api {post} /api/album_post/info 获取海报数据(info)
  19. * @apiDescription 获取海报数据(info)
  20. * @apiGroup Album_Post
  21. * @apiPermission 需要登录
  22. * @apiVersion 0.1.0
  23. * @apiParam {int} [store_id] 商户id
  24. * @apiSuccessExample {json} Success-Response:
  25. * HTTP/1.1 200 OK
  26. * {
  27. * "status": true,
  28. * "status_code": 0,
  29. * "message": "",
  30. * "data": [
  31. * 'posters':'asdawd', //海报
  32. * 'words':'asdawd', //话术
  33. * 'introduce':'222', //介绍
  34. * 'share':'xxx' //分享图片
  35. * 'phone':'xxx' //电话
  36. * 'username':'xxx' //姓名
  37. * ]
  38. * }
  39. * @apiErrorExample {json} Error-Response:
  40. * HTTP/1.1 400 Bad Request
  41. * {
  42. * "state": false,
  43. * "code": 1000,
  44. * "message": "传入参数不正确",
  45. * "data": null or []
  46. * }
  47. * 可能出现的错误代码:
  48. * 1000 CLIENT_WRONG_PARAMS 传入参数不正确
  49. */
  50. public function posterInfo(Request $request)
  51. {
  52. $userAuth = Auth('api')->user();
  53. if (!$userAuth) {
  54. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  55. }
  56. $validator = Validator::make($request->all(), [
  57. 'store_id' => 'required',
  58. ], [
  59. 'store_id.required' => '店铺信息未知',
  60. ]);
  61. if ($validator->fails()) {
  62. return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS, '');
  63. }
  64. $store_id = $request->input('store_id');
  65. $info = AlbumPosterModel::where('store_id', $store_id)->first()->toArray();
  66. $wechat_app = AlbumManufacturerModel::where('store_id', $store_id)->first();
  67. if (!file_exists(public_path() . '/upload/QrCodee/' . $store_id . '.gif')) {
  68. $this->getQrCode($wechat_app->xyx_id, $wechat_app->xyx_secret, $store_id);
  69. }
  70. $info['phone'] = $userAuth->phone;
  71. $info['username'] = $userAuth->username;
  72. $info['QrCode'] = env('APP_URL') . '/upload/QrCode/' . $store_id . '.gif';
  73. return $this->api(compact('info'));
  74. }
  75. private function getQrCode($appId, $appSecret, $store_id)
  76. {
  77. $access = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appId&secret=$appSecret";
  78. $res_access = $this->curlGet($access);
  79. $res_access = json_decode($res_access, true);
  80. if (isset($res_access['access_token'])) {
  81. $ACCESS_TOKEN = $res_access['access_token'];
  82. $url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" . $ACCESS_TOKEN;
  83. $data = array();
  84. $data['scene'] = "scene";//自定义信息,可以填写诸如识别用户身份的字段,注意用中文时的情况
  85. $data['page'] = "pages/index/index";//扫描后对应的path
  86. $data['width'] = 800;//自定义的尺寸
  87. $data['auto_color'] = false;//是否自定义颜色
  88. $color = array(
  89. "r" => "0",
  90. "g" => "0",
  91. "b" => "0",
  92. );
  93. $data['line_color'] = $color;//自定义的颜色值
  94. //dd($data,$url);
  95. $data = json_encode($data);
  96. $this->getHttpArray($url, $data, $store_id);
  97. return true;
  98. } else {
  99. \Log::error($res_access);
  100. return false;
  101. }
  102. }
  103. public function createPoster(Request $request)
  104. {
  105. $userAuth = Auth('api')->user();
  106. if (!$userAuth) {
  107. return $this->error(ErrorCode::CLIENT_WRONG_PARAMS, '未登录!');
  108. }
  109. $validator = Validator::make($request->all(), [
  110. 'store_id' => 'required',
  111. 'store_id' => 'required',
  112. ], [
  113. 'store_id.required' => '店铺信息未知',
  114. ]);
  115. if ($validator->fails()) {
  116. return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS, '');
  117. }
  118. $data = $request->input('store_id');
  119. $editor = Grafika::createEditor();
  120. $editor->open($image_poster, public_path() . '/base/poster/img/poster_canvas.png');
  121. $editor->open($image_poduct, public_path() . '');
  122. }
  123. private function curlGet($url)
  124. {
  125. $ch = curl_init();
  126. curl_setopt($ch, CURLOPT_URL, $url);
  127. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  128. curl_setopt($ch, CURLOPT_HEADER, 0);
  129. $output = curl_exec($ch);
  130. curl_close($ch);
  131. return $output;
  132. }
  133. private function getHttpArray($url, $post_data, $store_id)
  134. {
  135. $ch = curl_init();
  136. curl_setopt($ch, CURLOPT_URL, $url);
  137. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  138. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //没有这个会自动输出,不用print_r()也会在后面多个1
  139. curl_setopt($ch, CURLOPT_POST, 1);
  140. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  141. $output = curl_exec($ch);
  142. file_put_contents(public_path() . '/upload/QrCode/' . $store_id . '.gif', $output . PHP_EOL, FILE_APPEND);
  143. curl_close($ch);
  144. //$out = json_decode($output);
  145. return true;
  146. }
  147. }