<?php
namespace Business\User;
/**
 * User: liuyuzhen
 * Date: 2018/5/24
 * Time: 15:32
 * Description:
 */
class ThirdplatformServiceModel extends \Business\AbstractModel{

    public function init(){

    }

    public function getAuthInfo($data){
        if($data['paltformType']){
            if($data['paltformType']==\Our\ApiConst::userFromWeibo){
                return array(
                    'appId' => \Our\SecretKeys::weiboAppId,
                    'appSecret' => \Our\SecretKeys::weiboAppSecret
                );
            }else if($data['paltformType']==\Our\ApiConst::userFromQQ){
                return array(
                    'appId' => \Our\SecretKeys::qqAppId,
                    'appSecret' => \Our\SecretKeys::qqAppSecret
                );
            }else if($data['paltformType']==\Our\ApiConst::userFromWx){
                return array(
                    'appId' => \Our\SecretKeys::wxAppId,
                    'appSecret' => \Our\SecretKeys::wxAppSecret
                );
            }
        }
        \Error\ErrorModel::throwException(\Error\CodeConfigModel::emptyPlatformType);
    }


    public function wechatAppAuth($where){
        if(!$where['code']){
            \Error\ErrorModel::throwException(\Error\CodeConfigModel::illegalAccess);
        }
        $resource = \Our\ArrayConst::wechatInfo;
        $url = \Our\NameConst::wechatAuthUrl;
        foreach($resource as $key=>$value){
            $url .= $key.'='.$value.'&';
        }
        $url .= 'js_code='.$where['code'];
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        $data = curl_exec($curl);
        curl_close($curl);
        $data = json_decode($data,true);
        if($data['openid']){
            $authInfo['openid'] = $data['openid'];
            $authInfo['session_key'] = $data['session_key'];
            $sess = \Yaf\Session::getInstance();
            $sess['wxAuthInfo'] = $authInfo;
            return array('status'=>true);
        }
        return array('status'=>false,'msg'=>$data['errmsg']);
    }

    /**
     */
    private static $_instance = null;

    /**
     * 单例模式获取类实例
     *
     */
    public static function getInstance() {
        if (!(self::$_instance instanceof self)) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }


}
