-
Notifications
You must be signed in to change notification settings - Fork 317
payment_address
thenbsp edited this page Apr 29, 2016
·
8 revisions
共享用户收货地址需要用户 AccessToken
,因此需要先通过网页授权获取 用户 AccessToken
。
use Thenbsp\Wechat\OAuth\Client;
use Thenbsp\Wechat\Payment\Address\ConfigGenerator;
$client = new Client('appid', 'appsecret');
if( !isset($_GET['code']) ) {
header('Location: '.$client->getAuthorizeUrl());
}
try {
$accessToken = $client->getAccessToken($_GET['code']);
} catch (\Exception $e) {
exit($e->getMessage());
}
// 参数生成器帮你生成参数
$config = new ConfigGenerator($accessToken);
ConfigGenerator::getConfig()
方法默认返回 JSON
字符串:
$json = $config->getConfig();
可以使用第一个参数让其返回 Array
:
$array = $config->getConfig(true);
Javascript
ConfigGenerator
中使用了 __toString() 方法让你有能力直接输出 $config
对象:
<script>
var getAddress = function() {
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('area').innerHTML = res.addressCountiesThirdStageName;
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>
HTML
<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="area"></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>