Commit 03e0272b authored by 朱招明's avatar 朱招明

update

parent 361a5ce8
.idea/
/composer.lock
/vendor/
{
"name": "smg/third-api",
"description": "SMG Support Package",
"type": "library",
"authors": [
{
"name": "syw",
"email": "syw0602@163.com"
}
],
"autoload": {
"psr-4": {
"SMG\\ThirdApi\\": "src/"
},
"files": [
"src/helpers.php",
"src/config/wike.php"
]
},
"require": {
"guzzlehttp/guzzle": "^7.0"
},
"extra": {
"laravel": {
"providers": [
"SMG\\ThirdApi\\ThirdApiServiceProvider"
]
}
}
}
<?php
return [
'app_key' => '1085616747',
'app_secret' => '6fc630bcedc5e65a3fc37363d7dd2e4f',
];
\ No newline at end of file
<?php
require 'vendor/autoload.php';
$Power = new \SMG\ThirdApi\WeiKe\Rest\Power();
$params = [
'cardId' => 1,
'store_id' => 1,
'order_no' => date('YmdHis').rand(100,999),
'amount' => 100,
'recharge_type' => 1,
'notify_url' => '',
'change' => 0,
];
$result = $Power->pushOrder($params);
var_dump(json_decode($result));
<?php
/**
* @desc
* @author [ZZM]
* @since 2023/10/14
* @copyright
*/
namespace SMG\ThirdApi;
use Illuminate\Support\ServiceProvider;
class ThirdApiServiceProvider extends ServiceProvider
{
/**
* 在服务容器里注册
*
* @return void
*/
public function register()
{
$this->mergeConfigFrom(realpath(__DIR__.'/../config/wike.php'), 'wike');
}
}
\ No newline at end of file
<?php
/**
* @desc
* @author [ZZM]
* @since 2023/10/14
* @copyright
*/
namespace SMG\ThirdApi\WeiKe;
class Base
{
protected $base_uri = 'https://router.wikeyun.cn/';
protected $config = [];
public function __construct() {
$this->config = config('wike');
if(empty($this->config['app_key'])){
throw new \Exception("缺少 app_key");
}
if(empty($this->config['app_secret'])){
throw new \Exception("缺少 app_secret");
}
}
/**
* 签名
* @desc
*
* @param $params
*
* @return string
* @author [ZZM]
* @since 2023/10/14
* @modify
*/
public function getSign($params){
$str = '';
#首字母以ASCII方式升序排列
uksort($params, function($a, $b) {
return strcmp($a, $b);
});
foreach ($params as $key => $value){
$str .= $key.$value;
}
$str = $this->config['app_secret']."{$str}".$this->config['app_secret'];
return strtoupper(md5($str));
}
}
\ No newline at end of file
<?php
/**
* @desc
* @author [ZZM]
* @since 2023/10/14
* @copyright
*/
namespace SMG\ThirdApi\WeiKe\Rest;
use GuzzleHttp\Client;
use SMG\ThirdApi\WeiKe\Base;
class Power extends Base
{
/**
* 电费充值
* @desc
*
* @param $params
*
* @return false|string
* @throws \GuzzleHttp\Exception\GuzzleException
* @since 2023/10/14
* @modify
* @author [ZZM]
*/
public function pushOrder($params)
{
$url = $this->base_uri.'rest/Power/pushOrder';
$client = new Client();
$common_params = [
'app_key' => $this->config['app_key'],
'timestamp' => time(),
'client' => getClientIP(),
'v' => '1.0',
'format' => 'json',
];
$common_params['sign'] = $this->getSign(array_merge($common_params,$params));
$url = $url. '?' . http_build_query($common_params);
$response = $client->post($url, [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded',
],
'form_params' => $params,
]);
if ($response->getStatusCode() === 200) {
return $response->getBody()->getContents();
} else {
throw new \Exception("请求失败,状态码:{$response->getStatusCode()}");
}
}
}
\ No newline at end of file
<?php
/**
* 获取客户端地址
* @desc
* @return mixed
* @since 2023/10/14
* @modify
* @author [ZZM]
*/
if(!function_exists('getClientIP')){
function getClientIP() {
// 检查是否存在 HTTP_CLIENT_IP 头部
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
return $_SERVER['HTTP_CLIENT_IP'];
}
// 检查是否存在 HTTP_X_FORWARDED_FOR 头部
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
return $_SERVER['HTTP_X_FORWARDED_FOR'];
}
// 使用默认的 REMOTE_ADDR 备选方案
return $_SERVER['REMOTE_ADDR'] ?? '127.0.0.1';
}
}
if(!function_exists('config')){
function config($configKey = '') {
// 按点分割键路径
$keys = explode('.', $configKey);
$configFileName = array_shift($keys);
// 构建配置文件路径
$configFilePath = __DIR__ . '/../config/' . $configFileName . '.php';
// 加载配置文件
if(file_exists($configFilePath)){
$config = require $configFilePath;
}else{
return $configKey;
}
// 逐层遍历数组以查找值
foreach ($keys as $key) {
if (isset($config[$key])) {
$config = $config[$key];
} else {
return null; // 如果键不存在,返回 null 或其他适当的默认值
}
}
return $config;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment