极光登录··

//原生
use app\common\model\JiguangSend;
use Ws\Http\Request;

public function verify()
{
        $post     = $this->post();
        $headers  = array('Content-Type' => 'application/json', 'Authorization' => 'Basic MzZmYWQ5ZDAxNzRkNGU0YWE3NDhjNTczOjU2Yzg1ZmY1OGZkZjZhMGJmZTBkN2NkNA==');
        $data     = array('loginToken' => $post['loginToken'], 'exID' => '');
        $body     = \Ws\Http\Request\Body::json($data);
        $http     = Request::create();
        $response = $http->post('https://api.verification.jpush.cn/v1/web/loginTokenVerify', $headers, $body);
        if ($response->body->code == 8000) {
            $phone_md = $response->body->phone;
            // 解密手机号
            $prefix    = '-----BEGIN RSA PRIVATE KEY-----';
            $suffix    = '-----END RSA PRIVATE KEY-----';
            $result    = '';
            $encrypted = $response->body->phone;
            $prikey    = config('jiguang.prikey');
            $key       = $prefix . "\n" . $prikey . "\n" . $suffix;
            $r         = openssl_private_decrypt(base64_decode($encrypted), $result, openssl_pkey_get_private($key));
            $user      = User::where('phone', $result)->where('cancel', '<>', 2)->find();
            if (!is_null($user)) {
                $this->log_suc($user, '一键登陆成功');
            } else {
                $user = User::create([
                    'phone'    => $result,
                    'pwd'      => '',
                    'nick'     => 'z_' . mt_rand(100000, 999999),
                    'head'     => '',
                    'vip'      => 0,
                    'disabled' => 0,
                    'pass'     => '',
                ]);
                $this->log_suc($user, '一键登陆成功');
            }
        }
        returnAPI($response->body->content);
    }
//封装
<?php
namespace Jiguang;

class jiguang {
	protected $appkey= '';
	protected $appsecret= '';
	protected $rsakey = '';
	public function __construct($config){
		$this->appkey= $config['appkey'];
		$this->appsecret= $config['appsecret'];
		$this->rsakey = $config['rsakey'];
	}
    public function getdecode($loginToken){
        $data = [
            'loginToken'    =>  $loginToken,
            'exID'          =>  'asdasdASasd'
        ];
        
        $Authorization = 'Basic '.base64_encode($this->appkey.':'.$this->appsecret);
        
        $url = 'https://api.verification.jpush.cn/v1/web/loginTokenVerify';
        
        $headerArray =array("Content-type:application/json;charset='utf-8'","Accept:application/json",'Authorization:'.$Authorization);
        $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_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return json_decode($output, true);
    }
    
    public function decodeRsa($phone){
        $result = '';
        
        openssl_private_decrypt(base64_decode($phone), $result, $this->rsakey);
        
        return $result;
    }
}
//使用
    /**
     * @api {get}  api/index/jiguangLogin
     * @apiName 本机号码一键登录
     * @apiGroup    注册登录
     * @apiParam {string}   loginToken
     */
    public function jiguangLogin($loginToken, UserModel $userModel)
    {
        $config = config('jiguang.default');
        $jiguang = new jiguang($config);
        $ras = $jiguang->getdecode($loginToken);
        if ($ras['code'] != 8000) {
            return outJson('', 0, '授权失败');
        }

        $mobile = $jiguang->decodeRsa($ras['phone']);
        if (!$mobile) {
            return outJson('', 0, '获取手机号码失败');
        }

        $res = $userModel->dologin(['mobile', $mobile]);
        if ($res['status'] != 1) {
            return outJson($res['return'], 0, $res['msg']);
        }

        return outJson($res['data'], 1, '登录成功');
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值