canyin-project/ybcy/controllers/index/WechatController.php
2024-11-01 16:07:54 +08:00

123 lines
3.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace api\controllers;
use Yii;
use yii\rest\ActiveController;
class WechatController extends ActiveController
{
public $modelClass = '';
public function actionValid()
{
$echoStr = $_GET["echostr"];
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
//valid signature , option
if($this->checkSignature($signature,$timestamp,$nonce)){
echo $echoStr;
}
}
private function checkSignature($signature,$timestamp,$nonce)
{
// you must define TOKEN by yourself
$token = Yii::$app->params['wechat']['token'];
if (!$token) {
echo 'TOKEN is not defined!';
} else {
$tmpArr = array($token, $timestamp, $nonce);
// use SORT_STRING rule
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
//1.用户授权接口获取access_token、openid等获取并保存用户资料到数据库
public function actionAccesstoken()
{
$code = $_GET["code"];
$state = $_GET["state"];
$appid = Yii::$app->params['wechat']['appid'];
$appsecret = Yii::$app->params['wechat']['appsecret'];
$request_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
//初始化一个curl会话
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
$result = $this->response($result);
//获取token和openid成功数据解析
$access_token = $result['access_token'];
$refresh_token = $result['refresh_token'];
$openid = $result['openid'];
//请求微信接口,获取用户信息
$userInfo = $this->getUserInfo($access_token,$openid);
$user_check = WechatUser::find()->where(['openid'=>$openid])->one();
if ($user_check) {
//更新用户资料
} else {
//保存用户资料
}
//前端网页的重定向
if ($openid) {
return $this->redirect($state.$openid);
} else {
return $this->redirect($state);
}
}
//2.从微信获取用户资料
public function getUserInfo($access_token,$openid)
{
$request_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';
//初始化一个curl会话
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
$result = $this->response($result);
return $result;
}
//3.获取用户资料接口
public function actionUserinfo()
{
if(isset($_REQUEST["openid"])){
$openid = $_REQUEST["openid"];
$user = WechatUser::find()->where(['openid'=>$openid])->one();
if ($user) {
$result['error'] = 0;
$result['msg'] = '获取成功';
$result['user'] = $user;
} else {
$result['error'] = 1;
$result['msg'] = '没有该用户';
}
} else {
$result['error'] = 1;
$result['msg'] = 'openid为空';
}
return $result;
}
}