微信登录

//创建菜单  这是一个二级菜单(一级菜单最多为3个,二级菜单最多5个)

//在微信公共平台配置自己的线上域名

public $enableCsrfValidation = false;//必须加这句,否则报错


public function actionMenu(){
$config = [
'app_id' => 'wxa025f2544f08189a',
'secret' => 'd287ddceae3801fbc246c9dddd95f1d3',
'token'=>'zhangjinchai',
'response_type' => 'array',
];
$app=Factory::officialAccount($config);
$buttons = [
[
"name" => "组长团队",
"sub_button" => [
[
"type" => "view",
"name" => "登录",
"url" => "http://106.12.144.217/advanced/frontend/web/index.php?r=month/send" //跳转send方法
],
[
"type" => "view",
"name" => "二组",
"url" => "http://v.qq.com/"
],
[
"type" => "click",
"name" => "三组",
"key" => "V1001_GOOD"
],
[
"type" => "click",
"name" => "四组",
"key" => "V1001_GOOD"
],
[
"type" => "click",
"name" => "五组",
"key" => "V1001_GOOD"
],
],
],
[
"name" => "班委团队",
"sub_button" => [
[
"type" => "view",
"name" => "班长",
"url" => "http://www.soso.com/"
],
[
"type" => "view",
"name" => "书记",
"url" => "http://v.qq.com/"
],
[
"type" => "click",
"name" => "学委",
"key" => "V1001_GOOD"
],
[
"type" => "click",
"name" => "纪委",
"key" => "V1001_GOOD"
],
[
"type" => "click",
"name" => "生委",
"key" => "V1001_GOOD"
]
],
],
[
"type" => "click",
"name" => "优秀学员",
"key" => "V1001_TODAY_MUSIC"
],
];
$app->menu->create($buttons);
}


public function actionSend(){
$config = [
'app_id' => 'wxa025f2544f08189a',
'secret' => 'd287ddceae3801fbc246c9dddd95f1d3',
'token'=>'zhangjinchai',
'aes_key'=>'BYsaHlHQBvZdMOOLmpY8Tq6jeiZjHAmX2N0XGaY5scX',
'response_type' => 'array',
];(此配置信息为个人微信平台上的配置信息。请按需要填写)
$app=Factory::officialAccount($config);
$response = $app->oauth->scopes(['snsapi_userinfo'])
->redirect("http://106.12.144.217/advanced/frontend/web/index.php?r=month/getuser");//跳转个人用户信息,获取用户信息
$response->send();

//获取用户信息

public function actionGetuser(){
$user=$this->getCacheData('weixin_user');//若redis缓存中有相关数据,取出即可
if(!$user){//如没有,获取用户信息
$config = [
'app_id' => 'wxa025f2544f08189a',
'secret' => 'd287ddceae3801fbc246c9dddd95f1d3',
'token'=>'zhangjinchai',
'response_type' => 'array',
];
$app=Factory::officialAccount($config);
$user=$app->oauth->user()->toJSON();//获取到的数据为json格式,请按需要转换
$this->setCacheData('weixin_user',$user,7200);//存入redis
}
$last=json_decode($user,true);
print_r($last);//实现登录 打印用户数据
}

}


///数据存入缓存    
public function setCacheData($key,$value,$time=3600)
{
$cache = Yii::$app->cache;
if (is_array($value)){
$value = json_encode($value);
}
$cache->set($key,$value,$time);
}
//缓存取出数据
public function getCacheData($key,$is_array=false)
{
$cache = Yii::$app->cache;
$returnData = $cache->get($key);
if ($is_array){
$returnData = json_decode($returnData,true);
}
return $returnData;
}

转载于:https://www.cnblogs.com/chaihtml/p/10254955.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值