Bläddra i källkod

修改用户数据服务

邹景立 4 år sedan
förälder
incheckning
7b7db7ee4f

+ 2 - 2
app/data/command/UserBalance.php

@@ -2,7 +2,7 @@
 
 namespace app\data\command;
 
-use app\data\service\UpgradeService;
+use app\data\service\UserUpgradeService;
 use think\admin\Command;
 use think\admin\Exception;
 use think\console\Input;
@@ -33,7 +33,7 @@ class UserBalance extends Command
             [$total, $count] = [$this->app->db->name('DataUser')->count(), 0];
             foreach ($this->app->db->name('DataUser')->field('id')->cursor() as $user) {
                 $this->queue->message($total, ++$count, "正在计算用户 [{$user['id']}] 的余额");
-                UpgradeService::instance()->balance($user['id']);
+                UserUpgradeService::instance()->balance($user['id']);
                 $this->queue->message($total, $count, "完成计算用户 [{$user['id']}] 的余额", 1);
             }
         } catch (\Exception $exception) {

+ 2 - 2
app/data/command/UserUpgrade.php

@@ -2,7 +2,7 @@
 
 namespace app\data\command;
 
-use app\data\service\UpgradeService;
+use app\data\service\UserUpgradeService;
 use think\admin\Command;
 use think\admin\Exception;
 use think\console\Input;
@@ -33,7 +33,7 @@ class UserUpgrade extends Command
             [$total, $count] = [$this->app->db->name('DataUser')->count(), 0];
             foreach ($this->app->db->name('DataUser')->field('id')->cursor() as $user) {
                 $this->queue->message($total, ++$count, "正在计算用户 [{$user['id']}] 的等级");
-                UpgradeService::instance()->syncLevel($user['id']);
+                UserUpgradeService::instance()->syncLevel($user['id']);
                 $this->queue->message($total, $count, "完成计算用户 [{$user['id']}] 的等级", 1);
             }
         } catch (\Exception $exception) {

+ 3 - 3
app/data/controller/UserBalance.php

@@ -2,7 +2,7 @@
 
 namespace app\data\controller;
 
-use app\data\service\UpgradeService;
+use app\data\service\UserUpgradeService;
 use app\data\service\UserService;
 use think\admin\Controller;
 use think\admin\extend\CodeExtend;
@@ -91,7 +91,7 @@ class UserBalance extends Controller
     protected function _form_result(bool $state, array $data)
     {
         if ($state && isset($data['uid'])) {
-            UpgradeService::instance()->balance($data['uid']);
+            UserUpgradeService::instance()->balance($data['uid']);
         }
     }
 
@@ -116,7 +116,7 @@ class UserBalance extends Controller
             $ids = str2arr(input('id', ''));
             $query = $this->app->db->name($this->table);
             foreach ($query->whereIn('id', $ids)->cursor() as $vo) {
-                UpgradeService::instance()->balance($vo['uid']);
+                UserUpgradeService::instance()->balance($vo['uid']);
             }
         }
     }

+ 2 - 1
app/data/controller/api/Auth.php

@@ -3,6 +3,7 @@
 namespace app\data\controller\api;
 
 use app\data\service\UserService;
+use app\data\service\UserTokenService;
 use think\admin\Controller;
 use think\exception\HttpResponseException;
 
@@ -66,7 +67,7 @@ abstract class Auth extends Controller
             if (empty($this->uuid)) {
                 $token = input('token') ?: $this->request->header('api-token');
                 if (empty($token)) $this->error('登录认证TOKEN不能为空!');
-                [$state, $info, $this->uuid] = UserService::instance()->check($this->type, $token);
+                [$state, $info, $this->uuid] = UserTokenService::instance()->check($this->type, $token);
                 if (empty($state)) $this->error($info, '{-null-}', 401);
             }
             return UserService::instance()->get($this->type, $this->uuid);

+ 2 - 1
app/data/controller/api/Login.php

@@ -38,7 +38,7 @@ class Login extends Controller
 
     /**
      * 用户登录接口
-     * @throws \think\Exception
+     * @throws \think\admin\Exception
      * @throws \think\db\exception\DataNotFoundException
      * @throws \think\db\exception\DbException
      * @throws \think\db\exception\ModelNotFoundException
@@ -63,6 +63,7 @@ class Login extends Controller
 
     /**
      * 用户统一注册入口
+     * @throws \think\admin\Exception
      * @throws \think\db\exception\DbException
      */
     public function register()

+ 1 - 0
app/data/controller/api/Wechat.php

@@ -65,6 +65,7 @@ class Wechat extends Controller
     /**
      * 加载网页授权数据
      * @return \think\Response
+     * @throws \think\admin\Exception
      * @throws \think\db\exception\DbException
      */
     public function oauth(): Response

+ 1 - 0
app/data/controller/api/Wxapp.php

@@ -57,6 +57,7 @@ class Wxapp extends Controller
 
     /**
      * 授权Code换取会话信息
+     * @throws \think\admin\Exception
      * @throws \think\db\exception\DbException
      */
     public function session()

+ 4 - 4
app/data/controller/api/auth/Balance.php

@@ -3,7 +3,7 @@
 namespace app\data\controller\api\auth;
 
 use app\data\controller\api\Auth;
-use app\data\service\UpgradeService;
+use app\data\service\UserUpgradeService;
 use app\data\service\UserService;
 use think\admin\extend\CodeExtend;
 
@@ -61,12 +61,12 @@ class Balance extends Auth
         $user = $this->app->db->name('DataUser')->where($map)->find();
         if (empty($user)) $this->error('目标用户不存在!');
         // 检测余额否有足够
-        [$total, $count] = UpgradeService::instance()->balance($this->uuid);
+        [$total, $count] = UserUpgradeService::instance()->balance($this->uuid);
         if ($data['amount'] > $total - $count) $this->error('可转账余额不足!');
         // 写入余额转账记录
         if ($this->app->db->name($this->table)->insert($data) !== false) {
-            UpgradeService::instance()->balance($data['uid']);
-            UpgradeService::instance()->balance($data['from']);
+            UserUpgradeService::instance()->balance($data['uid']);
+            UserUpgradeService::instance()->balance($data['from']);
             $this->success('余额转账成功!');
         } else {
             $this->error('余额转账失败!');

+ 2 - 2
app/data/controller/api/auth/Center.php

@@ -3,7 +3,7 @@
 namespace app\data\controller\api\auth;
 
 use app\data\controller\api\Auth;
-use app\data\service\UpgradeService;
+use app\data\service\UserUpgradeService;
 use app\data\service\UserService;
 use think\admin\Storage;
 use think\exception\HttpResponseException;
@@ -135,7 +135,7 @@ class Center extends Auth
     public function bindFrom()
     {
         $data = $this->_vali(['from.require' => '邀请人不能为空']);
-        [$state, $message] = UpgradeService::instance()->bindAgent($this->uuid, $data['from'], false);
+        [$state, $message] = UserUpgradeService::instance()->bindAgent($this->uuid, $data['from'], false);
         if ($state) {
             $this->success($message, UserService::instance()->total($this->uuid));
         } else {

+ 3 - 3
app/data/service/OrderService.php

@@ -5,7 +5,7 @@ namespace app\data\service;
 use think\admin\Service;
 
 /**
- * 订单数据服务
+ * 商城订单数据服务
  * Class OrderService
  * @package app\data\service
  */
@@ -80,7 +80,7 @@ class OrderService extends Service
         // 尝试绑定代理用户
         if (empty($user['pid1']) && ($order['puid1'] > 0 || $user['pid1'] > 0)) {
             $puid1 = $order['puid1'] > 0 ? $order['puid1'] : $user['bid'];
-            UpgradeService::instance()->bindAgent($user['id'], $puid1);
+            UserUpgradeService::instance()->bindAgent($user['id'], $puid1);
         }
         // 重置用户信息并绑定订单
         $user = $this->app->db->name('DataUser')->where(['id' => $order['uid']])->find();
@@ -90,7 +90,7 @@ class OrderService extends Service
             ]);
         }
         // 重新计算用户等级
-        UpgradeService::instance()->syncLevel($user['id']);
+        UserUpgradeService::instance()->syncLevel($user['id']);
         return [$user, $order, $entry];
     }
 

+ 4 - 4
app/data/service/RebateCurrentService.php

@@ -128,7 +128,7 @@ class RebateCurrentService extends Service
                 'uid' => $this->from1['id'], 'name' => $name, 'amount' => $amount, 'order_amount' => $this->order['amount_total'],
             ]));
             // 更新用户奖利金额
-            UpgradeService::instance()->syncLevel($this->from1['id']);
+            UserUpgradeService::instance()->syncLevel($this->from1['id']);
         }
         return true;
     }
@@ -160,7 +160,7 @@ class RebateCurrentService extends Service
                 'uid' => $this->from1['id'], 'name' => $name, 'amount' => $amount, 'order_amount' => $this->order['amount_total'],
             ]));
             // 更新用户奖利金额
-            UpgradeService::instance()->syncLevel($this->from1['id']);
+            UserUpgradeService::instance()->syncLevel($this->from1['id']);
         }
         return true;
     }
@@ -185,7 +185,7 @@ class RebateCurrentService extends Service
                 'uid' => $this->from1['id'], 'name' => $name, 'amount' => $amount, 'order_amount' => $this->order['amount_total'],
             ]));
             // 更新用户奖利金额
-            UpgradeService::instance()->syncLevel($this->from1['id']);
+            UserUpgradeService::instance()->syncLevel($this->from1['id']);
         }
         return true;
     }
@@ -209,7 +209,7 @@ class RebateCurrentService extends Service
                 'uid' => $this->from2['id'], 'name' => $name, 'amount' => $amount, 'order_amount' => $this->order['amount_total'],
             ]));
             // 更新代理奖利金额
-            UpgradeService::instance()->syncLevel($this->from2['id']);
+            UserUpgradeService::instance()->syncLevel($this->from2['id']);
         }
         return true;
     }

+ 12 - 94
app/data/service/UserService.php

@@ -4,12 +4,10 @@ namespace app\data\service;
 
 use think\admin\Exception;
 use think\admin\Service;
-use think\db\exception\DataNotFoundException;
 use think\db\exception\DbException;
-use think\db\exception\ModelNotFoundException;
 
 /**
- * 用户数据接口服务
+ * 用户数据服务
  * Class UserService
  * @package app\store\service
  */
@@ -51,18 +49,12 @@ class UserService extends Service
     ];
 
     /**
-     * 认证有效时间
-     * @var integer
-     */
-    private $expire = 7200;
-
-    /**
      * 获取用户数据
      * @param string $type 接口类型
      * @param integer $uuid 用户UID
      * @return array
-     * @throws Exception
      * @throws DbException
+     * @throws Exception
      */
     public function get(string $type, int $uuid): array
     {
@@ -70,7 +62,7 @@ class UserService extends Service
         if (empty($user)) throw new Exception('指定UID用户不存在');
         $data = $this->app->db->name('DataUserToken')->where(['uid' => $uuid, 'type' => $type])->findOrEmpty();
         if (empty($data)) {
-            [$state, $info, $data] = $this->token($uuid, $type);
+            [$state, $info, $data] = UserTokenService::instance()->token($uuid, $type);
             if (empty($state) || empty($data)) throw new Exception($info);
         }
         $user['token'] = ['token' => $data['token'], 'expire' => $data['time']];
@@ -99,50 +91,21 @@ class UserService extends Service
         } else {
             $uuid = $this->app->db->name('DataUser')->strict(false)->insertGetId($data);
         }
-        if ($force) $this->token(intval($uuid), $type);
-        return $this->get($type, $uuid);
-    }
-
-    /**
-     * 检查 TOKEN 是否有效
-     * @param string $type 接口类型
-     * @param string $token 认证令牌
-     * @param array $data 认证数据
-     * @return array [ 检查状态,状态描述,用户UID, 有效时间 ]
-     * @throws DataNotFoundException
-     * @throws DbException
-     * @throws 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();
-        }
-        if (empty($data) || empty($data['uid'])) {
-            return [0, '请重新登录,登录认证无效', 0, 0];
-        } elseif ($token !== 'token' && $data['time'] < time()) {
-            return [0, '请重新登录,登录认证失效', 0, 0];
-        } elseif ($token !== 'token' && $data['tokenv'] !== $this->_buildTokenVerify()) {
-            return [0, '请重新登录,客户端已更换', 0, 0];
-        } else {
-            $this->expire($type, $token);
-            return [1, '登录验证成功', $data['uid'], $data['time']];
+        if ($force) {
+            UserTokenService::instance()->token(intval($uuid), $type);
         }
+        return $this->get($type, $uuid);
     }
 
     /**
-     * 延期 TOKEN 有效时间
-     * @param string $type 接口类型
-     * @param string $token 授权令牌
-     * @throws DbException
+     * 获取用户数据统计
+     * @param int $uuid 用户UID
+     * @return array
      */
-    public function expire(string $type, string $token)
+    public function total(int $uuid): array
     {
-        $map = ['type' => $type, 'token' => $token];
-        $this->app->db->name('DataUserToken')->where($map)->update([
-            'time' => time() + $this->expire,
-        ]);
+        $query = $this->app->db->name('DataUser');
+        return ['my_invite' => $query->where(['pid1' => $uuid])->count()];
     }
 
     /**
@@ -161,49 +124,4 @@ class UserService extends Service
         foreach ($list as &$vo) $vo[$bind] = $users[$vo[$keys]] ?? [];
         return $list;
     }
-
-    /**
-     * 获取用户数据统计
-     * @param int $uuid 用户UID
-     * @return array
-     */
-    public function total(int $uuid): array
-    {
-        $query = $this->app->db->name('DataUser');
-        return ['my_invite' => $query->where(['pid1' => $uuid])->count()];
-    }
-
-    /**
-     * 生成新的用户令牌
-     * @param int $uuid 授权用户
-     * @param string $type 接口类型
-     * @return array [创建状态, 状态描述, 令牌数据]
-     * @throws DbException
-     */
-    public function token(int $uuid, string $type): array
-    {
-        // 清理无效认证数据
-        $map1 = [['uid', '=', $uuid], ['type', '=', $type]];
-        $map2 = [['time', '<', $time = time()], ['token', '<>', 'token']];
-        $this->app->db->name('DataUserToken')->whereOr([$map1, $map2])->delete();
-        // 创建新的认证数据
-        do $map = ['type' => $type, 'token' => md5(uniqid() . rand(100, 999))];
-        while ($this->app->db->name('DataUserToken')->where($map)->count() > 0);
-        // 写入用户认证数据
-        $data = array_merge($map, ['uid' => $uuid, 'time' => $time + $this->expire, 'tokenv' => $this->_buildTokenVerify()]);
-        if ($this->app->db->name('DataUserToken')->insert($data) !== false) {
-            return [1, '刷新认证成功', $data];
-        } else {
-            return [0, '刷新认证失败', []];
-        }
-    }
-
-    /**
-     * 获取令牌的认证值
-     * @return string
-     */
-    private function _buildTokenVerify(): string
-    {
-        return md5($this->app->request->server('HTTP_USER_AGENT', '-'));
-    }
 }

+ 99 - 0
app/data/service/UserTokenService.php

@@ -0,0 +1,99 @@
+<?php
+
+namespace app\data\service;
+
+use think\admin\Service;
+use think\db\exception\DataNotFoundException;
+use think\db\exception\DbException;
+use think\db\exception\ModelNotFoundException;
+
+/**
+ * 用户接口授权服务
+ * Class UserTokenService
+ * @package app\data\service
+ */
+class UserTokenService extends Service
+{
+
+    /**
+     * 认证有效时间
+     * @var integer
+     */
+    private $expire = 7200;
+
+    /**
+     * 检查 TOKEN 是否有效
+     * @param string $type 接口类型
+     * @param string $token 认证令牌
+     * @param array $data 认证数据
+     * @return array [ 检查状态,状态描述,用户UID, 有效时间 ]
+     * @throws DataNotFoundException
+     * @throws DbException
+     * @throws 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();
+        }
+        if (empty($data) || empty($data['uid'])) {
+            return [0, '请重新登录,登录认证无效', 0, 0];
+        } elseif ($token !== 'token' && $data['time'] < time()) {
+            return [0, '请重新登录,登录认证失效', 0, 0];
+        } elseif ($token !== 'token' && $data['tokenv'] !== $this->_buildTokenVerify()) {
+            return [0, '请重新登录,客户端已更换', 0, 0];
+        } else {
+            $this->expire($type, $token);
+            return [1, '登录验证成功', $data['uid'], $data['time']];
+        }
+    }
+
+    /**
+     * 生成新的用户令牌
+     * @param int $uuid 授权用户
+     * @param string $type 接口类型
+     * @return array [创建状态, 状态描述, 令牌数据]
+     * @throws DbException
+     */
+    public function token(int $uuid, string $type): array
+    {
+        // 清理无效认证数据
+        $map1 = [['uid', '=', $uuid], ['type', '=', $type]];
+        $map2 = [['time', '<', $time = time()], ['token', '<>', 'token']];
+        $this->app->db->name('DataUserToken')->whereOr([$map1, $map2])->delete();
+        // 创建新的认证数据
+        do $map = ['type' => $type, 'token' => md5(uniqid() . rand(100, 999))];
+        while ($this->app->db->name('DataUserToken')->where($map)->count() > 0);
+        // 写入用户认证数据
+        $data = array_merge($map, ['uid' => $uuid, 'time' => $time + $this->expire, 'tokenv' => $this->_buildTokenVerify()]);
+        if ($this->app->db->name('DataUserToken')->insert($data) !== false) {
+            return [1, '刷新认证成功', $data];
+        } else {
+            return [0, '刷新认证失败', []];
+        }
+    }
+
+    /**
+     * 延期 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([
+            'time' => time() + $this->expire,
+        ]);
+    }
+
+    /**
+     * 获取令牌的认证值
+     * @return string
+     */
+    private function _buildTokenVerify(): string
+    {
+        return md5($this->app->request->server('HTTP_USER_AGENT', '-'));
+    }
+}

+ 3 - 3
app/data/service/UpgradeService.php → app/data/service/UserUpgradeService.php

@@ -6,17 +6,17 @@ use think\admin\Service;
 
 /**
  * 用户等级升级服务
- * Class UpgradeService
+ * Class UserUpgradeService
  * @package app\data\service
  */
-class UpgradeService extends Service
+class UserUpgradeService extends Service
 {
 
     /**
      * 同步刷新用户余额
      * @param int $uuid 用户UID
      * @param array $nots 排除的订单
-     * @return array [total,count]
+     * @return array [total, count]
      * @throws \think\db\exception\DbException
      */
     public function balance(int $uuid, array $nots = []): array

+ 3 - 3
app/data/service/payment/BalancePyamentService.php

@@ -3,7 +3,7 @@
 namespace app\data\service\payment;
 
 use app\data\service\PaymentService;
-use app\data\service\UpgradeService;
+use app\data\service\UserUpgradeService;
 use think\admin\Exception;
 use think\admin\extend\CodeExtend;
 
@@ -56,13 +56,13 @@ class BalancePyamentService extends PaymentService
         // 创建支付行为
         $this->createPaymentAction($orderNo, $paymentTitle, $paymentAmount);
         // 扣减用户余额
-        [$total, $used] = UpgradeService::instance()->balance($order['uid'], [$orderNo]);
+        [$total, $used] = UserUpgradeService::instance()->balance($order['uid'], [$orderNo]);
         if ($paymentAmount > $total - $used) throw new Exception("可抵扣余额不足");
         $this->app->db->name('ShopOrder')->where(['order_no' => $orderNo])->update(['amount_balance' => $paymentAmount]);
         // 更新支付行为
         $this->updatePaymentAction($orderNo, CodeExtend::uniqidDate(20), $paymentAmount, '账户余额支付');
         // 刷新用户余额
-        UpgradeService::instance()->balance($order['uid']);
+        UserUpgradeService::instance()->balance($order['uid']);
         return ['info' => '余额支付完成'];
     }
 }