<?php

/* ========================================================================
 * $Id: login.third.php 2624 2019-08-13 09:56:30Z onez $
 * 
 * Email: www@onez.cn
 * QQ: 6200103
 * HomePage: http://www.onezphp.com
 * ========================================================================
 * Copyright 2016-2017 佳蓝科技.
 * 
 * ======================================================================== */

!defined('IN_ONEZ') && exit('Access Denied');
#名称：第三方登录
#标识：login.third

class onezphp_login_third extends onezphp{
  function __construct(){
    
  }
  function init($option){
    global $G;
    if(!$option['third_name'] || !$option['third_token'] || !$option['nickname']){
      return;
    }
    $T=$G['this']->data()->open('login.third')->one("token='$option[third_name]' and key1='$option[third_token]'");
    if(!$T){
      $T=$option;
      $T['token']=$option['third_name'];
      $T['key1']=$option['third_token'];
      $T['id']=$G['this']->data()->open('login.third')->insert($T);
    }else{
      $G['this']->data()->open('login.third')->update($option,"id='$T[id]'");
      $T=$G['this']->data()->open('login.third')->one("token='$option[third_name]' and key1='$option[third_token]'");
    }
    onez('scookie')->call('login_third',$T['id']);
    $G['login.third']=$T;
    $this->reset();
    if($G['userid']){
      $this->bind($G['userid'],$ltid);
      return 'bind';
    }
    if($T['userid']){
      $G['userid']=$T['userid'];
      return 'login';
    }
    if($T['index1']){
      $G['userid']=$T['index1'];
      return 'login';
    }
  }
  function bind($userid,$ltid=false){
    global $G;
    !$ltid && $ltid=(int)onez('scookie')->call('login_third');
    !$ltid && $ltid=$G['login.third']['id'];
    if($ltid){
      $G['login.third']['index1']=$userid;
      $G['login.third']['userid']=$userid;
      $G['this']->data()->open('login.third')->update(array(
        'index1'=>$userid,
        'userid'=>$userid,
      ),"id='$ltid'");
      onez('scookie')->call('login_third','');
    }
  }
  function update($arr){
    global $G;
    $arr && $G['this']->data()->open('login.third')->update($arr,"id='{$G['login.third']['id']}'");
  }
  function info(){
    global $G;
    $ltid=(int)onez('scookie')->call('login_third');
    if($ltid){
      $G['login.third']=$G['this']->data()->open('login.third')->one("id='$ltid'");
      $this->reset();
    }
    return $G['login.third'];
  }
  function reset(){
    global $G;
    if($G['login.third']){
      if(!$G['login.third']['name']){
        if($G['login.third']['third_name']=='qq'){
          $G['login.third']['name']='QQ';
        }elseif($G['login.third']['third_name']=='weixin'){
          $G['login.third']['name']='微信';
        }
      }
    }
  }
  function check($name,$xxx){
    global $G;
    $T=$G['this']->data()->open('login.third')->one("token='$name'$xxx");
    if($T){
      $G['login.third']=$T;
      $this->reset();
      onez('scookie')->call('login_third',$T['id']);
    }
    return $T;
  }
}