forked from fecshop/yii2_fecshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWx.php
138 lines (111 loc) · 4.23 KB
/
Wx.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/*
* FecShop file.
*
* @link http://www.fecshop.com/
* @copyright Copyright (c) 2016 FecShop Software LLC
* @license http://www.fecshop.com/license/
*/
namespace fecshop\services\helper;
use fecshop\services\Service;
use Yii;
/**
* Format services.
* @author Terry Zhao <[email protected]>
* @since 1.0
*/
// use \fecshop\services\helper\Format;
class Wx extends Service
{
public $wxApiBaseUrl = 'https://api.weixin.qq.com';
public $configFile;
// APPID:绑定支付的APPID
public $microProgramAppId ;
// 小程序secert
public $microProgramSecret;
public function init()
{
parent::init();
$wxpayConfigFile = Yii::getAlias($this->configFile);
if (!is_file($wxpayConfigFile)) {
throw new InvalidConfigException('wxpay config file:['.$wxpayConfigFile.'] is not exist');
}
$appId = Yii::$app->store->get('payment_wxpay', 'wechat_micro_app_id' );
$appSecret = Yii::$app->store->get('payment_wxpay', 'wechat_micro_app_secret');
$mchKey = Yii::$app->store->get('payment_wxpay', 'merchant_key');
$mchId = Yii::$app->store->get('payment_wxpay', 'merchant_mch_id');
define('WX_APP_ID', $appId);
define('WX_APP_SECRET', $appSecret);
define('WX_MCH_KEY', $mchKey);
define('WX_MCH_ID', $mchId);
require_once($wxpayConfigFile);
// 通过上面的小程序,设置配置信息
$this->microProgramAppId = \WxPayConfig::APPID;
$this->microProgramSecret = \WxPayConfig::APPSECRET;
}
/**
* @param $code | string, 微信登陆的code
* @return array , example: ['session_key' => '', 'openid' => '']
*/
public function getUserInfoByCode($code)
{
$urlKey = '/sns/jscode2session';
$apiId = $this->microProgramAppId;
$secret = $this->microProgramSecret;
$grant_type = 'authorization_code';
$url = $this->wxApiBaseUrl . $urlKey . "?appid=$apiId&secret=$secret&js_code=$code&grant_type=$grant_type";
$returnStr = \fec\helpers\CApi::getCurlData($url);
$wxUserInfo = json_decode($returnStr, true);
if (!isset($wxUserInfo['session_key']) || !isset($wxUserInfo['openid']) ) {
return null;
}
return $wxUserInfo;
}
/**
* @param $session_key | string, 微信登陆返回的session_key
* @param $openid | string, 微信登陆返回的openid
* @return bolean,将值保存到session中
*/
//public function setWxSessionKeyAndOpenid($session_key, $openid)
//{
// $openidStatus = Yii::$service->session->set('wx_openid', $openid);
// $sessionKeyStatus = Yii::$service->session->set('wx_session_key', $session_key);
//var_dump([Yii::$service->session->get('wx_session_key'), Yii::$service->session->get('wx_openid')]);
//exit;
// return $openidStatus && $sessionKeyStatus;
//}
/**
* @return string, 从session中取出来session_key
*/
//public function getWxSessionKey()
//{
// return Yii::$service->session->get('wx_session_key');
//}
/**
* @return string, 从session中取出来 openid
*/
//public function getWxOpenid()
//{
// return Yii::$service->session->get('wx_openid');
//}
/*
public function createQRCode()
{
/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN
$urlKey = '/sns/jscode2session';
$apiId = $this->microProgramAppId;
$secret = $this->microProgramSecret;
$grant_type = 'authorization_code';
$url = $this->wxApiBaseUrl . $urlKey . "?appid=$apiId&secret=$secret&js_code=$code&grant_type=$grant_type";
// echo $url;
$returnStr = \fec\helpers\CApi::getCurlData($url);
$wxUserInfo = json_decode($returnStr, true);
if (!isset($wxUserInfo['session_key']) || !isset($wxUserInfo['openid']) ) {
return null;
}
// 保存到session
Yii::$service->helper->wx->setWxSessionKeyAndOpenid($wxUserInfo['session_key'], $wxUserInfo['openid']);
return $wxUserInfo;
}
*/
}