SubscribeController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 Cookie;
  9. use Hashids;
  10. use Log;
  11. class SubscribeController extends BaseController
  12. {
  13. /**
  14. * @apiDefine Subscribe 强关
  15. */
  16. /**
  17. * @apiVersion 1.0.0
  18. * @apiDescription 获取强关二维码
  19. * @api {get} subscribe/qrcode 获取强关二维码
  20. * @apiGroup Subscribe
  21. * @apiName getSubscribeQrcode
  22. * @apiParam {int} bid bid
  23. * @apiSuccess {int} code 状态码
  24. * @apiSuccess {String} msg 信息
  25. * @apiSuccess {object} data 结果集
  26. * @apiSuccess {String} data.src 分页结果集
  27. * @apiSuccessExample {json} Success-Response:
  28. * HTTP/1.1 200 OK
  29. * {
  30. * code: 0,
  31. * msg: "",
  32. * data:{
  33. * src:'sdfasdfas'
  34. * }
  35. */
  36. public function getSubscribeQrcode(Request $request)
  37. {
  38. $bid = $request->input('bid', 0);
  39. if ($bid) {
  40. try {
  41. $bid = Hashids::decode($bid)[0];
  42. } catch (\Exception $e) {
  43. }
  44. }
  45. $bid = (int)$bid;
  46. $officialAccount = $this->getOfficialAccount();
  47. if ($officialAccount) {
  48. $info['appid'] = $officialAccount->appid;
  49. $src = $this->getRcodeInfo($info, $bid);
  50. if ($src) {
  51. return response()->success(['src' => $src,'head_img'=>$officialAccount->head_img]);
  52. }
  53. }
  54. return response()->success();
  55. }
  56. protected function getOfficialAccount()
  57. {
  58. $distribution_channel_id = $this->distribution_channel_id;
  59. $res = OfficialAccountService::canUseOfficialAccountByChannelId(compact('distribution_channel_id'));
  60. if (isset($res->nickname) && !empty($res->nickname)) {
  61. Cookie::queue('force_subscribe_name', $res->nickname, env('U_COOKIE_EXPIRE'));
  62. }
  63. return $res;
  64. }
  65. protected function getRcodeInfo($param, $bid)
  66. {
  67. $param_need = [
  68. 'gzh_app_id' => $param['appid'],
  69. 'scene_id' => $this->uid,
  70. 'timestamp' => time(),
  71. ];
  72. $param_need['sign'] = $this->getSign($param_need);
  73. $client = new Client(['timeout' => 3.0,]);
  74. try {
  75. Log::info('ruo guan get qrcode url is:'.'https://zsyauth.aizhuishu.com/api/get_qrcode_url?'.http_build_query($param_need));
  76. $qrcode_url_res = $client->request('get', 'https://zsyauth.aizhuishu.com/api/get_qrcode_url?' . http_build_query($param_need))->getBody()->getContents();
  77. if ($qrcode_url_res) {
  78. $qrcode_url = json_decode($qrcode_url_res, true);
  79. if ($qrcode_url['code'] == 1) {
  80. //保存强关时的bid
  81. if (isset($qrcode_url['data']) && !empty($qrcode_url['data'])) {
  82. Redis::hset('force_subscribe_from_bid', $param['appid'] . '_' . $this->uid, $bid);
  83. $send_order_id = Cookie::get('send_order_id') ? Cookie::get('send_order_id') : 0;
  84. Redis::hset('force_subscribe_from_send_order_id', $param['appid'] . '_' . $this->uid, $send_order_id);
  85. Log::info('ruo guan uid is'.$this->uid.'--qrcode is:'.$qrcode_url['data']);
  86. return $qrcode_url['data'];
  87. }
  88. }
  89. }
  90. } catch (\Exception $e) {
  91. Redis::sadd('force_subscribe_qrcode:error', $param['appid'] . '_' . time());
  92. }
  93. return false;
  94. }
  95. }