微信H5獲取code、openid並載入頁面
阿新 • • 發佈:2018-12-21
public function share_list1Op() { header("Access-Control-Allow-Origin:*"); if($_GET['code']){ //通過code獲取openid $code = $_GET['code']; $appid = $this->payment_config['wxpay_appid']; $secret = '4d3f5f2fcfffb22c63abb7390a2eb3a9'; $weixin = file_get_contents("https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$secret."&code=".$code."&grant_type=authorization_code");//通過code換取網頁授權access_token $result = json_decode($weixin,true); //對JSON格式的字串進行編碼 $openid = $result['openid']; setcookie("openid",$openid); } if(!$_COOKIE['openid']){ //獲取code授權 $appid = $this->payment_config['wxpay_appid']; $redirect_uri=urlencode("http://www.jztlchn.com/app/index.php?act=share&op=share_list1&pay_sn=".$pay_sn."&leader_id=".$leader_id); //回跳地址 就回調到本方法,這樣有openid就執行下面載入頁面的程式碼,沒有了就先獲取openid再跳轉回來 $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_base&state=1#wechat_redirect"; header("location:".$url); } $group_info = Model()->table('order_sec_pay')->field('goods_id,initiator_id')->where(array('pay_sn'=>$pay_sn))->find(); }