1. 程式人生 > >php例項-微信第三方登入

php例項-微信第三方登入

下面小編就為大家帶來一篇微信第三方登入(原生)demo【必看篇】。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

 

在一家ecstore二開公司有一段時間了,公司希望往自己研發產品上面走,一直在培養新人。

最近要自己去微信登入,自己就在ectore的框架基礎上,寫的原生微信第三方登入以此來熟悉微信第三方登入,在ecstore上面去寫。

一個簡單的demo,不是很好,分享給大家,希望能給想做微信第三方登入的朋友帶來思路...

首先:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

資料準備:

 

水印圖片 28*28  png格式 公司logo

高清圖片 108*108   png格式 公司logo

 

1.在微信開放平臺 https://open.weixin.qq.com/中填寫基本資訊,

2.完成郵箱驗證,

3.並完善開發者資料,註冊成為開發者。

4.在“賬號中心”完成開發者資質認證。

5.進入微信開放平臺管理中心網站應用,點選“建立應用”按鈕。

6.填寫基本資訊,完成點選下一步

7.填寫平臺資訊

 應用簽名:可在微信開發平臺的資源中心》》資源下載》》中下載“簽名生成工具”,使用者獲取已經安裝到手機的第三方應用的簽名。輸入應用包名,即可獲得該應用的簽名值。

8.提交稽核後,在7個工作日內騰訊將給出稽核結果。(通常較快,幾個小時就可反饋結果)

獲取AppID,AppSecret,之後的處理:

1

2

需要在你想加的網站登入頁給一個連結:<a href="https://open.weixin.qq.com/connect/qrconnect?appid=your appid &redirect_uri=http://www.sunmil.cn/passport-linshi.html&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect"

rel="external nofollow" >微信登入</a>

<br><br>

處理微信登入開始

定義appid appsecert

1

2

3

private $appid = "your appid";

private $appsecert = "your appsecert";

private $redirect_uri = http://www.sunmil.cn/wxGetCode;

繫結賬號頁面

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

public function weixin(){

   //連結資料庫

   $link = mysqli_connect(DB_HOST, DB_USER,DB_PASSWORD,DB_NAME ) or die('資料庫連線失敗4');

   $link = mysqli_connect('127.0.0.1', 'root','root', 'ec') or die('資料庫連線失敗');

   mysqli_set_charset($link,'utf8');

  

   //接收openid

   $openid = $_COOKIE['openid'];

  

   $sql = "select * from sdb_trustlogin_trustinfo where openid = '$openid' ";

   $res = mysqli_query($link, $sql);

   $row = mysqli_fetch_assoc($res);

  

   $this->pagedata['realname'] = $row['realname'];

   $this->pagedata['avatar'] = $row['avatar'];

  

   $this->page("site/passport/weixin.html");

  

 }

當第一次微信登入,處理繫結賬號頁面

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

public function handle($url=null){

    // $url = "http://www.sunmil.cn";

     //連結資料庫

    $link = mysqli_connect(DB_HOST, DB_USER,DB_PASSWORD,DB_NAME ) or die('資料庫連線失敗3');

      mysqli_set_charset($link,'utf8');

 

      $post = utils::_filter_input($_POST);

 

    $userData = array(

      'login_account' => $post['uname'],

      'login_password' => $post['password']

    );

 

    $member_id = kernel::single('pam_passport_site_basic')->login($userData,$post['verifycode'],$msg);

 

 

    $b2c_members_model = $this->app->model('members');

    $member_point_model = $this->app->model('member_point');

 

    $member_data = $b2c_members_model->getList( 'member_lv_id,experience,point', array('member_id'=>$member_id) );

     

    $member_data = $member_data[0];

    $member_data['order_num'] = $this->app->model('orders')->count( array('member_id'=>$member_id) );

 

    $b2c_members_model->update($member_data,array('member_id'=>$member_id));

    $this->userObject->set_member_session($member_id);

    $this->bind_member($member_id);

    $this->set_cookie('loginName',$post['uname'],time()+31536000);//用於記住密碼

    // setCartNum()需要傳入一個引數

    $aCart = array();

    $this->app->model('cart_objects')->setCartNum($aCart);

    $url = $this->userPassport->get_next_page('pc');

    if( !$url ){

      $url = kernel::single('b2c_frontpage')->gen_url(array('app'=>'b2c','ctl'=>'site_member','act'=>'index'));

    }

 

    //查詢資料庫

    $uname = $_POST['uname'];

    $sql = "select * from sdb_pam_members where password_account = '$uname' ";

    $res = mysqli_query($link, $sql);

    $row = mysqli_fetch_assoc($res);

    $member_id = $row['member_id'];

    //連結資料庫,插入資料

    $openid = $_COOKIE['openid'];

    $sql = "update sdb_trustlogin_trustinfo set member_id = ('{$member_id}') where openid = '{$openid}'";

    $res = mysqli_query($link, $sql);

 

    if($res && mysqli_affected_rows($link) > 0){

      //刪除cookie

      setcookie("openid", $openid, time()-3600);

      kernel::single('pam_lock')->flush_lock($member_id);

      $this->splash('success',$url,app::get('b2c')->_('登入成功'),true);

    }else{

      echo '失敗了';

    }

  }

處理繫結註冊頁面

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

public function weixin1(){

     //連結資料庫

    $link = mysqli_connect(DB_HOST, DB_USER,DB_PASSWORD,DB_NAME ) or die('資料庫連線失敗2');

    mysqli_set_charset($link,'utf8');

 

    //接收openid

    $openid = $_COOKIE['openid'];

 

    $sql = "select * from sdb_trustlogin_trustinfo where openid = '$openid' ";

    $res = mysqli_query($link, $sql);

    $row = mysqli_fetch_assoc($res);

 

    $this->pagedata['realname'] = $row['realname'];

    $this->pagedata['avatar'] = $row['avatar'];

    $this->page("site/passport/weixin1.html");

 

  }

繫結註冊頁面

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

public function handle1($url=null){

    //連結資料庫

    $link = mysqli_connect(DB_HOST, DB_USER,DB_PASSWORD,DB_NAME ) or die('資料庫連線失敗1');

    mysqli_set_charset($link,'utf8');

 

    $_POST = utils::_filter_input($_POST);

     

    $saveData = $this->userPassport->pre_signup_process($_POST);

 

    if( $member_id = $this->userPassport->save_members($saveData,$msg) ){

      $this->userObject->set_member_session($member_id);

      $this->bind_member($member_id);

      foreach(kernel::servicelist('b2c_save_post_om') as $object) {

        $object->set_arr($member_id, 'member');

        $refer_url = $object->get_arr($member_id, 'member');

      }

 

      /*註冊完成後做某些操作! begin*/

      foreach(kernel::servicelist('b2c_register_after') as $object) {

        $object->registerActive($member_id);

      }

      /*end*/

      $data['member_id'] = $member_id;

      $data['uname'] = $saveData['pam_account']['login_account'];

      $data['passwd'] = $_POST['pam_account']['psw_confirm'];

      $data['email'] = $_POST['contact']['email'];

      $data['refer_url'] = $refer_url ? $refer_url : '';

      $data['is_frontend'] = true;

      $obj_account=$this->app->model('member_account');

      $obj_account->fireEvent('register',$data,$member_id);

      if(!strpos($_SESSION['pc_next_page'],'cart')){

        $url = $this->gen_url(array('app'=>'b2c','ctl'=>'site_passport','act'=>'sign_tips'));

      }else{

        $url = $_SESSION['pc_next_page'];

      }

 

      //會員註冊成功,處理member_id 開始

      $login_name = $_POST["pam_account"]["login_name"];

      $sql = "select member_id from sdb_pam_members where password_account = '{$login_name}'";

      // echo $sql;

      $res = mysqli_query($link, $sql);

      $row = mysqli_fetch_assoc($res);

      $member_id = $row['member_id'];

 

      //連結資料庫,插入資料

      $openid = $_COOKIE['openid'];

      $sql = "update sdb_trustlogin_trustinfo set member_id = ('{$member_id}') where openid = '{$openid}'";

      $res = mysqli_query($link, $sql);

 

      if($res && mysqli_affected_rows($link) > 0){

 

         //刪除cookie

        setcookie("openid", $openid, time()-3600);

        $this->splash('success',$url,app::get('b2c')->_('註冊成功'),$ajax_request);

        }  

    //會員註冊成功,處理member_id 結束

      $this->splash('failed',$back_url,app::get('b2c')->_('註冊失敗'),$ajax_request);

    }

  }

臨時頁面

1

2

3

4

5

6

7

8

9

10

11

12

13

public function linshi(){

 

    $code = $_GET['code'];

    $state = $_GET['state'];

 

    if($state === 'STATE'){

      $this->loginWeixin($code);

    }elseif($state === 'wxBind'){

      $this->bindWeixin($code);

    }else{

      return redirect("http://www.sunmil.cn");

    }

  }

loginWeixin

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

private function loginWeixin($code){

 

    //連結資料庫

    $link = mysqli_connect('127.0.0.1', 'root','root', 'ec') or die('資料庫連線失敗');

    mysqli_set_charset($link,'utf8');

 

    $appid = $this->appid;

    $appsecert = $this->appsecert;

    $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecert."&code=".$code."&grant_type=authorization_code";

 

    //curl模擬get請求,獲取結果

    $res = $this->http_curl($url);

    //轉化為陣列

    $result = json_decode($res,true);

    $openid = $result['openid'];

 

    //openid存在,直接登入,openid不存在,先註冊再登入

    $sql = "select openid from sdb_trustlogin_trustinfo";

    $res = mysqli_query($link, $sql);

    $row = mysqli_fetch_all($res);

       

    //foreach 判斷

    foreach ($row As $v) {

      if(in_array($openid,$v,true)){

        //缺少登入資訊

 

        $sql = "select member_id from sdb_trustlogin_trustinfo where openid = '{$openid}'";

        //查出member_id

        $res = mysqli_query($link, $sql);

        $row = mysqli_fetch_assoc($res);

 

        $member_id = $row['member_id'];

        //查出會員資訊

        $sql = "select * from sdb_pam_members where member_id = '{$member_id}'";

        $res = mysqli_query($link, $sql);

        $row = mysqli_fetch_assoc($res);

 

        $login_name = $row['login_account'];

 

        //存入cookie

        setcookie('UNAME',$login_name, time()+360000);

        echo "<script>window.location.href='http://www.sunmil.cn'</script>";

        exit;

      }

    }

 

    $access_token = $result['access_token'];

    //獲取使用者基本資訊

    $getInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";

    $userInfo = $this->http_curl($getInfoUrl);

    $trustinfo = json_decode($userInfo);

 

    //判斷trustinfo表裡面是否存在該條資料

    $openid   $trustinfo->openid;

    $nickname  $trustinfo->nickname;

    $sex    $trustinfo->sex;

    $city    $trustinfo->city;

    $province  $trustinfo->province;

    $country  $trustinfo->country;

    $avatar   $trustinfo->headimgurl;

    $trust_source = 'trustlogin_plugin_weixin';

    //連線資料庫,插入資料

    $sql = "insert into sdb_trustlogin_trustinfo (openid,realname,avatar,gender,province,city,trust_source) values ('{$openid}','{$nickname}','{$avatar}','{$sex}','{$province}','{$city}','{$trust_source}')";

    $res = mysqli_query($link, $sql);

 

    if($res && mysqli_affected_rows($link) > 0){

       //存入cookie

      setcookie('openid',$openid, time()+3600);

      header("Location:http://www.sunmil.cn/passport-weixin.html");

    }else{

      echo '失敗了';

    

  }

curl模擬get請求

1

2

3

4

5

6

7

8

9

10

11

12

private function http_curl($url){

    $curlobj = curl_init();

    curl_setopt($curlobj, CURLOPT_URL, $url);

    curl_setopt($curlobj, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($curlobj, CURLOPT_SSL_VERIFYPEER, FALSE);

    curl_setopt($curlobj, CURLOPT_SSL_VERIFYHOST, FALSE);

 

    $output = curl_exec($curlobj);

    curl_close($curlobj);

 

    return $output;

  }

處理微信登入結束

就可以實現微信登入了,當然了,一個簡單熟悉微信第三登入的demo,有機會用tp框架分裝完善一個完整的微信第三方登入。