2024-04-26 11:26:12 +08:00
|
|
|
|
<?php
|
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
|
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
|
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
|
|
|
|
// | 开源版本可自由商用,可去除界面版权logo
|
|
|
|
|
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
|
|
|
|
// | github下载:https://github.com/likeshop-github/likeadmin
|
|
|
|
|
// | 访问官网:https://www.likeadmin.cn
|
|
|
|
|
// | likeadmin团队 版权所有 拥有最终解释权
|
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
|
// | author: likeadminTeam
|
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
|
namespace app\common\service\wechat;
|
|
|
|
|
|
2024-05-14 17:30:17 +08:00
|
|
|
|
use DateTime;
|
2024-04-26 11:26:12 +08:00
|
|
|
|
use EasyWeChat\Kernel\Exceptions\Exception;
|
|
|
|
|
use EasyWeChat\MiniApp\Application;
|
2024-05-14 17:23:51 +08:00
|
|
|
|
use support\Cache;
|
2024-04-26 11:26:12 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 微信功能类
|
|
|
|
|
* Class WeChatMnpService
|
|
|
|
|
* @package app\common\service
|
|
|
|
|
*/
|
|
|
|
|
class WeChatMnpService
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
protected $app;
|
|
|
|
|
|
|
|
|
|
protected $config;
|
|
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->config = $this->getConfig();
|
|
|
|
|
$this->app = new Application($this->config);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @notes 配置
|
|
|
|
|
* @return array
|
|
|
|
|
* @throws \Exception
|
|
|
|
|
* @author 段誉
|
|
|
|
|
* @date 2023/2/27 12:03
|
|
|
|
|
*/
|
|
|
|
|
protected function getConfig()
|
|
|
|
|
{
|
|
|
|
|
$config = WeChatConfigService::getMnpConfig();
|
|
|
|
|
if (empty($config['app_id']) || empty($config['secret'])) {
|
|
|
|
|
throw new \Exception('请先设置小程序配置');
|
|
|
|
|
}
|
|
|
|
|
return $config;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @notes 小程序-根据code获取微信信息
|
|
|
|
|
* @param string $code
|
|
|
|
|
* @return array
|
|
|
|
|
* @throws Exception
|
|
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\HttpException
|
|
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
|
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
|
|
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface
|
|
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
|
|
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
|
|
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
|
|
|
|
|
* @author 段誉
|
|
|
|
|
* @date 2023/2/27 11:03
|
|
|
|
|
*/
|
|
|
|
|
public function getMnpResByCode(string $code)
|
|
|
|
|
{
|
|
|
|
|
$utils = $this->app->getUtils();
|
|
|
|
|
$response = $utils->codeToSession($code);
|
|
|
|
|
|
|
|
|
|
if (!isset($response['openid']) || empty($response['openid'])) {
|
|
|
|
|
throw new Exception('获取openID失败');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @notes 获取手机号
|
|
|
|
|
* @param string $code
|
|
|
|
|
* @return \EasyWeChat\Kernel\HttpClient\Response|\Symfony\Contracts\HttpClient\ResponseInterface
|
|
|
|
|
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
|
|
|
|
|
* @author 段誉
|
|
|
|
|
* @date 2023/2/27 11:46
|
|
|
|
|
*/
|
|
|
|
|
public function getUserPhoneNumber(string $code)
|
|
|
|
|
{
|
|
|
|
|
return $this->app->getClient()->postJson('wxa/business/getuserphonenumber', [
|
|
|
|
|
'code' => $code,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-14 16:11:08 +08:00
|
|
|
|
/**
|
|
|
|
|
* @notes 发货
|
|
|
|
|
*/
|
2024-05-14 17:56:33 +08:00
|
|
|
|
public function delivery($order_id,$openid)
|
2024-05-14 16:11:08 +08:00
|
|
|
|
{
|
2024-05-14 17:23:51 +08:00
|
|
|
|
$token=Cache::get('mmp_access_token');
|
|
|
|
|
if(!$token){
|
2024-05-14 17:30:17 +08:00
|
|
|
|
$token=$this->getAccessToken();
|
2024-05-14 17:23:51 +08:00
|
|
|
|
}
|
2024-05-14 17:30:17 +08:00
|
|
|
|
$dateTime = new DateTime(date('Y-m-d H:i:s'));
|
|
|
|
|
$formattedDateTime = $dateTime->format('Y-m-d\TH:i:s.uP');
|
2024-05-14 17:56:33 +08:00
|
|
|
|
return $this->app->getClient()->postJson("wxa/sec/order/upload_shipping_info?access_token=$token", [
|
2024-05-14 17:23:51 +08:00
|
|
|
|
'order_key' =>[
|
|
|
|
|
'order_number_type'=>1,
|
2024-05-14 17:56:33 +08:00
|
|
|
|
'out_trade_no'=>$order_id,
|
|
|
|
|
'mchid'=>config('payment.wechat.default.mch_id')
|
2024-05-14 17:23:51 +08:00
|
|
|
|
],
|
2024-05-27 16:58:38 +08:00
|
|
|
|
'logistics_type'=>2,
|
2024-05-14 17:23:51 +08:00
|
|
|
|
'delivery_mode'=>'UNIFIED_DELIVERY',
|
|
|
|
|
'shipping_list'=>[
|
|
|
|
|
['item_desc'=>'商品']
|
|
|
|
|
],
|
2024-05-14 17:30:17 +08:00
|
|
|
|
'upload_time'=>$formattedDateTime,
|
2024-05-14 17:23:51 +08:00
|
|
|
|
'payer'=>[
|
2024-05-14 17:56:33 +08:00
|
|
|
|
'openid'=>$openid
|
2024-05-14 17:23:51 +08:00
|
|
|
|
]
|
2024-05-14 16:11:08 +08:00
|
|
|
|
])->toArray();
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* @notes 获取access_token
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2024-05-14 17:30:17 +08:00
|
|
|
|
public function getAccessToken()
|
2024-05-14 16:11:08 +08:00
|
|
|
|
{
|
2024-05-14 17:30:17 +08:00
|
|
|
|
$token=$this->app->getAccessToken()->getToken();
|
|
|
|
|
if($token){
|
|
|
|
|
Cache::set('mmp_access_token',$token,7200);
|
|
|
|
|
return $token;
|
2024-05-14 17:56:33 +08:00
|
|
|
|
}else{
|
|
|
|
|
throw new Exception('获取access_token失败');
|
2024-05-14 17:23:51 +08:00
|
|
|
|
}
|
2024-05-14 16:11:08 +08:00
|
|
|
|
}
|
|
|
|
|
}
|