学习笔记

  > 公众号开发的redirect参数不固定接收携带

公众号开发的redirect参数不固定接收携带

跳转代码

	public function mkl()
	{

        $str_all_param = $this->url_get();

        $redirect_uri = urlencode("https://abc.com/star2/index?{$str_all_param}");
	
        $url_oauth = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->wechat_appId}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_base&connect_redirect=1#wechat_redirect";

		header("location:".$url_oauth); 
	}

容错核心代码

public function index()
{
    $data['title'] = "自定义标题";
    $param = $this->input->get('fm',TRUE);
    $str_all_param = $this->url_get();

    $nonceStr = $this->Wechat_model->nonce_str();
	$now = time();


	if( !isset($_SESSION['openid']) )
    {

        $code = $this->input->get('code',TRUE);

        if ( isset($code) ) 
        {
            $res = $this->Wechat_model->getOpenidByWeb($code);
            if ( $res['status'] == 200 ) {
                $json = $res['json'];
                $openid = $json->openid;
                $row = $this->db->select('id,pid2')->where('openid',$openid)->get("members")->row_array();
                if( $row['id'] )
                {
                    // 直接赋值openid
                    $_SESSION['openid'] = $openid;
                    if ( $row['pid2'] != 1 ) 
                    {
                        $this->db->set('pid2',1)->where('openid',$openid)->update("members");
                    }
                }
                else
                {
                    if ( $openid ) 
                    {
                        $arr = array(
                            'openid'    => $openid,
                            'createtime'=> $now,
                            'param'     => $param,
                            'pid2'      => 1,
                            );
                        $this->db->insert('members',$arr);
                    }

                    $_SESSION['openid'] = $openid;
                    
                }
            }
            else
            {
                redirect("star2/mkl/?{$str_all_param}");
            }

            
        }
        else
        {
            redirect("star2/mkl/?{$str_all_param}");
        }

        // 公众号环境end
    }
    else
    {
        $openid = $_SESSION['openid'];
    }

    // 是否重置今天时间
    $row_member = $this->db->select("firsttime2,usetime2,num2")->where('openid',$openid)->get('members')->row_array();
    $firsttime = date("Y-m-d",time());
    if( $row_member['firsttime2'] != $firsttime )
    {
        $this->db
                ->set('num2',1)
                ->set('firsttime2',$firsttime)
                ->set('usetime2',0)
                ->where("openid",$openid)->update('members');
    }

    $data['num'] = $row_member['usetime2'] >= 5 ? 0 : $row_member['num2'];


    // 公众号环境
    // $nonceStr           = $this->Wechat_model->nonce_str();
    $jsapiTicket        = $this->Wechat_model->getJsApiTicket();
    $url                = "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    $string             = "jsapi_ticket=".$jsapiTicket."&noncestr=".$nonceStr."&timestamp=".$now."&url=".$url;
    $signature          = sha1($string);
    $data['signature']  = $signature;
    $data['appId']      = $this->wechat_appId;
    $data['timeStamp']  = $now;
    $data['nonceStr']   = $nonceStr;

    // 公众号环境end
    $data['fm'] = $param;

	$this->load->view('star/index0310.html',$data);
}

最关键的代码

private function url_get()
{
    $arr_param = $_GET;
    if( empty($arr_param) )
    {
        $str_all_param = '';
    }
    else
    {
        $str = '';
        $arr_temp = array();
        foreach ($arr_param as $k => $v) 
        {
            if ( $k == 'code' ) continue;
            $arr_temp[] = "{$k}={$v}";
        }

        $str_all_param = implode('&', $arr_temp);
    }

    return $str_all_param;
}

ci框架,3.0版本。

发布时间:2021-03-16,15:27:06

公众号开发,获取openid,外加跳转参数的设置。

作者:澎蠡

让学习成为一种习惯,让知识交流变成一种生活方式。