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

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 15:07   44   0

控制器:

/**
 * 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 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP