<?php

namespace App\Http\Controllers\WapBrowser\Subscribe;

use Illuminate\Http\Request;
use App\Http\Controllers\WapBrowser\BaseController;
use GuzzleHttp\Client;
use App\Modules\OfficialAccount\Services\OfficialAccountService;
use App\Modules\SendOrder\Services\SendOrderService;
use Cookie;
use Log;
use Hashids;

class SubscribeController extends BaseController
{

    protected function getRcodeInfo($appid,$bid,$cid)
    {
        if($bid && $cid){
            $scene_id = sprintf('outer:%s_%s',$bid,$cid);
        }else{
            $scene_id = 1;
        }
        $param_need = [
            'gzh_app_id' => $appid,
            'scene_id' => $scene_id,
            'timestamp' => time(),
        ];
        $param_need['sign'] = $this->getSign($param_need);
        $client = new Client(['timeout' => 3.0,]);
        try {
            Log::info('WapBrowsergetRcodeInfo  get qrcode url is:' . 'https://zsyauth.aizhuishu.com/api/get_qrcode_url?' . http_build_query($param_need));
            $qrcode_url_res = $client->request('get', 'https://zsyauth.aizhuishu.com/api/get_qrcode_url?' . http_build_query($param_need))->getBody()->getContents();
            if ($qrcode_url_res) {
                $qrcode_url = json_decode($qrcode_url_res, true);
                if ($qrcode_url['code'] == 1) {
                    //保存强关时的bid
                    if (isset($qrcode_url['data']) && !empty($qrcode_url['data'])) {
                        return $qrcode_url['data'];
                    }
                }
            }
        } catch (\Exception $e) {
        }

        return false;
    }

    /**
     * 获取公众号信息
     * @param $distribution_channel_id
     */
    protected function getOfficialAccount()
    {
        $distribution_channel_id = $this->distribution_channel_id;
        $res = OfficialAccountService::canUseOfficialAccountByChannelId(compact('distribution_channel_id'));
        Log::info($res);
        if (isset($res->nickname) && !empty($res->nickname)) {
            Cookie::queue('force_subscribe_name', $res->nickname, env('U_COOKIE_EXPIRE'));
        }
        return $res;
    }

    public function subscribeView(Request $request){
        $title = '扫一扫';
        $send_order_id = Cookie::get('send_order_id');
        $bid = $request->get('bid');
        $cid = $request->get('cid');
        $url = '';
        if($send_order_id){
            $h5_scheme = env('H5_SCHEME', 'https');
            if($bid && $cid){
                $bid = Hashids::encode($bid);
                $redirect_path = urlencode(sprintf('/reader?bid=%s&cid=%s',$bid,$cid));
                $url = $h5_scheme . '://' . str_replace('bsite','site',_domain()). '/yun/'.$send_order_id.'?redirect_path='.$redirect_path;
            }else{
                $url = $h5_scheme . '://' . str_replace('bsite','site',_domain()). '/yun/'.$send_order_id;
            }
            $img = '';
            return view('wap-browser.subscribe',compact('title','img','url'));
        }
        $appid = $this->getOfficialAccount();
        $bid = $request->get('bid');
        $cid = $request->get('cid');
        Log::info("this->distribution_channel_id:".$this->distribution_channel_id);
        $img = '';
        if($appid && $appid->appid){
            $img = $this->getRcodeInfo($appid->appid,$bid,$cid);
        }
        return view('wap-browser.subscribe',compact('title','img','url'));
    }

    private function getRcodeInfoV2(){

    }
}