Fastadmin+Esaywechat获取微信地址列表

  1. 在用fastadmin写一个公众号, 需要获取用户收货地址,因为fastadmin是自带Esaywechat的,所以不用下载.
  2. 自带的Esaywechat的版本是3.x https://www.easywechat.com/docs/3.x/js
  3. 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

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

苏范霖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值