374 lines
13 KiB
PHP
374 lines
13 KiB
PHP
<?php
|
|
|
|
namespace app\api\controller;
|
|
|
|
use app\api\model\JunkaCard;
|
|
use app\api\model\JunkaCardLog;
|
|
use app\api\model\JunkaCode;
|
|
use app\api\model\JunkaList;
|
|
use app\api\model\JunkaPurchcardLog;
|
|
use app\api\model\JunkaStore;
|
|
use app\api\model\Order;
|
|
use app\common\controller\Api;
|
|
use fast\Http;
|
|
use fast\Random;
|
|
use think\Db;
|
|
use think\Exception;
|
|
use think\Log;
|
|
|
|
/**
|
|
* 卡接口
|
|
*/
|
|
class Block extends Api
|
|
{
|
|
protected $noNeedLogin = ['*'];
|
|
protected $noNeedRight = ['*'];
|
|
protected $key = 'LAUXWZVRTMPONKEHILPXSBST';
|
|
|
|
/**
|
|
* 获取卡种类
|
|
*/
|
|
public function getcard()
|
|
{
|
|
$order = new Order;
|
|
// 获取骏卡类型
|
|
// $junkatype_list = $order->getjunkatype();
|
|
|
|
// 购买骏卡
|
|
|
|
// $order = new Order;
|
|
// $junka_list = $order->getjunka([]);
|
|
// if(!$junka_list) {
|
|
// $this->error($order->getError());
|
|
// }
|
|
// echo '看看明显减少';
|
|
}
|
|
|
|
|
|
/**
|
|
* 提卡购卡接口
|
|
* @param string price 价格
|
|
* @param string number 数量
|
|
* @param string type 卡种类
|
|
* @param string order_no 订单号
|
|
* @param string sign 签名
|
|
* @param string store_id 商户ID
|
|
*/
|
|
public function takecard()
|
|
{
|
|
$post = $this->request->post();
|
|
$get = $this->request->get();
|
|
Log::write('购卡POST' . json_encode($post));
|
|
Log::write('购卡GET' .json_encode($get));
|
|
if(empty($post['price'])) {
|
|
$this->error('价格不能为空');
|
|
}
|
|
if(empty($post['number'])) {
|
|
$this->error('数量不能为空');
|
|
}
|
|
if(empty($post['type'])) {
|
|
$this->error('卡编码不能为空');
|
|
}
|
|
if(empty($post['order_no'])) {
|
|
$this->error('订单号不能为空');
|
|
}
|
|
|
|
if(empty($post['sign'])) {
|
|
$this->error('sign 不能为空');
|
|
}
|
|
$sign = $post['sign'];
|
|
unset($post['sign']);
|
|
$new_sign = md5(http_build_query(['order_no' => $post['order_no'], 'number' => $post['number']]) . '|' . $this->key);
|
|
if($new_sign != $sign) {
|
|
$this->error('签名不正确');
|
|
}
|
|
if(empty($get['store_id'])) {
|
|
$this->error('商户ID不能为空');
|
|
}
|
|
// 查询商家余额
|
|
$store = JunkaStore::where(['id' => $get['store_id']])->find();
|
|
if(!$store) {
|
|
$this->error('此商户不存在');
|
|
}
|
|
if($store->status == 2) {
|
|
$this->error('此商户已锁定');
|
|
}
|
|
$codelist = JunkaCode::where(['code' => $post['type']])->find();
|
|
if(!$codelist){
|
|
$this->error('卡不存在');
|
|
}
|
|
$JunkaPurchcardLog = JunkaPurchcardLog::where(['bill_id' => $post['order_no']])->find();
|
|
if($JunkaPurchcardLog) {
|
|
// $this->error('订单号已存在');
|
|
$this->success('ok', json_decode($JunkaPurchcardLog->data));
|
|
}
|
|
$p = $post['number'] * $codelist->par_value;
|
|
$total_num = $post['number'];
|
|
$minus_price = $p;
|
|
if(is_numeric($store->discount) && $store->discount > 0) { // 折扣
|
|
$minus_price = $p * ($store->discount / 100);
|
|
}
|
|
if($minus_price > $store->money) {
|
|
$this->error('商户可用余额不足');
|
|
}
|
|
try {
|
|
Db::startTrans();
|
|
$junka_list_data = JunkaList::where(['id' => 1])->find();
|
|
$order = new Order;
|
|
// // 查询库存
|
|
$inventory = $order->queryInventory($post['type'], $post['number']);
|
|
// // 库存够直接返回
|
|
if($inventory['number'] >= $post['number']) {
|
|
// 添加购卡记录
|
|
JunkaPurchcardLog::create([
|
|
'admin_id' => $store->admin_id,
|
|
'store_id' => $store->id,
|
|
'bill_id' => $post['order_no'],
|
|
'bill_time' => date('YmdHis'),
|
|
'product_code' => $post['type'],
|
|
'price' => $post['price'],
|
|
't_price' => $minus_price,
|
|
'card_price' => $codelist->par_value,
|
|
'product_num' => $post['number'],
|
|
'total_num' => $total_num,
|
|
'status' => 1,
|
|
'send_status' => 1,
|
|
'card_list_id' => $junka_list_data->id,
|
|
'card_list_name' => $junka_list_data->junka_name,
|
|
'data' => json_encode($inventory['card_data']),
|
|
]);
|
|
|
|
// 扣除商家余额
|
|
JunkaStore::moneychange($store, 0, 2, '购卡【库存】-' . $codelist->name, $post['order_no']);
|
|
Db::commit();
|
|
$this->success('ok', $inventory['card_data']);
|
|
// $this->success('ok');
|
|
}
|
|
|
|
$post['number'] = $post['number'] - $inventory['number'];
|
|
$k_minus_price = ($minus_price / $total_num) * $post['number'];
|
|
|
|
// 不够继续去购卡
|
|
$data = [
|
|
'agent_id' => config('junka.agent_id'),
|
|
'bill_id' => $post['order_no'],
|
|
'bill_time' => date('YmdHis'),
|
|
'product_code' => $post['type'],
|
|
'product_num' => $post['number'],
|
|
];
|
|
$order = new Order;
|
|
// 购卡
|
|
$junka_list = $order->getjunka($data, $store, true, $codelist, $post['price']);
|
|
if(!$junka_list) {
|
|
if(!empty($inventory['card_data'])) {
|
|
$data_n = $inventory['card_data'];
|
|
}else {
|
|
$data_n = [];
|
|
}
|
|
// 添加购卡记录
|
|
JunkaPurchcardLog::create([
|
|
'admin_id' => $store->admin_id,
|
|
'store_id' => $store->id,
|
|
'bill_id' => $post['order_no'],
|
|
'bill_time' => date('YmdHis'),
|
|
'product_code' => $post['type'],
|
|
'price' => $post['price'],
|
|
't_price' => $minus_price,
|
|
'card_price' => $codelist->par_value,
|
|
'product_num' => $post['number'],
|
|
'total_num' => $total_num,
|
|
'status' => 0,
|
|
'send_status' => 0,
|
|
'card_list_id' => $junka_list_data->id,
|
|
'card_list_name' => $junka_list_data->junka_name,
|
|
'data' => json_encode($data_n),
|
|
'notes' => $order->getError(),
|
|
]);
|
|
Db::commit();
|
|
$this->error($order->getError());
|
|
}
|
|
$junka_list_n = [];
|
|
foreach ($junka_list as $k => $v) {
|
|
$junka_list_n[$k]['card_number'] = $v['card_number'];
|
|
$junka_list_n[$k]['card_password'] = $v['card_password'];
|
|
}
|
|
|
|
if(!empty($inventory['card_data'])) {
|
|
// 扣除商家余额
|
|
JunkaStore::moneychange($store, 0, 2, '购卡【库存】-' . $codelist->name, $post['order_no']);
|
|
$junka_list_n = array_merge($junka_list_n, $inventory['card_data']);
|
|
}
|
|
|
|
// 扣除商家余额
|
|
JunkaStore::moneychange($store, $k_minus_price, 2, '购卡-' . $codelist->name, $post['order_no']);
|
|
|
|
// 添加购卡记录
|
|
JunkaPurchcardLog::create([
|
|
'admin_id' => $store->admin_id,
|
|
'store_id' => $store->id,
|
|
'bill_id' => $post['order_no'],
|
|
'bill_time' => date('YmdHis'),
|
|
'product_code' => $post['type'],
|
|
'price' => $post['price'],
|
|
't_price' => $minus_price,
|
|
'card_price' => $codelist->par_value,
|
|
'product_num' => $post['number'],
|
|
'total_num' => $total_num,
|
|
'status' => 1,
|
|
'send_status' => 1,
|
|
'card_list_id' => $junka_list_data->id,
|
|
'card_list_name' => $junka_list_data->junka_name,
|
|
'data' => json_encode($junka_list_n),
|
|
]);
|
|
|
|
Db::commit();
|
|
$this->success('ok', $junka_list_n);
|
|
// $this->success('ok');
|
|
}catch (Exception $e) {
|
|
Log::write('购卡错误---' . $e);
|
|
Db::rollback();
|
|
}
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* 回调/查询
|
|
*/
|
|
public function seach()
|
|
{
|
|
$this->success('ok');
|
|
$post = $this->request->post();
|
|
Log::write($post);
|
|
if(empty($post['order_no'])) {
|
|
$this->error('订单号不能为空');
|
|
}
|
|
if(empty($post['sign'])) {
|
|
$this->error('sign 不能为空');
|
|
}
|
|
$order = new Order;
|
|
$sign = $post['sign'];
|
|
unset($post['sign']);
|
|
$new_sign = md5(http_build_query(['order_no' => $post['order_no']]) . '|' . $this->key);
|
|
if($new_sign != $sign) {
|
|
$this->error('签名不正确');
|
|
}
|
|
try {
|
|
Db::startTrans();
|
|
$junkapur = JunkaPurchcardLog::where(['bill_id' => $post['order_no'], 'send_status' => 0])->find();
|
|
$junka_card = [];
|
|
$cardunlock = [];
|
|
if($junkapur) {
|
|
$data = json_decode($junkapur->data, true);
|
|
foreach ($data as $k => $v) {
|
|
$junka_card[] = [
|
|
'card_number' => $v['card_number'], // 卡号
|
|
'card_password' => $v['card_password'], // 卡密
|
|
];
|
|
$cardunlock[] = $v['card_number'];
|
|
}
|
|
$card = JunkaCard::where('card_number', 'in', $cardunlock)->select();
|
|
if($card) {
|
|
foreach ($card as $k => $v) {
|
|
if($v->status == 3) {
|
|
// 解锁 - 发货
|
|
$order->cardunlock($v, 2);
|
|
}else{
|
|
$v->status = 2;
|
|
$v->save();
|
|
}
|
|
}
|
|
}
|
|
$junkapur->send_status = 1;
|
|
$junkapur->save();
|
|
}
|
|
Db::commit();
|
|
$this->success('ok', $junka_card);
|
|
}catch (Exception $e) {
|
|
Log::write('查询错误---' . $e);
|
|
Db::rollback();
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
//
|
|
//
|
|
// $params['agent_id'] = config('junka.agent_id');
|
|
// $params['bill_id'] = $post['order_no'];
|
|
// $params['time_stamp'] = date('YmdHis');
|
|
// $params['sign'] = $order->getsign($params);
|
|
// $get = Http::get('http://Service.800j.com/UCard/UCardFetchQuery.aspx', $params);
|
|
// $get = $order->returnparamssave($get);
|
|
// $junka_card = [];
|
|
// $sql_arr = [];
|
|
// $purch = JunkaPurchcardLog::where(['bill_id' => $params['bill_id'], 'status' => 0])->find();
|
|
// if($get['ret_code'] == 0 && $get['ret_msg'] == '提卡成功') {
|
|
// $data = $order->decry($get['card_no_data']);
|
|
// foreach ($data as $k => $v) {
|
|
// $junka_data = explode(',', $v);
|
|
// $sql_arr[] = $junka_data[0];
|
|
// $junka_card[] = [
|
|
// 'card_number' => $junka_data[0], // 卡号
|
|
// 'card_password' => $junka_data[1], // 卡密
|
|
// ];
|
|
// }
|
|
// if($purch) {
|
|
// // 没发货
|
|
// $purch->status = 1;
|
|
// $purch->send_status = 1;
|
|
// JunkaCardLog::where(['bill_id' => $params['bill_id']])->update(['send_status' => 1, 'status' => 1]);
|
|
// JunkaCard::where('card_number', 'in', $sql_arr)->update(['status' => 2]);
|
|
// $purch->data = json_encode($junka_card);
|
|
// $this->success('ok', $junka_card);
|
|
// }
|
|
// $this->success('ok', $junka_card);
|
|
// }else {
|
|
// if($purch) {
|
|
// $purch->notes = $get['ret_msg'];
|
|
// $purch->save();
|
|
// }
|
|
// $this->error('查询失败');
|
|
// }
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
* 撤单接口
|
|
*/
|
|
public function cancelorder()
|
|
{
|
|
$post = $this->request->post();
|
|
if(empty($post['order_no'])) {
|
|
$this->error('订单号不能为空');
|
|
}
|
|
if(empty($post['sign'])) {
|
|
$this->error('sign 不能为空');
|
|
}
|
|
$sign = $post['sign'];
|
|
unset($post['sign']);
|
|
$new_sign = md5(http_build_query(['order_no' => $post['order_no']]) . '|' . $this->key);
|
|
if($new_sign != $sign) {
|
|
$this->error('签名不正确');
|
|
}
|
|
$card_log = JunkaCardLog::where(['bill_id' => $post['order_no']])->with('junkacard')->find();
|
|
foreach ($card_log->junkacard as $k => $v) {
|
|
$junka_card[] = [
|
|
'card_number' => $v['card_number'], // 卡号
|
|
'card_price' => $v['card_price'], // 卡密
|
|
];
|
|
}
|
|
$order = new Order;
|
|
$order->cardlock($junka_card);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|