SubscribeController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. namespace App\Http\Controllers\Wap\Subscribe;
  3. use Illuminate\Http\Request;
  4. use GuzzleHttp\Client;
  5. use App\Http\Controllers\Wap\BaseController;
  6. use Redis;
  7. use App\Modules\OfficialAccount\Services\OfficialAccountService;
  8. use App\Modules\OfficialAccount\Services\ForceSubscribeService;
  9. use Cookie;
  10. use Hashids;
  11. use Log;
  12. class SubscribeController extends BaseController
  13. {
  14. /**
  15. * @apiDefine Subscribe 强关
  16. */
  17. /**
  18. * @apiVersion 1.0.0
  19. * @apiDescription 获取强关二维码
  20. * @api {get} subscribe/qrcode 获取强关二维码
  21. * @apiGroup Subscribe
  22. * @apiName getSubscribeQrcode
  23. * @apiParam {int} bid bid
  24. * @apiSuccess {int} code 状态码
  25. * @apiSuccess {String} msg 信息
  26. * @apiSuccess {object} data 结果集
  27. * @apiSuccess {String} data.src 分页结果集
  28. * @apiSuccessExample {json} Success-Response:
  29. * HTTP/1.1 200 OK
  30. * {
  31. * code: 0,
  32. * msg: "",
  33. * data:{
  34. * src:'sdfasdfas'
  35. * }
  36. */
  37. public function getSubscribeQrcode(Request $request)
  38. {
  39. $bid = $request->input('bid', 0);
  40. if ($bid) {
  41. try {
  42. $bid = Hashids::decode($bid)[0];
  43. } catch (\Exception $e) {
  44. }
  45. }
  46. $bid = (int)$bid;
  47. $officialAccount = $this->getOfficialAccount();
  48. if ($officialAccount) {
  49. $info['appid'] = $officialAccount->appid;
  50. $src = $this->getRcodeInfo($info, $bid);
  51. if ($src) {
  52. return response()->success(['src' => $src,'head_img'=>$officialAccount->head_img]);
  53. }
  54. }
  55. return response()->success();
  56. }
  57. protected function getOfficialAccount()
  58. {
  59. $distribution_channel_id = $this->distribution_channel_id;
  60. $res = OfficialAccountService::canUseOfficialAccountByChannelId(compact('distribution_channel_id'));
  61. if (isset($res->nickname) && !empty($res->nickname)) {
  62. Cookie::queue('force_subscribe_name', $res->nickname, env('U_COOKIE_EXPIRE'));
  63. }
  64. return $res;
  65. }
  66. protected function getRcodeInfo($param, $bid)
  67. {
  68. $param_need = [
  69. 'gzh_app_id' => $param['appid'],
  70. 'scene_id' => $this->uid,
  71. 'timestamp' => time(),
  72. ];
  73. $param_need['sign'] = $this->getSign($param_need);
  74. $client = new Client(['timeout' => 3.0,]);
  75. try {
  76. Log::info('ruo guan get qrcode url is:'.'https://zsyauth.aizhuishu.com/api/get_qrcode_url?'.http_build_query($param_need));
  77. $qrcode_url_res = $client->request('get', 'https://zsyauth.aizhuishu.com/api/get_qrcode_url?' . http_build_query($param_need))->getBody()->getContents();
  78. if ($qrcode_url_res) {
  79. $qrcode_url = json_decode($qrcode_url_res, true);
  80. if ($qrcode_url['code'] == 1) {
  81. //保存强关时的bid
  82. if (isset($qrcode_url['data']) && !empty($qrcode_url['data'])) {
  83. Redis::hset('force_subscribe_from_bid', $param['appid'] . '_' . $this->uid, $bid);
  84. $send_order_id = Cookie::get('send_order_id') ? Cookie::get('send_order_id') : 0;
  85. Redis::hset('force_subscribe_from_send_order_id', $param['appid'] . '_' . $this->uid, $send_order_id);
  86. Log::info('ruo guan uid is'.$this->uid.'--qrcode is:'.$qrcode_url['data']);
  87. return $qrcode_url['data'];
  88. }
  89. }
  90. }
  91. } catch (\Exception $e) {
  92. Redis::sadd('force_subscribe_qrcode:error', $param['appid'] . '_' . time());
  93. }
  94. return false;
  95. }
  96. //只获取用户的强关二维码
  97. protected function getSimpleRcodeInfo($appid)
  98. {
  99. $param_need = [
  100. 'gzh_app_id' => $appid,
  101. 'scene_id' => $this->uid,
  102. 'timestamp' => time(),
  103. ];
  104. $param_need['sign'] = $this->getSign($param_need);
  105. $client = new Client(['timeout' => 3.0,]);
  106. try {
  107. $qrcode_url_res = $client->request('get', 'https://zsyauth.aizhuishu.com/api/get_qrcode_url?' . http_build_query($param_need))->getBody()->getContents();
  108. if ($qrcode_url_res) {
  109. $qrcode_url = json_decode($qrcode_url_res, true);
  110. if ($qrcode_url['code'] == 1) {
  111. if (isset($qrcode_url['data']) && !empty($qrcode_url['data'])) {
  112. return $qrcode_url['data'];
  113. }
  114. }
  115. }
  116. } catch (\Exception $e) {
  117. }
  118. return false;
  119. }
  120. //获取用户的强关信息
  121. function getFromUser()
  122. {
  123. Cookie::queue('force_show_qrcode', 1, -1);
  124. $uid = $this->uid;
  125. $user_info = ForceSubscribeService::forceSubscribeUsersByUid(compact('uid'));
  126. if(isset($_GET['d']))
  127. {
  128. dump($user_info);
  129. }
  130. if($user_info)
  131. {
  132. $appid = $user_info->appid;
  133. $official_account = OfficialAccountService::officialAccountByAppid(compact('appid'));
  134. if(isset($_GET['d']))
  135. {
  136. dump($official_account);
  137. }
  138. $title = $official_account->nickname;
  139. $img = 'https://open.weixin.qq.com/qr/code?username='.$official_account->name;
  140. if(isset($_GET['d']))
  141. {
  142. $qrcode_url = $this->getSimpleRcodeInfo($appid);dump($qrcode_url);
  143. }
  144. if($qrcode_url = $this->getSimpleRcodeInfo($appid))//用服务号强关二维码
  145. {
  146. $img = $qrcode_url;
  147. }
  148. }else{
  149. return redirect()->to('/recent');
  150. }
  151. $head_img_pool = [
  152. 'https://cdn-novel.iycdm.com/h5/subscribe/headimg/1.png',
  153. 'https://cdn-novel.iycdm.com/h5/subscribe/headimg/2.png',
  154. 'https://cdn-novel.iycdm.com/h5/subscribe/headimg/3.png'
  155. ];
  156. $head_img = array_random($head_img_pool);
  157. return view('wap.userSubInfo',compact('head_img','img','title'));
  158. }
  159. }