<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ? **不需要在網上找什么亂七八糟的東西,直接來之即用豈不是很完美。** 只需要有一臺服務器即可,沒備案都可以玩這個功能。不需要擁有服務號,看完全文你就明白了。 數據庫篇: ~~~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的命名空間 http://www.hmoore.net/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")?這個代碼還不錯。 ?
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看