企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
​ **不需要在网上找什么乱七八糟的东西,直接来之即用岂不是很完美。** 只需要有一台服务器即可,没备案都可以玩这个功能。不需要拥有服务号,看完全文你就明白了。 数据库篇: ~~~sql -- Adminer 4.6.3 MySQL dump SET NAMES utf8; SET time_zone = '+00:00'; SET foreign_key_checks = 0; SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO'; DROP TABLE IF EXISTS `yy_wxuser`; CREATE TABLE `yy_wxuser` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID', `nickname` varchar(255) DEFAULT '' COMMENT '昵称', `openid` varchar(255) DEFAULT '' COMMENT 'openid', `avatar` varchar(255) DEFAULT '' COMMENT '头像', `gender` tinyint(1) unsigned DEFAULT '1' COMMENT '性别', `province` varchar(20) DEFAULT '0' COMMENT '省', `city` varchar(20) DEFAULT '0' COMMENT '市', `county` varchar(20) DEFAULT '0' COMMENT '县', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='用户表'; -- 2019-04-20 03:13:42 ~~~ ![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动") 基本库,用于调起授权授权获取用户信息 Wxgetuserinfo.php ~~~php <?php /** * Created by PhpStorm. * User: wudi * Date: 2019/1/28 * Time: 10:31 */ namespace app\card\controller; use think\Db; class Wxgetuserinfo { /** * 调用方法 * 具体参考tp5的命名空间 https://www.kancloud.cn/manual/thinkphp5/118014 * $class = new \app\card\controller\Wxgetuserinfo(); * $class->index(); */ /** * 1、获取用户信息 */ public function delsession(){ session('userinfo', null); session(null); } public function index(){ $appid = config('appid'); $secret = config('secret'); if(!session('?userinfo')){ if (!isset($_GET['code'])){//没有code,去微信接口获取code码 $request = request(); $callback = $request->url(true);//微信服务器回调url,这里是本页url // $this->get_code($callback); $redirect_uri=urlencode($callback); $url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect'; header("location:$url");exit(); } else {//获取code后跳转回来到这里了 $code = $_GET['code']; $data = $this->get_access_token($code);//获取网页授权access_token和用户openid $data_all = $this->get_user_info($data['access_token'],$data['openid']);//获取微信用户信息 session('userinfo',$data_all); $this->checkuser($data_all['openid'],$data_all); return json($data_all); } }else{ $ret=session('userinfo'); return json($ret); //返回的获取到的微信用户信息 } } /** * 3、使用code换取access_token * @param string 用于换取access_token的code,微信提供 * @return array access_token和用户openid数组 */ private function get_access_token($code){ $appid = config('appid'); $appsecret = config('secret'); $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $appid . '&secret=' . $appsecret . '&code=' . $code . '&grant_type=authorization_code'; $user = json_decode(file_get_contents($url)); if (isset($user->errcode)) { echo 'error:' . $user->errcode.'<hr>msg :' . $user->errmsg;exit; } $data = json_decode(json_encode($user),true);//返回的json数组转换成array数组 return $data; } /** * 4、使用access_token获取用户信息 * @param string access_token * @param string 用户的openid * @return array 用户信息数组 */ private function get_user_info($access_token,$openid){ $url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token . '&openid=' . $openid . '&lang=zh_CN'; $user = json_decode(file_get_contents($url)); if (isset($user->errcode)) { // echo 'error:' . $user->errcode.'<hr>msg :' . $user->errmsg;exit; } $data = json_decode(json_encode($user),true);//返回的json数组转换成array数组 return $data; } /** * 检查用户信息 * TODO 在这个地方如果在用户表加一个时间字段,就可以每隔多少次对数据库进行一次操作,而不是一直更新操作。 */ public function checkuser($openid,$data){ $check=Db::name('wxuser')->where(array('openid'=>$openid))->find(); if($check){ /*更新用户信息*/ $updata=array(); $updata['avatar']=$data['headimgurl']; $updata['createtime']=time(); $updata['nickname']=$data['nickname']; Db::name('wxuser')->where('openid',$openid)->update($updata); /*获取最新用户信息并存入缓存*/ $newinfo=Db::name('wxuser')->where(array('id'=>$check['id']))->find(); session('userinfo',$newinfo); }else{ /*新增用户信息*/ $intdata=array(); $intdata = ['nickname' =>$data['nickname'], 'avatar' => $data['headimgurl'],'createtime'=>time(),'openid' => $data['openid'],'gender' => $data['sex'],'city' => $data['city'],'province' => $data['province'],'county' => $data['country']]; Db::name('wxuser')->insert($intdata); $userId = Db::name('wxuser')->getLastInsID(); /*获取最新用户信息并存入缓存*/ if(!session('?userinfo')){ $newinfo=Db::name('wxuser')->where(array('id'=>$userId))->find(); // $newinfo=Db::name('wxuser')->where('id',$userId)->find();array('openid'=>$openid,'delstatus'=>1) session('userinfo',$newinfo); } } } /*↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓*/ /*检测是否关注公众号*/ /*subscribe 用户是否订阅该公众号标识,值为0时,代表此用户没有关注该公众号,拉取不到其余信息。*/ /*https://www.cnblogs.com/mracale/p/9318349.html*/ public function checkisgz(){ $request = request(); $callback = $request->url(true);//微信服务器回调url,这里是本页url $appid = config('appid'); $secret = config('secret'); //微信网页授权获取openid $web_url=$callback; if (!isset($_GET['code'])) { $redirect_uri=urlencode($web_url); $url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.'&redirect_uri='.$redirect_uri.'&response_type=code&scope=snsapi_base&state=1#wechat_redirect'; header("location:$url");exit(); } $code=trim($_GET['code']); $url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code'; $access=file_get_contents($url); $data=json_decode($access,true); $access_token=$data['access_token']; $url='https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid=OPENID&lang=zh_CN'; $user=file_get_contents($url); $arr=json_decode($user,true); //获取用户的openid $openid=$arr['openid']; $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret; $access=file_get_contents($url); $access_arr=json_decode($access,true); //非网页的access_token $access_token=$access_arr['access_token']; $url="https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$openid."&lang=zh_CN"; $res=file_get_contents($url); // var_dump($res); return $res['subscribe']; } } ~~~ ![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动") 中间库:Basesdns.php ~~~php <?php /** * Created by PhpStorm. * User: wudi * Date: 2019/1/28 * Time: 14:37 */ namespace app\card\controller; use think\Controller; use think\Db; use think\Request; class Basesdns extends Controller { /** * 检测是否授权登录 * 初始化方法,可以控制用户权限、获取菜单等等,只要是继承base类的其它业务类就不需要再重写 */ public function _initialize() { parent::_initialize(); $class = new \app\card\controller\Wxgetuserinfo(); $class->index(); $userinfo=session('userinfo'); if(!$userinfo){ $class = new \app\card\controller\Wxgetuserinfo(); $class->index(); } $this->userinfo=session('userinfo'); } } ~~~ ![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动") 应用库,写日常逻辑的,需要继承中间库,个人认为前面所有的缓存只需要存一个openid就可以了。 Index.php ~~~php <?php namespace app\card\controller; use think\Db; use think\Request; class Index extends Basesdns { /*首页*/ public function index() { $userinfo=session('userinfo'); $userinfo=Db::name('wxuser')->where('openid',$userinfo['openid'])->find(); session('userinfo',$userinfo); $this->assign('userinfo',$userinfo); return $this->fetch(); } } ~~~ ![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动") ### config.php 用来配置 微信的appid跟secret。 ![2019-04-20T03:28:00.png](https://imgconvert.csdnimg.cn/aHR0cDovL2Nzc25iLmNvbS91c3IvdXBsb2Fkcy8yMDE5LzA0LzI5Nzc4NTU4NjcucG5n?x-oss-process=image/format,png)![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动")​编辑 另外需要注意的是,公众号必须要加上对应服务器的ip白名单,以及网页授权回调域名配置。 这里再说一个很多人不知道的小消息,就是即使你没有服务号,也可以完成这个代码测试,只要你有一台服务器就可以了,没备案都没关系,那就是微信测试号。 注册微信测试号的地址是: [http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login](http://cssnb.com/go/aHR0cDovL21wLndlaXhpbi5xcS5jb20vZGVidWcvY2dpLWJpbi9zYW5kYm94P3Q9c2FuZGJveC9sb2dpbg== "http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login") ![2019-04-20T03:33:02.png](https://imgconvert.csdnimg.cn/aHR0cDovL2Nzc25iLmNvbS91c3IvdXBsb2Fkcy8yMDE5LzA0LzI3MjcyNDM4MjEucG5n?x-oss-process=image/format,png)![](data:image/gif;base64,R0lGODlhAQABAPABAP///wAAACH5BAEKAAAALAAAAAABAAEAAAICRAEAOw== "点击并拖拽以移动")​编辑 推进观看的链接:[http://www.cnblogs.com/hui9527/p/8473982.html](http://cssnb.com/go/aHR0cDovL3d3dy5jbmJsb2dzLmNvbS9odWk5NTI3L3AvODQ3Mzk4Mi5odG1s "http://www.cnblogs.com/hui9527/p/8473982.html") 这个代码还不错。 ​