diff --git a/app/data/controller/api/Auth.php b/app/data/controller/api/Auth.php index 0fd92349c..99958efad 100644 --- a/app/data/controller/api/Auth.php +++ b/app/data/controller/api/Auth.php @@ -8,7 +8,7 @@ use think\exception\HttpResponseException; /** * 授权认证基类 - * Class Member + * Class Auth * @package app\store\controller\api */ abstract class Auth extends Controller @@ -17,7 +17,7 @@ abstract class Auth extends Controller * 当前用户UID * @var int */ - protected $uid; + protected $uuid; /** * 当前用户数据 @@ -25,13 +25,19 @@ abstract class Auth extends Controller */ protected $user; + /** + * 当前接口类型 + * @var string + */ + protected $type = 'wxapp'; + /** * 控制器初始化 */ protected function initialize() { $this->user = $this->getUser(); - $this->uid = $this->user['id']; + $this->uuid = $this->user['id']; } /** @@ -41,9 +47,13 @@ abstract class Auth extends Controller protected function getUser() { try { - $this->token = input('token') ?: $this->request->header('token'); - if (empty($this->token)) $this->error('接口请求认证令牌不能为空!'); - return UserService::instance()->get(['token' => $this->token]); + if (empty($this->uuid)) { + $token = input('token') ?: $this->request->header('token'); + if (empty($token)) $this->error('接口认证令牌不能为空!'); + [$state, $message, $this->uuid] = UserService::instance()->checkUserToken($this->type, $token); + if ($state) $this->error($message); + } + return UserService::instance()->get($this->type, $this->uuid); } catch (HttpResponseException $exception) { throw $exception; } catch (\Exception $exception) { diff --git a/app/data/controller/api/Login.php b/app/data/controller/api/Login.php index 0ccc4b5da..aad7def7e 100644 --- a/app/data/controller/api/Login.php +++ b/app/data/controller/api/Login.php @@ -38,7 +38,7 @@ class Login extends Controller if (empty($user)) $this->error('该手机号还没有注册哦!'); if (empty($user['status'])) $this->error('该用户账号状态异常!'); if (md5($data['password']) === $user['password']) { - $this->success('手机登录成功!', UserService::instance()->get($map, true)); + $this->success('手机登录成功!', UserService::instance()->save($map, [], 'web', true)); } else { $this->error('账号登录失败,请稍候再试!'); } @@ -46,6 +46,7 @@ class Login extends Controller /** * 用户统一注册入口 + * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException @@ -72,7 +73,7 @@ class Login extends Controller $this->error('手机号已注册,请使用其它手机号!'); } $data['password'] = md5($data['password']); - $user = UserService::instance()->save($map, $data, true); + $user = UserService::instance()->save($map, $data, 'web', true); empty($user) ? $this->success('用户注册成功!', $user) : $this->error('手机注册失败!'); } diff --git a/app/data/controller/api/Wechat.php b/app/data/controller/api/Wechat.php index c311b4c97..a15cbe3cc 100644 --- a/app/data/controller/api/Wechat.php +++ b/app/data/controller/api/Wechat.php @@ -82,7 +82,7 @@ class Wechat extends Controller $data['base_sex'] = ['未知', '男', '女'][$data['sex']] ?? '未知'; if (isset($data['headimgurl'])) $data['headimg'] = $data['headimgurl']; $map = isset($data['unionid']) ? ['unionid' => $data['unionid']] : ['openid2' => $this->openid]; - $this->userInfo = UserService::instance()->save($map, array_merge($map, $data), true); + $this->userInfo = UserService::instance()->save($map, array_merge($map, $data), 'wechat', true); $content = $this->_buildContent(); } return Response::create($content)->contentType('application/x-javascript'); diff --git a/app/data/controller/api/Wxapp.php b/app/data/controller/api/Wxapp.php index 032f728b4..5a009a5d5 100644 --- a/app/data/controller/api/Wxapp.php +++ b/app/data/controller/api/Wxapp.php @@ -51,7 +51,7 @@ class Wxapp extends Controller [$openid, $unionid, $sessionKey] = $this->_getSessionKey($input['code']); $map = empty($unionid) ? ['openid1' => $openid] : ['unionid' => $unionid]; $data = array_merge($map, ['openid1' => $openid, 'session_key' => $sessionKey]); - $this->success('授权换取成功!', UserService::instance()->save($map, $data, true)); + $this->success('授权换取成功!', UserService::instance()->save($map, $data, 'wxapp', true)); } /** @@ -76,7 +76,7 @@ class Wxapp extends Controller $sex = ['未知', '男', '女'][$result['gender']] ?? '未知'; $map = empty($result['unionId']) ? ['openid1' => $result['openId']] : ['unionid' => $result['unionId']]; $data = ['openid1' => $result['openId'], 'headimg' => $result['avatarUrl'], 'nickname' => $result['nickName'], 'base_sex' => $sex]; - $this->success('数据解密成功!', UserService::instance()->save($map, array_merge($map, $data), true)); + $this->success('数据解密成功!', UserService::instance()->save($map, array_merge($map, $data), 'wxapp', true)); } elseif (is_array($result) && isset($result['phoneNumber'])) { $this->success('数据解密成功!', $result); } else { diff --git a/app/data/controller/api/auth/Address.php b/app/data/controller/api/auth/Address.php index b58366193..8c2f9959a 100644 --- a/app/data/controller/api/auth/Address.php +++ b/app/data/controller/api/auth/Address.php @@ -25,7 +25,7 @@ class Address extends Auth public function set() { $data = $this->_vali([ - 'uid.value' => $this->uid, + 'uid.value' => $this->uuid, 'code.default' => '', 'type.default' => 0, 'type.in:0,1' => '地址状态不在范围!', @@ -47,14 +47,14 @@ class Address extends Auth $this->error('添加收货地址失败!'); } } else { - $map = ['uid' => $this->uid, 'code' => $data['code']]; + $map = ['uid' => $this->uuid, 'code' => $data['code']]; $address = $this->app->db->name($this->table)->where($map)->find(); if (empty($address)) $this->error('修改收货地址不存在!'); $this->app->db->name($this->table)->where($map)->update($data); } // 去除其它默认选项 if (isset($data['type']) && $data['type'] > 0) { - $map = [['uid', '=', $this->uid], ['code', '<>', $data['code']]]; + $map = [['uid', '=', $this->uuid], ['code', '<>', $data['code']]]; $this->app->db->name($this->table)->where($map)->update(['type' => 0]); } $this->success('添加收货地址成功!', $this->_getAddress($data['code'])); @@ -69,7 +69,7 @@ class Address extends Auth public function get() { $query = $this->_query($this->table)->withoutField('deleted'); - $query->equal('code')->where(['uid' => $this->uid, 'deleted' => 0]); + $query->equal('code')->where(['uid' => $this->uuid, 'deleted' => 0]); $result = $query->order('type desc,id desc')->page(false, false, false, 15); $this->success('获取收货地址数据!', $result); } @@ -81,7 +81,7 @@ class Address extends Auth public function state() { $data = $this->_vali([ - 'uid.value' => $this->uid, + 'uid.value' => $this->uuid, 'type.in:0,1' => '地址状态不在范围!', 'type.require' => '地址状态不能为空!', 'code.require' => '地址编号不能为空!', @@ -96,7 +96,7 @@ class Address extends Auth $this->app->db->name($this->table)->where($map)->update(['type' => $data['type']]); // 去除其它默认选项 if ($data['type'] > 0) { - $map = [['uid', '=', $this->uid], ['code', '<>', $data['code']]]; + $map = [['uid', '=', $this->uuid], ['code', '<>', $data['code']]]; $this->app->db->name($this->table)->where($map)->update(['type' => 0]); } $this->success('默认设置成功!', $this->_getAddress($data['code'])); @@ -109,7 +109,7 @@ class Address extends Auth public function remove() { $map = $this->_vali([ - 'uid.value' => $this->uid, + 'uid.value' => $this->uuid, 'code.require' => '地址编号不能为空!', ]); $address = $this->app->db->name($this->table)->where($map)->find(); @@ -131,7 +131,7 @@ class Address extends Auth */ private function _getAddress(string $code) { - $map = ['code' => $code, 'uid' => $this->uid, 'deleted' => 0]; + $map = ['code' => $code, 'uid' => $this->uuid, 'deleted' => 0]; return $this->app->db->name($this->table)->withoutField('deleted')->where($map)->find(); } diff --git a/app/data/controller/api/auth/Center.php b/app/data/controller/api/auth/Center.php index 0563dc45b..276451f2d 100644 --- a/app/data/controller/api/auth/Center.php +++ b/app/data/controller/api/auth/Center.php @@ -39,7 +39,7 @@ class Center extends Auth if ($vo === '') unset($data[$key]); } if (empty($data)) $this->error('没有修改的数据!'); - if ($this->app->db->name($this->table)->where(['id' => $this->uid])->update($data) !== false) { + if ($this->app->db->name($this->table)->where(['id' => $this->uuid])->update($data) !== false) { $this->success('更新资料成功!', $this->getUser()); } else { $this->error('更新资料失败!'); @@ -59,7 +59,7 @@ class Center extends Auth */ public function total() { - $this->success('获取用户统计!', UserService::instance()->total($this->uid)); + $this->success('获取用户统计!', UserService::instance()->total($this->uuid)); } /** @@ -93,16 +93,16 @@ class Center extends Auth public function bindFrom() { $data = $this->_vali(['from.require' => '邀请人不能为空']); - if ($data['from'] == $this->uid) { - $this->error('邀请人不能是自己哦', UserService::instance()->total($this->uid)); + if ($data['from'] == $this->uuid) { + $this->error('邀请人不能是自己哦', UserService::instance()->total($this->uuid)); } $from = $this->app->db->name($this->table)->where(['id' => $data['from']])->find(); - if (empty($from)) $this->error('邀请人状态异常', UserService::instance()->get($this->uid)); - if ($this->user['from'] > 0) $this->error('您已经绑定了邀请人', UserService::instance()->total($this->uid)); - if ($this->app->db->name($this->table)->where(['id' => $this->uid])->update($data) !== false) { - $this->success('绑定邀请人成功!', UserService::instance()->total($this->uid)); + if (empty($from)) $this->error('邀请人状态异常', UserService::instance()->get($this->type, $this->uuid)); + if ($this->user['from'] > 0) $this->error('您已经绑定了邀请人', UserService::instance()->total($this->uuid)); + if ($this->app->db->name($this->table)->where(['id' => $this->uuid])->update($data) !== false) { + $this->success('绑定邀请人成功!', UserService::instance()->total($this->uuid)); } else { - $this->error('绑定邀请人失败!', UserService::instance()->total($this->uid)); + $this->error('绑定邀请人失败!', UserService::instance()->total($this->uuid)); } } @@ -115,7 +115,7 @@ class Center extends Auth public function getFrom() { $query = $this->_query($this->table); - $query->where(['from' => $this->uid])->field('id,from,username,nickname,headimg,create_at'); + $query->where(['from' => $this->uuid])->field('id,from,username,nickname,headimg,create_at'); $this->success('获取我邀请的朋友', $query->order('id desc')->page(true, false, false, 15)); } } \ No newline at end of file diff --git a/app/data/controller/api/auth/News.php b/app/data/controller/api/auth/News.php index 10a5150b9..4cf7ffae2 100644 --- a/app/data/controller/api/auth/News.php +++ b/app/data/controller/api/auth/News.php @@ -19,7 +19,7 @@ class News extends Auth public function addComment() { $data = $this->_vali([ - 'uid.value' => $this->uid, + 'uid.value' => $this->uuid, 'code.require' => '文章不能为空!', 'content.require' => '内容不能为空!', ]); @@ -39,7 +39,7 @@ class News extends Auth */ public function getComment() { - $map = $this->_vali(['uid.value' => $this->uid, 'code.require' => '文章不能为空!']); + $map = $this->_vali(['uid.value' => $this->uuid, 'code.require' => '文章不能为空!']); $result = $this->_query('DataNewsXComment')->where($map)->order('id desc')->page(true, false); if (count($result['list']) > 0) { NewsService::instance()->buildListByMinAndCode($result); @@ -54,7 +54,7 @@ class News extends Auth public function delComment() { $map = $this->_vali([ - 'uid.value' => $this->uid, + 'uid.value' => $this->uuid, 'id.require' => '评论ID不能为空!', 'code.require' => '文章CODE不能为空!', ]); @@ -104,7 +104,7 @@ class News extends Auth */ public function getCollect() { - $map = ['uid' => $this->uid, 'type' => 1]; + $map = ['uid' => $this->uuid, 'type' => 1]; $query = $this->_query('DataNewsXCollect')->where($map); $result = $query->order('id desc')->page(true, false, false, 15); if (count($result['list']) > 0) { @@ -153,7 +153,7 @@ class News extends Auth public function getLike() { $query = $this->_query('DataNewsXCollect')->order('id desc'); - $result = $query->where(['uid' => $this->uid, 'type' => 2])->page(true, false, false, 15); + $result = $query->where(['uid' => $this->uuid, 'type' => 2])->page(true, false, false, 15); NewsService::instance()->buildListByMinAndCode($result['list']); $this->success('获取点赞记录成功!', $result); } @@ -167,7 +167,7 @@ class News extends Auth public function getHistory() { $query = $this->_query('DataNewsXCollect')->order('id desc'); - $result = $query->where(['uid' => $this->uid, 'type' => 3])->page(true, false, false, 15); + $result = $query->where(['uid' => $this->uuid, 'type' => 3])->page(true, false, false, 15); NewsService::instance()->buildListByMinAndCode($result['list']); $this->success('获取浏览历史成功!', $result); } @@ -180,7 +180,7 @@ class News extends Auth private function _getCollectWhere(int $type = 1): array { return $this->_vali([ - 'uid.value' => $this->uid, + 'uid.value' => $this->uuid, 'type.value' => $type, 'code.require' => '编号不能为空!', ]); diff --git a/app/data/controller/api/auth/Order.php b/app/data/controller/api/auth/Order.php index 423625281..6f834142d 100644 --- a/app/data/controller/api/auth/Order.php +++ b/app/data/controller/api/auth/Order.php @@ -36,7 +36,7 @@ class Order extends Auth */ public function get() { - $map = [['uid', '=', $this->uid]]; + $map = [['uid', '=', $this->uuid]]; if (!$this->request->has('order_no', 'param', true)) { $map[] = ['status', 'in', [0, 2, 3, 4, 5]]; } @@ -59,10 +59,10 @@ class Order extends Auth if (empty($rules)) $this->error('商品规则不能为空!'); // 订单数据 [$codes, $items] = [[], []]; - $order = ['uid' => $this->uid, 'from' => input('from_mid', '0'), 'status' => 1]; + $order = ['uid' => $this->uuid, 'from' => input('from_mid', '0'), 'status' => 1]; $order['order_no'] = CodeExtend::uniqidDate(18, 'N'); // 推荐人处理 - if ($order['from'] == $this->uid) { + if ($order['from'] == $this->uuid) { $order['from'] = 0; } if ($order['from'] > 0) { @@ -137,11 +137,11 @@ class Order extends Auth 'order_no.require' => '订单单号不能为空!', ]); // 用户收货地址 - $map = ['uid' => $this->uid, 'code' => $data['code'], 'deleted' => 0]; + $map = ['uid' => $this->uuid, 'code' => $data['code'], 'deleted' => 0]; $addr = $this->app->db->name('DataUserAddress')->where($map)->find(); if (empty($addr)) $this->error('用户收货地址异常!'); // 订单状态检查 - $map = ['uid' => $this->uid, 'order_no' => $data['order_no']]; + $map = ['uid' => $this->uuid, 'order_no' => $data['order_no']]; $order = $this->app->db->name('ShopOrder')->where($map)->whereIn('status', [1, 2])->find(); $tCount = $this->app->db->name('ShopOrderItem')->where($map)->sum('truck_count'); if (empty($order)) $this->error('不能修改收货地址哦!'); @@ -151,9 +151,8 @@ class Order extends Auth [$amount, $tCount, $tCode, $remark] = TruckService::instance()->amount($tCode, $addr['province'], $addr['city'], $tCount); // 创建订单发货信息 $express = [ - 'uid' => $this->uid, 'status' => 1, - 'template_code' => $tCode, 'template_count' => $tCount, - 'template_remark' => $remark, 'template_amount' => $amount, + 'template_code' => $tCode, 'template_count' => $tCount, 'uid' => $this->uuid, + 'template_remark' => $remark, 'template_amount' => $amount, 'status' => 1, ]; $express['order_no'] = $data['order_no']; $express['address_code'] = $data['code']; @@ -166,7 +165,7 @@ class Order extends Auth $express['address_datetime'] = date('Y-m-d H:i:s'); data_save('ShopOrderSend', $express, 'order_no'); // 更新订单状态,刷新订单金额 - $map = ['uid' => $this->uid, 'order_no' => $data['order_no']]; + $map = ['uid' => $this->uuid, 'order_no' => $data['order_no']]; $update = ['status' => 2, 'amount_express' => $express['template_amount']]; $update['amount_total'] = $order['amount_goods'] + $amount - $order['amount_reduct'] - $order['amount_discount']; if ($this->app->db->name('ShopOrder')->where($map)->update($update) !== false) { @@ -232,7 +231,7 @@ class Order extends Auth public function cancel() { $map = $this->_vali([ - 'uid.value' => $this->uid, + 'uid.value' => $this->uuid, 'order_no.require' => '订单号不能为空!', ]); $order = $this->app->db->name('ShopOrder')->where($map)->find(); @@ -263,7 +262,7 @@ class Order extends Auth public function confirm() { $map = $this->_vali([ - 'uid.value' => $this->uid, + 'uid.value' => $this->uuid, 'order_no.require' => '订单号不能为空!', ]); $order = $this->app->db->name('ShopOrder')->where($map)->find(); @@ -288,7 +287,7 @@ class Order extends Auth */ public function total() { - $map = ['uid' => $this->uid, 'deleted' => 0]; + $map = ['uid' => $this->uuid, 'deleted' => 0]; $data = ['t0' => 0, 't1' => 0, 't2' => 0, 't3' => 0, 't4' => 0, 't5' => 0]; $query = $this->app->db->name('ShopOrder')->fieldRaw('status,count(1) count'); $query->where($map)->group('status')->select()->each(function ($item) use (&$data) { @@ -304,7 +303,8 @@ class Order extends Auth { try { $data = $this->_vali([ - 'code.require' => '快递编号不能为空!', 'number.require' => '配送单号不能为空!', + 'code.require' => '快递编号不能为空!', + 'number.require' => '配送单号不能为空!', ]); $result = TruckService::instance()->query($data['code'], $data['number']); empty($result['code']) ? $this->error($result['info']) : $this->success('快递追踪信息', $result); diff --git a/app/data/data.sql b/app/data/data.sql index 591832798..1660149b2 100644 --- a/app/data/data.sql +++ b/app/data/data.sql @@ -11,7 +11,7 @@ Target Server Version : 50562 File Encoding : 65001 - Date: 24/11/2020 15:59:13 + Date: 24/11/2020 17:59:00 */ SET NAMES utf8mb4; @@ -82,7 +82,7 @@ CREATE TABLE `data_news_x_collect` ( INDEX `idx_data_news_x_collect_mid`(`uid`) USING BTREE, INDEX `idx_data_news_x_collect_type`(`type`) USING BTREE, INDEX `idx_data_news_x_collect_code`(`code`) USING BTREE -) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '数据-文章-标记' ROW_FORMAT = Compact; +) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '数据-文章-标记' ROW_FORMAT = COMPACT; -- ---------------------------- -- Records of data_news_x_collect @@ -113,9 +113,7 @@ CREATE TABLE `data_news_x_comment` ( DROP TABLE IF EXISTS `data_user`; CREATE TABLE `data_user` ( `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, - `from` bigint(20) UNSIGNED NULL DEFAULT 0 COMMENT '邀请者UID', - `token` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '授权TOKEN令牌', - `tokenv` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '授权TOKEN验证', + `from` bigint(20) UNSIGNED NULL DEFAULT 0 COMMENT '邀请者MID', `openid1` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '小程序OPENID', `openid2` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '服务号OPENID', `unionid` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '公众号UnionID', @@ -138,13 +136,12 @@ CREATE TABLE `data_user` ( `deleted` tinyint(1) UNSIGNED NULL DEFAULT 0 COMMENT '删除状态', `create_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '注册时间', PRIMARY KEY (`id`) USING BTREE, - INDEX `idx_data_user_token`(`token`) USING BTREE, INDEX `idx_data_user_status`(`status`) USING BTREE, INDEX `idx_data_user_deleted`(`deleted`) USING BTREE, INDEX `idx_data_user_openid1`(`openid1`) USING BTREE, INDEX `idx_data_user_openid2`(`openid2`) USING BTREE, INDEX `idx_data_user_unionid`(`unionid`) USING BTREE -) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '数据-用户' ROW_FORMAT = COMPACT; +) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '数据-用户-记录' ROW_FORMAT = COMPACT; -- ---------------------------- -- Records of data_user @@ -172,7 +169,7 @@ CREATE TABLE `data_user_address` ( INDEX `idx_data_user_address_type`(`type`) USING BTREE, INDEX `idx_data_user_address_code`(`code`) USING BTREE, INDEX `idx_data_user_address_deleted`(`deleted`) USING BTREE -) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '数据-用户-地址' ROW_FORMAT = Compact; +) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '数据-用户-地址' ROW_FORMAT = COMPACT; -- ---------------------------- -- Records of data_user_address @@ -210,7 +207,7 @@ DROP TABLE IF EXISTS `data_user_coin_used`; CREATE TABLE `data_user_coin_used` ( `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, `uid` bigint(20) UNSIGNED NULL DEFAULT 0 COMMENT '用户UID', - `from` bigint(20) NULL DEFAULT 0 COMMENT '来自UID', + `from` bigint(20) NULL DEFAULT 0 COMMENT '来自MID', `type` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT '记录类型', `target` bigint(20) UNSIGNED NULL DEFAULT 0 COMMENT '目标ID', `name` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' COMMENT '记录名称', @@ -246,12 +243,34 @@ CREATE TABLE `data_user_message` ( INDEX `idx_data_user_message_status`(`status`) USING BTREE, INDEX `idx_data_user_message_phone`(`phone`) USING BTREE, INDEX `idx_data_user_message_msgid`(`msgid`) USING BTREE -) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '系统-用户-短信' ROW_FORMAT = Compact; +) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '系统-用户-短信' ROW_FORMAT = COMPACT; -- ---------------------------- -- Records of data_user_message -- ---------------------------- +-- ---------------------------- +-- Table structure for data_user_token +-- ---------------------------- +DROP TABLE IF EXISTS `data_user_token`; +CREATE TABLE `data_user_token` ( + `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT, + `uid` bigint(20) UNSIGNED NULL DEFAULT 0 COMMENT '用户UID', + `type` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '授权类型', + `time` bigint(20) UNSIGNED NULL DEFAULT 0 COMMENT '有效时间', + `token` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '授权TOKEN令牌', + `tokenv` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '授权TOKEN验证', + `create_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '注册时间', + PRIMARY KEY (`id`) USING BTREE, + INDEX `idx_data_user_token_type`(`type`) USING BTREE, + INDEX `idx_data_user_token_time`(`time`) USING BTREE, + INDEX `idx_data_user_token_token`(`token`) USING BTREE +) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '数据-用户-认证' ROW_FORMAT = COMPACT; + +-- ---------------------------- +-- Records of data_user_token +-- ---------------------------- + -- ---------------------------- -- Table structure for shop_goods -- ---------------------------- @@ -284,7 +303,7 @@ CREATE TABLE `shop_goods` ( PRIMARY KEY (`id`) USING BTREE, INDEX `idx_data_news_item_status`(`status`) USING BTREE, INDEX `idx_data_news_item_deleted`(`deleted`) USING BTREE -) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '商城-商品-内容' ROW_FORMAT = Compact; +) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '商城-商品-内容' ROW_FORMAT = COMPACT; -- ---------------------------- -- Records of shop_goods @@ -308,7 +327,7 @@ CREATE TABLE `shop_goods_cate` ( INDEX `idx_shop_goods_cate_sort`(`sort`) USING BTREE, INDEX `idx_shop_goods_cate_status`(`status`) USING BTREE, INDEX `idx_shop_goods_cate_deleted`(`deleted`) USING BTREE -) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '商城-商品-分类' ROW_FORMAT = Compact; +) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '商城-商品-分类' ROW_FORMAT = COMPACT; -- ---------------------------- -- Records of shop_goods_cate @@ -355,7 +374,7 @@ CREATE TABLE `shop_goods_mark` ( PRIMARY KEY (`id`) USING BTREE, INDEX `idx_shop_goods_mark_sort`(`sort`) USING BTREE, INDEX `idx_shop_goods_mark_status`(`status`) USING BTREE -) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '商城-商品-标签' ROW_FORMAT = Compact; +) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '商城-商品-标签' ROW_FORMAT = COMPACT; -- ---------------------------- -- Records of shop_goods_mark @@ -377,7 +396,7 @@ CREATE TABLE `shop_goods_stock` ( PRIMARY KEY (`id`) USING BTREE, INDEX `idx_data_news_item_status`(`status`) USING BTREE, INDEX `idx_data_news_item_deleted`(`deleted`) USING BTREE -) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '商城-商品-库存' ROW_FORMAT = Compact; +) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '商城-商品-库存' ROW_FORMAT = COMPACT; -- ---------------------------- -- Records of shop_goods_stock @@ -417,7 +436,7 @@ CREATE TABLE `shop_order` ( INDEX `idx_shop_order_orderno`(`order_no`) USING BTREE, INDEX `idx_shop_order_cancel_status`(`cancel_status`) USING BTREE, INDEX `idx_shop_order_payment_status`(`payment_status`) USING BTREE -) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '商城-订单-内容' ROW_FORMAT = Compact; +) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '商城-订单-内容' ROW_FORMAT = COMPACT; -- ---------------------------- -- Records of shop_order @@ -453,7 +472,7 @@ CREATE TABLE `shop_order_item` ( INDEX `idx_shop_order_item_goods_sku`(`goods_sku`) USING BTREE, INDEX `idx_shop_order_item_goods_code`(`goods_code`) USING BTREE, INDEX `idx_shop_order_item_goods_spec`(`goods_spec`) USING BTREE -) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '商城-订单-商品' ROW_FORMAT = Compact; +) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '商城-订单-商品' ROW_FORMAT = COMPACT; -- ---------------------------- -- Records of shop_order_item @@ -492,7 +511,7 @@ CREATE TABLE `shop_order_send` ( INDEX `idx_shop_order_send_status`(`status`) USING BTREE, INDEX `idx_shop_order_send_deleted`(`deleted`) USING BTREE, INDEX `idx_shop_order_send_order_no`(`order_no`) USING BTREE -) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '商城-订单-配送' ROW_FORMAT = Compact; +) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '商城-订单-配送' ROW_FORMAT = COMPACT; -- ---------------------------- -- Records of shop_order_send @@ -520,7 +539,7 @@ CREATE TABLE `shop_order_service` ( PRIMARY KEY (`id`) USING BTREE, INDEX `idx_data_news_item_status`(`status`) USING BTREE, INDEX `idx_data_news_item_deleted`(`deleted`) USING BTREE -) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '商城-订单-售后' ROW_FORMAT = Compact; +) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '商城-订单-售后' ROW_FORMAT = COMPACT; -- ---------------------------- -- Records of shop_order_service @@ -547,7 +566,7 @@ CREATE TABLE `shop_truck_company` ( INDEX `idx_shop_truck_company_code3`(`code_3`) USING BTREE, INDEX `idx_shop_truck_company_status`(`status`) USING BTREE, INDEX `idx_shop_truck_company_deleted`(`deleted`) USING BTREE -) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '商城-快递-公司' ROW_FORMAT = Compact; +) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '商城-快递-公司' ROW_FORMAT = COMPACT; -- ---------------------------- -- Records of shop_truck_company @@ -572,7 +591,7 @@ CREATE TABLE `shop_truck_region` ( PRIMARY KEY (`id`) USING BTREE, INDEX `idx_shop_truck_region_pid`(`pid`) USING BTREE, INDEX `idx_shop_truck_region_name`(`name`) USING BTREE -) ENGINE = InnoDB AUTO_INCREMENT = 4019 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '商城-快递-区域' ROW_FORMAT = Compact; +) ENGINE = InnoDB AUTO_INCREMENT = 4019 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '商城-快递-区域' ROW_FORMAT = COMPACT; -- ---------------------------- -- Records of shop_truck_region @@ -4337,7 +4356,7 @@ CREATE TABLE `shop_truck_template` ( `deleted` tinyint(1) UNSIGNED NULL DEFAULT 0 COMMENT '删除状态', `create_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', PRIMARY KEY (`id`) USING BTREE -) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '商城-快递-模板' ROW_FORMAT = Compact; +) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '商城-快递-模板' ROW_FORMAT = COMPACT; -- ---------------------------- -- Records of shop_truck_template diff --git a/app/data/service/UserService.php b/app/data/service/UserService.php index 5ff816ead..23e4a6ca5 100644 --- a/app/data/service/UserService.php +++ b/app/data/service/UserService.php @@ -12,35 +12,29 @@ use think\admin\Service; class UserService extends Service { /** - * 绑定数据表 - * @var string + * 认证有效时间 + * @var integer */ - protected $table = 'DataUser'; + private $expire = 3600; /** - * 获取用户资料 - * @param mixed $map 查询条件 - * @param boolean $force 刷新令牌 + * 获取用户数据 + * @param string $type 接口类型 + * @param integer $uuid 用户UID * @return array * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ - public function get($map, bool $force = false): array + public function get(string $type, int $uuid) { - if (is_numeric($map)) { - $map = ['id' => $map]; - } elseif (is_string($map)) { - $map = ['token|openid1|openid2|unionid' => $map]; - } - $user = $this->save($map, [], $force); - if (empty($user)) { - throw new \think\Exception('登录授权失败'); - } - // if ($member['tokenv'] !== $this->buildTokenVerify()) { - // throw new \think\Exception('请重新登录授权'); - // } + $user = $this->app->db->name('DataUser')->where(['id' => $uuid, 'deleted' => 0])->findOrEmpty(); + $data = $this->app->db->name('DataUserToken')->where(['uid' => $uuid, 'type' => $type])->findOrEmpty(); + [$state, $message] = $this->checkUserToken($type, $data['token'], $data); + if (empty($state)) throw new \think\Exception($message); + unset($user['deleted'], $user['password']); + $user['token'] = ['token' => $data['token'], 'expire' => $data['time']]; return $user; } @@ -48,52 +42,89 @@ class UserService extends Service * 更新用户用户参数 * @param array $map 查询条件 * @param array $data 更新数据 + * @param string $type 接口类型 * @param boolean $force 强刷令牌 * @return array + * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ - public function save(array $map, array $data = [], bool $force = false): array + public function save(array $map, array $data, string $type, bool $force = false): array { - $query = $this->app->db->name($this->table)->where($map); - $member = $query->withoutField('deleted,password')->where(['deleted' => 0])->find() ?: []; - unset($data['id'], $data['token'], $data['tokenv'], $data['deleted'], $data['create_at']); - if (empty($data['phone']) && empty($data['unionid']) && empty($data['openid1']) && empty($data['openid2'])) { - return $member; - } - if ($force) $data = array_merge($data, $this->_buildUserToken()); - if (isset($member['id']) && $member['id'] > 0) { - $map = ['id' => $member['id'], 'deleted' => 0]; - $this->app->db->name($this->table)->strict(false)->where($map)->update($data); + unset($data['id'], $data['deleted'], $data['create_at']); + if ($uid = $this->app->db->name('DataUser')->where($map)->where(['deleted' => 0])->value('id')) { + if (!empty($data)) { + $map = ['id' => $uid, 'deleted' => 0]; + $this->app->db->name('DataUser')->strict(false)->where($map)->update($data); + } } else { - $member['id'] = $this->app->db->name($this->table)->strict(false)->insertGetId($data); + $uid = $this->app->db->name('DataUser')->strict(false)->insertGetId($data); } - $map = ['id' => $member['id'], 'deleted' => 0]; - $query = $this->app->db->name($this->table)->where($map); - return $query->withoutField('deleted,password')->find() ?: []; + if ($force) $this->buildUserToken($uid, $type); + return $this->get($uid, $type); } /** * 获取用户数据统计 - * @param int $mid 用户UID + * @param int $uid 用户UID * @return array */ - public function total(int $mid): array + public function total(int $uid): array { - $query = $this->app->db->name($this->table); - return ['my_invite' => $query->where(['from' => $mid])->count()]; + $query = $this->app->db->name('DataUser'); + return ['my_invite' => $query->where(['from' => $uid])->count()]; } /** * 生成新的用户令牌 - * @return array + * @param int $uid 授权用户 + * @param string $type 接口类型 + * @return array [创建状态, 状态描述, 令牌数据] + * @throws \think\db\exception\DbException */ - private function _buildUserToken(): array + public function buildUserToken(int $uid, string $type): array { - do $map = ['token' => md5(uniqid('', true) . rand(100, 999))]; - while ($this->app->db->name($this->table)->where($map)->count() > 0); - return ['token' => $map['token'], 'tokenv' => $this->_buildTokenVerify()]; + // 清理历史认证及已过期的认证 + $map1 = [['time', '<', $time = time()]]; + $map2 = [['uid', '=', $uid], ['type', '=', $type]]; + $this->app->db->name('DataUserToken')->whereOr([$map1, $map2])->delete(); + // 创建用户新的用户认证数据 + do $map = ['type' => $type, 'token' => md5(uniqid('', true) . rand(100, 999))]; + while ($this->app->db->name('DataUser')->where($map)->count() > 0); + $token = array_merge($map, ['time' => $time + $this->expire, 'tokenv' => $this->_buildTokenVerify()]); + if ($this->app->db->name('DataUserToken')->insert($token) !== false) { + return [1, '刷新用户认证成功', $token]; + } else { + return [0, '刷新用户认证失败', []]; + } + } + + /** + * 检查接口授权 TOKEN 是否有效 + * @param string $type 接口类型 + * @param string $token 认证令牌 + * @param array $data 认证数据 + * @return array [ 检查状态,状态描述,用户UID, 有效时间 ] + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function checkUserToken(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 ($data['time'] < time()) { + return [0, '接口认证令牌已失效', 0, 0]; + } elseif ($data['tokenv'] !== $this->_buildTokenVerify()) { + return [0, '接口请求客户端已更换', 0, 0]; + } else { + return [1, '接口认证令牌验证成功', $data['uid'], $data['time']]; + } } /**