
- 在 Channel 控制器中添加 queryBalance 方法,用于查询渠道余额 - 在 Core 控制器中添加 curl 方法,用于发起 HTTP 请求 - 新增 Feedov 服务类,实现飞之度商户自动提单功能- 更新 Meicheng 服务类,添加账户余额查询接口 - 在前端页面添加查询余额按钮和相关事件处理逻辑
373 lines
9.3 KiB
PHP
373 lines
9.3 KiB
PHP
<?php
|
|
|
|
namespace app\openapi\controller;
|
|
|
|
use app\channel\service\ChannelService;
|
|
use app\gateway\service\AuthService;
|
|
use app\gateway\service\CurlService;
|
|
use app\gateway\service\RedisService;
|
|
use app\merchant\service\MerchantService;
|
|
use app\merchant\service\OrderAutoService;
|
|
use app\merchant\service\OrderHistoryService;
|
|
use app\merchant\service\OrderLastHistoryService;
|
|
use app\merchant\service\OrderLastweekHistoryService;
|
|
use app\merchant\service\OrderService;
|
|
use app\merchant\service\OrderTwoHistoryService;
|
|
use app\merchant\service\ProductService as MerchantProductService;
|
|
use dever\Log;
|
|
use think\admin\Controller;
|
|
|
|
/**
|
|
* 接口核心控制器
|
|
* Class Core
|
|
* @package app\gateway\service
|
|
*/
|
|
class Core extends Controller
|
|
{
|
|
# 是否检测数据
|
|
protected $check = true;
|
|
# 商户appid
|
|
protected $appid = '';
|
|
# 商户id
|
|
protected $mid = 0;
|
|
# 商户信息
|
|
protected $merchant = array();
|
|
# token key
|
|
protected $key = '';
|
|
# product key
|
|
protected $product = 'query';
|
|
# proid
|
|
protected $proid = 0;
|
|
# input
|
|
protected $input = array();
|
|
# use_product
|
|
protected $use_product = array();
|
|
# sign_type
|
|
protected $sign_type = 1;
|
|
# code码定义
|
|
protected $code = array
|
|
(
|
|
# 成功
|
|
1 => 'ok',
|
|
# 小于0为失败
|
|
0 => '暂时未处理',
|
|
-1 => 'appid为空',
|
|
-2 => '产品错误',
|
|
-3 => 'appid无效',
|
|
-4 => '余额不足或者没有传入价格',
|
|
|
|
-5 => 'signature不能为空',
|
|
-6 => 'nonce不能为空',
|
|
-7 => 'time不能为空',
|
|
-8 => 'signature已失效',
|
|
-9 => 'signature验证失败',
|
|
|
|
# 京东下单错误代码
|
|
'JDI_00001' => '传入参数不正确(可退款)',
|
|
'JDI_00002' => '签名验证不正确(可退款)',
|
|
'JDI_00003' => '没有对应商品(可退款)',
|
|
'JDI_00004' => '此商品不可售(可退款)',
|
|
'JDI_00005' => '销售价不正确(可退款)',
|
|
'JDI_00006' => '订单号不允许重复',
|
|
'JDI_00010' => '系统错误',
|
|
'JDI_00007' => '没有对应订单',
|
|
|
|
|
|
# 快手错误代码
|
|
'4013001' => 'appKey参数缺失',
|
|
'4013022' => '黑名单',
|
|
'4012004' => '查询不到该手机号信息',
|
|
'4013008' => 'Param参数缺失',
|
|
|
|
# 淘宝错误代码
|
|
'9999' => '未知错误',
|
|
'0101' => '缺少必需参数',
|
|
'0102' => '签名失败',
|
|
'0104' => '找不到对应的订单',
|
|
'0302' => '购买数量非法',
|
|
'0303' => '商品库存不足',
|
|
'0304' => '商品维护中',
|
|
'0305' => '找不到对应的商品',
|
|
'0501' => '厂商例行维护,无法充值',
|
|
'0503' => '充值失败 ',
|
|
'0901' => '非法的订单状态转换',
|
|
|
|
|
|
|
|
|
|
|
|
-100 => '请求错误',
|
|
-101 => '订单不存在',
|
|
-102 => '订单号重复',
|
|
|
|
-1000 => '系统维护中',
|
|
|
|
);
|
|
|
|
public function initialize()
|
|
{
|
|
|
|
parent::initialize();
|
|
$this->sign_type = sysconf('sign_type');
|
|
if ($this->check) {
|
|
$this->check();
|
|
}
|
|
}
|
|
|
|
# 获取输入的信息
|
|
public function input()
|
|
{
|
|
// var_dump(8888);exit;
|
|
$this->input = input();
|
|
if (!$this->input) {
|
|
$this->no(-100);
|
|
}
|
|
|
|
$this->appid = input('appmid');
|
|
if (!$this->appid) {
|
|
$this->no(-1);
|
|
}
|
|
|
|
$this->proid = input('proid');
|
|
if ($this->proid) {
|
|
# 根据产品id 获取信息
|
|
$pinfo = \app\channel\service\ProductBaseService::instance()->getOne($this->proid);
|
|
if (!$pinfo) {
|
|
$this->no(-2);
|
|
}
|
|
$this->input['proid'] = $this->proid;
|
|
$this->product = $pinfo['key'];
|
|
if ($this->product != 'dhcz') {
|
|
if (!strstr($this->product, '_cash')) {
|
|
$this->input['cash'] = $pinfo['value'];
|
|
}
|
|
} else {
|
|
$this->proid = false;
|
|
}
|
|
} else {
|
|
$product = input('api_product');
|
|
if ($product) {
|
|
$this->product = $product;
|
|
}
|
|
}
|
|
|
|
if (!$this->product) {
|
|
$this->no(-2);
|
|
}
|
|
|
|
# 从数据库中或者缓存中取出商户的信息,并验证是否有效
|
|
$this->getMerchant();
|
|
}
|
|
|
|
/**
|
|
* 检测输入信息是否合法
|
|
*/
|
|
protected function check()
|
|
{
|
|
$this->input();
|
|
$input = $this->input;
|
|
if ($this->proid && !strstr($this->product, '_cash')) {
|
|
unset($input['cash']);
|
|
}
|
|
if($input['appid'] != 'app574223018568'){#测试验签
|
|
$signature = AuthService::check($input, $this->key, $this->sign_type);
|
|
|
|
if (is_numeric($signature)) {
|
|
$this->no($signature);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
unset($this->input['signature']);
|
|
if (isset($this->input['nonce'])) {
|
|
unset($this->input['nonce']);
|
|
}
|
|
if (isset($this->input['time'])) {
|
|
unset($this->input['time']);
|
|
}
|
|
}
|
|
|
|
# 检测开放或者维护时间
|
|
protected function checkOpenTime($opentime)
|
|
{
|
|
if ($opentime && strstr($opentime, ':')) {
|
|
$opentime = str_replace(':', '', $opentime);
|
|
if (strstr($opentime, '-')) {
|
|
$value = explode('-', $opentime);
|
|
$cur = intval(date('Hi'));
|
|
$value[0] = intval($value[0]);
|
|
$value[1] = intval($value[1]);
|
|
if ($value[1] < $value[0]) {
|
|
if ($cur >= $value[0] || $cur < $value[1]) {
|
|
$this->no(-1000);
|
|
}
|
|
} else {
|
|
if ($cur >= $value[0] && $cur < $value[1]) {
|
|
$this->no(-1000);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 对输入的信息进行加密
|
|
*/
|
|
protected function getSignature()
|
|
{
|
|
$this->input();
|
|
$input = $this->input;
|
|
if ($this->proid && !strstr($this->product, '_cash')) {
|
|
unset($input['cash']);
|
|
}
|
|
return AuthService::get($input, $this->key, $this->sign_type);
|
|
}
|
|
|
|
# 获取商户信息
|
|
protected function getMerchant()
|
|
{
|
|
$this->merchant = MerchantService::instance()->get($this->appid);
|
|
|
|
|
|
|
|
if (!$this->merchant) {
|
|
$this->no(-3);
|
|
}
|
|
|
|
if ($this->product != 'query') {
|
|
if (empty($this->input['cash'])) {
|
|
$this->no(-4);
|
|
}
|
|
if ($this->product == 'dxdc') {
|
|
$cash = 0.23;//这里后续处理
|
|
if ($this->merchant['credit_surplus'] < $cash && $this->merchant['account_surplus'] < $cash) {
|
|
$this->no(-4);
|
|
}
|
|
} else {
|
|
if ($this->merchant['credit_surplus'] < $this->input['cash'] && $this->merchant['account_surplus'] < $this->input['cash']) {
|
|
$this->no(-4);
|
|
}
|
|
}
|
|
}
|
|
|
|
$this->mid = $this->merchant['id'];
|
|
$this->agentkey = $this->merchant['agentkey'];
|
|
|
|
if ($this->sign_type == 2) {
|
|
$this->key = $this->merchant['appsecret'];
|
|
} else {
|
|
$this->key = $this->appid . '|' . $this->merchant['appsecret'];
|
|
}
|
|
}
|
|
|
|
# 查找订单
|
|
protected function getOrder($order_id, $merchant_order_id = false)
|
|
{
|
|
$orderService = OrderService::instance();
|
|
$order = $orderService->get($order_id, $merchant_order_id, $this->mid);
|
|
if (!$order) {
|
|
$orderLastweekHistoryService = OrderLastweekHistoryService::instance();
|
|
$order = $orderLastweekHistoryService->get($order_id, $merchant_order_id, $this->mid);
|
|
}
|
|
if (!$order) {
|
|
$orderLastHistoryService = OrderLastHistoryService::instance();
|
|
$order = $orderLastHistoryService->get($order_id, $merchant_order_id, $this->mid);
|
|
}
|
|
if (!$order) {
|
|
$orderTwoHistoryService = OrderTwoHistoryService::instance();
|
|
$order = $orderTwoHistoryService->get($order_id, $merchant_order_id, $this->mid);
|
|
}
|
|
if (!$order) {
|
|
$orderHistoryService = OrderHistoryService::instance();
|
|
$order = $orderHistoryService->get($order_id, $merchant_order_id, $this->mid);
|
|
}
|
|
return $order;
|
|
}
|
|
|
|
|
|
# 通用签名方法,如果有渠道不同的签名方式,需要单独实现
|
|
protected function sign($request, $type = 'sha1', $suffix = '')
|
|
{
|
|
return AuthService::signature($request, $type, $suffix);
|
|
}
|
|
|
|
# 调取服务
|
|
protected function channel($merchant, $product, $async = false, $param = false, $order = '')
|
|
{
|
|
if (!$param) {
|
|
$param = $this->input;
|
|
}
|
|
if ($product == 'cardbuy') {
|
|
$async = false;
|
|
}
|
|
return ChannelService::instance()->use($merchant, $product, $param, $async, $order);
|
|
}
|
|
|
|
/**
|
|
* 返回失败的消息
|
|
* @param mixed $info
|
|
* @param string $data
|
|
* @param integer $code
|
|
*/
|
|
protected function no($code = 0, $info = '', $data = '{-null-}')
|
|
{
|
|
$msg = $this->code[$code] ?? 'error';
|
|
if ($info) {
|
|
$msg .= ':' . $info;
|
|
}
|
|
$data = '{-null-}';
|
|
$this->error($msg, $data, $code);
|
|
}
|
|
|
|
/**
|
|
* 返回成功的消息
|
|
* @param mixed $info
|
|
* @param string $data
|
|
* @param integer $code
|
|
*/
|
|
protected function yes($data = '{-null-}', $info = 'ok', $code = 1)
|
|
{
|
|
if (is_string($data) && $data != 'ok' && $data != 'success') {
|
|
if ($data == '订单号重复') {
|
|
return $this->no(-102);
|
|
}
|
|
return $this->no(-100, $data);
|
|
}
|
|
$this->success($info, $data, $code);
|
|
}
|
|
|
|
# 请求数据
|
|
protected function curl($method, $url, $param = array(), $json = false, $header = false)
|
|
{
|
|
if ($param) {
|
|
$log['type'] = 'request';
|
|
$log['url'] = $url;
|
|
$log['param'] = $param;
|
|
$this->log($log);
|
|
}
|
|
$data = CurlService::getInstance($url, $param, $method, $json, $header)->result();
|
|
if (!$data) {
|
|
$data = CurlService::getInstance($url, $param, $method, $json, $header)->result();
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
# 记录日志
|
|
protected function log($data)
|
|
{
|
|
Log::write('gateway', 'callback', $data);
|
|
}
|
|
|
|
# 队列
|
|
protected function queue($key, $value = false)
|
|
{
|
|
$redis = RedisService::getInstance();
|
|
if (!$value) {
|
|
return $redis->pop($key);
|
|
} else {
|
|
return $redis->push($key, $value);
|
|
}
|
|
}
|
|
} |