forked from thenbsp/wechat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
require './example.php'; | ||
|
||
use Thenbsp\Wechat\Bridge\Util; | ||
use Thenbsp\Wechat\OAuth\Client; | ||
use Thenbsp\Wechat\Payment\Address\ConfigGenerator; | ||
|
||
/** | ||
* 只能在微信中打开 | ||
*/ | ||
if ( Util::isWechat() ) { | ||
exit('请在微信中打开'); | ||
} | ||
|
||
/** | ||
* 获取用户 AccessToken | ||
*/ | ||
if( !isset($_SESSION['token']) ) { | ||
|
||
$client = new Client(APPID, APPSECRET); | ||
|
||
if( !isset($_GET['code']) ) { | ||
header('Location: '.$client->getAuthorizeUrl()); | ||
} | ||
|
||
try { | ||
$token = $client->getAccessToken($_GET['code']); | ||
} catch (\Exception $e) { | ||
exit($e->getMessage()); | ||
} | ||
|
||
$_SESSION['token'] = $token; | ||
} | ||
|
||
/** | ||
* 生成收货地址共享组件配置 | ||
*/ | ||
$config = new ConfigGenerator($_SESSION['token']); | ||
|
||
?> | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"> | ||
<title>Wechat SDK</title> | ||
</head> | ||
<body ontouchstart=""> | ||
|
||
<h1>共享收货地址 <a href="javascript:;" onclick="window.location.reload()">刷新</a></h1> | ||
|
||
<button type="button" onclick="getAddress()" style="font-size:16px;height:38px;">选择收货地址</button> | ||
|
||
<ul> | ||
<li><strong>姓名:</strong><span id="name"></span></li> | ||
<li><strong>手机:</strong><span id="mobile"></span></li> | ||
<li><strong>省份:</strong><span id="provice"></span></li> | ||
<li><strong>城市:</strong><span id="city"></span></li> | ||
<li><strong>详细地址:</strong><span id="address"></span></li> | ||
<li><strong>邮政编码:</strong><span id="postalCode"></span></li> | ||
<li><strong>收货地址国家码:</strong><span id="nationalCode"></span></li> | ||
</ul> | ||
|
||
<script> | ||
var getAddress = function() { | ||
if( typeof WeixinJSBridge === 'undefined' ) { | ||
alert('请在微信在打开页面!'); | ||
return false; | ||
} | ||
WeixinJSBridge.invoke('editAddress', <?php echo $config; ?>, function(res) { | ||
switch(res.err_msg) { | ||
case 'edit_address:ok': | ||
document.getElementById('name').innerHTML = res.userName; | ||
document.getElementById('mobile').innerHTML = res.telNumber; | ||
document.getElementById('provice').innerHTML = res.proviceFirstStageName; | ||
document.getElementById('city').innerHTML = res.addressCitySecondStageName; | ||
document.getElementById('address').innerHTML = res.addressDetailInfo; | ||
document.getElementById('postalCode').innerHTML = res.addressPostalCode; | ||
document.getElementById('nationalCode').innerHTML = res.nationalCode; | ||
break; | ||
case 'edit_address:fail': | ||
alert('获取编辑收货地址失败!'); | ||
break; | ||
case 'edit_address:cancel': | ||
alert('您已取消获取地址!'); | ||
break; | ||
default: | ||
alert(JSON.stringify(res)); | ||
break; | ||
} | ||
}); | ||
} | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
namespace Thenbsp\Wechat\Payment\Address; | ||
|
||
use Thenbsp\Wechat\Bridge\Util; | ||
use Thenbsp\Wechat\Bridge\Serializer; | ||
use Thenbsp\Wechat\OAuth\AccessToken; | ||
|
||
class ConfigGenerator | ||
{ | ||
/** | ||
* Thenbsp\Wechat\OAuth\AccessToken | ||
*/ | ||
protected $accessToken; | ||
|
||
/** | ||
* 构造方法 | ||
*/ | ||
public function __construct(AccessToken $accessToken) | ||
{ | ||
$this->accessToken = $accessToken; | ||
} | ||
|
||
/** | ||
* 设置用户 AccessToken | ||
*/ | ||
public function setAccessToken(AccessToken $accessToken) | ||
{ | ||
if( !$accessToken->isValid() ) { | ||
$accessToken->refresh(); | ||
} | ||
|
||
$this->accessToken = $accessToken; | ||
} | ||
|
||
/** | ||
* 获取配置 | ||
*/ | ||
public function getConfig($asArray = false) | ||
{ | ||
$options = array( | ||
'appid' => $this->accessToken->getAppid(), | ||
'url' => Util::getCurrentUrl(), | ||
'timestamp' => Util::getTimestamp(), | ||
'noncestr' => Util::getRandomString(), | ||
'accesstoken' => $this->accessToken['access_token'] | ||
); | ||
|
||
// 按 ASCII 码排序 | ||
ksort($options); | ||
|
||
$signature = http_build_query($options); | ||
$signature = urldecode($signature); | ||
$signature = sha1($signature); | ||
|
||
$config = array( | ||
'appId' => $options['appid'], | ||
'scope' => 'jsapi_address', | ||
'signType' => 'sha1', | ||
'addrSign' => $signature, | ||
'timeStamp' => $options['timestamp'], | ||
'nonceStr' => $options['noncestr'], | ||
); | ||
|
||
return $asArray ? $config : Serializer::jsonEncode($config); | ||
} | ||
|
||
/** | ||
* 输出对象 | ||
*/ | ||
public function __toString() | ||
{ | ||
return $this->getConfig(); | ||
} | ||
} |