diff --git a/app/controller/api/Business.php b/app/controller/api/Business.php index df2527c..2e6e529 100644 --- a/app/controller/api/Business.php +++ b/app/controller/api/Business.php @@ -7,7 +7,11 @@ use app\repository\BusinessRepository; use app\repository\DictionaryRepository; use app\validate\BusinessValidate; use think\exception\ValidateException; -use app\model\Business as BusinessModel; +use app\model\{ + Business as BusinessModel, + Account as AccountModel +}; +use think\response\Json; /** * 商家端:商家 @@ -122,5 +126,76 @@ class Business extends Base } } + /** + * 获取当前账号所属商家的信息 + * 适用范围:商户账号 或 商户员工账号 + */ + public function getMyBusinessInfo() + { + $accountId = $this->request->user['user_id'] ?? 0; + + try { + $account = AccountRepository::getInstance()->findById($accountId, [], function ($q) { + return $q->with(['business', 'parent']); + }); + if (empty($account)) { + return $this->json(4004, '无效请求!'); + } + + $result = []; + if ($account['type'] == AccountModel::type_staff) { + // 员工账号 + $businessCode = $account['parent']['business_code'] ?? ''; + if (!empty($businessCode)) { + $business = BusinessRepository::getInstance()->findOneByWhere(['code' => $businessCode]); + if ($business) { + $result = $business->toArray(); + } + } + + $result = arrayKeysExcludeFilter($result, ['pay_account', 'total_recharge']); + + } elseif(!empty($account['business'])) { + // 商户认证信息 + $result = $account['business']->toArray(); + } + + if (count($result)) { + $result = DictionaryRepository::getInstance()->parseAreaText($result); + } + + $result = arrayKeysExcludeFilter($result, ['update_time']); + return $this->json(0, 'success', $result); + } catch (\Exception $e) { + return $this->json(5001, '商家信息查询失败!'); + } + } + + /** + * 获取商家信息(已认证通过的商户) + * @return Json + */ + public function getBusinessInfo() + { + $businessCode = $this->request->param('businessCode/s', ''); + + $busRepo = BusinessRepository::getInstance(); + try { + $business = $busRepo->findOneByWhere(['code' => $businessCode, 'state' => BusinessModel::state_on]); + if (empty($business)) { + return $this->json(4004, '没有相关的商户!'); + } + + $list = $business->toArray(); + $list = DictionaryRepository::getInstance()->parseAreaText($list); + $disableFields = ['is_delete', 'update_time', 'total_recharge', 'balance', 'is_assign', 'agency_code', + 'reason', 'pay_account', 'business_license' + ]; + $result = arrayKeysExcludeFilter($list, $disableFields); + return $this->json(0, 'success', $result); + } catch (\Exception $e) { + return $this->json(5004, '商家信息查询失败!'); + } + } } \ No newline at end of file diff --git a/app/controller/api/User.php b/app/controller/api/User.php index c98dfaf..bf48f84 100644 --- a/app/controller/api/User.php +++ b/app/controller/api/User.php @@ -4,6 +4,7 @@ namespace app\controller\api; use app\exception\RepositoryException; use app\model\Account; use app\repository\AccountRepository; +use app\repository\BusinessRepository; use app\repository\CouponRepository; use app\service\File; use app\service\Jwt; @@ -132,6 +133,7 @@ class User extends Base try { $accountRepo = AccountRepository::getInstance(); $couponRepo = CouponRepository::getInstance(); + $busRepo = BusinessRepository::getInstance(); $account = $accountRepo->findById($accountId, [], function ($q) { return $q->with(['business', 'parent']); @@ -174,26 +176,44 @@ class User extends Base 'couponCount' => $couponCount, ]; - // 商户认证记录 - if (!empty($account['business'])) { + + if ($account['type'] == Account::type_staff) { + // 员工 + $businessCode = $account['parent']['business_code'] ?? ''; + if (!empty($businessCode)) { + $business = $busRepo->findOneByWhere(['code' => $businessCode]); + if ($business) { + $personalRes['business'] = [ + 'code' => $business['code'], + 'businessName' => $business['business_name'], + 'state' => $business['state'], + 'reason' => $business['reason'], + 'balance' => $business['balance'], + 'enable' => $business['enable'], + ]; + } + } + + $personalRes['parent'] = []; + if (!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'], + ]; + } + + } elseif(!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); } diff --git a/app/repository/DictionaryRepository.php b/app/repository/DictionaryRepository.php index bb533b6..a6eae2f 100644 --- a/app/repository/DictionaryRepository.php +++ b/app/repository/DictionaryRepository.php @@ -2,6 +2,7 @@ namespace app\repository; +use app\model\Area; use app\model\BusinessCircle; use app\model\Category; use app\model\Model; @@ -100,4 +101,52 @@ class DictionaryRepository extends Repository } } + /** + * 省市区地址配置数据 + */ + public function findAreaList(array $where = []) + { + try { + return Area::where($where)->select(); + } catch (\Exception $e) { + return new Collection(); + } + } + + + /** + * 通过地址编号查询地址信息 + * @param string $areaCode + * @return Area|array|\think\Model|null + */ + public function findAreaInfo(string $areaCode) + { + try { + return Area::where('code', $areaCode)->find(); + } catch (\Exception $e) { + return null; + } + } + + /** + * 解析地址编码 + * [province, city, county] + */ + public function parseAreaText(array $list, array $areaList = []): array + { + if (empty($list)) { + return []; + } + + if (empty($areaList)) { + $areaList = $this->findAreaList()->column('name', 'code'); + } + + $list['province_text'] = (!isset($list['province']) || empty($list['province'])) ? '' : ($areaList[$list['province']] ?? ''); + $list['city_text'] = (!isset($list['city']) || empty($list['city'])) ? '' : ($areaList[$list['city']] ?? ''); + $list['county_text'] = (!isset($list['county']) || empty($list['county'])) ? '' : ($areaList[$list['county']] ?? ''); + + return $list; + } + } \ No newline at end of file