SubscribeController.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. function getFromUser()
  98. {
  99. $uid = $this->uid;
  100. $user_info = ForceSubscribeService::forceSubscribeUsersByUid(compact('uid'));
  101. dump($user_info);
  102. if($user_info)
  103. {
  104. $appid = $user_info->appid;
  105. $official_account = OfficialAccountService::officialAccountByAppid(compact('appid'));
  106. dump($official_account);
  107. $title = $official_account->nickname;
  108. $img = $official_account->qrcode_url;
  109. }else{
  110. return redirect()->to('/recent');
  111. }
  112. $head_img_pool = [
  113. 'https://cdn-novel.iycdm.com/h5/subscribe/headimg/1.png',
  114. 'https://cdn-novel.iycdm.com/h5/subscribe/headimg/2.png',
  115. 'https://cdn-novel.iycdm.com/h5/subscribe/headimg/3.png'
  116. ];
  117. $head_img = array_random($head_img_pool);
  118. return view('wap.user-sub-info',compact('head_img','img','title'));
  119. }
  120. }