更新:获取用户个人资料接口

master
zwesy 2021-12-01 14:56:09 +08:00
parent a47ef3b410
commit 29e4c9629b
3 changed files with 121 additions and 3 deletions

View File

@ -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, '服务器繁忙!获取用户个人信息失败');
}
}
}

View File

@ -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');
}
}

View File

@ -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();
}
}