- 在用fastadmin写一个公众号, 需要获取用户收货地址,因为fastadmin是自带Esaywechat的,所以不用下载.
- 自带的Esaywechat的版本是3.x https://www.easywechat.com/docs/3.x/js
- Esaywechat的路径 在
/vendor/overtrue/wechat
目录下
代码
<?php
namespace app\index\controller;
use EasyWeChat\Foundation\Application;
use app\common\controller\Frontend;
class Index extends Frontend
{
protected $noNeedLogin = '*';
protected $noNeedRight = '*';
protected $layout = '';
protected $wechat = null;
// 初始化
public function _initialize()
{
$config = get_addon_config('wechat'); //此处 看文档末尾
$config['oauth']['scopes'] = ['snsapi_userinfo'];
$config['oauth']['callback'] = "http://地址/index.php/index/index/home"; //回调地址
$this->wechat = new Application($config);
}
public function index()
{
if (empty(Session::get("wechat_user"))) {
$response = $this->wechat->oauth->redirect();
$response->send();
}
// $token = Session::get("wechat_user")['token']->access_token;
// 用户收货地址
$js = $this->wechat->js->config(['checkJsApi','openAddress']); //openAddress是地址列表
$this->assign("js",$js); // 到页面
return $this->view->fetch("index");
}
public function home()
{
$user = $this->wechat->oauth->user();
$userInfo = User::get(['open'=>$user->id]); // 数据库根据openid查找数据
if (!$userInfo) {
// 进行注册
//此处代码略过
}
// 进行登录
Session::set("wechat_user",$user->toArray()); // 将openid等信息转化为数组
Session::set("user_info",$userInfo->toArray()); //将数据表查询的信息转化为数组
$this->success("登录成功","index");
}
}
在上面的代码可以看到 实例化的是Application.
在Application文件内 有相应的注释
/**
* Class Application.
*
* @property \EasyWeChat\Core\AccessToken $access_token
* @property \EasyWeChat\Server\Guard $server
* @property \EasyWeChat\User\User $user
* @property \EasyWeChat\User\Tag $user_tag
* @property \EasyWeChat\User\Group $user_group
* @property \EasyWeChat\Js\Js $js
* @property \Overtrue\Socialite\Providers\WeChatProvider $oauth
* @property \EasyWeChat\Menu\Menu $menu
* @property \EasyWeChat\Notice\Notice $notice
* @property \EasyWeChat\Material\Material $material
* @property \EasyWeChat\Material\Temporary $material_temporary
* @property \EasyWeChat\Staff\Staff $staff
* @property \EasyWeChat\Url\Url $url
* @property \EasyWeChat\QRCode\QRCode $qrcode
* @property \EasyWeChat\Semantic\Semantic $semantic
* @property \EasyWeChat\Stats\Stats $stats
* @property \EasyWeChat\Payment\Merchant $merchant
* @property \EasyWeChat\Payment\Payment $payment
* @property \EasyWeChat\Payment\LuckyMoney\LuckyMoney $lucky_money
* @property \EasyWeChat\Payment\MerchantPay\MerchantPay $merchant_pay
* @property \EasyWeChat\Payment\CashCoupon\CashCoupon $cash_coupon
* @property \EasyWeChat\Reply\Reply $reply
* @property \EasyWeChat\Broadcast\Broadcast $broadcast
* @property \EasyWeChat\Card\Card $card
* @property \EasyWeChat\Device\Device $device
* @property \EasyWeChat\Comment\Comment $comment
* @property \EasyWeChat\ShakeAround\ShakeAround $shakearound
* @property \EasyWeChat\OpenPlatform\OpenPlatform $open_platform
* @property \EasyWeChat\MiniProgram\MiniProgram $mini_program
*
* @method \EasyWeChat\Support\Collection clearQuota()
* @method \EasyWeChat\Support\Collection getCallbackIp()
*/
具体要使用的代码自己去查询
然后根据微信文档查找到共享收货地址接口
下面是前端的代码
<!DOCTYPE html>
<html>
<head>
<title>首页</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=2.0, user-scalable=yes" />
</head>
<body>
<button onclick="getsite()">获取收货地址</button>
<input type="" id="userName">
</body>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
<script type="text/javascript" charset="utf-8">
wx.config({$js});
wx.ready(function(){
})
function getsite(){
wx.openAddress({
success: function (res) {
var userName = res.userName; // 收货人姓名
var postalCode = res.postalCode; // 邮编
var provinceName = res.provinceName; // 国标收货地址第一级地址(省)
var cityName = res.cityName; // 国标收货地址第二级地址(市)
var countryName = res.countryName; // 国标收货地址第三级地址(国家)
var detailInfo = res.detailInfo; // 详细收货地址信息
var nationalCode = res.nationalCode; // 收货地址国家码
var telNumber = res.telNumber; // 收货人手机号码
$("#userName").val(userName);
},
fail: function(res){
alert("出错了");
}
});
}
</script>
</html>
然后直接可以用手机来测试了
大家有什么看不明白的可以直接发提问. 我看到了就会回的~
get_addon_config的内容
<?php
return array (
0 =>
array (
'name' => 'app_id',
'title' => 'app_id',
'type' => 'string',
'content' =>
array (
),
'value' => '',
'rule' => 'required',
'msg' => '',
'tip' => '你的微信公众号appid',
'ok' => '',
'extend' => '',
),
1 =>
array (
'name' => 'secret',
'title' => 'secret',
'type' => 'string',
'content' =>
array (
),
'value' => '',
'rule' => 'required',
'msg' => '',
'tip' => '你的微信公众号appsecret',
'ok' => '',
'extend' => '',
),
2 =>
array (
'name' => 'token',
'title' => 'token',
'type' => 'string',
'content' =>
array (
),
'value' => '',
'rule' => 'required',
'msg' => '',
'tip' => '通信token',
'ok' => '',
'extend' => '',
),
3 =>
array (
'name' => 'aes_key',
'title' => 'aes_key',
'type' => 'string',
'content' =>
array (
),
'value' => 'your aes_key',
'rule' => '',
'msg' => '',
'tip' => '',
'ok' => '',
'extend' => '',
),
4 =>
array (
'name' => 'debug',
'title' => '调试模式',
'type' => 'radio',
'content' =>
array (
0 => '否',
1 => '是',
),
'value' => '1',
'rule' => 'required',
'msg' => '',
'tip' => '',
'ok' => '',
'extend' => '',
),
5 =>
array (
'name' => 'log_level',
'title' => '日志记录等级',
'type' => 'select',
'content' =>
array (
'debug' => 'debug',
'info' => 'info',
'notice' => 'notice',
'warning' => 'warning',
'error' => 'error',
'critical' => 'critical',
'alert' => 'alert',
'emergency' => 'emergency',
),
'value' => 'debug',
'rule' => 'required',
'msg' => '',
'tip' => '',
'ok' => '',
'extend' => '',
),
6 =>
array (
'name' => 'oauth_callback',
'title' => '登录回调',
'type' => 'string',
'content' =>
array (
),
'value' => '',
'rule' => 'required',
'msg' => '',
'tip' => '',
'ok' => '',
'extend' => '',
),
);
$config内 加的内容 可以看这里 ->https://www.easywechat.com/docs/3.x/configuration