网上有很多关于YII2.0微信开发教程,但是太过复杂凌乱,所以今天在这里给大家整理总结利用Yii2微信后台开发的系列了,给需要的小伙伴们参考。
一:接入微信
Yii2后台配置
1.在app/config/params.php中配置token参数
return [ //微信接入 'wechat' =>[ 'token' => 'your token', ], ];
2.在app/config/main.php中配置路由
因为接口模块使用的RESTful API,所以需要定义路由规则。
'urlManager' => [ 'enablePrettyUrl' => true, 'enableStrictParsing' => true, 'showScriptName' => false, 'rules' => [ [ 'class' => 'yii\rest\UrlRule', 'controller' => 'wechat', 'extraPatterns' => [ 'GET valid' => 'valid', ], ], ], ],
3.在app/controllers中新建WechatController
<?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; } } } }
微信公众号后台配置
在微信公众号后台配置URL和Token,然后提交验证即可。
URL: Token:your token
二:获取用户信息
用户表设计
复制代码 代码如下:
CREATE TABLE `wechat_user` (
`id` int(11) NOT NULL,
`openid` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`nickname` varchar(50) COLLATE utf8_unicode_ci NOT NULL COMMENT '微信昵称',
`sex` tinyint(4) NOT NULL COMMENT '性别',
`headimgurl` varchar(255) COLLATE utf8_unicode_ci NOT NULL COMMENT '头像',
`country` varchar(50) COLLATE utf8_unicode_ci NOT NULL COMMENT '国家',
`province` varchar(50) COLLATE utf8_unicode_ci NOT NULL COMMENT '省份',
`city` varchar(50) COLLATE utf8_unicode_ci NOT NULL COMMENT '城市',
`access_token` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`refresh_token` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
ALTER TABLE `wechat_user`
ADD PRIMARY KEY (`id`);
获取用户信息的相关接口
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.从微信获取用户资料
复制代码 代码如下: