xo_kf/application/common/model/Order.php

315 lines
11 KiB
PHP
Raw Permalink Blame History

<?php
namespace app\common\model;
use app\admin\model\JunkaCardlock;
use app\admin\model\JunkaCardunlock;
use app\api\model\JunkaCardLog;
use app\api\model\JunkaList;
use fast\Http;
use fast\Random;
use think\Db;
use think\Exception;
use think\Log;
use think\Model;
/**
* 订单模型
*/
class Order extends Model
{
// 开启自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
// 获取卡种类
protected $get_junka_type_url = 'http://Service.800j.com/UCard/UCardKindList.aspx';
// 购卡接口
protected $get_junka_url = 'http://Service.800j.com/UCard/UCardFetchSubmit.aspx';
// 锁卡/解锁
protected $cardlock_url = 'http://Service.800j.com/UCard/UCardLock.aspx';
// 接口签名key
protected $junka_key = '4FC3D043CBE3';
// 加密密钥
protected $junka_secret_key = '90B85D57A29043AFA5F9618A';
/**
* 骏卡获取卡种类
*/
public function getjunkatype()
{
$agent_id = config('junka.agent_id');
$data = Http::get($this->get_junka_type_url, ['agent_id' => $agent_id]);
p($data);
}
/**
* 购卡
* @param array $params 参数
* @param int $store_id 商户
* @param int $is_z 1 直购
*/
public function getjunka($params, $store, $is_z = true, $codelist, $price)
{
$junka_list = JunkaList::where(['id' => 1])->find();
try {
Db::startTrans();
$params['admin_id'] = $store->admin_id;
$log_arr = array_merge($params, ['price' => $price, 'store_id' => $store->id]);
$junka = JunkaCardLog::create($log_arr);
unset($params['admin_id']);
$params['card_kind'] = $junka_list->junka_id;
$params['time_stamp'] = date('YmdHis');
$params['sign'] = $this->getsign($params);
if(empty($params['is_dynamic']) && !empty($params['product_code'])) {
$params['card_price'] = $codelist->par_value;
}
$junka->card_price = $params['card_price'];
$data = Http::get($this->get_junka_url, $params);
// $data = 'ret_code=0&ret_msg=<3D><EFBFBD>ɹ<EFBFBD>&agent_id=2193854&bill_id=61961621651265961&jnet_bill_no=P240119247419915&par_price=10.000&purchase_amt=20.000&card_no_data=584ABBCD815FCD8D9734779E7906A9761FF7AAE142E458E732C9D944F69023124070E666834731BD809081BFCA3CF93391EA1B394A14D2B2C931CD7D46D4AA81B1513617D29D237567B29D79DC9119351D4B3ADBC8523570705600099C018AFC2AB7D35B1B5BCE87CD7C8032230A51C9&ext_param=&sign=d6744a50ea926ff304b25b0d6b71576f';
Log::write($data);
$res = $this->returnparamssave($data);
$junka_card = [];
// 购买成功
if($res['ret_code'] == 0) {
$junka->status = 1;
$junka_data = $this->decry($res['card_no_data']);
foreach ($junka_data as $k => $v) {
$junka_data = explode(',', $v);
$junka_card[] = [
'card_number' => $junka_data[0], // 卡号
'card_password' => $junka_data[1], // 卡密
'expiration_time' => $junka_data[2], // 过期时间
'card_log_id' => $junka->id,
'product_code' => $params['product_code'],
'store_id' => $store->id,
'card_price' => $params['card_price'],
'price' => $price,
'status' => 2,
'admin_id' => $store->admin_id,
'card_list_id' => $junka_list->id,
'card_list_name' => $junka_list->junka_name,
];
}
if($junka_card) {
// 加入库存后锁卡
$this->addkc($junka_card);
// 如果不是直接购买,则需要锁卡
if($is_z == false) {
// 锁卡
// $this->cardlock($junka_card);
}
}
}else {
$junka->error_msg = $res['ret_code'] . '-' . $res['ret_msg'];
$this->error = $res['ret_code'] . '-' . $res['ret_msg'];
$junka->save();
Db::commit();
return false;
}
$junka->save();
Db::commit();
return $junka_card;
}catch (Exception $e) {
Log::write('购卡错误---' . $e);
Db::rollback();
}
}
/**
* 解密
*/
public function decry($data)
{
$encrypted = hex2bin($data);//十六进制字符串转为加密前参数
$decrypted = openssl_decrypt($encrypted, 'des-ede3', $this->junka_secret_key, OPENSSL_RAW_DATA);
// 将解密结果转换为中文 GBK 格式
$decrypted = iconv('GBK', 'UTF-8', $decrypted);
$junka_data = explode('|', $decrypted);
return $junka_data;
}
/**
* 加密
*/
public function getsign($params)
{
$string = http_build_query($params) . '|||' . $this->junka_key;
return md5($string);
}
/**
* 卡锁定
*/
public function cardlock($card, $bill_id = 0)
{
$msg = '';
$money_save_card = [];
foreach ($card as $k => $v) {
$get_arr = [
'agent_id' => config('junka.agent_id'),
'account_type' => 2,
'card_no' => $v['card_number'],
'card_par_amt' => $v['card_price'],
'card_style' => 1,
'lock_type' => 1, // 锁卡
'time_stamp' => date('YmdHis'),
];
$get_arr['sign'] = $this->getsign($get_arr);
$res = Http::get($this->cardlock_url, $get_arr);
$res = $this->returnparamssave($res);
$msg .= '【' .$res['ret_msg'] . '】';;
if($res['ret_code'] == 0) { // 锁定成功
$end_time = (Config::get(['name' => 'card_expire_time'])->value * 24 * 3600) + time();
\app\api\model\JunkaCard::where(['card_number' => $v['card_number']])->update(['status' => 3, 'status_notes' => $res['ret_msg'], 'end_time' => $end_time]);
$money_save_card[] = $v['card_number'];
if($bill_id) {
// 改状态
\app\admin\model\JunkaPurchcardLog::where(['bill_id' => $bill_id])->update(['status' => 5]);
}
}else {
if($res['ret_msg'] == '该卡已被锁定') {
\app\api\model\JunkaCard::where(['card_number' => $v['card_number']])->update(['status' => 3, 'status_notes' => $res['ret_msg']]);
}else {
\app\api\model\JunkaCard::where(['card_number' => $v['card_number']])->update(['status' => 4, 'status_notes' => $res['ret_msg']]);
}
}
// 锁卡记录
JunkaCardlock::create([
'bill_id' => $bill_id,
'card_number' => $v['card_number'],
'ret_code' => $res['ret_code'],
'ret_msg' => $res['ret_msg'],
]);
}
return ['msg' => $msg, 'money_save_card' => $money_save_card];
}
/**
* 卡解锁
*/
public function jx($card, $bill_id = 0)
{
$msg = '';
$money_save_card = [];
$is_j = false;
foreach ($card as $k => $v) {
$get_arr = [
'agent_id' => config('junka.agent_id'),
'account_type' => 2,
'card_no' => $v['card_number'],
'card_par_amt' => $v['card_price'],
'card_style' => 1,
'lock_type' => 2, // 解锁
'time_stamp' => date('YmdHis'),
];
$get_arr['sign'] = $this->getsign($get_arr);
$res = Http::get($this->cardlock_url, $get_arr);
$res = $this->returnparamssave($res);
$msg .= '【' .$res['ret_msg'] . '】';
if($res['ret_code'] == 0) { // 解锁
$money_save_card[] = $v['card_number'];
\app\api\model\JunkaCard::where(['card_number' => $v['card_number']])->update(['status' => 1, 'status_notes' => $res['ret_msg']]);
$is_j = true;
}
// 解锁记录
JunkaCardunlock::create([
'bill_id' => $bill_id,
'card_number' => $v['card_number'],
'ret_code' => $res['ret_code'],
'ret_msg' => $res['ret_msg'],
]);
}
if($is_j) {
if($bill_id) {
// 改状态
$log = \app\admin\model\JunkaPurchcardLog::where(['bill_id' => $bill_id])->find();
if($log) {
$log->status = 1;
$log->save();
}
}else {
$log = \app\admin\model\JunkaPurchcardLog::where('data', 'like', '%' . $card[0]['card_number'] . '%')->select();
if($log) {
foreach ($log as $k => $v) {
if($v->status == 5) {
$v->status = 1;
$v->save();
}
}
}
}
}
return ['msg' => $msg, 'money_save_card' => $money_save_card];
}
/**
* 卡解锁
*/
public function cardunlock($card, $status = 1)
{
$get_arr = [
'agent_id' => config('junka.agent_id'),
'account_type' => 2,
'card_no' => $card->card_number,
'card_par_amt' => $card->card_price,
'card_style' => 1,
'lock_type' => 2, // 解锁
'time_stamp' => date('YmdHis'),
];
$get_arr['sign'] = $this->getsign($get_arr);
$res = Http::get($this->cardlock_url, $get_arr);
$res = $this->returnparamssave($res);
$card->status_notes = $res['ret_msg'];
if($res['ret_code'] == 0) { // 解锁
$card->status = $status;
}
$card->save();
return $res;
}
/**
* 加入库存
*/
public function addkc($data)
{
$card = new \app\api\model\JunkaCard;
return $card->saveAll($data);
}
/**
* 处理返回参数
*/
public function returnparamssave($data)
{
$data = iconv("gbk", "utf-8", $data);
$res_data = explode('&', $data);
$res = [];
foreach ($res_data as $k => $v) {
$n_res = explode('=', $v);
$res[$n_res[0]] = $n_res[1];
}
return $res;
}
}