Laravel微信授权接口之小程序获取openid与session_key

控制器:

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/8/16
 * Time: 7:36
 */

namespace App\Http\Controllers\Grant;

use App\Http\Controllers\Controller;
use App\Services\GrantService;
use App\Services\InfoService;
use Illuminate\Http\Request;

class GrantController  extends Controller
{

    public $WeChat;


    public function __construct()
    {
        $this->WeChat = new GrantService();
    }

    /**
     * 微信授权接口
     */
    public function index(Request $request){

        $code = $request->get('code');

        $UserInfo = $this->WeChat->getUserOpenId($code);

        return $UserInfo;

    }
}

Service:

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/10
 * Time: 9:56
 */

namespace App\Services;


use function AlibabaCloud\Client\json;
use App\Models\DrpUser;

class GrantService
{
    public $appid;
    public $secret;
    public $wxdecrypt;

    public function __construct()
    {
        $this->appid = env('APPID');
        $this->secret = env('APPSECRET');
        $this->wxdecrypt = new WXBizDataCryptService();
    }

    /**
     * 获取openid
     * @param string $code
     * @return mixed
     */
    public  function  getUserOpenId($code = '')
    {
        $url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $this->appid . '&secret=' . $this->secret . '&js_code=' . $code . '&grant_type=authorization_code';

        $result = curlGet($url);

        $info=json_decode($result,true);

        $openid['openid'] = array_get($info, 'openid');

        $judge = DrpUser::where('openid',$openid['openid'])->first();

        if(!empty($openid['openid'])){

            if(empty($judge)){

                DrpUser::insert($openid);

            }

        }

        return json_decode($result, true);
    }

}

Service中__construct()方法的检验数据的真实性的(类)

<?php
/**
 * 微信数据解密
 * Date: 2019/5/10
 * Author: Mr.Z
 */

namespace App\Services;


class WXBizDataCryptService
{
    private $OK = 0;
    private $IllegalAesKey = -41001;
    private $IllegalIv = -41002;
    private $IllegalBuffer = -41003;

    public function __construct()
    {
        $this->appid  = env('APPID');
    }

    /**
     * 检验数据的真实性,并且获取解密后的明文.
     * @param $encryptedData string 加密的用户数据
     * @param $iv string 与用户数据一同返回的初始向量
     * @param $data string 解密后的原文
     *
     * @return int 成功0,失败返回对应的错误码
     */
    public function decryptData($appid, $sessionKey, $encryptedData, $iv, &$data)
    {
        if (strlen($sessionKey) != 24) {
            return $this->IllegalAesKey;
        }
        $aesKey = base64_decode($sessionKey);
        if (strlen($iv) != 24) {
            return $this->IllegalIv;
        }
        $aesIV = base64_decode($iv);
        $aesCipher = base64_decode($encryptedData);
        $result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV);
        $dataObj = json_decode($result);
        if ($dataObj == NULL) {
            return $this->IllegalBuffer;
        }
        if ($dataObj->watermark->appid != $appid) {
            return $this->IllegalBuffer;
        }
        $data = $result;
        return $this->OK;
    }
}

 

 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值