
小程序可以通过微信官方提供的登录能力方便地获取微信提供的用户身份标识,快速建立小程序内的用户体系。
参考链接:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html
之后开发者服务器可以根据用户标识来生成自定义登录态,用于后续业务逻辑中前后端交互时识别用户身份。
session_key
是对用户数据进行 加密签名 的密钥。为了应用自身的数据安全,开发者服务器不应该把会话密钥下发到小程序,也不应该对外提供这个密钥。
下面是微信小程序登录的服务端代码实例,
但是注意的是 最新小程序无法获取用户头像和昵称,具体查看官网接口调整:https://developers.weixin.qq.com/community/develop/doc/00022c683e8a80b29bed2142b56c01
<?php
/*
* +----------------------------------------------------------------------
* | wangqy
* +----------------------------------------------------------------------
* | Copyright (c) 2023 http://upwqy.com All rights reserved.
* +----------------------------------------------------------------------
* | Author: wangqy <529857614@qq.com>
* +----------------------------------------------------------------------
*/
namespace common\service\wechat\applet;
use think\exception\ValidateException;
use wxapp\aes\WXBizDataCrypt;
class AuthService
{
public const URL = 'https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code';
protected $appId;
protected $appSecret;
protected $jsCode;
protected $iv;
protected $encryptedData;
protected $openId;
protected $sessionKey;
protected $unionId;
protected $gender;
protected $nickname;
protected $avatar;
/**
* @param mixed $appId
*/
public function setAppId($appId): void
{
$this->appId = $appId;
}
/**
* @param mixed $appSecret
*/
public function setAppSecret($appSecret): void
{
$this->appSecret = $appSecret;
}
/**
* @param mixed $jsCode
*/
public function setJsCode($jsCode): void
{
$this->jsCode = $jsCode;
}
/**
* @param mixed $iv
*/
public function setIv($iv): void
{
$this->iv = $iv;
}
/**
* @param mixed $encryptedData
*/
public function setEncryptedData($encryptedData): void
{
$this->encryptedData = $encryptedData;
}
/**
* @return mixed
*/
public function getOpenId()
{
return $this->openId;
}
/**
* @return mixed
*/
public function getSessionKey()
{
return $this->sessionKey;
}
/**
* @return mixed
*/
public function getUnionId()
{
return $this->unionId;
}
/**
* @return mixed
*/
public function getGender()
{
return $this->gender;
}
/**
* @return mixed
*/
public function getNickname()
{
return $this->nickname;
}
/**
* @return mixed
*/
public function getAvatar()
{
return $this->avatar;
}
public function jscode2session()
{
$url = sprintf(self::URL, $this->appId, $this->appSecret, $this->jsCode);
$result = curl_get($url);
$result = json_decode($result, true);
if (!empty($result['errcode'])) {
throw new ValidateException('操作失败!errcode:'.$result['errcode']);
}
$this->openId = $result['openid'];
$this->sessionKey = $result['session_key'];
}
// 解密数据
public function decryptData()
{
$wxUserData = [];
$pc = new WXBizDataCrypt($this->appId, $this->sessionKey);
$errCode = $pc->decryptData($this->encryptedData, $this->iv, $wxUserData);
if (0 != $errCode) {
throw new ValidateException('操作失败!errcode:'.$errCode);
}
$this->unionId = $wxUserData['unionId'] ?? null;
$this->gender = $wxUserData['gender'];
$this->nickname = $wxUserData['nickName'];
$this->avatar = $wxUserData['avatarUrl'];
return $wxUserData;
}
}
$wxAppletService = new AuthService();
$wxAppletService->setAppId($this->appId);
$wxAppletService->setAppSecret($this->appSecret);
$wxAppletService->setJsCode($jsCode);
$wxAppletService->setIv($iv);
$wxAppletService->setEncryptedData($encryptedData);
$wxAppletService->jscode2session();
$wxData = $wxAppletService->decryptData();
$wxAppletService->getAvatar()
微信解密类
gitee: https://gitee.com/wangqy415/wxbiz-data-crypt/tree/master/wxapp/aes
百度网盘:https://pan.baidu.com/s/1z28Q4yUnQMUp9YFhuMwM6w?pwd=1d7s
发布时间 : 2023-02-28,阅读量:1262