From d44cad80c965ac298e853e921d8c577b9026912b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=AF=E7=AB=8B?= Date: Mon, 13 Sep 2021 12:11:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=95=86=E5=9F=8E=E6=A8=A1=E5=9E=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/data/command/OrderClean.php | 19 ++++--- app/data/command/UserAgent.php | 25 +++++---- app/data/command/UserAmount.php | 5 +- app/data/command/UserTransfer.php | 17 +++--- app/data/command/UserUpgrade.php | 5 +- app/data/controller/api/Goods.php | 1 - app/data/controller/api/Notify.php | 2 + app/data/controller/api/Wechat.php | 3 +- app/data/controller/api/Wxapp.php | 4 +- app/data/controller/api/auth/Address.php | 37 ++++++------- app/data/controller/api/auth/Center.php | 1 - app/data/controller/api/auth/News.php | 4 ++ app/data/controller/api/auth/Order.php | 54 +++++++++---------- app/data/controller/api/auth/Rebate.php | 23 ++++---- app/data/controller/api/auth/Transfer.php | 25 ++++----- app/data/controller/base/postage/Company.php | 3 +- app/data/model/DataUser.php | 15 ++++++ app/data/model/DataUserAddress.php | 15 ++++++ app/data/model/DataUserBalance.php | 15 ++++++ app/data/model/DataUserMessage.php | 15 ++++++ app/data/model/DataUserPayment.php | 15 ++++++ app/data/model/DataUserRebate.php | 15 ++++++ app/data/model/DataUserToken.php | 15 ++++++ app/data/model/DataUserTransfer.php | 15 ++++++ app/data/model/ShopOrder.php | 15 ++++++ app/data/model/ShopOrderItem.php | 15 ++++++ app/data/service/MessageService.php | 5 +- app/data/service/PaymentService.php | 12 +++-- app/data/service/RebateService.php | 49 ++++++++--------- app/data/service/UserAdminService.php | 31 +++++------ app/data/service/UserBalanceService.php | 14 ++--- app/data/service/UserRebateService.php | 23 ++++---- app/data/service/UserTokenService.php | 22 ++++---- app/data/service/UserTransferService.php | 17 +++--- app/data/service/UserUpgradeService.php | 36 +++++++------ .../service/payment/AlipayPaymentService.php | 19 ++++--- .../service/payment/BalancePyamentService.php | 10 ++-- .../service/payment/EmptyPaymentService.php | 14 +++-- .../service/payment/VoucherPaymentService.php | 5 +- 39 files changed, 401 insertions(+), 234 deletions(-) create mode 100644 app/data/model/DataUser.php create mode 100644 app/data/model/DataUserAddress.php create mode 100644 app/data/model/DataUserBalance.php create mode 100644 app/data/model/DataUserMessage.php create mode 100644 app/data/model/DataUserPayment.php create mode 100644 app/data/model/DataUserRebate.php create mode 100644 app/data/model/DataUserToken.php create mode 100644 app/data/model/DataUserTransfer.php create mode 100644 app/data/model/ShopOrder.php create mode 100644 app/data/model/ShopOrderItem.php diff --git a/app/data/command/OrderClean.php b/app/data/command/OrderClean.php index af64621d2..e1e2aefc0 100644 --- a/app/data/command/OrderClean.php +++ b/app/data/command/OrderClean.php @@ -2,11 +2,14 @@ namespace app\data\command; +use app\data\model\ShopOrder; +use app\data\model\ShopOrderItem; use app\data\service\OrderService; use think\admin\Command; use think\admin\Exception; use think\console\Input; use think\console\Output; +use think\Model; /** * 商城订单自动清理 @@ -43,10 +46,10 @@ class OrderClean extends Command try { $map = [['status', '<', 3], ['payment_status', '=', 0]]; $map[] = ['create_at', '<', date('Y-m-d H:i:s', strtotime('-30 minutes'))]; - [$total, $count] = [$this->app->db->name('ShopOrder')->where($map)->count(), 0]; - $this->app->db->name('ShopOrder')->where($map)->select()->map(function ($item) use ($total, &$count) { + [$total, $count] = [ShopOrder::mk()->where($map)->count(), 0]; + ShopOrder::mk()->where($map)->select()->map(function (Model $item) use ($total, &$count) { $this->queue->message($total, ++$count, "开始取消未支付的订单 {$item['order_no']}"); - $this->app->db->name('ShopOrder')->where(['order_no' => $item['order_no']])->update([ + $item->save([ 'status' => 0, 'cancel_status' => 1, 'cancel_datetime' => date('Y-m-d H:i:s'), @@ -69,13 +72,13 @@ class OrderClean extends Command try { $map = [['status', '=', 0], ['payment_status', '=', 0]]; $map[] = ['create_at', '<', date('Y-m-d H:i:s', strtotime('-3 days'))]; - [$total, $count] = [$this->app->db->name('ShopOrder')->where($map)->count(), 0]; - foreach ($this->app->db->name('ShopOrder')->where($map)->cursor() as $item) { + [$total, $count] = [ShopOrder::mk()->where($map)->count(), 0]; + ShopOrder::mk()->where($map)->select()->map(function (Model $item) use ($total, &$count) { $this->queue->message($total, ++$count, "开始清理已取消的订单 {$item['order_no']}"); - $this->app->db->name('ShopOrder')->where(['order_no' => $item['order_no']])->delete(); - $this->app->db->name('ShopOrderItem')->where(['order_no' => $item['order_no']])->delete(); + ShopOrder::mk()->where(['order_no' => $item['order_no']])->delete(); + ShopOrderItem::mk()->where(['order_no' => $item['order_no']])->delete(); $this->queue->message($total, $count, "完成清理已取消的订单 {$item['order_no']}", 1); - } + }); } catch (\Exception $exception) { $this->queue->error($exception->getMessage()); } diff --git a/app/data/command/UserAgent.php b/app/data/command/UserAgent.php index ce19b56f7..71affb4da 100644 --- a/app/data/command/UserAgent.php +++ b/app/data/command/UserAgent.php @@ -2,11 +2,16 @@ namespace app\data\command; +use app\data\model\DataUser; use app\data\service\UserUpgradeService; use think\admin\Command; +use think\admin\Exception; use think\console\Input; use think\console\input\Argument; use think\console\Output; +use think\db\exception\DataNotFoundException; +use think\db\exception\DbException; +use think\db\exception\ModelNotFoundException; /** * 更新用户代理关系 @@ -27,10 +32,10 @@ class UserAgent extends Command * @param Input $input * @param Output $output * @return void - * @throws \think\admin\Exception - * @throws \think\db\exception\DataNotFoundException - * @throws \think\db\exception\DbException - * @throws \think\db\exception\ModelNotFoundException + * @throws Exception + * @throws DataNotFoundException + * @throws DbException + * @throws ModelNotFoundException */ protected function execute(Input $input, Output $output) { @@ -39,11 +44,11 @@ class UserAgent extends Command if (empty($puid)) $this->setQueueError("参数PID无效,请传入正确的参数!"); // 检查当前用户资料 - $user = $this->app->db->name('DataUser')->where(['id' => $uuid])->find(); + $user = DataUser::mk()->where(['id' => $uuid])->find(); if (empty($user)) $this->setQueueError("读取用户数据失败!"); // 检查上级代理用户 - $parant = $this->app->db->name('DataUser')->where(['id' => $puid])->find(); + $parant = DataUser::mk()->where(['id' => $puid])->find(); if (empty($parant)) $this->setQueueError('读取代理数据失败!'); // 检查异常关系处理 @@ -53,7 +58,7 @@ class UserAgent extends Command // 更新自己的代理关系 $path1 = rtrim($parant['path'] ?: '-', '-') . "-{$parant['id']}-"; - $this->app->db->name('DataUser')->where(['id' => $user['id']])->update([ + DataUser::mk()->where(['id' => $user['id']])->update([ 'path' => $path1, 'layer' => substr_count($path1, '-'), 'pid0' => $parant['id'], 'pid1' => $parant['id'], 'pid2' => $parant['pid1'], ]); @@ -62,13 +67,13 @@ class UserAgent extends Command // 更新下级的代理关系 $path2 = "{$user['path']}{$user['id']}-"; - [$total, $count] = [$this->app->db->name('DataUser')->whereLike('path', "{$path2}%")->count(), 0]; - foreach ($this->app->db->name('DataUser')->whereLike('path', "{$path2}%")->order('layer desc')->select() as $vo) { + [$total, $count] = [DataUser::mk()->whereLike('path', "{$path2}%")->count(), 0]; + foreach (DataUser::mk()->whereLike('path', "{$path2}%")->order('layer desc')->select() as $vo) { $this->setQueueMessage($total, ++$count, "开始更新下级用户[{$vo['id']}]代理绑定!"); // 更新下级用户代理数据 $path3 = preg_replace("#^{$path2}#", "{$path1}{$user['id']}-", $vo['path']); $attrs = array_reverse(str2arr($path3, '-')); - $this->app->db->name('DataUser')->where(['id' => $vo['id']])->update([ + DataUser::mk()->where(['id' => $vo['id']])->update([ 'path' => $path3, 'layer' => substr_count($path3, '-'), 'pid0' => $attrs[0] ?? 0, 'pid1' => $attrs[0] ?? 0, 'pid2' => $attrs[1] ?? 0, ]); diff --git a/app/data/command/UserAmount.php b/app/data/command/UserAmount.php index c50fba2f9..ba7127a30 100644 --- a/app/data/command/UserAmount.php +++ b/app/data/command/UserAmount.php @@ -2,6 +2,7 @@ namespace app\data\command; +use app\data\model\DataUser; use app\data\service\UserBalanceService; use app\data\service\UserRebateService; use think\admin\Command; @@ -30,8 +31,8 @@ class UserAmount extends Command */ protected function execute(Input $input, Output $output) { - [$total, $count, $error] = [$this->app->db->name('DataUser')->count(), 0, 0]; - foreach ($this->app->db->name('DataUser')->field('id')->cursor() as $user) try { + [$total, $count, $error] = [DataUser::mk()->count(), 0, 0]; + foreach (DataUser::mk()->field('id')->cursor() as $user) try { $this->queue->message($total, ++$count, "刷新用户 [{$user['id']}] 余额及返利开始"); UserRebateService::instance()->amount($user['id']); UserBalanceService::instance()->amount($user['id']); diff --git a/app/data/command/UserTransfer.php b/app/data/command/UserTransfer.php index f5fa55c97..bb7aee3c6 100644 --- a/app/data/command/UserTransfer.php +++ b/app/data/command/UserTransfer.php @@ -2,6 +2,7 @@ namespace app\data\command; +use app\data\model\DataUserTransfer; use app\data\service\UserRebateService; use think\admin\Command; use think\admin\Exception; @@ -41,8 +42,8 @@ class UserTransfer extends Command protected function execute(Input $input, Output $output) { $map = [['type', 'in', ['wechat_banks', 'wechat_wallet']], ['status', 'in', [3, 4]]]; - [$total, $count, $error] = [$this->app->db->name('DataUserTransfer')->where($map)->count(), 0, 0]; - foreach ($this->app->db->name('DataUserTransfer')->where($map)->cursor() as $vo) try { + [$total, $count, $error] = [DataUserTransfer::mk()->where($map)->count(), 0, 0]; + foreach (DataUserTransfer::mk()->where($map)->cursor() as $vo) try { $this->queue->message($total, ++$count, "开始处理订单 {$vo['code']} 提现"); if ($vo['status'] === 3) { $this->queue->message($total, $count, "尝试处理订单 {$vo['code']} 打款", 1); @@ -52,7 +53,7 @@ class UserTransfer extends Command [$config, $result] = $this->createTransferWallet($vo); } if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') { - $this->app->db->name('DataUserTransfer')->where(['code' => $vo['code']])->update([ + DataUserTransfer::mk()->where(['code' => $vo['code']])->update([ 'status' => 4, 'appid' => $config['appid'], 'openid' => $config['openid'], @@ -62,7 +63,7 @@ class UserTransfer extends Command 'change_desc' => '创建微信提现成功', ]); } else { - $this->app->db->name('DataUserTransfer')->where(['code' => $vo['code']])->update([ + DataUserTransfer::mk()->where(['code' => $vo['code']])->update([ 'change_time' => date('Y-m-d H:i:s'), 'change_desc' => $result['err_code_des'] ?? '线上提现失败', ]); } @@ -77,7 +78,7 @@ class UserTransfer extends Command } catch (\Exception $exception) { $error++; $this->queue->message($total, $count, "处理提现订单 {$vo['code']} 失败, {$exception->getMessage()}", 1); - $this->app->db->name('DataUserTransfer')->where(['code' => $vo['code']])->update([ + DataUserTransfer::mk()->where(['code' => $vo['code']])->update([ 'change_time' => date('Y-m-d H:i:s'), 'change_desc' => $exception->getMessage(), ]); } @@ -217,7 +218,7 @@ class UserTransfer extends Command $result = TransfersBank::instance($config)->query($item['trade_no']); if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') { if ($result['status'] === 'SUCCESS') { - $this->app->db->name('DataUserTransfer')->where(['code' => $item['code']])->update([ + DataUserTransfer::mk()->where(['code' => $item['code']])->update([ 'status' => 5, 'appid' => $config['appid'], 'openid' => $config['openid'], @@ -227,7 +228,7 @@ class UserTransfer extends Command ]); } if (in_array($result['status'], ['FAILED', 'BANK_FAIL'])) { - $this->app->db->name('DataUserTransfer')->where(['code' => $item['code']])->update([ + DataUserTransfer::mk()->where(['code' => $item['code']])->update([ 'status' => 0, 'change_time' => date('Y-m-d H:i:s'), 'change_desc' => '微信提现打款失败', @@ -254,7 +255,7 @@ class UserTransfer extends Command [$config['appid'], $config['openid']] = [$item['appid'], $item['openid']]; $result = Transfers::instance($config)->query($item['trade_no']); if ($result['return_code'] === 'SUCCESS' && $result['result_code'] === 'SUCCESS') { - $this->app->db->name('DataUserTransfer')->where(['code' => $item['code']])->update([ + DataUserTransfer::mk()->where(['code' => $item['code']])->update([ 'status' => 5, 'appid' => $config['appid'], 'openid' => $config['openid'], diff --git a/app/data/command/UserUpgrade.php b/app/data/command/UserUpgrade.php index b4f2fb82a..613e0e598 100644 --- a/app/data/command/UserUpgrade.php +++ b/app/data/command/UserUpgrade.php @@ -2,6 +2,7 @@ namespace app\data\command; +use app\data\model\DataUser; use app\data\service\UserUpgradeService; use think\admin\Command; use think\admin\Exception; @@ -30,8 +31,8 @@ class UserUpgrade extends Command protected function execute(Input $input, Output $output) { try { - [$total, $count] = [$this->app->db->name('DataUser')->count(), 0]; - foreach ($this->app->db->name('DataUser')->field('id')->cursor() as $user) { + [$total, $count] = [DataUser::mk()->count(), 0]; + foreach (DataUser::mk()->field('id')->cursor() as $user) { $this->queue->message($total, ++$count, "正在计算用户 [{$user['id']}] 的等级"); UserUpgradeService::instance()->upgrade($user['id']); $this->queue->message($total, $count, "完成计算用户 [{$user['id']}] 的等级", 1); diff --git a/app/data/controller/api/Goods.php b/app/data/controller/api/Goods.php index d96a6b6ae..4b7249214 100644 --- a/app/data/controller/api/Goods.php +++ b/app/data/controller/api/Goods.php @@ -57,5 +57,4 @@ class Goods extends Controller { $this->success('获取区域成功', ExpressService::instance()->region(3, 1)); } - } \ No newline at end of file diff --git a/app/data/controller/api/Notify.php b/app/data/controller/api/Notify.php index 14510e13d..3ac7797f3 100644 --- a/app/data/controller/api/Notify.php +++ b/app/data/controller/api/Notify.php @@ -6,6 +6,8 @@ use app\data\service\payment\AlipayPaymentService; use app\data\service\payment\JoinpayPaymentService; use app\data\service\payment\WechatPaymentService; use think\admin\Controller; +use think\admin\Exception; +use WeChat\Exceptions\InvalidResponseException; /** * 异步通知处理 diff --git a/app/data/controller/api/Wechat.php b/app/data/controller/api/Wechat.php index 3c535ecd3..1ce9d4ee6 100644 --- a/app/data/controller/api/Wechat.php +++ b/app/data/controller/api/Wechat.php @@ -5,6 +5,7 @@ namespace app\data\controller\api; use app\data\service\UserAdminService; use app\wechat\service\WechatService; use think\admin\Controller; +use think\admin\Exception; use think\Response; /** @@ -64,7 +65,7 @@ class Wechat extends Controller /** * 加载网页授权数据 - * @return \think\Response + * @return Response * @throws \WeChat\Exceptions\InvalidResponseException * @throws \WeChat\Exceptions\LocalCacheException * @throws \think\admin\Exception diff --git a/app/data/controller/api/Wxapp.php b/app/data/controller/api/Wxapp.php index a6d279398..1f5890d61 100644 --- a/app/data/controller/api/Wxapp.php +++ b/app/data/controller/api/Wxapp.php @@ -72,7 +72,8 @@ class Wxapp extends Controller /** * 小程序数据解密 - * @throws HttpResponseException + * @throws \think\admin\Exception + * @throws \think\db\exception\DbException */ public function decode() { @@ -106,6 +107,7 @@ class Wxapp extends Controller * 授权CODE换取会话信息 * @param string $code 换取授权CODE * @return array [openid, sessionkey] + * @throws \WeChat\Exceptions\LocalCacheException */ private function _getSessionKey(string $code): array { diff --git a/app/data/controller/api/auth/Address.php b/app/data/controller/api/auth/Address.php index 7fa4ea434..713f86796 100644 --- a/app/data/controller/api/auth/Address.php +++ b/app/data/controller/api/auth/Address.php @@ -3,6 +3,7 @@ namespace app\data\controller\api\auth; use app\data\controller\api\Auth; +use app\data\model\DataUserAddress; use think\admin\extend\CodeExtend; /** @@ -12,15 +13,11 @@ use think\admin\extend\CodeExtend; */ class Address extends Auth { - /** - * 绑定数据表 - * @var string - */ - private $table = 'DataUserAddress'; - /** * 添加收货地址 + * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException */ public function set() { @@ -43,22 +40,22 @@ class Address extends Auth ]); if (empty($data['code'])) { unset($data['code']); - $count = $this->app->db->name($this->table)->where($data)->count(); + $count = DataUserAddress::mk()->where($data)->count(); if ($count > 0) $this->error('抱歉,该地址已经存在!'); $data['code'] = CodeExtend::uniqidDate(20, 'A'); - if ($this->app->db->name($this->table)->insert($data) === false) { + if (DataUserAddress::mk()->insert($data) === false) { $this->error('添加地址失败!'); } } else { $map = ['uuid' => $this->uuid, 'code' => $data['code']]; - $address = $this->app->db->name($this->table)->where($map)->find(); + $address = DataUserAddress::mk()->where($map)->find(); if (empty($address)) $this->error('修改地址不存在!'); - $this->app->db->name($this->table)->where($map)->update($data); + DataUserAddress::mk()->where($map)->update($data); } // 去除其它默认选项 if (isset($data['type']) && $data['type'] > 0) { $map = [['uuid', '=', $this->uuid], ['code', '<>', $data['code']]]; - $this->app->db->name($this->table)->where($map)->update(['type' => 0]); + DataUserAddress::mk()->where($map)->update(['type' => 0]); } $this->success('地址保存成功!', $this->_getAddress($data['code'])); } @@ -71,7 +68,7 @@ class Address extends Auth */ public function get() { - $query = $this->_query($this->table)->withoutField('deleted'); + $query = $this->_query('DataUserAddress')->withoutField('deleted'); $query->equal('code')->where(['uuid' => $this->uuid, 'deleted' => 0]); $result = $query->order('type desc,id desc')->page(false, false, false, 15); $this->success('获取地址数据!', $result); @@ -79,7 +76,9 @@ class Address extends Auth /** * 修改地址状态 + * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException */ public function state() { @@ -91,32 +90,34 @@ class Address extends Auth ]); // 检查地址是否存在 $map = ['uuid' => $data['uuid'], 'code' => $data['code']]; - if ($this->app->db->name($this->table)->where($map)->count() < 1) { + if (DataUserAddress::mk()->where($map)->count() < 1) { $this->error('修改的地址不存在!'); } // 更新默认地址状态 $data['type'] = intval($data['type']); - $this->app->db->name($this->table)->where($map)->update(['type' => $data['type']]); + DataUserAddress::mk()->where($map)->update(['type' => $data['type']]); // 去除其它默认选项 if ($data['type'] > 0) { $map = [['uuid', '=', $this->uuid], ['code', '<>', $data['code']]]; - $this->app->db->name($this->table)->where($map)->update(['type' => 0]); + DataUserAddress::mk()->where($map)->update(['type' => 0]); } $this->success('默认设置成功!', $this->_getAddress($data['code'])); } /** * 删除收货地址 + * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException */ public function remove() { $map = $this->_vali([ 'uuid.value' => $this->uuid, 'code.require' => '地址编号不能为空!', ]); - $address = $this->app->db->name($this->table)->where($map)->find(); + $address = DataUserAddress::mk()->where($map)->find(); if (empty($address)) $this->error('需要删除的地址不存在!'); - if ($this->app->db->name($this->table)->where($map)->update(['deleted' => 1]) !== false) { + if (DataUserAddress::mk()->where($map)->update(['deleted' => 1]) !== false) { $this->success('删除地址成功!'); } else { $this->error('删除地址失败!'); @@ -134,7 +135,7 @@ class Address extends Auth private function _getAddress(string $code): ?array { $map = ['code' => $code, 'uuid' => $this->uuid, 'deleted' => 0]; - return $this->app->db->name($this->table)->withoutField('deleted')->where($map)->find(); + return DataUserAddress::mk()->withoutField('deleted')->where($map)->find(); } } \ No newline at end of file diff --git a/app/data/controller/api/auth/Center.php b/app/data/controller/api/auth/Center.php index 64fdb3f01..c932ee387 100644 --- a/app/data/controller/api/auth/Center.php +++ b/app/data/controller/api/auth/Center.php @@ -141,7 +141,6 @@ class Center extends Auth /** * 绑定用户邀请人 - * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException diff --git a/app/data/controller/api/auth/News.php b/app/data/controller/api/auth/News.php index 80049a53e..69ad707ca 100644 --- a/app/data/controller/api/auth/News.php +++ b/app/data/controller/api/auth/News.php @@ -116,7 +116,9 @@ class News extends Auth /** * 获取用户收藏的资讯 + * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException */ public function getCollect() { @@ -171,7 +173,9 @@ class News extends Auth /** * 获取用户收藏的资讯 + * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException */ public function getLike() { diff --git a/app/data/controller/api/auth/Order.php b/app/data/controller/api/auth/Order.php index e0db0e0bf..f4aaa7f04 100644 --- a/app/data/controller/api/auth/Order.php +++ b/app/data/controller/api/auth/Order.php @@ -3,6 +3,9 @@ namespace app\data\controller\api\auth; use app\data\controller\api\Auth; +use app\data\model\DataUserAddress; +use app\data\model\ShopOrder; +use app\data\model\ShopOrderItem; use app\data\service\ExpressService; use app\data\service\GoodsService; use app\data\service\OrderService; @@ -38,7 +41,7 @@ class Order extends Auth public function get() { $map = ['uuid' => $this->uuid, 'deleted_status' => 0]; - $query = $this->_query('ShopOrder')->in('status')->equal('order_no'); + $query = $this->_query(ShopOrder::class)->in('status')->equal('order_no'); $result = $query->where($map)->order('id desc')->page(true, false, false, 20); if (count($result['list']) > 0) OrderService::instance()->buildData($result['list']); $this->success('获取订单数据成功!', $result); @@ -82,7 +85,7 @@ class Order extends Auth // 限制购买数量 if (isset($goods['limit_max_num']) && $goods['limit_max_num'] > 0) { $map = [['a.uuid', '=', $this->uuid], ['a.status', 'in', [2, 3, 4, 5]], ['b.goods_code', '=', $goods['code']]]; - $buys = $this->app->db->name('ShopOrderr')->alias('a')->join('store_order_item b', 'a.order_no=b.order_no')->where($map)->sum('b.stock_sales'); + $buys = ShopOrder::mk()->alias('a')->join('store_order_item b', 'a.order_no=b.order_no')->where($map)->sum('b.stock_sales'); if ($buys + $count > $goods['limit_max_num']) $this->error('超过限购数量'); } // 限制购买身份 @@ -166,8 +169,8 @@ class Order extends Auth $order['amount_total'] = $order['amount_goods']; // 写入商品数据 $this->app->db->transaction(function () use ($order, $items) { - $this->app->db->name('ShopOrder')->insert($order); - $this->app->db->name('ShopOrderItem')->insertAll($items); + ShopOrder::mk()->insert($order); + ShopOrderItem::mk()->insertAll($items); }); // 同步商品库存销量 foreach (array_unique(array_column($items, 'goods_code')) as $code) { @@ -211,14 +214,14 @@ class Order extends Auth ]); // 用户收货地址 $map = ['uuid' => $this->uuid, 'code' => $data['code']]; - $addr = $this->app->db->name('DataUserAddress')->where($map)->find(); + $addr = DataUserAddress::mk()->where($map)->find(); if (empty($addr)) $this->error('收货地址异常'); // 订单状态检查 $map = ['uuid' => $this->uuid, 'order_no' => $data['order_no']]; - $tCount = $this->app->db->name('ShopOrderItem')->where($map)->sum('truck_number'); + $tCount = ShopOrderItem::mk()->where($map)->sum('truck_number'); // 根据地址计算运费 $map = ['status' => 1, 'deleted' => 0, 'order_no' => $data['order_no']]; - $tCode = $this->app->db->name('ShopOrderItem')->where($map)->column('truck_code'); + $tCode = ShopOrderItem::mk()->where($map)->column('truck_code'); [$amount, , , $remark] = ExpressService::instance()->amount($tCode, $addr['province'], $addr['city'], $tCount); $this->success('计算运费成功', ['amount' => $amount, 'remark' => $remark]); } @@ -238,17 +241,17 @@ class Order extends Auth ]); // 用户收货地址 $map = ['uuid' => $this->uuid, 'code' => $data['code'], 'deleted' => 0]; - $addr = $this->app->db->name('DataUserAddress')->where($map)->find(); + $addr = DataUserAddress::mk()->where($map)->find(); if (empty($addr)) $this->error('收货地址异常'); // 订单状态检查 $map1 = ['uuid' => $this->uuid, 'order_no' => $data['order_no']]; - $order = $this->app->db->name('ShopOrder')->where($map1)->whereIn('status', [1, 2])->find(); + $order = ShopOrder::mk()->where($map1)->whereIn('status', [1, 2])->find(); if (empty($order)) $this->error('不能修改地址'); if (empty($order['truck_type'])) $this->success('无需快递配送', ['order_no' => $order['order_no']]); // 根据地址计算运费 $map2 = ['status' => 1, 'deleted' => 0, 'order_no' => $data['order_no']]; - $tCount = $this->app->db->name('ShopOrderItem')->where($map1)->sum('truck_number'); - $tCodes = $this->app->db->name('ShopOrderItem')->where($map2)->column('truck_code'); + $tCount = ShopOrderItem::mk()->where($map1)->sum('truck_number'); + $tCodes = ShopOrderItem::mk()->where($map2)->column('truck_code'); [$amount, $tCount, $tCode, $remark] = ExpressService::instance()->amount($tCodes, $addr['province'], $addr['city'], $tCount); // 创建订单发货信息 $express = [ @@ -283,7 +286,7 @@ class Order extends Auth if ($update['amount_total'] <= 0) $update['amount_total'] = 0.00; // 更新用户订单数据 $map = ['uuid' => $this->uuid, 'order_no' => $data['order_no']]; - if ($this->app->db->name('ShopOrder')->where($map)->update($update) !== false) { + if (ShopOrder::mk()->where($map)->update($update) !== false) { // 触发订单确认事件 $this->app->event->trigger('ShopOrderPerfect', $order['order_no']); // 返回处理成功数据 @@ -299,7 +302,7 @@ class Order extends Auth public function channel() { $data = $this->_vali(['uuid.value' => $this->uuid, 'order_no.require' => '单号不能为空']); - $payments = $this->app->db->name('ShopOrder')->where($data)->value('payment_allow'); + $payments = ShopOrder::mk()->where($data)->value('payment_allow'); if (empty($payments)) $this->error('获取订单支付参数失败'); // 读取支付通道配置 $query = $this->app->db->name('BaseUserPayment')->where(['status' => 1, 'deleted' => 0]); @@ -311,9 +314,7 @@ class Order extends Auth /** * 获取订单支付状态 - * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException - * @throws \think\db\exception\ModelNotFoundException */ public function payment() { @@ -330,7 +331,7 @@ class Order extends Auth if ($order['payment_status'] > 0) $this->error('已经完成支付'); // 更新订单备注 if (!empty($data['order_remark'])) { - $this->app->db->name('ShopOrder')->where($map)->update([ + ShopOrder::mk()->where($map)->update([ 'order_remark' => $data['order_remark'], ]); } @@ -344,7 +345,7 @@ class Order extends Auth // 返回订单数据及支付发起参数 $type = $order['amount_real'] <= 0 ? 'empty' : $data['payment_code']; $param = PaymentService::instance($type)->create($openid, $order['order_no'], $order['amount_real'], '商城订单支付', '', $data['payment_back'], $data['payment_image']); - $this->success('获取支付参数', ['order' => $this->app->db->name('ShopOrder')->where($map)->find() ?: new \stdClass(), 'param' => $param]); + $this->success('获取支付参数', ['order' => ShopOrder::mk()->where($map)->find() ?: new \stdClass(), 'param' => $param]); } catch (HttpResponseException $exception) { throw $exception; } catch (\Exception $exception) { @@ -362,7 +363,7 @@ class Order extends Auth { [$map, $order] = $this->getOrderData(); if (in_array($order['status'], [1, 2, 3])) { - $result = $this->app->db->name('ShopOrder')->where($map)->update([ + $result = ShopOrder::mk()->where($map)->update([ 'status' => 0, 'cancel_status' => 1, 'cancel_remark' => '用户主动取消订单', @@ -383,16 +384,14 @@ class Order extends Auth /** * 用户主动删除已取消的订单 - * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException - * @throws \think\db\exception\ModelNotFoundException */ public function remove() { [$map, $order] = $this->getOrderData(); if (empty($order)) $this->error('读取订单失败'); if (in_array($order['status'], [0])) { - $result = $this->app->db->name('ShopOrder')->where($map)->update([ + $result = ShopOrder::mk()->where($map)->update([ 'status' => 0, 'deleted_status' => 1, 'deleted_remark' => '用户主动删除订单', @@ -413,15 +412,13 @@ class Order extends Auth /** * 订单确认收货 - * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException - * @throws \think\db\exception\ModelNotFoundException */ public function confirm() { [$map, $order] = $this->getOrderData(); if (in_array($order['status'], [5])) { - if ($this->app->db->name('ShopOrder')->where($map)->update(['status' => 6]) !== false) { + if (ShopOrder::mk()->where($map)->update(['status' => 6]) !== false) { // 触发订单确认事件 $this->app->event->trigger('ShopOrderConfirm', $order['order_no']); // 返回处理成功数据 @@ -443,8 +440,11 @@ class Order extends Auth */ private function getOrderData(): array { - $map = $this->_vali(['uuid.value' => $this->uuid, 'order_no.require' => '单号不能为空']); - $order = $this->app->db->name('ShopOrder')->where($map)->find(); + $map = $this->_vali([ + 'uuid.value' => $this->uuid, + 'order_no.require' => '单号不能为空', + ]); + $order = ShopOrder::mk()->where($map)->find(); if (empty($order)) $this->error('读取订单失败'); return [$map, $order]; } @@ -455,7 +455,7 @@ class Order extends Auth public function total() { $data = ['t0' => 0, 't1' => 0, 't2' => 0, 't3' => 0, 't4' => 0, 't5' => 0, 't6' => 0]; - $query = $this->app->db->name('ShopOrder')->where(['uuid' => $this->uuid, 'deleted_status' => 0]); + $query = ShopOrder::mk()->where(['uuid' => $this->uuid, 'deleted_status' => 0]); foreach ($query->field('status,count(1) count')->group('status')->cursor() as $item) { $data["t{$item['status']}"] = $item['count']; } diff --git a/app/data/controller/api/auth/Rebate.php b/app/data/controller/api/auth/Rebate.php index ad158c86d..317cc8132 100644 --- a/app/data/controller/api/auth/Rebate.php +++ b/app/data/controller/api/auth/Rebate.php @@ -4,6 +4,9 @@ namespace app\data\controller\api\auth; use app\data\controller\api\Auth; use app\data\service\RebateService; +use think\db\exception\DataNotFoundException; +use think\db\exception\DbException; +use think\db\exception\ModelNotFoundException; /** * 用户返利管理 @@ -12,27 +15,21 @@ use app\data\service\RebateService; */ class Rebate extends Auth { - /** - * 绑定数据表 - * @var string - */ - private $table = 'DataUserRebate'; - /** * 获取用户返利记录 - * @throws \think\db\exception\DataNotFoundException - * @throws \think\db\exception\DbException - * @throws \think\db\exception\ModelNotFoundException + * @throws DataNotFoundException + * @throws DbException + * @throws ModelNotFoundException */ public function get() { $date = trim(input('date', date('Y-m')), '-'); [$map, $year] = [['uuid' => $this->uuid], substr($date, 0, 4)]; - $query = $this->_query($this->table)->where($map)->equal('type,status')->whereLike('date', "{$date}%"); + $query = $this->_query('DataUserRebate')->where($map)->equal('type,status')->whereLike('date', "{$date}%"); $this->success('获取返利统计', array_merge($query->order('id desc')->page(true, false, false, 10), [ 'total' => [ - '年度' => $this->_query($this->table)->where($map)->equal('type,status')->whereLike('date', "{$year}%")->db()->sum('amount'), - '月度' => $this->_query($this->table)->where($map)->equal('type,status')->whereLike('date', "{$date}%")->db()->sum('amount'), + '年度' => $this->_query('DataUserRebate')->where($map)->equal('type,status')->whereLike('date', "{$year}%")->db()->sum('amount'), + '月度' => $this->_query('DataUserRebate')->where($map)->equal('type,status')->whereLike('date', "{$date}%")->db()->sum('amount'), ], ])); } @@ -43,7 +40,7 @@ class Rebate extends Auth public function prize() { [$map, $data] = [['number' => $this->user['vip_code']], []]; - $prizes = $this->app->db->name($this->table)->group('name')->column('name'); + $prizes = $this->app->db->name('DataUserRebate')->group('name')->column('name'); $rebate = $this->app->db->name('BaseUserUpgrade')->where($map)->value('rebate_rule', ''); $codemap = array_merge($prizes, str2arr($rebate)); foreach (RebateService::PRIZES as $prize) { diff --git a/app/data/controller/api/auth/Transfer.php b/app/data/controller/api/auth/Transfer.php index f7e8ea80f..e88803059 100644 --- a/app/data/controller/api/auth/Transfer.php +++ b/app/data/controller/api/auth/Transfer.php @@ -3,6 +3,7 @@ namespace app\data\controller\api\auth; use app\data\controller\api\Auth; +use app\data\model\DataUserTransfer; use app\data\service\UserRebateService; use app\data\service\UserTransferService; use think\admin\extend\CodeExtend; @@ -14,12 +15,6 @@ use think\admin\extend\CodeExtend; */ class Transfer extends Auth { - /** - * 绑定数据表 - * @var string - */ - private $table = 'DataUserTransfer'; - /** * 提交提现处理 * @throws \think\db\exception\DataNotFoundException @@ -84,7 +79,7 @@ class Transfer extends Auth } // 当日提现次数限制 $map = ['uuid' => $this->uuid, 'type' => $data['type'], 'date' => $data['date']]; - $count = $this->app->db->name($this->table)->where($map)->count(); + $count = DataUserTransfer::mk()->where($map)->count(); if ($count >= $transfers[$data['type']]['dayNumber']) $this->error("当日提现次数受限"); // 提现金额范围控制 if ($transfers[$data['type']]['minAmount'] > $data['amount']) { @@ -94,7 +89,7 @@ class Transfer extends Auth $this->error("不能大于{$transfers[$data['type']]['minAmount']}元"); } // 写入用户提现数据 - if ($this->app->db->name($this->table)->insert($data) !== false) { + if (DataUserTransfer::mk()->insert($data) !== false) { UserRebateService::instance()->amount($this->uuid); $this->success('提现申请成功'); } else { @@ -110,7 +105,7 @@ class Transfer extends Auth */ public function get() { - $query = $this->_query($this->table)->where(['uuid' => $this->uuid]); + $query = $this->_query(DataUserTransfer::mk())->where(['uuid' => $this->uuid]); $result = $query->like('date,code')->in('status')->order('id desc')->page(true, false, false, 10); // 统计历史数据 $map = [['uuid', '=', $this->uuid], ['status', '>', 0]]; @@ -119,21 +114,20 @@ class Transfer extends Auth 'total' => [ '锁定' => $locks, '可提' => $total - $count, - '上月' => $this->app->db->name($this->table)->where($map)->whereLike('date', date("Y-m-%", strtotime('-1 month')))->sum('amount'), - '本月' => $this->app->db->name($this->table)->where($map)->whereLike('date', date("Y-m-%"))->sum('amount'), - '全年' => $this->app->db->name($this->table)->where($map)->whereLike('date', date("Y-%"))->sum('amount'), + '上月' => DataUserTransfer::mk()->where($map)->whereLike('date', date("Y-m-%", strtotime('-1 month')))->sum('amount'), + '本月' => DataUserTransfer::mk()->where($map)->whereLike('date', date("Y-m-%"))->sum('amount'), + '全年' => DataUserTransfer::mk()->where($map)->whereLike('date', date("Y-%"))->sum('amount'), ], ])); } /** * 用户取消提现 - * @throws \think\db\exception\DbException */ public function cancel() { $data = $this->_vali(['uuid.value' => $this->uuid, 'code.require' => '单号不能为空!']); - $this->app->db->name($this->table)->where($data)->whereIn('status', [1, 2, 3])->update([ + DataUserTransfer::mk()->where($data)->whereIn('status', [1, 2, 3])->update([ 'status' => 0, 'change_time' => date("Y-m-d H:i:s"), 'change_desc' => '用户主动取消提现', ]); UserRebateService::instance()->amount($this->uuid); @@ -142,12 +136,11 @@ class Transfer extends Auth /** * 用户确认提现 - * @throws \think\db\exception\DbException */ public function confirm() { $data = $this->_vali(['uuid.value' => $this->uuid, 'code.require' => '单号不能为空!']); - $this->app->db->name($this->table)->where($data)->whereIn('status', [4])->update([ + DataUserTransfer::mk()->where($data)->whereIn('status', [4])->update([ 'status' => 5, 'change_time' => date("Y-m-d H:i:s"), 'change_desc' => '用户主动确认收款', ]); UserRebateService::instance()->amount($this->uuid); diff --git a/app/data/controller/base/postage/Company.php b/app/data/controller/base/postage/Company.php index c24de7dc9..9f1a17a8b 100644 --- a/app/data/controller/base/postage/Company.php +++ b/app/data/controller/base/postage/Company.php @@ -4,7 +4,6 @@ namespace app\data\controller\base\postage; use app\data\service\ExpressService; use think\admin\Controller; -use think\admin\service\SystemService; use think\exception\HttpResponseException; /** @@ -99,7 +98,7 @@ class Company extends Controller try { $result = ExpressService::instance()->company(); if (empty($result['code'])) $this->error($result['info']); - foreach ($result['data'] as $vo) SystemService::instance()->save($this->table, [ + foreach ($result['data'] as $vo) data_save($this->table, [ 'code_1' => $vo['code_1'], 'code_2' => $vo['code_2'], 'code_3' => $vo['code_3'], 'name' => $vo['title'], 'deleted' => 0, ], 'code_1'); diff --git a/app/data/model/DataUser.php b/app/data/model/DataUser.php new file mode 100644 index 000000000..f6c33b980 --- /dev/null +++ b/app/data/model/DataUser.php @@ -0,0 +1,15 @@ +app->cache->get($ckey = md5("code-{$tplcode}-{$phone}"), []); + $cache = $this->app->cache->get(md5("code-{$tplcode}-{$phone}"), []); return is_array($cache) && isset($cache['code']) && $cache['code'] == $code; } @@ -154,7 +155,7 @@ class MessageService extends Service public function send(string $phone, string $content): array { [$state, $message, $record] = $this->_request('v2/sendSms', ['mobile' => $phone, 'content' => $content]); - $this->app->db->name('DataUserMessage')->insert([ + DataUserMessage::mk()->insert([ 'phone' => $phone, 'content' => $content, 'result' => $message, 'status' => $state ? 1 : 0, ]); return [$state, $message, $record]; diff --git a/app/data/service/PaymentService.php b/app/data/service/PaymentService.php index 5d6b4fd2d..71da7de99 100644 --- a/app/data/service/PaymentService.php +++ b/app/data/service/PaymentService.php @@ -2,6 +2,8 @@ namespace app\data\service; +use app\data\model\DataUserPayment; +use app\data\model\ShopOrder; use app\data\service\payment\AlipayPaymentService; use app\data\service\payment\BalancePyamentService; use app\data\service\payment\EmptyPaymentService; @@ -162,7 +164,7 @@ abstract class PaymentService * 根据配置实例支付服务 * @param string $code 支付配置编号 * @return JoinpayPaymentService|WechatPaymentService|AlipayPaymentService|BalancePyamentService|VoucherPaymentService|EmptyPaymentService - * @throws Exception + * @throws \think\admin\Exception */ public static function instance(string $code): PaymentService { @@ -291,7 +293,7 @@ abstract class PaymentService */ protected function createPaymentAction(string $orderNo, string $paymentTitle, string $paymentAmount) { - $this->app->db->name('DataUserPayment')->insert([ + DataUserPayment::mk()->insert([ 'payment_code' => $this->code, 'payment_type' => $this->type, 'order_no' => $orderNo, @@ -314,7 +316,7 @@ abstract class PaymentService protected function updatePaymentAction(string $orderNo, string $paymentTrade, string $paymentAmount, string $paymentRemark = '在线支付'): bool { // 更新支付记录 - data_save('DataUserPayment', [ + data_save(DataUserPayment::mk(), [ 'order_no' => $orderNo, 'payment_code' => $this->code, 'payment_type' => $this->type, @@ -345,7 +347,7 @@ abstract class PaymentService public function updateOrder(string $orderNo, string $paymentTrade, string $paymentAmount, string $paymentRemark = '在线支付', string $paymentImage = ''): bool { $map = ['status' => 2, 'order_no' => $orderNo, 'payment_status' => 0]; - $order = $this->app->db->name('ShopOrder')->where($map)->find(); + $order = ShopOrder::mk()->where($map)->find(); if (empty($order)) return false; // 检查订单支付状态 if ($this->type === self::PAYMENT_VOUCHER) { @@ -368,7 +370,7 @@ abstract class PaymentService 'payment_datetime' => date('Y-m-d H:i:s'), ]; if (empty($data['payment_type'])) unset($data['payment_type']); - $this->app->db->name('ShopOrder')->where($map)->update($data); + ShopOrder::mk()->where($map)->update($data); // 触发订单更新事件 if ($status >= 4) { $this->app->event->trigger('ShopOrderPayment', $orderNo); diff --git a/app/data/service/RebateService.php b/app/data/service/RebateService.php index 593e8d576..33a4fb4a2 100644 --- a/app/data/service/RebateService.php +++ b/app/data/service/RebateService.php @@ -2,6 +2,9 @@ namespace app\data\service; +use app\data\model\DataUser; +use app\data\model\DataUserRebate; +use app\data\model\ShopOrder; use think\admin\Exception; use think\admin\extend\CodeExtend; use think\admin\Service; @@ -58,16 +61,10 @@ class RebateService extends Service protected $from1; protected $from2; - /** - * 绑定数据表 - * @var string - */ - private $table = 'DataUserRebate'; - /** * 执行订单返利处理 * @param string $orderNo - * @throws Exception + * @throws \think\admin\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException @@ -76,7 +73,7 @@ class RebateService extends Service { // 获取订单数据 $map = ['order_no' => $orderNo, 'payment_status' => 1]; - $this->order = $this->app->db->name('ShopOrder')->where($map)->find(); + $this->order = ShopOrder::mk()->where($map)->find(); if (empty($this->order)) throw new Exception('订单不存在'); if ($this->order['payment_type'] === 'balance') return; // throw new Exception('余额支付不反利'); @@ -85,18 +82,18 @@ class RebateService extends Service if ($this->order['rebate_amount'] <= 0) throw new Exception('订单返利为零'); // 获取用户数据 $map = ['id' => $this->order['uuid'], 'deleted' => 0]; - $this->user = $this->app->db->name('DataUser')->where($map)->find(); + $this->user = DataUser::mk()->where($map)->find(); if (empty($this->user)) throw new Exception('用户不存在'); // 获取直接代理数据 if ($this->order['puid1'] > 0) { $map = ['id' => $this->order['puid1']]; - $this->from1 = $this->app->db->name('DataUser')->where($map)->find(); + $this->from1 = DataUser::mk()->where($map)->find(); if (empty($this->from1)) throw new Exception('直接代理不存在'); } // 获取间接代理数据 if ($this->order['puid2'] > 0) { $map = ['id' => $this->order['puid2']]; - $this->from2 = $this->app->db->name('DataUser')->where($map)->find(); + $this->from2 = DataUser::mk()->where($map)->find(); if (empty($this->from2)) throw new Exception('间接代理不存在'); } // 批量发放配置奖励 @@ -148,12 +145,12 @@ class RebateService extends Service { if (empty($this->from1)) return false; $map = ['order_uuid' => $this->user['id']]; - if ($this->app->db->name($this->table)->where($map)->count() > 0) return false; + if (DataUserRebate::mk()->where($map)->count() > 0) return false; if (!$this->checkPrizeStatus(self::PRIZE_01, $this->from1['vip_code'])) return false; // 创建返利奖励记录 $key = "{$this->from1['vip_code']}_{$this->user['vip_code']}"; $map = ['type' => self::PRIZE_01, 'order_no' => $this->order['order_no'], 'order_uuid' => $this->order['uuid']]; - if ($this->config("frist_state_vip_{$key}") && $this->app->db->name($this->table)->where($map)->count() < 1) { + if ($this->config("frist_state_vip_{$key}") && DataUserRebate::mk()->where($map)->count() < 1) { $value = $this->config("frist_value_vip_{$key}"); if ($this->config("frist_type_vip_{$key}") == 1) { $val = floatval($value ?: '0.00'); @@ -196,11 +193,10 @@ class RebateService extends Service * @param array $map * @param string $name * @param float $amount - * @throws \think\db\exception\DbException */ private function writeRabate(int $uuid, array $map, string $name, float $amount) { - $this->app->db->name($this->table)->insert(array_merge($map, [ + DataUserRebate::mk()->insert(array_merge($map, [ 'uuid' => $uuid, 'date' => date('Y-m-d'), 'code' => CodeExtend::uniqidDate(20, 'R'), @@ -227,14 +223,14 @@ class RebateService extends Service $map = []; $map[] = ['order_uuid', '=', $this->user['id']]; $map[] = ['order_no', '<>', $this->order['order_no']]; - if ($this->app->db->name($this->table)->where($map)->count() < 1) return false; + if (DataUserRebate::mk()->where($map)->count() < 1) return false; // 检查上级可否奖励 if (empty($this->from1) || empty($this->from1['vip_code'])) return false; if (!$this->checkPrizeStatus(self::PRIZE_02, $this->from1['vip_code'])) return false; // 创建返利奖励记录 $key = "vip_{$this->from1['vip_code']}_{$this->user['vip_code']}"; $map = ['type' => self::PRIZE_02, 'order_no' => $this->order['order_no'], 'order_uuid' => $this->order['uuid']]; - if ($this->config("repeat_state_{$key}") && $this->app->db->name($this->table)->where($map)->count() < 1) { + if ($this->config("repeat_state_{$key}") && DataUserRebate::mk()->where($map)->count() < 1) { $value = $this->config("repeat_value_{$key}"); if ($this->config("repeat_type_{$key}") == 1) { $val = floatval($value ?: '0.00'); @@ -263,7 +259,7 @@ class RebateService extends Service // 创建返利奖励记录 $key = "{$this->user['vip_code']}"; $map = ['type' => self::PRIZE_03, 'order_no' => $this->order['order_no'], 'order_uuid' => $this->order['uuid']]; - if ($this->config("direct_state_vip_{$key}") && $this->app->db->name($this->table)->where($map)->count() < 1) { + if ($this->config("direct_state_vip_{$key}") && DataUserRebate::mk()->where($map)->count() < 1) { $value = $this->config("direct_value_vip_{$key}"); if ($this->config("direct_type_vip_{$key}") == 1) { $val = floatval($value ?: '0.00'); @@ -291,7 +287,7 @@ class RebateService extends Service if (!$this->checkPrizeStatus(self::PRIZE_04, $this->from2['vip_code'])) return false; $key = "{$this->user['vip_code']}"; $map = ['type' => self::PRIZE_04, 'order_no' => $this->order['order_no'], 'order_uuid' => $this->order['uuid']]; - if ($this->config("indirect_state_vip_{$key}") && $this->app->db->name($this->table)->where($map)->count() < 1) { + if ($this->config("indirect_state_vip_{$key}") && DataUserRebate::mk()->where($map)->count() < 1) { $value = $this->config("indirect_value_vip_{$key}"); if ($this->config("indirect_type_vip_{$key}") == 1) { $val = floatval($value ?: '0.00'); @@ -319,7 +315,7 @@ class RebateService extends Service if (empty($puids) || $this->order['amount_total'] <= 0) return false; // 获取可以参与奖励的代理 $vips = $this->app->db->name('BaseUserUpgrade')->whereLike('rebate_rule', '%,' . self::PRIZE_05 . ',%')->column('number'); - $users = $this->app->db->name('DataUser')->whereIn('vip_code', $vips)->whereIn('id', $puids)->orderField('id', $puids)->select()->toArray(); + $users = DataUser::mk()->whereIn('vip_code', $vips)->whereIn('id', $puids)->orderField('id', $puids)->select()->toArray(); // 查询需要计算奖励的商品 foreach ($this->app->db->name('ShopOrderItem')->where(['order_no' => $this->order['order_no']])->cursor() as $item) { $itemJson = $this->app->db->name('BaseUserDiscount')->where(['status' => 1, 'deleted' => 0])->value('items'); @@ -330,7 +326,7 @@ class RebateService extends Service if ($tRate > $rule['discount'] && $tRate < 100) { $map = ['uuid' => $user['id'], 'type' => self::PRIZE_05]; $map['code'] = "{$this->order['order_no']}#{$item['id']}#{$tVip}.{$user['vip_code']}"; - if ($this->app->db->name($this->table)->where($map)->count() < 1) { + if (DataUserRebate::mk()->where($map)->count() < 1) { $dRate = ($rate = $tRate - $rule['discount']) / 100; $name = "等级差额奖励{$tVip}#{$user['vip_code']}商品原价{$item['total_selling']}元的{$rate}%"; $amount = $dRate * $item['total_selling']; @@ -360,11 +356,11 @@ class RebateService extends Service $prevLevel = $this->user['vip_code']; // 获取可以参与奖励的代理 $vips = $this->app->db->name('BaseUserUpgrade')->whereLike('rebate_rule', '%,' . self::PRIZE_06 . ',%')->column('number'); - foreach ($this->app->db->name('DataUser')->whereIn('vip_code', $vips)->whereIn('id', $puids)->orderField('id', $puids)->cursor() as $user) { + foreach (DataUser::mk()->whereIn('vip_code', $vips)->whereIn('id', $puids)->orderField('id', $puids)->cursor() as $user) { if ($user['vip_code'] > $prevLevel) { if (($amount = $this->_prize06amount($prevLevel + 1, $user['vip_code'])) > 0.00) { $map = ['uuid' => $user['id'], 'type' => self::PRIZE_06, 'order_no' => $this->order['order_no'], 'order_uuid' => $this->order['uuid']]; - if ($this->app->db->name($this->table)->where($map)->count() < 1) { + if (DataUserRebate::mk()->where($map)->count() < 1) { $name = "{$this->name(self::PRIZE_06)},[ VIP{$prevLevel} > VIP{$user['vip_code']} ] 每单 {$amount} 元"; $this->writeRabate($user['id'], $map, $name, $amount); } @@ -417,7 +413,7 @@ class RebateService extends Service // 创建返利奖励记录 $key = "{$this->user['vip_code']}"; $map = ['type' => self::PRIZE_07, 'order_no' => $this->order['order_no'], 'order_uuid' => $this->order['uuid']]; - if ($this->config("upgrade_state_vip_{$key}") && $this->app->db->name($this->table)->where($map)->count() < 1) { + if ($this->config("upgrade_state_vip_{$key}") && DataUserRebate::mk()->where($map)->count() < 1) { $value = $this->config("upgrade_value_vip_{$key}"); if ($this->config("upgrade_type_vip_{$key}") == 1) { $val = floatval($value ?: '0.00'); @@ -435,16 +431,13 @@ class RebateService extends Service /** * 用户平推奖励发放 * @return boolean - * @throws \think\db\exception\DataNotFoundException - * @throws \think\db\exception\DbException - * @throws \think\db\exception\ModelNotFoundException */ private function _prize08(): bool { if (empty($this->from1)) return false; $map = ['vip_code' => $this->user['vip_code']]; $uuids = array_reverse(str2arr(trim($this->user['path'], '-'), '-')); - $puids = $this->app->db->name('DataUser')->whereIn('id', $uuids)->orderField('id', $uuids)->where($map)->column('id'); + $puids = DataUser::mk()->whereIn('id', $uuids)->orderField('id', $uuids)->where($map)->column('id'); if (count($puids) < 2) return false; } } \ No newline at end of file diff --git a/app/data/service/UserAdminService.php b/app/data/service/UserAdminService.php index 1540965f3..0832e5999 100644 --- a/app/data/service/UserAdminService.php +++ b/app/data/service/UserAdminService.php @@ -2,9 +2,10 @@ namespace app\data\service; +use app\data\model\DataUser; +use app\data\model\DataUserToken; use think\admin\Exception; use think\admin\Service; -use think\db\exception\DbException; /** * 用户数据管理服务 @@ -55,19 +56,19 @@ class UserAdminService extends Service * @param string $type 接口类型 * @param boolean $force 强刷令牌 * @return array - * @throws Exception - * @throws DbException + * @throws \think\admin\Exception + * @throws \think\db\exception\DbException */ public function set(array $map, array $data, string $type, bool $force = false): array { unset($data['id'], $data['deleted'], $data['create_at']); - if ($uuid = $this->app->db->name('DataUser')->where($map)->where(['deleted' => 0])->value('id')) { + if ($uuid = DataUser::mk()->where($map)->where(['deleted' => 0])->value('id')) { if (!empty($data)) { $map = ['id' => $uuid, 'deleted' => 0]; - $this->app->db->name('DataUser')->strict(false)->where($map)->update($data); + DataUser::mk()->strict(false)->where($map)->update($data); } } else { - $uuid = $this->app->db->name('DataUser')->strict(false)->insertGetId($data); + $uuid = DataUser::mk()->strict(false)->insertGetId($data); } if ($force) { UserTokenService::instance()->token(intval($uuid), $type); @@ -80,17 +81,18 @@ class UserAdminService extends Service * @param integer $uuid 用户UID * @param ?string $type 接口类型 * @return array - * @throws DbException - * @throws Exception + * @throws \think\admin\Exception + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException */ public function get(int $uuid, ?string $type = null): array { - $map = ['id' => $uuid, 'deleted' => 0]; - $user = $this->app->db->name('DataUser')->where($map)->find(); + $user = DataUser::mk()->where(['id' => $uuid, 'deleted' => 0])->find(); if (empty($user)) throw new Exception('指定UID用户不存在'); if (!is_null($type)) { $map = ['uuid' => $uuid, 'type' => $type]; - $data = $this->app->db->name('DataUserToken')->where($map)->find(); + $data = DataUserToken::mk()->where($map)->find(); if (empty($data)) { [$state, $info, $data] = UserTokenService::instance()->token($uuid, $type); if (empty($state) || empty($data)) throw new Exception($info); @@ -108,8 +110,7 @@ class UserAdminService extends Service */ public function total(int $uuid): array { - $query = $this->app->db->name('DataUser'); - return ['my_invite' => $query->where(['pid1' => $uuid])->count()]; + return ['my_invite' => DataUser::mk()->where(['pid1' => $uuid])->count()]; } /** @@ -123,7 +124,7 @@ class UserAdminService extends Service { if (!empty($unionid)) { [$map1, $map2] = [[['unionid', '=', $unionid]], [[$field, '=', $openid]]]; - if ($uuid = $this->app->db->name('DataUser')->whereOr([$map1, $map2])->value('id')) { + if ($uuid = DataUser::mk()->whereOr([$map1, $map2])->value('id')) { return ['id' => $uuid]; } } @@ -142,7 +143,7 @@ class UserAdminService extends Service { if (count($list) < 1) return $list; $uids = array_unique(array_column($list, $keys)); - $users = $this->app->db->name('DataUser')->whereIn('id', $uids)->column($cols, 'id'); + $users = DataUser::mk()->whereIn('id', $uids)->column($cols, 'id'); foreach ($list as &$vo) $vo[$bind] = $users[$vo[$keys]] ?? []; return $list; } diff --git a/app/data/service/UserBalanceService.php b/app/data/service/UserBalanceService.php index 38e5a1524..e911556e7 100644 --- a/app/data/service/UserBalanceService.php +++ b/app/data/service/UserBalanceService.php @@ -2,6 +2,8 @@ namespace app\data\service; +use app\data\model\DataUserBalance; +use think\admin\Exception; use think\admin\Service; /** @@ -25,7 +27,7 @@ class UserBalanceService extends Service { $map = [['status', '>=', 4], ['order_no', '=', $orderNo]]; $order = $this->app->db->name('ShopOrder')->where($map)->find(); - if (empty($order)) throw new \think\admin\Exception('需处理的订单状态异常'); + if (empty($order)) throw new Exception('需处理的订单状态异常'); if ($order['reward_balance'] > 0) data_save('DataUserBalance', [ 'uuid' => $order['uuid'], @@ -48,16 +50,16 @@ class UserBalanceService extends Service public function amount(int $uuid, array $nots = []): array { if ($uuid > 0) { - $total = abs($this->app->db->name('DataUserBalance')->whereRaw("uuid='{$uuid}' and amount>0 and deleted=0")->sum('amount')); - $count = abs($this->app->db->name('DataUserBalance')->whereRaw("uuid='{$uuid}' and amount<0 and deleted=0")->sum('amount')); + $total = abs(DataUserBalance::mk()->whereRaw("uuid='{$uuid}' and amount>0 and deleted=0")->sum('amount')); + $count = abs(DataUserBalance::mk()->whereRaw("uuid='{$uuid}' and amount<0 and deleted=0")->sum('amount')); if (empty($nots)) { $this->app->db->name('DataUser')->where(['id' => $uuid])->update(['balance_total' => $total, 'balance_used' => $count]); } else { - $count -= $this->app->db->name('DataUserBalance')->whereRaw("uuid={$uuid}")->whereIn('code', $nots)->sum('amount'); + $count -= DataUserBalance::mk()->whereRaw("uuid={$uuid}")->whereIn('code', $nots)->sum('amount'); } } else { - $total = abs($this->app->db->name('DataUserBalance')->whereRaw("amount>0 and deleted=0")->sum('amount')); - $count = abs($this->app->db->name('DataUserBalance')->whereRaw("amount<0 and deleted=0")->sum('amount')); + $total = abs(DataUserBalance::mk()->whereRaw("amount>0 and deleted=0")->sum('amount')); + $count = abs(DataUserBalance::mk()->whereRaw("amount<0 and deleted=0")->sum('amount')); } return [$total, $count]; } diff --git a/app/data/service/UserRebateService.php b/app/data/service/UserRebateService.php index dff026dcd..2aac49d25 100644 --- a/app/data/service/UserRebateService.php +++ b/app/data/service/UserRebateService.php @@ -2,6 +2,10 @@ namespace app\data\service; +use app\data\model\DataUser; +use app\data\model\DataUserRebate; +use app\data\model\DataUserTransfer; +use app\data\model\ShopOrder; use think\admin\Service; /** @@ -15,19 +19,18 @@ class UserRebateService extends Service * 同步刷新用户返利 * @param integer $uuid * @return array [total, count, lock] - * @throws \think\db\exception\DbException */ public function amount(int $uuid): array { if ($uuid > 0) { - $count = $this->app->db->name('DataUserTransfer')->whereRaw("uuid='{$uuid}' and status>0")->sum('amount'); - $total = $this->app->db->name('DataUserRebate')->whereRaw("uuid='{$uuid}' and status=1 and deleted=0")->sum('amount'); - $locks = $this->app->db->name('DataUserRebate')->whereRaw("uuid='{$uuid}' and status=0 and deleted=0")->sum('amount'); - $this->app->db->name('DataUser')->where(['id' => $uuid])->update(['rebate_total' => $total, 'rebate_used' => $count, 'rebate_lock' => $locks]); + $count = DataUserTransfer::mk()->whereRaw("uuid='{$uuid}' and status>0")->sum('amount'); + $total = DataUserRebate::mk()->whereRaw("uuid='{$uuid}' and status=1 and deleted=0")->sum('amount'); + $locks = DataUserRebate::mk()->whereRaw("uuid='{$uuid}' and status=0 and deleted=0")->sum('amount'); + DataUser::mk()->where(['id' => $uuid])->update(['rebate_total' => $total, 'rebate_used' => $count, 'rebate_lock' => $locks]); } else { - $count = $this->app->db->name('DataUserTransfer')->whereRaw("status>0")->sum('amount'); - $total = $this->app->db->name('DataUserRebate')->whereRaw("status=1 and deleted=0")->sum('amount'); - $locks = $this->app->db->name('DataUserRebate')->whereRaw("status=0 and deleted=0")->sum('amount'); + $count = DataUserTransfer::mk()->whereRaw("status>0")->sum('amount'); + $total = DataUserRebate::mk()->whereRaw("status=1 and deleted=0")->sum('amount'); + $locks = DataUserRebate::mk()->whereRaw("status=0 and deleted=0")->sum('amount'); } return [$total, $count, $locks]; } @@ -43,11 +46,11 @@ class UserRebateService extends Service public function confirm(string $orderNo): array { $map = [['status', '>=', 4], ['order_no', '=', $orderNo]]; - $order = $this->app->db->name('ShopOrder')->where($map)->find(); + $order = ShopOrder::mk()->where($map)->find(); if (empty($order)) return [0, '需处理的订单状态异常!']; $map = [['status', '=', 0], ['order_no', 'like', "{$orderNo}%"]]; - $this->app->db->name('DataUserRebate')->where($map)->update(['status' => 1]); + DataUserRebate::mk()->where($map)->update(['status' => 1]); if (UserUpgradeService::instance()->upgrade($order['uuid'])) { return [1, '重新计算用户金额成功!']; } else { diff --git a/app/data/service/UserTokenService.php b/app/data/service/UserTokenService.php index 8123d90a4..f9f92e12d 100644 --- a/app/data/service/UserTokenService.php +++ b/app/data/service/UserTokenService.php @@ -2,10 +2,8 @@ namespace app\data\service; +use app\data\model\DataUserToken; use think\admin\Service; -use think\db\exception\DataNotFoundException; -use think\db\exception\DbException; -use think\db\exception\ModelNotFoundException; /** * 用户接口授权服务 @@ -27,15 +25,15 @@ class UserTokenService extends Service * @param string $token 认证令牌 * @param array $data 认证数据 * @return array [ 检查状态,状态描述,用户UID, 有效时间 ] - * @throws DataNotFoundException - * @throws DbException - * @throws ModelNotFoundException + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException */ public function check(string $type, string $token, array $data = []): array { if (empty($data)) { $map = ['type' => $type, 'token' => $token]; - $data = $this->app->db->name('DataUserToken')->where($map)->find(); + $data = DataUserToken::mk()->where($map)->find(); } if (empty($data) || empty($data['uuid'])) { return [0, '请重新登录,登录认证无效', 0, 0]; @@ -62,12 +60,11 @@ class UserTokenService extends Service * 延期 TOKEN 有效时间 * @param string $type 接口类型 * @param string $token 授权令牌 - * @throws DbException */ public function expire(string $type, string $token) { $map = ['type' => $type, 'token' => $token]; - $this->app->db->name('DataUserToken')->where($map)->update([ + DataUserToken::mk()->where($map)->update([ 'time' => time() + $this->expire, ]); } @@ -77,7 +74,6 @@ class UserTokenService extends Service * @param int $uuid 授权用户 * @param string $type 接口类型 * @return array [创建状态, 状态描述, 令牌数据] - * @throws DbException */ public function token(int $uuid, string $type): array { @@ -85,13 +81,13 @@ class UserTokenService extends Service $time = time(); $map1 = [['token', '<>', 'token'], ['time', '<', $time]]; $map2 = [['token', '<>', 'token'], ['type', '=', $type], ['uuid', '=', $uuid]]; - $this->app->db->name('DataUserToken')->whereOr([$map1, $map2])->delete(); + DataUserToken::mk()->whereOr([$map1, $map2])->delete(); // 创建新的认证数据 do $map = ['type' => $type, 'token' => md5(uniqid() . rand(100, 999))]; - while ($this->app->db->name('DataUserToken')->where($map)->count() > 0); + while (DataUserToken::mk()->where($map)->count() > 0); // 写入用户认证数据 $data = array_merge($map, ['uuid' => $uuid, 'time' => $time + $this->expire, 'tokenv' => $this->_buildTokenVerify()]); - if ($this->app->db->name('DataUserToken')->insert($data) !== false) { + if (DataUserToken::mk()->insert($data) !== false) { return [1, '刷新认证成功', $data]; } else { return [0, '刷新认证失败', []]; diff --git a/app/data/service/UserTransferService.php b/app/data/service/UserTransferService.php index 45750c392..b30a19c39 100644 --- a/app/data/service/UserTransferService.php +++ b/app/data/service/UserTransferService.php @@ -2,6 +2,7 @@ namespace app\data\service; +use app\data\model\DataUserTransfer; use think\admin\Service; /** @@ -100,15 +101,15 @@ class UserTransferService extends Service public function amount(int $uuid): array { if ($uuid > 0) { - $locks = abs($this->app->db->name('DataUserTransfer')->whereRaw("uuid='{$uuid}' and status=3")->sum('amount')); - $total = abs($this->app->db->name('DataUserTransfer')->whereRaw("uuid='{$uuid}' and status>=1")->sum('amount')); - $count = abs($this->app->db->name('DataUserTransfer')->whereRaw("uuid='{$uuid}' and status>=4")->sum('amount')); - $audit = abs($this->app->db->name('DataUserTransfer')->whereRaw("uuid='{$uuid}' and status>=1 and status<3")->sum('amount')); + $locks = abs(DataUserTransfer::mk()->whereRaw("uuid='{$uuid}' and status=3")->sum('amount')); + $total = abs(DataUserTransfer::mk()->whereRaw("uuid='{$uuid}' and status>=1")->sum('amount')); + $count = abs(DataUserTransfer::mk()->whereRaw("uuid='{$uuid}' and status>=4")->sum('amount')); + $audit = abs(DataUserTransfer::mk()->whereRaw("uuid='{$uuid}' and status>=1 and status<3")->sum('amount')); } else { - $locks = abs($this->app->db->name('DataUserTransfer')->whereRaw("status=3")->sum('amount')); - $total = abs($this->app->db->name('DataUserTransfer')->whereRaw("status>=1")->sum('amount')); - $count = abs($this->app->db->name('DataUserTransfer')->whereRaw("status>=4")->sum('amount')); - $audit = abs($this->app->db->name('DataUserTransfer')->whereRaw("status>=1 and status<3")->sum('amount')); + $locks = abs(DataUserTransfer::mk()->whereRaw("status=3")->sum('amount')); + $total = abs(DataUserTransfer::mk()->whereRaw("status>=1")->sum('amount')); + $count = abs(DataUserTransfer::mk()->whereRaw("status>=4")->sum('amount')); + $audit = abs(DataUserTransfer::mk()->whereRaw("status>=1 and status<3")->sum('amount')); } return [$total, $count, $audit, $locks]; } diff --git a/app/data/service/UserUpgradeService.php b/app/data/service/UserUpgradeService.php index 054af828a..dae050f0b 100644 --- a/app/data/service/UserUpgradeService.php +++ b/app/data/service/UserUpgradeService.php @@ -2,6 +2,10 @@ namespace app\data\service; +use app\data\model\DataUser; +use app\data\model\DataUserBalance; +use app\data\model\ShopOrder; +use app\data\model\ShopOrderItem; use think\admin\Service; /** @@ -34,7 +38,7 @@ class UserUpgradeService extends Service */ public function bindAgent(int $uuid, int $pid0 = 0, int $mode = 1): array { - $user = $this->app->db->name('DataUser')->where(['id' => $uuid])->find(); + $user = DataUser::mk()->where(['id' => $uuid])->find(); if (empty($user)) return [0, '用户查询失败']; if ($user['pids'] && in_array($mode, [0, 1])) return [1, '已经绑定代理']; // 检查代理用户 @@ -42,7 +46,7 @@ class UserUpgradeService extends Service if (empty($pid0)) return [0, '绑定的代理不存在']; if ($uuid == $pid0) return [0, '不能绑定自己为代理']; // 检查代理资格 - $agent = $this->app->db->name('DataUser')->where(['id' => $pid0])->find(); + $agent = DataUser::mk()->where(['id' => $pid0])->find(); if (empty($agent['vip_code'])) return [0, '代理无推荐资格']; if (stripos($agent['path'], "-{$uuid}-") !== false) return [0, '不能绑定下属']; // 组装代理数据 @@ -51,16 +55,16 @@ class UserUpgradeService extends Service $this->app->db->transaction(function () use ($user, $agent, $mode) { // 更新用户代理 $path1 = rtrim($agent['path'] ?: '-', '-') . "-{$agent['id']}-"; - $this->app->db->name('DataUser')->where(['id' => $user['id']])->update([ + DataUser::mk()->where(['id' => $user['id']])->update([ 'pid0' => $agent['id'], 'pid1' => $agent['id'], 'pid2' => $agent['pid1'], 'pids' => $mode > 0 ? 1 : 0, 'path' => $path1, 'layer' => substr_count($path1, '-'), ]); // 更新下级代理 $path2 = "{$user['path']}{$user['id']}-"; - if ($this->app->db->name('DataUser')->whereLike('path', "{$path2}%")->count() > 0) { - foreach ($this->app->db->name('DataUser')->whereLike('path', "{$path2}%")->order('layer desc')->select() as $vo) { + if (DataUser::mk()->whereLike('path', "{$path2}%")->count() > 0) { + foreach (DataUser::mk()->whereLike('path', "{$path2}%")->order('layer desc')->select() as $vo) { $attr = array_reverse(str2arr($path3 = preg_replace("#^{$path2}#", "{$path1}{$user['id']}-", $vo['path']), '-')); - $this->app->db->name('DataUser')->where(['id' => $vo['id']])->update([ + DataUser::mk()->where(['id' => $vo['id']])->update([ 'pid0' => $attr[0] ?? 0, 'pid1' => $attr[0] ?? 0, 'pid2' => $attr[1] ?? 0, 'path' => $path3, 'layer' => substr_count($path3, '-'), ]); } @@ -85,16 +89,16 @@ class UserUpgradeService extends Service */ public function upgrade(int $uuid, bool $parent = true, ?string $orderNo = null): bool { - $user = $this->app->db->name('DataUser')->where(['id' => $uuid])->find(); + $user = DataUser::mk()->where(['id' => $uuid])->find(); if (empty($user)) return true; // 初始化等级参数 $levels = $this->levels(); [$vipName, $vipCode, $vipTeam] = [$levels[0]['name'] ?? '普通用户', 0, []]; // 统计用户数据 foreach ($levels as $key => $level) if ($level['upgrade_team'] === 1) $vipTeam[] = $key; - $orderAmount = $this->app->db->name('ShopOrder')->where("uuid={$uuid} and status>=4")->sum('amount_total'); - $teamsDirect = $this->app->db->name('DataUser')->where(['pid1' => $uuid])->whereIn('vip_code', $vipTeam)->count(); - $teamsIndirect = $this->app->db->name('DataUser')->where(['pid2' => $uuid])->whereIn('vip_code', $vipTeam)->count(); + $orderAmount = ShopOrder::mk()->where("uuid={$uuid} and status>=4")->sum('amount_total'); + $teamsDirect = DataUser::mk()->where(['pid1' => $uuid])->whereIn('vip_code', $vipTeam)->count(); + $teamsIndirect = DataUser::mk()->where(['pid2' => $uuid])->whereIn('vip_code', $vipTeam)->count(); $teamsUsers = $teamsDirect + $teamsIndirect; // 动态计算用户等级 foreach ($levels as $item) { @@ -113,7 +117,7 @@ class UserUpgradeService extends Service } } // 购买入会商品升级 - $query = $this->app->db->name('ShopOrderItem')->alias('b')->join('shop_order a', 'b.order_no=a.order_no'); + $query = ShopOrderItem::mk()->alias('b')->join('shop_order a', 'b.order_no=a.order_no'); $tmpCode = $query->whereRaw("a.uuid={$uuid} and a.payment_status=1 and a.status>=4 and b.vip_entry=1")->max('b.vip_upgrade'); if ($tmpCode > $vipCode && isset($levels[$tmpCode])) { [$vipName, $vipCode] = [$levels[$tmpCode]['name'], $levels[$tmpCode]['number']]; @@ -121,14 +125,14 @@ class UserUpgradeService extends Service $orderNo = null; } // 后台余额充值升级 - $tmpCode = $this->app->db->name('DataUserBalance')->where(['uuid' => $uuid, 'deleted' => 0])->max('upgrade'); + $tmpCode = DataUserBalance::mk()->where(['uuid' => $uuid, 'deleted' => 0])->max('upgrade'); if ($tmpCode > $vipCode && isset($levels[$tmpCode])) { [$vipName, $vipCode] = [$levels[$tmpCode]['name'], $levels[$tmpCode]['number']]; } // 统计用户订单金额 - $orderAmountTotal = $this->app->db->name('ShopOrder')->whereRaw("uuid={$uuid} and status>=4")->sum('amount_goods'); - $teamsAmountDirect = $this->app->db->name('ShopOrder')->whereRaw("puid1={$uuid} and status>=4")->sum('amount_goods'); - $teamsAmountIndirect = $this->app->db->name('ShopOrder')->whereRaw("puid2={$uuid} and status>=4")->sum('amount_goods'); + $orderAmountTotal = ShopOrder::mk()->whereRaw("uuid={$uuid} and status>=4")->sum('amount_goods'); + $teamsAmountDirect = ShopOrder::mk()->whereRaw("puid1={$uuid} and status>=4")->sum('amount_goods'); + $teamsAmountIndirect = ShopOrder::mk()->whereRaw("puid2={$uuid} and status>=4")->sum('amount_goods'); // 更新用户团队数据 $data = [ 'vip_name' => $vipName, @@ -143,7 +147,7 @@ class UserUpgradeService extends Service ]; if (!empty($orderNo)) $data['vip_order'] = $orderNo; if ($data['vip_code'] !== $user['vip_code']) $data['vip_datetime'] = date('Y-m-d H:i:s'); - $this->app->db->name('DataUser')->where(['id' => $uuid])->update($data); + DataUser::mk()->where(['id' => $uuid])->update($data); // 用户升级事件 if ($user['vip_code'] < $vipCode) $this->app->event->trigger('UserUpgradeLevel', [ 'uuid' => $user['id'], 'order_no' => $orderNo, 'vip_code_old' => $user['vip_code'], 'vip_code_new' => $vipCode, diff --git a/app/data/service/payment/AlipayPaymentService.php b/app/data/service/payment/AlipayPaymentService.php index c256f02c6..447cd7a48 100644 --- a/app/data/service/payment/AlipayPaymentService.php +++ b/app/data/service/payment/AlipayPaymentService.php @@ -2,8 +2,13 @@ namespace app\data\service\payment; +use AliPay\App; +use AliPay\Wap; +use AliPay\Web; use app\data\service\PaymentService; use think\admin\Exception; +use WeChat\Exceptions\InvalidResponseException; +use WeChat\Exceptions\LocalCacheException; /** * 支付宝支付基础服务 @@ -48,11 +53,11 @@ class AlipayPaymentService extends PaymentService } } if ($tradeType === static::PAYMENT_WECHAT_APP) { - $payment = \AliPay\App::instance($this->config); + $payment = App::instance($this->config); } elseif ($tradeType === static::PAYMENT_ALIPAY_WAP) { - $payment = \AliPay\Wap::instance($this->config); + $payment = Wap::instance($this->config); } elseif ($tradeType === static::PAYMENT_ALIPAY_WEB) { - $payment = \AliPay\Web::instance($this->config); + $payment = Web::instance($this->config); } else { throw new Exception("支付类型[{$tradeType}]暂时不支持!"); } @@ -80,7 +85,7 @@ class AlipayPaymentService extends PaymentService */ public function notify(): string { - $notify = \AliPay\App::instance($this->config)->notify(); + $notify = App::instance($this->config)->notify(); if (in_array($notify['trade_status'], ['TRADE_SUCCESS', 'TRADE_FINISHED'])) { if ($this->updatePaymentAction($notify['out_trade_no'], $notify['trade_no'], $notify['total_amount'])) { return 'success'; @@ -96,12 +101,12 @@ class AlipayPaymentService extends PaymentService * 查询订单数据 * @param string $orderNo * @return array - * @throws \WeChat\Exceptions\InvalidResponseException - * @throws \WeChat\Exceptions\LocalCacheException + * @throws InvalidResponseException + * @throws LocalCacheException */ public function query(string $orderNo): array { - return \AliPay\App::instance($this->config)->query($orderNo); + return App::instance($this->config)->query($orderNo); } /** diff --git a/app/data/service/payment/BalancePyamentService.php b/app/data/service/payment/BalancePyamentService.php index 54a1f6e40..0c05174da 100644 --- a/app/data/service/payment/BalancePyamentService.php +++ b/app/data/service/payment/BalancePyamentService.php @@ -2,6 +2,8 @@ namespace app\data\service\payment; +use app\data\model\DataUserBalance; +use app\data\model\ShopOrder; use app\data\service\PaymentService; use app\data\service\UserBalanceService; use think\admin\Exception; @@ -43,14 +45,14 @@ class BalancePyamentService extends PaymentService * @param string $paymentReturn 完成回跳地址 * @param string $paymentImage 支付凭证图片 * @return array - * @throws Exception + * @throws \think\admin\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function create(string $openid, string $orderNo, string $paymentAmount, string $paymentTitle, string $paymentRemark, string $paymentReturn = '', string $paymentImage = ''): array { - $order = $this->app->db->name('ShopOrder')->where(['order_no' => $orderNo])->find(); + $order = ShopOrder::mk()->where(['order_no' => $orderNo])->find(); if (empty($order)) throw new Exception("订单不存在"); if ($order['status'] !== 2) throw new Exception("不可发起支付"); // 创建支付行为 @@ -62,11 +64,11 @@ class BalancePyamentService extends PaymentService // 扣减用户余额 $this->app->db->transaction(function () use ($order, $paymentAmount) { // 更新订单余额 - $this->app->db->name('ShopOrder')->where(['order_no' => $order['order_no']])->update([ + ShopOrder::mk()->where(['order_no' => $order['order_no']])->update([ 'payment_balance' => $paymentAmount, ]); // 扣除余额金额 - data_save('DataUserBalance', [ + data_save(DataUserBalance::mk(), [ 'uuid' => $order['uuid'], 'code' => "KC{$order['order_no']}", 'name' => "账户余额支付", diff --git a/app/data/service/payment/EmptyPaymentService.php b/app/data/service/payment/EmptyPaymentService.php index e894df273..5d27087b5 100644 --- a/app/data/service/payment/EmptyPaymentService.php +++ b/app/data/service/payment/EmptyPaymentService.php @@ -2,12 +2,10 @@ namespace app\data\service\payment; +use app\data\model\ShopOrder; use app\data\service\PaymentService; use think\admin\Exception; use think\admin\extend\CodeExtend; -use think\db\exception\DataNotFoundException; -use think\db\exception\DbException; -use think\db\exception\ModelNotFoundException; /** * 空支付支付 @@ -46,14 +44,14 @@ class EmptyPaymentService extends PaymentService * @param string $paymentReturn 完成回跳地址 * @param string $paymentImage 支付凭证图片 * @return array - * @throws Exception - * @throws DataNotFoundException - * @throws DbException - * @throws ModelNotFoundException + * @throws \think\admin\Exception + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException */ public function create(string $openid, string $orderNo, string $paymentAmount, string $paymentTitle, string $paymentRemark, string $paymentReturn = '', string $paymentImage = ''): array { - $order = $this->app->db->name('ShopOrder')->where(['order_no' => $orderNo])->find(); + $order = ShopOrder::mk()->where(['order_no' => $orderNo])->find(); if (empty($order)) throw new Exception("订单不存在"); if ($order['status'] !== 2) throw new Exception("不可发起支付"); // 创建支付行为 diff --git a/app/data/service/payment/VoucherPaymentService.php b/app/data/service/payment/VoucherPaymentService.php index 5e8def5cb..862ca9532 100644 --- a/app/data/service/payment/VoucherPaymentService.php +++ b/app/data/service/payment/VoucherPaymentService.php @@ -2,6 +2,7 @@ namespace app\data\service\payment; +use app\data\model\ShopOrder; use app\data\service\PaymentService; use think\admin\Exception; use think\admin\extend\CodeExtend; @@ -42,14 +43,14 @@ class VoucherPaymentService extends PaymentService * @param string $paymentReturn 完成回跳地址 * @param string $paymentImage 支付凭证图片 * @return array - * @throws Exception + * @throws \think\admin\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function create(string $openid, string $orderNo, string $paymentAmount, string $paymentTitle, string $paymentRemark, string $paymentReturn = '', string $paymentImage = ''): array { - $order = $this->app->db->name('ShopOrder')->where(['order_no' => $orderNo])->find(); + $order = ShopOrder::mk()->where(['order_no' => $orderNo])->find(); if (empty($order)) throw new Exception("订单不存在"); if ($order['status'] !== 2) throw new Exception("不可发起支付"); if (empty($paymentImage)) throw new Exception('支付凭证不能为空');