• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
钗的php生涯
博客园    首页    新随笔    联系   管理    订阅  订阅
微信登录

//创建菜单  这是一个二级菜单(一级菜单最多为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;
}
posted on 2019-01-11 14:25  php钗  阅读(587)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3