├── .gitignore ├── composer.json ├── src ├── WechatUser.php └── Wechat.php ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # phpstorm project files 2 | .idea -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jianyan74/yii2-easy-wechat", 3 | "description": "WeChat SDK for yii2, 基于 overtrue/easywechat", 4 | "keywords": ["yii2", "wechat", "easywechat", "php", "jianyan74"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "jianyan74" 9 | } 10 | ], 11 | "type": "yii2-extension", 12 | "require": { 13 | "php": ">=7.4", 14 | "yiisoft/yii2": "~2.0.6", 15 | "overtrue/wechat": "~5.0" 16 | }, 17 | "autoload": { 18 | "psr-4": { 19 | "jianyan\\easywechat\\": "./src" 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /src/WechatUser.php: -------------------------------------------------------------------------------- 1 | original['openid']) ? $this->original['openid'] : ''; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Max.wen 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 | # yii2-easy-wechat 2 | 3 | > 支持 overtrue/wechat 4.x 和 5.x 4 | 5 | 由于 [max-wen/yii2-easy-wechat](https://github.com/max-wen/yii2-easy-wechat) 不支持 EasyWechat 4.x 所以建立该项目 6 | 7 | WeChat SDK for Yii2 , 基于 [overtrue/wechat](https://github.com/overtrue/wechat). 8 | 这个扩展可以简单的用yii2的方式去调用EasyWechat: `Yii::$app->wechat`. 9 | 10 | [![Latest Stable Version](https://poser.pugx.org/jianyan74/yii2-easy-wechat/v/stable)](https://packagist.org/packages/jianyan74/yii2-easy-wechat) 11 | [![Total Downloads](https://poser.pugx.org/jianyan74/yii2-easy-wechat/downloads)](https://packagist.org/packages/jianyan74/yii2-easy-wechat) 12 | [![License](https://poser.pugx.org/jianyan74/yii2-easy-wechat/license)](https://packagist.org/packages/jianyan74/yii2-easy-wechat) 13 | 14 | ## 安装 EasyWechat 4.x 15 | ``` 16 | composer require jianyan74/yii2-easy-wechat:~1.0 17 | ``` 18 | 19 | ## 安装 EasyWechat 5.x 20 | ``` 21 | composer require jianyan74/yii2-easy-wechat:~2.0 22 | ``` 23 | 24 | ## 配置 25 | 26 | 添加 SDK 到Yii2的 `config/main.php` 的 `component`: 27 | 28 | ```php 29 | 30 | 'components' => [ 31 | // ... 32 | 'wechat' => [ 33 | 'class' => 'jianyan\easywechat\Wechat', 34 | 'userOptions' => [], // 用户身份类参数 35 | 'sessionParam' => 'wechatUser', // 微信用户信息将存储在会话在这个密钥 36 | 'returnUrlParam' => '_wechatReturnUrl', // returnUrl 存储在会话中 37 | 'rebinds' => [ // 自定义服务模块 38 | // 'cache' => 'common\components\Cache', 39 | ] 40 | ], 41 | // ... 42 | ] 43 | ``` 44 | 45 | 设置基础配置信息和微信支付信息到 `config/params.php`: 46 | ```php 47 | // 微信配置 具体可参考EasyWechat 48 | 'wechatConfig' => [], 49 | 50 | // 微信支付配置 具体可参考EasyWechat 51 | 'wechatPaymentConfig' => [], 52 | 53 | // 微信小程序配置 具体可参考EasyWechat 54 | 'wechatMiniProgramConfig' => [], 55 | 56 | // 微信开放平台第三方平台配置 具体可参考EasyWechat 57 | 'wechatOpenPlatformConfig' => [], 58 | 59 | // 微信企业微信配置 具体可参考EasyWechat 60 | 'wechatWorkConfig' => [], 61 | 62 | // 微信企业微信开放平台 具体可参考EasyWechat 63 | 'wechatOpenWorkConfig' => [], 64 | 65 | // 微信小微商户 具体可参考EasyWechat 66 | 'wechatMicroMerchantConfig' => [], 67 | ``` 68 | 69 | 配置文档 70 | 71 | [微信配置说明文档.](https://www.easywechat.com/docs/master/official-account/configuration) 72 | [微信支付配置说明文档.](https://www.easywechat.com/docs/master/payment/jssdk) 73 | [微信小程序配置说明文档.](https://www.easywechat.com/docs/master/mini-program/index) 74 | [微信开放平台第三方平台](https://www.easywechat.com/docs/master/open-platform/index) 75 | [企业微信](https://www.easywechat.com/docs/master/wework/index) 76 | [企业微信开放平台](https://www.easywechat.com/docs/master/open-work/index) 77 | [小微商户](https://www.easywechat.com/docs/master/micro-merchant/index) 78 | 79 | ## 使用例子 80 | 81 | 82 | 微信网页授权+获取当前用户信息 83 | 84 | ```php 85 | if (Yii::$app->wechat->isWechat && !Yii::$app->wechat->isAuthorized()) { 86 | return Yii::$app->wechat->authorizeRequired()->send(); 87 | } 88 | 89 | // 获取微信当前用户信息方法一 90 | Yii::$app->session->get('wechatUser') 91 | 92 | // 获取微信当前用户信息方法二 93 | Yii::$app->wechat->user 94 | ``` 95 | 获取微信SDK实例 96 | 97 | ```php 98 | $app = Yii::$app->wechat->app; 99 | ``` 100 | 获取微信支付SDK实例 101 | 102 | ```php 103 | $payment = Yii::$app->wechat->payment; 104 | ``` 105 | 获取微信小程序实例 106 | 107 | ```php 108 | $miniProgram = Yii::$app->wechat->miniProgram; 109 | ``` 110 | 111 | 获取微信开放平台第三方平台实例 112 | 113 | ```php 114 | $openPlatform = Yii::$app->wechat->openPlatform; 115 | ``` 116 | 117 | 获取企业微信实例 118 | 119 | ```php 120 | $work = Yii::$app->wechat->work; 121 | ``` 122 | 123 | 获取微信企业微信开放平台 124 | 125 | ```php 126 | $work = Yii::$app->wechat->openWork; 127 | ``` 128 | 129 | 获取微信小微商户 130 | 131 | ```php 132 | $microMerchant = Yii::$app->wechat->microMerchant; 133 | ``` 134 | 135 | 136 | 微信支付(JsApi): 137 | 138 | ```php 139 | // 支付参数 140 | $orderData = [ 141 | 'openid' => '.. ' 142 | // ... etc. 143 | ]; 144 | 145 | // 生成支付配置 146 | $payment = Yii::$app->wechat->payment; 147 | $result = $payment->order->unify($orderData); 148 | if ($result['return_code'] == 'SUCCESS') { 149 | $prepayId = $result['prepay_id']; 150 | $config = $payment->jssdk->sdkConfig($prepayId); 151 | } else { 152 | throw new yii\base\ErrorException('微信支付异常, 请稍后再试'); 153 | } 154 | 155 | return $this->render('wxpay', [ 156 | 'jssdk' => $payment->jssdk, // $app通过上面的获取实例来获取 157 | 'config' => $config 158 | ]); 159 | 160 | ``` 161 | 162 | JSSDK发起支付 163 | ``` 164 | 165 | 180 | ``` 181 | 182 | ### 智能提示 183 | 184 | 如果你需要编辑器(PhpStorm等)的智能提示来使用`Yii::$app->wechat`,可以在`yii\base\Application`中加入: 185 | ``` 186 | 197 | * @since 2.0 198 | */ 199 | abstract class Application extends Module 200 | { 201 | 202 | } 203 | ``` 204 | 205 | ### 更多的文档 206 | 207 | [EasyWeChat Docs](https://www.easywechat.com/docs/master). 208 | 209 | ### 实例 210 | 211 | [RageFrame](https://github.com/jianyan74/rageframe2) 212 | 213 | ### 问题反馈 214 | 215 | 在使用中有任何问题,欢迎反馈给我,可以用以下联系方式跟我交流 216 | 217 | QQ群:[655084090](https://jq.qq.com/?_wv=1027&k=4BeVA2r) 218 | 219 | -------------------------------------------------------------------------------- /src/Wechat.php: -------------------------------------------------------------------------------- 1 | request->get('code'))) { 108 | // callback and authorize 109 | return $this->authorize($this->app->oauth->userFromCode($code)); 110 | } else { 111 | // redirect to wechat authorize page 112 | $this->setReturnUrl(Yii::$app->request->getUrl()); 113 | return Yii::$app->response->redirect($this->app->oauth->redirect(Yii::$app->request->absoluteUrl)->getTargetUrl()); 114 | } 115 | } 116 | 117 | /** 118 | * @param \Overtrue\Socialite\User $user 119 | * @return yii\web\Response 120 | */ 121 | public function authorize(\Overtrue\Socialite\User $user) 122 | { 123 | Yii::$app->session->set($this->sessionParam, $user->toJSON()); 124 | return Yii::$app->response->redirect($this->getReturnUrl()); 125 | } 126 | 127 | /** 128 | * check if current user authorized 129 | * @return bool 130 | */ 131 | public function isAuthorized() 132 | { 133 | $hasSession = Yii::$app->session->has($this->sessionParam); 134 | $sessionVal = Yii::$app->session->get($this->sessionParam); 135 | return ($hasSession && !empty($sessionVal)); 136 | } 137 | 138 | /** 139 | * @param string|array $url 140 | */ 141 | public function setReturnUrl($url) 142 | { 143 | Yii::$app->session->set($this->returnUrlParam, $url); 144 | } 145 | 146 | /** 147 | * @param null $defaultUrl 148 | * @return mixed|null|string 149 | */ 150 | public function getReturnUrl($defaultUrl = null) 151 | { 152 | $url = Yii::$app->session->get($this->returnUrlParam, $defaultUrl); 153 | if (is_array($url)) { 154 | if (isset($url[0])) { 155 | return Yii::$app->getUrlManager()->createUrl($url); 156 | } else { 157 | $url = null; 158 | } 159 | } 160 | 161 | return $url === null ? Yii::$app->getHomeUrl() : $url; 162 | } 163 | 164 | /** 165 | * 获取 EasyWeChat 微信实例 166 | * 167 | * @return Factory|\EasyWeChat\OfficialAccount\Application 168 | */ 169 | public function getApp() 170 | { 171 | if (!self::$_app instanceof \EasyWeChat\OfficialAccount\Application) { 172 | self::$_app = Factory::officialAccount(Yii::$app->params['wechatConfig']); 173 | !empty($this->rebinds) && self::$_app = $this->rebind(self::$_app); 174 | } 175 | 176 | return self::$_app; 177 | } 178 | 179 | /** 180 | * 获取 EasyWeChat 微信支付实例 181 | * 182 | * @return Factory|\EasyWeChat\Payment\Application 183 | */ 184 | public function getPayment() 185 | { 186 | if (!self::$_payment instanceof \EasyWeChat\Payment\Application) { 187 | self::$_payment = Factory::payment(Yii::$app->params['wechatPaymentConfig']); 188 | !empty($this->rebinds) && self::$_payment = $this->rebind(self::$_payment); 189 | } 190 | 191 | return self::$_payment; 192 | } 193 | 194 | /** 195 | * 获取 EasyWeChat 微信小程序实例 196 | * 197 | * @return Factory|\EasyWeChat\MiniProgram\Application 198 | */ 199 | public function getMiniProgram() 200 | { 201 | if (!self::$_miniProgram instanceof \EasyWeChat\MiniProgram\Application) { 202 | self::$_miniProgram = Factory::miniProgram(Yii::$app->params['wechatMiniProgramConfig']); 203 | !empty($this->rebinds) && self::$_miniProgram = $this->rebind(self::$_miniProgram); 204 | } 205 | 206 | return self::$_miniProgram; 207 | } 208 | 209 | /** 210 | * 获取 EasyWeChat 微信第三方开放平台实例 211 | * 212 | * @return Factory|\EasyWeChat\OpenPlatform\Application 213 | */ 214 | public function getOpenPlatform() 215 | { 216 | if (!self::$_openPlatform instanceof \EasyWeChat\OpenPlatform\Application) { 217 | self::$_openPlatform = Factory::openPlatform(Yii::$app->params['wechatOpenPlatformConfig']); 218 | !empty($this->rebinds) && self::$_openPlatform = $this->rebind(self::$_openPlatform); 219 | } 220 | 221 | return self::$_openPlatform; 222 | } 223 | 224 | /** 225 | * 获取 EasyWeChat 企业微信实例 226 | * 227 | * @return Factory|\EasyWeChat\Work\Application 228 | */ 229 | public function getWork() 230 | { 231 | if (!self::$_work instanceof \EasyWeChat\Work\Application) { 232 | self::$_work = Factory::work(Yii::$app->params['wechatWorkConfig']); 233 | !empty($this->rebinds) && self::$_work = $this->rebind(self::$_work); 234 | } 235 | 236 | return self::$_work; 237 | } 238 | 239 | /** 240 | * 获取 EasyWeChat 企业微信开放平台实例 241 | * 242 | * @return Factory|\EasyWeChat\OpenWork\Application 243 | */ 244 | public function getOpenWork() 245 | { 246 | if (!self::$_openWork instanceof \EasyWeChat\OpenWork\Application) { 247 | self::$_openWork = Factory::openWork(Yii::$app->params['wechatOpenWorkConfig']); 248 | !empty($this->rebinds) && self::$_openWork = $this->rebind(self::$_openWork); 249 | } 250 | 251 | return self::$_openWork; 252 | } 253 | 254 | /** 255 | * 获取 EasyWeChat 小微企业实例 256 | * 257 | * @return Factory|\EasyWeChat\OpenWork\Application 258 | */ 259 | public function getMicroMerchant() 260 | { 261 | if (!self::$_microMerchant instanceof \EasyWeChat\MicroMerchant\Application) { 262 | self::$_microMerchant = Factory::microMerchant(Yii::$app->params['wechatMicroMerchantConfig']); 263 | !empty($this->rebinds) && self::$_microMerchant = $this->rebind(self::$_microMerchant); 264 | } 265 | 266 | return self::$_microMerchant; 267 | } 268 | 269 | /** 270 | * $app 271 | * 272 | * @param $app 273 | * @return mixed 274 | */ 275 | public function rebind($app) 276 | { 277 | foreach ($this->rebinds as $key => $class) { 278 | $app->rebind($key, new $class()); 279 | } 280 | 281 | return $app; 282 | } 283 | 284 | /** 285 | * 获取微信身份信息 286 | * 287 | * @return WechatUser 288 | */ 289 | public function getUser() 290 | { 291 | if (!$this->isAuthorized()) { 292 | return new WechatUser(); 293 | } 294 | 295 | if (!self::$_user instanceof WechatUser) { 296 | $userInfo = Yii::$app->session->get($this->sessionParam); 297 | $config = $userInfo ? json_decode($userInfo, true) : []; 298 | self::$_user = new WechatUser($config); 299 | } 300 | return self::$_user; 301 | } 302 | 303 | /** 304 | * overwrite the getter in order to be compatible with this component 305 | * @param $name 306 | * @return mixed 307 | * @throws \Exception 308 | */ 309 | public function __get($name) 310 | { 311 | try { 312 | return parent::__get($name); 313 | } catch (\Exception $e) { 314 | throw $e->getPrevious(); 315 | } 316 | } 317 | 318 | /** 319 | * check if client is wechat 320 | * @return bool 321 | */ 322 | public function getIsWechat() 323 | { 324 | return strpos($_SERVER["HTTP_USER_AGENT"], "MicroMessenger") !== false; 325 | } 326 | } 327 | --------------------------------------------------------------------------------