├── .gitignore ├── LICENSE ├── README.md ├── config └── wx_library.php ├── libraries └── Wx_library.php └── third_party └── weixin ├── Wx.php ├── WxCache.php ├── WxConfig.php ├── WxException.php ├── WxJsApi.php ├── WxOAuth.php ├── WxUrl.php ├── WxUser.php └── wx_user.sql /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by .ignore support plugin (hsz.mobi) 2 | ### JetBrains template 3 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm 4 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 5 | 6 | # User-specific stuff: 7 | .idea/workspace.xml 8 | .idea/tasks.xml 9 | .idea/dictionaries 10 | .idea/vcs.xml 11 | .idea/jsLibraryMappings.xml 12 | 13 | # Sensitive or high-churn files: 14 | .idea/dataSources.ids 15 | .idea/dataSources.xml 16 | .idea/dataSources.local.xml 17 | .idea/sqlDataSources.xml 18 | .idea/dynamic.xml 19 | .idea/uiDesigner.xml 20 | 21 | # Gradle: 22 | .idea/gradle.xml 23 | .idea/libraries 24 | 25 | # Mongo Explorer plugin: 26 | .idea/mongoSettings.xml 27 | 28 | ## File-based project format: 29 | *.iws 30 | 31 | ## Plugin-specific files: 32 | 33 | # IntelliJ 34 | /out/ 35 | 36 | # mpeltonen/sbt-idea plugin 37 | .idea_modules/ 38 | 39 | # JIRA plugin 40 | atlassian-ide-plugin.xml 41 | 42 | # Crashlytics plugin (for Android Studio and IntelliJ) 43 | com_crashlytics_export_strings.xml 44 | crashlytics.properties 45 | crashlytics-build.properties 46 | fabric.properties 47 | 48 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Doma 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CodeIgniter-Weixin_Library 2 | 即插即用微信网页授权模块, 适用于[CodeIgniter](https://github.com/bcit-ci/CodeIgniter)框架。 3 | 4 | 开发测试环境 CI 3.0.6 5 | 6 | ## 起步 7 | 该模块为即插即用模块, 只需下载`release`即可开始使用 8 | 9 | ### 目录结构 10 | ``` 11 | CodeIgniter-Weixin_Library/ 12 | ├── config/ 13 | │ └── wx_library.php 14 | ├── libraries/ 15 | │ └── Wx_library.php 16 | └── third_party/ 17 | └── weixin/ 18 | └── ... 19 | ``` 20 | 21 | ### 安装 22 | 将项目中的: 23 | - `config/wx_library.php`文件放入你的项目中`application/config/`目录 24 | - `libraries/Wx_library.php`文件放入你的项目中`application/libraries/`目录 25 | - `third_party/weixin/`目录放入你的项目中`application/third_party/`目录 26 | 27 | 28 | ## 加载和使用 29 | 30 | ### 配置 31 | 修改`wx_library.php`配置文件, 填入所开发公众号的信息: 32 | ``` 33 | load->library()`来加载该库 49 | ``` 50 | // controller 51 | // (建议将别名设为`wx`) 52 | $this->load->library('Wx_library', null, 'wx') 53 | ``` 54 | 55 | ### API 56 | `Wx_library`提供2个API: 57 | ``` 58 | // 获取当前微信用户 59 | $this->wx->getWxUser(); 60 | 61 | // 返回微信 js sdk 配置选项 signPackage 62 | $this-wx->signPackage(); 63 | ``` 64 | 65 | ## Note 66 | 你的项目根目录需要有读写权限, `Wx_library`才能更好地工作。因为它需要在文件中缓存`access_token`和`jsapi_ticket`。 67 | 68 | ## License 69 | MIT License. Check the `LICENSE` file. -------------------------------------------------------------------------------- /config/wx_library.php: -------------------------------------------------------------------------------- 1 | config = $config; 21 | 22 | $this->CI = &get_instance(); 23 | $this->wx = new Wx($this->config); 24 | 25 | if (!$this->checkWxUserTable()) { 26 | show_error("Table `wx_user` doesn't exist."); 27 | } 28 | } 29 | 30 | /** 31 | * 检查 wx_user 表是否存在 不存在则尝试创建 32 | * @return bool 33 | */ 34 | protected function checkWxUserTable() 35 | { 36 | $db = $this->CI->db; 37 | if ($db->table_exists($this->config['wx_user_table_name'])) { 38 | return true; 39 | } 40 | 41 | return $this->createWxUserTable(); 42 | } 43 | 44 | /** 45 | * 创建 wx_user 表, 返回执行结果 46 | * @return bool 47 | */ 48 | protected function createWxUserTable() 49 | { 50 | 51 | $db = $this->CI->db; 52 | $table_name = $this->config['wx_user_table_name']; 53 | 54 | $sql = array( 55 | 'SET NAMES utf8;', 56 | 'SET FOREIGN_KEY_CHECKS = 0;', 57 | "DROP TABLE IF EXISTS `$table_name`;", 58 | 59 | "CREATE TABLE `$table_name` (" . 60 | '`wx_user_id` INT(11) NOT NULL AUTO_INCREMENT,' . 61 | '`openid` VARCHAR(32) NOT NULL,' . 62 | '`nickname` VARCHAR(32) DEFAULT NULL,' . 63 | '`sex` INT(2) DEFAULT NULL,' . 64 | '`province` VARCHAR(16) DEFAULT NULL,' . 65 | '`city` VARCHAR(16) DEFAULT NULL,' . 66 | '`country` VARCHAR(16) DEFAULT NULL,' . 67 | '`headimgurl` VARCHAR(255) DEFAULT NULL,' . 68 | '`unionid` VARCHAR(32) DEFAULT NULL,' . 69 | '`date_added` DATETIME NOT NULL,' . 70 | '`date_modified` DATETIME NOT NULL,' . 71 | 'PRIMARY KEY (`wx_user_id`),' . 72 | 'UNIQUE KEY `openid` (`openid`)' . 73 | ') ENGINE=MyISAM DEFAULT CHARSET=utf8;', 74 | 75 | 'SET FOREIGN_KEY_CHECKS = 1;', 76 | ); 77 | $result = true; 78 | 79 | foreach ($sql as $query) { 80 | $result = $result && $db->simple_query($query); 81 | } 82 | 83 | return $result; 84 | } 85 | 86 | /** 87 | * 微信授权登录 88 | * 将微信用户加入 wx_user 表, 89 | * wx_user_id 插入 session, 90 | * 跳回 return_url 91 | * @return bool 92 | * 登录成功返回 true, 否则返回 false 93 | */ 94 | protected function login() 95 | { 96 | try { 97 | return $this->wx->getUserInfo(true); 98 | } 99 | catch (WxException $e) { 100 | return null; 101 | } 102 | } 103 | 104 | /** 105 | * @return mixed|null|WxUser 106 | */ 107 | public function getWxUser() 108 | { 109 | if ($user = $this->loadWxUser()) { 110 | return $user; 111 | } 112 | 113 | if ($userInfo = $this->login()) { 114 | 115 | return $this->saveWxUser($userInfo); 116 | } 117 | 118 | return null; 119 | } 120 | 121 | 122 | /** 123 | * @return mixed|null 124 | */ 125 | protected function loadWxUser() 126 | { 127 | /** @var CI_Session $session */ 128 | $session = $this->CI->session; 129 | $key = $this->config['wx_user_session_key']; 130 | 131 | if (!$this->config['cache_wx_user']) { 132 | if ($data = $session->userdata("tmp_$key")) { 133 | $session->unset_userdata("tmp_$key"); 134 | 135 | return new WxUser($data); 136 | } 137 | 138 | return null; 139 | } 140 | 141 | if ($this->config['cache_type'] == 'session') { 142 | if ($data = $session->userdata($key)) { 143 | return new WxUser($data); 144 | } 145 | 146 | return null; 147 | } 148 | 149 | if ($this->config['cache_type'] == 'database') { 150 | $table_name = $this->config['wx_user_table_name']; 151 | $id = $session->userdata($key); 152 | $wx_user = $this->CI->db->get_where($table_name, array('wx_user_id' => $id)) 153 | ->row(0, 'WxUser'); 154 | 155 | return $wx_user; 156 | } 157 | 158 | return null; 159 | } 160 | 161 | protected function saveWxUser($userInfo) 162 | { 163 | 164 | $data = array( 165 | 'openid' => $userInfo['openid'], 166 | 'nickname' => $userInfo['nickname'], 167 | 'sex' => $userInfo['sex'], 168 | 'province' => $userInfo['province'], 169 | 'city' => $userInfo['city'], 170 | 'country' => $userInfo['country'], 171 | 'headimgurl' => $userInfo['headimgurl'], 172 | 'unionid' => $userInfo['unionid'], 173 | 'date_modified' => date('Y-m-d H:i:s'), 174 | ); 175 | $wx_user = new WxUser($data); 176 | 177 | /** @var CI_Session $session */ 178 | $session = $this->CI->session; 179 | $key = $this->config['wx_user_session_key']; 180 | 181 | if (!$this->config['cache_wx_user']) { 182 | $session->set_userdata("tmp_$key", $data); 183 | } else { 184 | if ($this->config['cache_type'] == 'session') { 185 | $session->set_userdata($key, $data); 186 | } elseif ($this->config['cache_type'] == 'database') { 187 | 188 | $table_name = $this->config['wx_user_table_name']; 189 | $stored_user = $this->CI->db->get_where($table_name, array('openid' => $wx_user->openid)) 190 | ->row(); 191 | 192 | if (!$stored_user) { 193 | $data['date_added'] = $data['date_modified']; 194 | $this->CI->db->insert($table_name, $data); 195 | $wx_user_id = $this->CI->db->insert_id(); 196 | } else { 197 | $this->CI->db 198 | ->where('wx_user_id', $stored_user->wx_user_id) 199 | ->update($table_name, $data); 200 | $wx_user_id = $stored_user->wx_user_id; 201 | } 202 | 203 | $session->set_userdata($key, $wx_user_id); 204 | 205 | } 206 | 207 | } 208 | $this->redirectCleanUrl(); 209 | 210 | return $wx_user; 211 | } 212 | 213 | /** 214 | * 获取 signPackage 215 | * 216 | * @param $url 217 | * 218 | * @return array 219 | * @throws WxException 220 | */ 221 | public function signPackage($url = '') 222 | { 223 | return $this->wx->getSignPackage($url); 224 | } 225 | 226 | /** 227 | * 登录成功后去除 url 中的 code 和 state 参数 228 | */ 229 | protected function redirectCleanUrl() 230 | { 231 | $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://"; 232 | $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; 233 | 234 | $clean = preg_replace('/[\?|&](code|state)=[^&]+/', '', $url); 235 | header("Location:$clean"); 236 | exit; 237 | } 238 | } -------------------------------------------------------------------------------- /third_party/weixin/Wx.php: -------------------------------------------------------------------------------- 1 | appid = isset($wx_config['appid']) ? $wx_config['appid'] : ''; 31 | $this->secret = isset($wx_config['secret']) ? $wx_config['secret'] : ''; 32 | $this->token = isset($wx_config['token']) ? $wx_config['token'] : ''; 33 | 34 | WxConfig::config($wx_config); 35 | WxJsApi::config($wx_config); 36 | 37 | } 38 | 39 | /** 40 | * 设置开发者模式,接入代码。 41 | */ 42 | public function check_signature() 43 | { 44 | if (empty($this->token)) { 45 | throw new WxException('user token is missing', 10000); 46 | } 47 | 48 | $signature = $_GET["signature"]; 49 | $timestamp = $_GET["timestamp"]; 50 | $nonce = $_GET["nonce"]; 51 | 52 | $data = array($this->token, $timestamp, $nonce); 53 | 54 | $result_str = self::sign($data); 55 | if ($result_str == $signature) { 56 | echo $_GET["echostr"]; 57 | } else { 58 | throw new WxException('check signature fail', 10001); 59 | } 60 | } 61 | 62 | /** 63 | * 微信授权登录 64 | * 65 | * @param bool $getFullInfo 66 | * 67 | * @return mixed 68 | * @throws WxException 69 | */ 70 | public function getUserInfo($getFullInfo = true) 71 | { 72 | 73 | return WxOAuth::getUserInfo($getFullInfo); 74 | } 75 | 76 | /** 77 | * @param string $url 要签名的页面 78 | * @param bool $debug 是否开启debug 79 | * 80 | * @return array 81 | */ 82 | public function getSignPackage($url = '', $debug = false) 83 | { 84 | return WxJsApi::getSignPackage($url, $debug); 85 | } 86 | 87 | public static function sign($data) 88 | { 89 | sort($data, SORT_STRING); 90 | $str = implode($data); 91 | 92 | return sha1($str); 93 | } 94 | } -------------------------------------------------------------------------------- /third_party/weixin/WxCache.php: -------------------------------------------------------------------------------- 1 | expire_time < time()) { 27 | return false; 28 | } 29 | 30 | return $data->jsapi_ticket; 31 | } 32 | 33 | /** 34 | * 全局缓存jsapi_ticket 35 | * 36 | * @param string $ticket 37 | */ 38 | public static function setJsApiTicket($ticket) 39 | { 40 | $data = array(); 41 | $data['expire_time'] = time() + 7000; 42 | $data['jsapi_ticket'] = $ticket; 43 | $fp = fopen(self::FILENAME_JSAPI_TICKET, "w"); 44 | fwrite($fp, json_encode($data)); 45 | fclose($fp); 46 | } 47 | 48 | /** 49 | * 读取缓存access_token 50 | * @return false|string 51 | */ 52 | public static function getAccessToken() 53 | { 54 | if (!file_exists(self::FILENAME_ACCESS_TOKEN)) { 55 | return false; 56 | } 57 | 58 | $data = json_decode(file_get_contents(self::FILENAME_ACCESS_TOKEN)); 59 | 60 | if ($data->expire_time < time()) { 61 | return false; 62 | } 63 | 64 | return $data->access_token; 65 | } 66 | 67 | /** 68 | * 全局缓存access_token 69 | * 70 | * @param string $access_token 71 | */ 72 | public static function setAccessToken($access_token) 73 | { 74 | $data = array(); 75 | $data['expire_time'] = time() + 7000; 76 | $data['access_token'] = $access_token; 77 | $fp = fopen(self::FILENAME_ACCESS_TOKEN, "w"); 78 | fwrite($fp, json_encode($data)); 79 | fclose($fp); 80 | } 81 | 82 | /** 83 | * 手动刷新 access_token 84 | */ 85 | public static function refreshAccessToken() 86 | { 87 | $url = WxUrl::urlAccessToken(WxConfig::$appid, WxConfig::$secret); 88 | $res = WxUrl::curlJSON($url); 89 | 90 | if (!property_exists($res, 'access_token')) { 91 | throw new WxException($res->errmsg, $res->errcode); 92 | } 93 | $access_token = $res->access_token; 94 | 95 | if ($access_token) { 96 | WxCache::setAccessToken($access_token); 97 | } 98 | 99 | return $access_token; 100 | } 101 | } -------------------------------------------------------------------------------- /third_party/weixin/WxConfig.php: -------------------------------------------------------------------------------- 1 | $value) { 46 | * $str .= $key . '=' . $value . '&'; 47 | * } 48 | * $str = substr($str, 0, count($str) - 2); 49 | * return sha1($str); 50 | */ 51 | 52 | // 这里参数的顺序要按照 key 值 ASCII 码升序排序 53 | $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; 54 | 55 | $signature = sha1($string); 56 | 57 | $signPackage = array( 58 | 'debug' => $debug, 59 | "appId" => WxConfig::$appid, 60 | "nonceStr" => $nonceStr, 61 | "timestamp" => $timestamp, 62 | "signature" => $signature, 63 | 'jsApiList' => self::$jsApiList, 64 | ); 65 | 66 | return $signPackage; 67 | } 68 | 69 | 70 | /** 71 | * 生成随机字符串 72 | * 73 | * @param int $length 74 | * 75 | * @return string 76 | */ 77 | private static function createNonceStr($length = 16) 78 | { 79 | $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; 80 | $str = ""; 81 | for ($i = 0; $i < $length; $i++) { 82 | $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); 83 | } 84 | 85 | return $str; 86 | } 87 | 88 | /** 89 | * 获取jsapi_ticket 90 | * @return false|string 91 | * @throws WxException 92 | */ 93 | private static function getJsApiTicket() 94 | { 95 | if ($ticket = WxCache::getJsApiTicket()) { 96 | return $ticket; 97 | } 98 | 99 | $accessToken = self::getAccessToken(); 100 | $url = WxUrl::urlJsApiTicket($accessToken); 101 | $res = WxUrl::curlJSON($url); 102 | 103 | if (!property_exists($res, 'ticket')) { 104 | throw new WxException($res->errmsg, $res->errcode); 105 | } 106 | $ticket = $res->ticket; 107 | 108 | if ($ticket) { 109 | WxCache::setJsApiTicket($ticket); 110 | } 111 | 112 | return $ticket; 113 | } 114 | 115 | /** 116 | * 获取access_token 117 | * @return string 118 | * @throws WxException 119 | */ 120 | private static function getAccessToken() 121 | { 122 | if ($access_token = WxCache::getAccessToken()) { 123 | return $access_token; 124 | } 125 | 126 | $url = WxUrl::urlAccessToken(WxConfig::$appid, WxConfig::$secret); 127 | $res = WxUrl::curlJSON($url); 128 | 129 | if (!property_exists($res, 'access_token')) { 130 | throw new WxException($res->errmsg, $res->errcode); 131 | } 132 | $access_token = $res->access_token; 133 | 134 | if ($access_token) { 135 | WxCache::setAccessToken($access_token); 136 | } 137 | 138 | return $access_token; 139 | } 140 | } -------------------------------------------------------------------------------- /third_party/weixin/WxOAuth.php: -------------------------------------------------------------------------------- 1 | $result['access_token'], 81 | 'scope' => $result['scope'], 82 | 'openid' => $result['openid'], 83 | ); 84 | } 85 | 86 | /** 87 | * 获取用户信息 88 | * 89 | * @param $accessToken 90 | * @param $openid 91 | * 92 | * @return mixed 93 | */ 94 | protected static function getOAuthUserInfo($accessToken, $openid) 95 | { 96 | $url = WxUrl::urlOAuthUserInfo($accessToken, $openid); 97 | $result = WxUrl::curlJSON($url, true); 98 | 99 | return $result; 100 | } 101 | } -------------------------------------------------------------------------------- /third_party/weixin/WxUrl.php: -------------------------------------------------------------------------------- 1 | $value) { 36 | if (property_exists($this, $key)) { 37 | $this->$key = $value; 38 | } 39 | } 40 | } 41 | } 42 | 43 | /** 44 | * 0(640), 46, 64, 96, 132 45 | * @param int $size 46 | * 47 | * @return mixed 48 | */ 49 | public function getAvatar($size = 640) 50 | { 51 | if ($size > 132) { 52 | return $this->avatarUrl(0); 53 | } 54 | if ($size > 96) { 55 | return $this->avatarUrl(132); 56 | } 57 | if ($size > 64) { 58 | return $this->avatarUrl(96); 59 | } 60 | if ($size > 46) { 61 | return $this->avatarUrl(64); 62 | } 63 | 64 | return $this->avatarUrl(46); 65 | } 66 | 67 | protected function avatarUrl($size) 68 | { 69 | return preg_replace('/\/\d+$/', "/$size", $this->headimgurl); 70 | } 71 | } -------------------------------------------------------------------------------- /third_party/weixin/wx_user.sql: -------------------------------------------------------------------------------- 1 | /* 2 | Navicat Premium Data Transfer 3 | 4 | Source Server : Pk 5 | Source Server Type : MySQL 6 | Source Server Version : 50077 7 | Source Host : 121.199.7.139 8 | Source Database : pk_database 9 | 10 | Target Server Type : MySQL 11 | Target Server Version : 50077 12 | File Encoding : utf-8 13 | 14 | Date: 05/16/2016 13:14:25 PM 15 | */ 16 | 17 | SET NAMES utf8; 18 | SET FOREIGN_KEY_CHECKS = 0; 19 | 20 | -- ---------------------------- 21 | -- Table structure for `wx_user` 22 | -- ---------------------------- 23 | DROP TABLE IF EXISTS `wx_user`; 24 | CREATE TABLE `wx_user` ( 25 | `wx_user_id` int(11) NOT NULL auto_increment, 26 | `openid` varchar(32) NOT NULL, 27 | `nickname` varchar(32) default NULL, 28 | `sex` int(2) default NULL, 29 | `province` varchar(16) default NULL, 30 | `city` varchar(16) default NULL, 31 | `country` varchar(16) default NULL, 32 | `headimgurl` varchar(255) default NULL, 33 | `unionid` varchar(32) default NULL, 34 | `date_added` datetime NOT NULL, 35 | `date_modified` datetime NOT NULL, 36 | PRIMARY KEY (`wx_user_id`), 37 | UNIQUE KEY `openid` (`openid`) 38 | ) ENGINE=MyISAM DEFAULT CHARSET=utf8; 39 | 40 | SET FOREIGN_KEY_CHECKS = 1; 41 | --------------------------------------------------------------------------------