From 29e4c9629b56e04697432535da0657cfbbde0c61 Mon Sep 17 00:00:00 2001 From: zwesy Date: Wed, 1 Dec 2021 14:56:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=EF=BC=9A=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E4=B8=AA=E4=BA=BA=E8=B5=84=E6=96=99=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/api/User.php | 83 +++++++++++++++++++++++++++++++++++-- app/model/Account.php | 17 ++++++++ app/model/Base.php | 24 +++++++++++ 3 files changed, 121 insertions(+), 3 deletions(-) diff --git a/app/controller/api/User.php b/app/controller/api/User.php index 7d77d31..c98dfaf 100644 --- a/app/controller/api/User.php +++ b/app/controller/api/User.php @@ -4,12 +4,14 @@ namespace app\controller\api; use app\exception\RepositoryException; use app\model\Account; use app\repository\AccountRepository; +use app\repository\CouponRepository; use app\service\File; use app\service\Jwt; use app\service\wx\WechatApplets; use app\validate\User as UserValidate; use EasyWeChat\Kernel\Exceptions\InvalidConfigException; use Exception; +use think\exception\ValidateException; use think\response\Json; /** @@ -22,7 +24,6 @@ class User extends Base { protected $noNeedLogin = [ 'login', - 'test', ]; /** @@ -121,10 +122,86 @@ class User extends Base return $this->json(0, 'success', $data); } - - public function test() + /* + * 获取个人中心资料 + */ + public function getUserCenterInfo() { + $accountId = $this->request->user['user_id'] ?? 0; + try { + $accountRepo = AccountRepository::getInstance(); + $couponRepo = CouponRepository::getInstance(); + + $account = $accountRepo->findById($accountId, [], function ($q) { + return $q->with(['business', 'parent']); + }); + if (empty($account)) { + throw new ValidateException('用户无效!'); + } + + if ($account['type'] == Account::type_business) { + // 商家用户 + if (empty($account['business'])) { + throw new ValidateException('用户无效!没有相关的商户信息记录'); + } + + $businessRes = [ + 'avatar' => File::convertCompleteFileUrl($account['avatar_url']), + 'nickName' => $account['nick_name'], + 'userType' => $account['type'], + 'userTypeDes' => Account::accountTypeDescList()[$account['type']] ?? '游客', + 'couponCount' => 0, + 'business' => [ + 'code' => $account['business']['code'], + 'businessName' => $account['business']['business_name'], + 'state' => $account['business']['state'], + 'reason' => $account['business']['reason'], + 'balance' => $account['business']['balance'], + 'enable' => $account['business']['enable'], + ], + ]; + + return $this->json(0, 'success', $businessRes); + } else { + // 用户领取的优惠卷总数量 + $couponCount = $couponRepo->getModel()::getCountByWhere(['consumer_code' => $account['user_code']]); + $personalRes = [ + 'avatar' => File::convertCompleteFileUrl($account['avatar_url']), + 'nickName' => $account['nick_name'], + 'userType' => $account['type'], + 'userTypeDes' => Account::accountTypeDescList()[$account['type']] ?? '游客', + 'couponCount' => $couponCount, + ]; + + // 商户认证记录 + if (!empty($account['business'])) { + $personalRes['business'] = [ + 'state' => $account['business']['state'], + 'reason' => $account['business']['reason'], + ]; + } + + // 员工 + if ($account['type'] == Account::type_staff && !empty($account['parent'])) { + $personalRes['parent'] = [ + 'avatar' => File::convertCompleteFileUrl($account['parent']['avatar_url']), + 'nickName' => $account['parent']['nick_name'], + 'userType' => $account['parent']['type'], + 'userTypeDes' => Account::accountTypeDescList()[$account['parent']['type']] ?? '', + 'couponCount' => 0, + 'businessCode' => $account['parent']['business_code'], + ]; + } + + return $this->json(0, 'success', $personalRes); + } + + } catch (ValidateException $e) { + return $this->json(4001, $e->getError()); + } catch (Exception $e) { + return $this->json(5001, '服务器繁忙!获取用户个人信息失败'); + } } } \ No newline at end of file diff --git a/app/model/Account.php b/app/model/Account.php index 8650038..43408e8 100644 --- a/app/model/Account.php +++ b/app/model/Account.php @@ -4,6 +4,7 @@ namespace app\model; use Exception; use think\exception\ValidateException; +use think\model\relation\HasOne; class Account extends Base { @@ -34,4 +35,20 @@ class Account extends Base return $this->hasOne(Tag::class,"id","tag_id"); } + /** + * 关联的商户信息 + * @return HasOne + */ + public function business() + { + return $this->hasOne(Business::class, 'code', 'business_code'); + } + + /** + * 关联的上级用户 + */ + public function parent() + { + return $this->hasOne(self::class, 'user_code', 'main_code'); + } } diff --git a/app/model/Base.php b/app/model/Base.php index 4163223..91af862 100644 --- a/app/model/Base.php +++ b/app/model/Base.php @@ -370,4 +370,28 @@ class Base extends Model return []; } } + + + /** + * 数量统计 + * + * @param array $where + * @param callable|null $call + * @return int + */ + public static function getCountByWhere(array $where = [], callable $call = null): int + { + $q = new static(); + + if (count($where)) { + $q = $q->where($where); + } + + if ($call) { + $q = $call($q); + } + + return $q->count(); + } + } \ No newline at end of file