国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁 php教程 PHP源碼 CI框架微信網(wǎng)頁授權(quán)庫

CI框架微信網(wǎng)頁授權(quán)庫

Jul 06, 2016 pm 01:28 PM

跳至 [1] [2] [全屏預(yù)覽]
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');	
	Class Weixin
	{
		private $appId;
		private $appSecret;
		function __construct()
		{
			$this->appId = trim('你的appid');
			$this->appSecret = trim('你的appsecret');
		}
		
		function redirect_url($redirect)
		{
			/*授權(quán)頁面*/
			
			$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$this->appId&redirect_uri=$redirect&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
			return $url;
		}
		/* 通過code換取access_token*/
		function access_token($code)
		{
			/*獲取到的code換取access_token和openid*/
			
			$post_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$this->appId&secret=$this->appSecret&code=$code&grant_type=authorization_code";
						 // echo $post_url;exit();
			$return = $this->postdata($post_url);
			// print_r($return);exit();
			$access_token = $return['access_token'];
			$openid = $return['openid'];
			
			/*獲取微信用戶數(shù)據(jù)*/
			$get_userinfo = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
			$userinfo = json_decode(file_get_contents($get_userinfo));
			return $userinfo;
		}
		
		function eff($access_token,$openid)
		{
			/*檢測access_token是否正確,errcode=0 為正確*/
			$eff_url = "https://api.weixin.qq.com/sns/auth?access_token=$access_token&openid=$openid";
			$get_eff =json_decode(file_get_contents($eff_url));
			return $get_eff;
			
		}
	
		//通過curl方式提交code換取access_token數(shù)據(jù)
		function postdata($url)
		{
			 header('Content-Type:text/html;charset=utf-8');
			 // echo $url;exit();
		    $curl = curl_init();
		    curl_setopt($curl, CURLOPT_URL, $url);
		    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
		    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
		    curl_setopt($curl, CURLOPT_SSLVERSION, 1);
		    // if (!empty($data)){
		        // curl_setopt($curl, CURLOPT_POST, 1);
		        // curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
		    // }
		    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		    $output = curl_exec($curl);
		    curl_close($curl);
		    // var_dump($output);exit();
		    // print_r($output);exit();
		    $access = json_decode($output,true);
		    return $access;
		}
		/*
			這個位置開始是控制器index()傳入的微信用戶資料處理
		*/
			function save_session($data)
			{
				foreach ($data as $key => $value) {
					// $_SESSION['uid'] = $value['uid'];
					// $_SESSION['nickname'] = $value['nickname'];
					// $_SESSION['fullname'] = $value['fullname'];
					// $_SESSION['status'] = $value['status'];
					// $_SESSION['groups'] = $value['groups'];
					$_SESSION[$key] = $value;
				}
				return $_SESSION;
				// print_r($_SESSION);exit();
				// unset($_SESSION[0]);
				
			}
		function obj_to_arr($data)
		{
			// 進(jìn)行轉(zhuǎn)換成數(shù)組 使用 obj_to_arr方式
			$data = is_object($data)?get_object_vars($data):$data;
				foreach ($data as $key => $value) 
				{
					
					$arr[$key] = $value;
				}
				return $arr;
		}
	}

2. [代碼]通過code換access_token獲取用戶信息,controller文件 跳至 [1] [2] [全屏預(yù)覽]

<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');	
	Class Coupon_index extends CI_Controller
	{
		function __construct()
		{
			parent::__construct();
			$this->load->library(array('weixin','session'));
            $this->load->helper('url');
			// $this->load->ldap_mod_del(link_identifier, dn, entry)
            $this->load->model('Coupon_model');
           
		}
	    /**
		 *優(yōu)惠券主程序
		 */
		function index()
		{
			$this->load->view('/coupon/index.html');
		}

        function User_exists()
        {
           /*
                檢測改微信用戶是否存在
                $user_arr 獲取的是通過get_code返回的微信用戶信息,此時的信息是通過微信服務(wù)器返回的,不能記錄session
                $user std_obj模式,轉(zhuǎn)換為數(shù)組
                $user_exists 扔入model中,檢測數(shù)據(jù)表中是否存在該用戶
                $redirect 走完流程后,跳轉(zhuǎn)到首頁
                if語句的作用,是 判斷通過model返回?cái)?shù)據(jù)表的信息,如果為空則把微信用戶信息錄入到表中,再讀取出來,存進(jìn)session。
                else 則數(shù)據(jù)表已經(jīng)存在該用戶,直接讀取,存進(jìn)session
                需要注意的是,使用foreach的原因,是二維數(shù)組轉(zhuǎn)一維數(shù)組
           */
                $user_arr = $this->Get_code();
                // var_dump($user_arr);exit();
                $user = $this->weixin->obj_to_arr($user_arr);
                // var_dump($user);exit();
                // print_r($user);exit();

                $user_exists = $this->Coupon_model->CheckUser('cou_user',$user);
                // print_r($user_exists);exit();
                // $redirect = 'http://h5.imchaotan.com/coupon/index.php/Coupon/Coupon_index/Coupon_Get/bid/1';
                // $return_url = $this->session->return_url;
                $redirect = 'http://h5.imchaotan.com'.$this->session->return_url;
                // echo $redirect;exit();
                if(empty($user_exists))
                {
                     /*
                  由于微信獲取到的用戶數(shù)據(jù)是stdclass對象格式
                  所以需要進(jìn)行轉(zhuǎn)換成數(shù)組 使用 obj_to_arr方式
                 */

                //加入自定義的字符進(jìn)入數(shù)組
                unset($user['privilege']);
                $user_exists['nickname']      = $user['nickname'];
                $user_exists['openid']        = $user['openid'];
                $user_exists['language']      = $user['language'];
                $user_exists['city']          = $user['city'];
                $user_exists['country']       = $user['country'];
                $user_exists['province']      = $user['province'];
                $user_exists['headimgurl']    = $user['headimgurl'];
                $user_exists['sex']           = $user['sex'];
                $user_exists['fullname']      = $user['nickname'];
                $user_exists['telphone']      = '';
                $user_exists['login_ip']      =$this->input->ip_address();
                $user_exists['last_ip']       =$this->input->ip_address();
                $user_exists['groups']        = REGISTER_GROUP_ID;
                $user_exists['status']        = 1; 
                $user_exists['login_time']    = date("Y-m-d");

                 $insert_id = $this->Coupon_model->insert_one('cou_user',$user_exists);
                $user_exists['uid'] = $insert_id;
                }
                else{
                  $user_exists = $user_exists[0];
                }
                
                // $return_url = $this->session->back_url;
                // if(isset($return_url))header('location:'.$return_url);

                /*由Coupon_idex中的Get_Coupon處理*/
                $this->session->set_userdata($user_exists);
                if(isset($this->session->return_url))header('location:'.$this->session->return_url);
                // print_r($user_exists);exit();
                header('location:'.$redirect);


        }

		function Coupon_start()
		{
			/*進(jìn)入領(lǐng)取頁面,需要先經(jīng)過授權(quán)*/
            $redirect_url = 'Coupon/Coupon_index/User_exists';
            $redirect = urlencode('http://h5.imchaotan.com/coupon/index.php/'.$redirect_url);
            // $redirect = urlencode('http://h5.imchaotan.com/coupon/index.php/Coupon/Coupon_index/Get_code');
            $return = $this->weixin->redirect_url($redirect);
             header('location:'.$return);
		}

        public function Get_code()
        {
            if(isset($_GET['code']))
            {
                $code = $_GET['code'];
                // echo $code;exit();
                $user_arr = $this->weixin->access_token($code);
                //跳轉(zhuǎn)到用戶檢測中check_exists()去
               // echo $user_arr;exit();
                // var_dump($user_arr);
                
                return $user_arr;
                
            }else{
                //否則檢測cookie中是否存在該用戶,如果有,則return回首頁
                    echo 'error';
            }
         }

         public function Coupon_Get()
         {
            
            /*獲取商家bid,讀取相關(guān)信息*/
            // $b_name = $this->uri->segment(4, 0);
           
            $nickname = $this->session->nickname;
            $openid = $this->session->openid;
            $status = $this->session->status;
            $_SESSION['return_url'] =  $_SERVER['REQUEST_URI'];
            // $this->session->set_userdata($return_url);
            // echo $this->session->return_url;exit();
            if(empty($nickname))header('location:'.'http://h5.imchaotan.com/coupon/index.php/Coupon/Coupon_index/Coupon_start');
            $bid = $this->uri->segment(5, 0);
            /*扔進(jìn)Coupon_model中,讀取bid中的商家信息*/
            $content = $this->Coupon_model->Coupon_Business('cou_business',$bid);

            // print_r($content);
            // echo $bid;
            // echo $b_name;
            $data['bname']      = $content['bname'];
            $data['discount']   = $content['discount'];
            $data['bimg']       = $content['bimg'];
            $data['contents']   = $content['contents'];
            $data['amount']     = $content['amount'];
            $data['nickname']   = $nickname;
            $data['status']     = $status;
            $data['js'] = json_encode(array($content['bname'],$content['discount'],$nickname,$status));
            // echo $data['js'];exit();
            // print_r($data);
            $this->load->view('/coupon/index.html',$data);
            // echo $nickname;
            // echo $status;

         
        }
       
}

      

	
本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強(qiáng)大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)