271 lines
9.9 KiB
PHP
271 lines
9.9 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
|
|
namespace app\store\controller;
|
|
|
|
use app\common\model\store\Setting as SettingModel;
|
|
use app\store\controller\Controller;
|
|
use app\store\model\Code as CodeModel;
|
|
use app\store\model\Shop as ShopModel;
|
|
use app\store\model\Order as OrderModel;
|
|
use app\store\model\store\User;
|
|
use think\facade\Cache;
|
|
use think\facade\Request;
|
|
use think\response\Json;
|
|
|
|
/**
|
|
* 产码控制器
|
|
* Class StoreUser
|
|
* @package app\store\controller
|
|
*/
|
|
class Code extends Controller
|
|
{
|
|
/**
|
|
* 店铺列表
|
|
* @return mixed
|
|
* @throws \think\exception\DbException
|
|
*/
|
|
public function index(): Json
|
|
{
|
|
$model = new CodeModel;
|
|
$getData = $this->request->param();
|
|
$data = [];
|
|
if (isset($getData['status']) && (int)$getData['status']>=0) {
|
|
$data[] = ['status', '=', (int)$getData['status']];
|
|
}
|
|
if (isset($getData['pay_status']) && (int)$getData['pay_status']>=0) {
|
|
$data[] = ['pay_status', '=', (int)$getData['pay_status']];
|
|
}
|
|
if (isset($getData['code_account']) && !empty($getData['code_account'])) {
|
|
$data[] = ['code_account', 'like', "%{$getData['code_account']}%"];
|
|
}
|
|
if (isset($getData['order_no']) && !empty($getData['order_no'])) {
|
|
$data[] = ['order_no', 'like', "%{$getData['order_no']}%"];
|
|
}
|
|
if (isset($getData['passageway']) && $getData['passageway'] != -1) {
|
|
$data[] = ['passageway', '=', $getData['passageway']];
|
|
}
|
|
if (isset($getData['state']) && $getData['state'] != -1) {
|
|
$data[] = ['state', '=', $getData['state']];
|
|
}
|
|
if($this->store['user']['is_super'] == 0) {
|
|
$data[] = ['store_user_id', 'in', User::getScope($this->store['user'])];
|
|
}
|
|
$list = $model->getList($data)->toArray();
|
|
$domain = Request::instance()->domain();
|
|
$url = $domain . '/wxScanPng.php';
|
|
$data = [];
|
|
foreach ((array)$list['data'] as $val){
|
|
$val['qrcode'] = $url . '?url=' . urlencode(htmlspecialchars_decode($val['link']));
|
|
$data[] = $val;
|
|
}
|
|
$list['data'] = $data;
|
|
return $this->renderSuccess(['list'=>$list,'user'=>$this->store['user']]);
|
|
}
|
|
|
|
|
|
/**
|
|
* 统计数据列表
|
|
* @return mixed
|
|
* @throws \think\exception\DbException
|
|
*/
|
|
public function stat(): Json
|
|
{
|
|
$model = new CodeModel;
|
|
$getData = $this->request->param();
|
|
$data = [];
|
|
if (isset($getData['status']) && (int)$getData['status']>=0) {
|
|
$data[] = ['status', '=', (int)$getData['status']];
|
|
}
|
|
if (isset($getData['pay_status']) && (int)$getData['pay_status']>=0) {
|
|
$data[] = ['pay_status', '=', (int)$getData['pay_status']];
|
|
}
|
|
if (isset($getData['code_account']) && !empty($getData['code_account'])) {
|
|
$data[] = ['code_account', 'like', "%{$getData['code_account']}%"];
|
|
}
|
|
if (isset($getData['order_no']) && !empty($getData['order_no'])) {
|
|
$data[] = ['order_no', 'like', "%{$getData['order_no']}%"];
|
|
}
|
|
if (isset($getData['passageway']) && $getData['passageway'] != -1) {
|
|
$data[] = ['passageway', '=', $getData['passageway']];
|
|
}
|
|
if (isset($getData['state']) && $getData['state'] != -1) {
|
|
$data[] = ['state', '=', $getData['state']];
|
|
}
|
|
if($this->store['user']['is_super'] == 0) {
|
|
$data[] = ['store_user_id', 'in', User::getScope($this->store['user'])];
|
|
}
|
|
$money_list = SettingModel::getItem('trade')['money_list'];
|
|
$money_arr = explode(',',$money_list);
|
|
$arr = [];
|
|
foreach ((array)$money_arr as $val){
|
|
$arr[] = [
|
|
'money' => $val,
|
|
'num' => $model->getCodeTotal(array_merge([['money', '=', (float)$val]],$data))
|
|
];
|
|
}
|
|
return $this->renderSuccess(['money'=>$arr]);
|
|
}
|
|
|
|
/**
|
|
* 添加支付码
|
|
* @return array|mixed
|
|
* @throws \think\Exception
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
* @throws \think\exception\DbException
|
|
*/
|
|
public function add()
|
|
{
|
|
$model = new CodeModel;
|
|
$data = $this->postForm();
|
|
$codeModel = CodeModel::detail(['order_no'=>$data['order_no']]);
|
|
if(isset($codeModel['code_id'])){
|
|
return $this->renderError('苏宁单号重复');
|
|
}
|
|
$data['store_user_id'] = $this->store['user']['store_user_id'];
|
|
$data['money'] = round($data['money'],2);
|
|
// 新增记录
|
|
if ($model->add($data)) {
|
|
if($data['state'] == 1) {
|
|
//写入队列
|
|
$code_queue = ['code_id' => (int)$model->{$model->getPk()}, 'expire_time' => time() + (float)$data['expiration_time']]; // 过期时间
|
|
$queue_name = $data['passageway'] . '_' . round($data['money'],2);
|
|
//先清楚队列再加入,避免重复
|
|
Cache::store('redis')->lRem($queue_name, json_encode($code_queue, 320), 1);
|
|
Cache::store('redis')->rPush($queue_name, json_encode($code_queue, 320));
|
|
|
|
/*$queue_name = $data['shop_id'] . '_' . $data['passageway'] . '_' . round($data['money'],2);
|
|
//店铺加入队列
|
|
$shop_queue = 'shop_list';
|
|
$elements = Cache::store('redis')->lRange($shop_queue, 0, -1);
|
|
if(!in_array($data['shop_id'],$elements)){
|
|
Cache::store('redis')->rPush($shop_queue, $data['shop_id']);
|
|
}
|
|
//先清楚队列再加入,避免重复
|
|
Cache::store('redis')->lRem($queue_name, json_encode($code_queue, 320), 1);
|
|
Cache::store('redis')->rPush($queue_name, json_encode($code_queue, 320));*/
|
|
}
|
|
/*$string = Cache::store('redis')->rPop($data['passageway'].'_'.$data['money']);
|
|
var_dump($string);
|
|
var_dump(json_decode($string,true));
|
|
die();*/
|
|
return $this->renderSuccess('添加成功');
|
|
}
|
|
return $this->renderError($model->getError() ?: '添加失败');
|
|
}
|
|
|
|
/**
|
|
* 产码详情
|
|
* @param int $codeId
|
|
* @return Json
|
|
* @throws BaseException
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
* @throws \cores\exception\BaseException
|
|
*/
|
|
public function detail(int $codeId): Json
|
|
{
|
|
$codeInfo = CodeModel::detail($codeId);
|
|
$domain = Request::instance()->domain();
|
|
$url = $domain . '/wxScanPng.php';
|
|
$codeInfo['qrcode'] = $url . '?url=' . urlencode(htmlspecialchars_decode($codeInfo['link']));
|
|
return $this->renderSuccess(compact('codeInfo'));
|
|
}
|
|
|
|
/**
|
|
* 编辑产码
|
|
* @param int $codeId
|
|
* @return Json
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
* @throws \cores\exception\BaseException
|
|
*/
|
|
public function edit(int $codeId): Json
|
|
{
|
|
// 商品详情
|
|
$model = CodeModel::detail($codeId);
|
|
$data = $this->postForm();
|
|
// 更新记录
|
|
if ($model->edit($data)) {
|
|
return $this->renderSuccess('更新成功');
|
|
}
|
|
return $this->renderError($model->getError() ?: '更新失败');
|
|
}
|
|
|
|
/**
|
|
* 删除支付码
|
|
* @return array
|
|
* @throws \think\exception\DbException
|
|
*/
|
|
public function delete(): Json
|
|
{
|
|
$orderModel = new OrderModel();
|
|
$arr = $this->postData();
|
|
foreach ((array)$arr as $code_id) {
|
|
$model = CodeModel::detail($code_id);
|
|
if ($model->setDelete()) {
|
|
$orderModel->overtimeUpdate(['code_id' => $code_id, 'order_status' => 10]);
|
|
}
|
|
}
|
|
return $this->renderSuccess('删除成功');
|
|
}
|
|
|
|
/**
|
|
* 修改产码列表状态
|
|
* @return Json
|
|
*/
|
|
public function changeState(): Json
|
|
{
|
|
$post = $this->postData();
|
|
$status = $post['state']?1:2;
|
|
// 商品详情
|
|
foreach ((array)$post['codeIds'] as $code_id) {
|
|
$model = CodeModel::detail($code_id);
|
|
$warn = $model->changeStatus($status);
|
|
if ($warn !== true) {
|
|
return $this->renderError($warn);
|
|
}
|
|
}
|
|
return $this->renderSuccess($post);
|
|
}
|
|
|
|
/**
|
|
* 切换支付码的启停状态
|
|
* @return Json
|
|
*/
|
|
public function stateSwitch(): Json
|
|
{
|
|
$post = $this->postData();
|
|
$state = $post['state']?1:2;
|
|
// 商品详情
|
|
foreach ((array)$post['codeIds'] as $code_id) {
|
|
$model = CodeModel::detail($code_id);
|
|
if($state != $model['state']) {
|
|
$warn = $model->stateSwitch($state);
|
|
if ($warn !== true) {
|
|
return $this->renderError($warn);
|
|
}
|
|
// 未使用才进行处理
|
|
if ($model['status'] == 1) {
|
|
$create_time = is_numeric($model['create_time']) ? $model['create_time'] : strtotime($model['create_time']);
|
|
$code_queue = ['code_id' => (int)$model['code_id'], 'expire_time' => $create_time + (float)$model['expiration_time']]; // 过期时间
|
|
$code_json = json_encode($code_queue, 320);
|
|
$queue_name = $model['passageway'] . '_' . round($model['money'], 2);
|
|
if ($state == 1) {
|
|
//加入队列
|
|
Cache::store('redis')->rPush($queue_name, $code_json);
|
|
} else {
|
|
//清楚队列
|
|
Cache::store('redis')->lRem($queue_name, $code_json, 2);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return $this->renderSuccess($post);
|
|
}
|
|
}
|