From 11fe3636d762aec19b7e8bcd95e1b9f5a1e92bd5 Mon Sep 17 00:00:00 2001 From: zwesy Date: Tue, 30 Nov 2021 18:52:02 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=9B=B4=E6=96=B0=EF=BC=9A=E5=95=86?= =?UTF-8?q?=E5=AE=B6=E6=B3=A8=E5=86=8C-2=E7=A7=8D=E6=96=B9=E5=BC=8F?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/api/Base.php | 13 ++ app/controller/api/Business.php | 211 ++++++++++++++++++++++++ app/controller/api/Dictionary.php | 26 ++- app/controller/api/file/Upload.php | 23 +-- app/model/Business.php | 17 ++ app/model/Category.php | 4 +- app/repository/BusinessRepository.php | 27 +++ app/repository/DictionaryRepository.php | 34 ++++ app/validate/BusinessValidate.php | 55 ++++++ 9 files changed, 388 insertions(+), 22 deletions(-) create mode 100644 app/validate/BusinessValidate.php diff --git a/app/controller/api/Base.php b/app/controller/api/Base.php index 118846a..76023c4 100644 --- a/app/controller/api/Base.php +++ b/app/controller/api/Base.php @@ -48,4 +48,17 @@ class Base extends BaseController return json($result); } + /** + * 取消程序运行时间和内存限制 + * max_execution_time PHP程序最大执行时间限制(单位:秒) + * memory_limit 此次的最大运行内存 (单位:字节Byte) + * set_time_limit 当前操作最长执行时间限制(单位:秒) + */ + protected function cancelTimeLimit(int $maxExecTime = 0, int $memory = -1, int $timeOut = 0) + { + ini_set('max_execution_time', $maxExecTime); + ini_set("memory_limit", $memory); + set_time_limit($timeOut); + } + } \ No newline at end of file diff --git a/app/controller/api/Business.php b/app/controller/api/Business.php index 9dc8e1a..3712ab1 100644 --- a/app/controller/api/Business.php +++ b/app/controller/api/Business.php @@ -1,6 +1,14 @@ $this->request->param('type/d', 0), + 'business_name'=> $this->request->param('business_name', ''), + 'business_subtitle'=> $this->request->param('business_subtitle', ''), + 'business_license'=> $this->request->param('business_license', ''), + 'contact_name'=> $this->request->param('contact_name', ''), + 'contact_phone'=> $this->request->param('contact_phone', ''), + 'lat'=> $this->request->param('lat', ''), + 'lng'=> $this->request->param('lng', ''), + 'province'=> $this->request->param('province', ''), + 'city'=> $this->request->param('city', ''), + 'county'=> $this->request->param('county', ''), + 'business_address' => $this->request->param('business_address', ''), + 'business_circle_id' => $this->request->param('business_circle_id/d', 0), + ]; + + $accountId = $this->request->user['user_id'] ?? 0; + + + try { + $validate = new BusinessValidate(); + $busRepo = BusinessRepository::getInstance(); + $dicRepo = DictionaryRepository::getInstance(); + $accountRepo = AccountRepository::getInstance(); + + $account = $accountRepo->findById($accountId); + if (empty($account)) { + throw new ValidateException('无效请求!'); + } + + if (!$validate->scene('registerByNormal')->check($params)) { + throw new ValidateException($validate->getError()); + } + + $businessCategory = $dicRepo->findBusinessTypeById($params['type']); + if (empty($businessCategory)) { + throw new ValidateException('请选择正确的商家分类信息!'); + } + + if ($params['business_circle_id'] > 0) { + $businessCircle = $dicRepo->findBusinessCircleById($params['business_circle_id']); + if (empty($businessCircle)) { + throw new ValidateException('请选择正确的商圈信息!'); + } + $params['business_circle'] = $businessCircle['name']; + } + + $business = null; + if (isset($account['business_code']) && !empty($account['business_code'])) { + $business = $busRepo->findOneByWhere(['code'=> $account['business_code']]); + } + + $params['create_time'] = date('Y-m-d H:i:s'); + $params['is_delete'] = 0; + $params['state'] = BusinessModel::state_reviewing; + $params['enable'] = 0; + $params['type_name'] = $businessCategory['name']; + + if ($business) { + if ($business['state'] == BusinessModel::state_reviewing) { + throw new ValidateException('商户认证审批中,请勿重复提交!'); + } + + // 更新审批信息,重新审批 + $params['update_time'] = date('Y-m-d H:i:s'); + $business = $business->save($params); + + } else { + // 添加审批记录 + $businessCode = createUuid(); + $params['code'] = $businessCode; + $business = $busRepo->create($params); + if (!$business) { + throw new RepositoryException('服务器繁忙!商户认证申请提交失败!'); + } + + $account->save(['business_code' => $businessCode]); + } + + $result = $busRepo->formatFrontBusinessInfo($business->toArray()); + return $this->json(0, 'success', $result); + } catch (ValidateException $e) { + return $this->json(4001, $e->getError()); + } catch (RepositoryException | \Exception $e) { + return $this->json(5001, '服务器繁忙!商户认证申请提交失败!'); + } + } + + /** + * 商家注册 + * + * 方式2:注册为平台代理商下的商家 + * 由平台代理商工作人员审核 + * + * 重新编辑注册则覆盖之前的审核信息,并重新进行审核 + */ + public function registerByAgency() + { + $params = [ + 'type'=> $this->request->param('type/d', 0), + 'business_name'=> $this->request->param('business_name', ''), + 'business_subtitle'=> $this->request->param('business_subtitle', ''), + 'business_license'=> $this->request->param('business_license', ''), + 'contact_name'=> $this->request->param('contact_name', ''), + 'contact_phone'=> $this->request->param('contact_phone', ''), + 'lat'=> $this->request->param('lat', ''), + 'lng'=> $this->request->param('lng', ''), + 'province'=> $this->request->param('province', ''), + 'city'=> $this->request->param('city', ''), + 'county'=> $this->request->param('county', ''), + 'business_address' => $this->request->param('business_address', ''), + 'business_circle_id' => $this->request->param('business_circle_id/d', 0), + 'agency_code' => $this->request->param('agency_code', ''), + ]; + + $accountId = $this->request->user['user_id'] ?? 0; + + try { + $validate = new BusinessValidate(); + $busRepo = BusinessRepository::getInstance(); + $dicRepo = DictionaryRepository::getInstance(); + $accountRepo = AccountRepository::getInstance(); + + $account = $accountRepo->findById($accountId); + if (empty($account)) { + throw new ValidateException('无效请求!'); + } + + if (!$validate->scene('registerByAgency')->check($params)) { + throw new ValidateException($validate->getError()); + } + + $businessCategory = $dicRepo->findBusinessTypeById($params['type']); + if (empty($businessCategory)) { + throw new ValidateException('请选择正确的商家分类信息!'); + } + + if ($params['business_circle_id'] > 0) { + $businessCircle = $dicRepo->findBusinessCircleById($params['business_circle_id']); + if (empty($businessCircle)) { + throw new ValidateException('请选择正确的商圈信息!'); + } + $params['business_circle'] = $businessCircle['name']; + } + + $agencyBusiness = $busRepo->findOneByWhere(['code'=> $params['agency_code'], 'is_agency'=> self::BOOL_TRUE, 'state' => BusinessModel::state_on]); + if (empty($agencyBusiness) || $agencyBusiness['is_delete'] == self::BOOL_TRUE) { + throw new ValidateException('没有相关的平台商记录!'); + } elseif ($agencyBusiness['enable'] == self::BOOL_TRUE) { + throw new ValidateException('该平台商已被封禁!'); + } + + $business = null; + if (isset($account['business_code']) && !empty($account['business_code'])) { + $business = $busRepo->findOneByWhere(['code'=> $account['business_code']]); + } + + $params['create_time'] = date('Y-m-d H:i:s'); + $params['is_delete'] = 0; + $params['state'] = BusinessModel::state_reviewing; + $params['enable'] = 0; + $params['type_name'] = $businessCategory['name']; + + if ($business) { + if ($business['state'] == BusinessModel::state_reviewing) { + throw new ValidateException('商户认证审批中,请勿重复提交!'); + } + + // 更新审批信息,重新审批 + $params['update_time'] = date('Y-m-d H:i:s'); + $business = $business->save($params); + + } else { + // 添加审批记录 + $businessCode = createUuid(); + $params['code'] = $businessCode; + $business = $busRepo->create($params); + if (!$business) { + throw new RepositoryException('服务器繁忙!商户认证申请提交失败!'); + } + + $account->save(['business_code' => $businessCode]); + } + + $result = $busRepo->formatFrontBusinessInfo($business->toArray(), [2]); + return $this->json(0, 'success', $result); + } catch (ValidateException $e) { + return $this->json(4001, $e->getError()); + } catch (RepositoryException | \Exception $e) { + return $this->json(5001, '服务器繁忙!商户认证申请提交失败!'); + } + } + } \ No newline at end of file diff --git a/app/controller/api/Dictionary.php b/app/controller/api/Dictionary.php index ad0edd5..b228750 100644 --- a/app/controller/api/Dictionary.php +++ b/app/controller/api/Dictionary.php @@ -49,12 +49,31 @@ class Dictionary extends Base public function getBusinessCircle(): Json { $items = DictionaryRepository::getInstance()->getAllBusinessCircleList(); + $items->each(function ($item) { + $areaText = ''; + if (!empty($item['province_text'])) { + $areaText = $item['province_text'].'·'; + } + if (!empty($item['city_text']) && $item['city_text'] != '市辖区') { + $areaText .= $item['city_text'].'·'; + } + if (!empty($item['county_text'])) { + $areaText .= $item['county_text']; + } + + $item['name_text'] = $item['name']; + if (!empty($areaText)) { + $item['name_text'] = $item['name'] . '「'.$areaText.'」'; + } + + return $item; + }); return $this->json(0, 'success', $items); } /** - * 获取代理机构(渠道代理商)列表 - * (来源商家表中的渠道代理商) + * 获取代理机构(代理商)关联的商家列表 + * (来源商家表中的代理商) * * $size 0 表示不分页,获取所有数据 * @@ -75,7 +94,7 @@ class Dictionary extends Base $fields = ['id', 'code', 'business_name', 'business_subtitle', 'business_license', 'lat', 'lng', 'business_address', 'contact_name', 'contact_phone', 'create_time', 'type', 'type_name', 'characteristic', 'intro', - 'business_circle', 'business_circle_id', 'background', 'score' + 'business_circle', 'business_circle_id', 'background', 'score', ]; $repo = BusinessRepository::getInstance(); @@ -88,7 +107,6 @@ class Dictionary extends Base } } - /** * 获取商家分类 */ diff --git a/app/controller/api/file/Upload.php b/app/controller/api/file/Upload.php index 3993c8e..49e12d8 100644 --- a/app/controller/api/file/Upload.php +++ b/app/controller/api/file/Upload.php @@ -40,7 +40,11 @@ class Upload extends Base } $this->validate = new VUpload(); $this->uploadPath = Config::get('filesystem.disks.local.url'); - if(is_writable(app()->getRootPath() . 'public' . $this->uploadPath)){ + $savePath = app()->getRootPath() . 'public' . $this->uploadPath; + if (!is_dir($savePath)) { + @mkdir($savePath, 0777); + } + if(is_writable($savePath)){ $this->uploadPathIsWritable = true; } @@ -69,8 +73,6 @@ class Upload extends Base $return['src'] = $src; $return['name'] = $file->getOriginalName(); - //加入上传文件表 - File::add($file, $src, 'file'); } catch (\Exception $e) { return $this->json(4003, $e->getMessage()); } @@ -93,7 +95,7 @@ class Upload extends Base if (empty($image)) { return $this->json(4001, '请上传图片文件'); } - $md5 = $image->md5();//文件md5 + if($this->validate->checkImage($image)){ try{ if(!$this->uploadPathIsWritable){ @@ -105,9 +107,7 @@ class Upload extends Base if($this->isCompress){ Image::resize($src); } - - //加入上传文件表 - File::add($image, $src,$md5); + } catch (\Exception $e) { return $this->json(4003, $e->getMessage()); } @@ -120,13 +120,4 @@ class Upload extends Base } } - - /** - * 同步到OOS服务器存储 - * @param string $src - */ - private function syncToOos(string $src) - { - - } } \ No newline at end of file diff --git a/app/model/Business.php b/app/model/Business.php index ebaf2f7..861b03f 100644 --- a/app/model/Business.php +++ b/app/model/Business.php @@ -2,13 +2,30 @@ namespace app\model; +use think\model\relation\HasOne; + class Business extends Base { const state_reviewing = 0; const state_on = 1; const state_off = 2; + + /** + * @remarks 代理商、平台代理商、渠道商等词组均描述的是平台商,因此文案统一为【平台商】 + */ + public function category() { return $this->hasOne(Category::class, 'id',"type"); } + + /** + * 关联的平台商 + * + * @return HasOne + */ + public function agency(): HasOne + { + return $this->hasOne(Business::class, 'agency_code',"code"); + } } diff --git a/app/model/Category.php b/app/model/Category.php index 1044cdb..1a9f923 100644 --- a/app/model/Category.php +++ b/app/model/Category.php @@ -48,7 +48,7 @@ class Category extends Base /** * 获取全部列表 * - * @return Disease[]|array|Collection + * @return Collection * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException @@ -64,7 +64,7 @@ class Category extends Base * @param int $pid * @param array $selected * @param array $disabled - * @return array|Disease[]|Collection + * @return array|Collection * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException diff --git a/app/repository/BusinessRepository.php b/app/repository/BusinessRepository.php index 951d76e..9414cae 100644 --- a/app/repository/BusinessRepository.php +++ b/app/repository/BusinessRepository.php @@ -144,4 +144,31 @@ class BusinessRepository extends Repository return $Flow; } + + /** + * 格式化前端输出商户信息 + * @param array $data + * @param array $formats + * @return array + */ + public function formatFrontBusinessInfo(array $data, array $formats=[]): array + { + $fields = ['id', 'code', 'business_name', 'business_subtitle', 'business_license', + 'lat', 'lng', 'business_address', 'contact_name', 'contact_phone', + 'create_time', 'type', 'type_name', 'characteristic', 'intro', + 'business_circle', 'business_circle_id', 'background', 'score', + ]; + + if (in_array(1, $formats)) { + // 返回审批相关字段 + $fields = array_merge($fields, ['state', 'reason']); + } + if (in_array(2, $formats)) { + // 返回关联的平台商相关字段 + $fields = array_merge($fields, [ 'is_agency', 'agency_code']); + } + + return arrayKeysFilter($data, $fields); + } + } \ No newline at end of file diff --git a/app/repository/DictionaryRepository.php b/app/repository/DictionaryRepository.php index ba38caf..bb533b6 100644 --- a/app/repository/DictionaryRepository.php +++ b/app/repository/DictionaryRepository.php @@ -53,6 +53,24 @@ class DictionaryRepository extends Repository } } + /** + * 获取商圈详情 + * + * @param int $id + * @param array $fields + * @param callable|null $call + * @return array|\think\Model|null + */ + public function findBusinessCircleById(int $id, array $fields = [], callable $call = null) + { + try { + return BusinessCircle::findById($id, $fields, $call); + } catch (\Exception $e) { + return null; + } + } + + /** * 获取商家分类数据 */ @@ -66,4 +84,20 @@ class DictionaryRepository extends Repository } } + /** + * 获取商家分类详情 + * @param int $id + * @param array $fields + * @param callable|null $call + * @return array|\think\Model|null + */ + public function findBusinessTypeById(int $id, array $fields = [], callable $call = null) + { + try { + return Category::findById($id, $fields, $call); + } catch (\Exception $e) { + return null; + } + } + } \ No newline at end of file diff --git a/app/validate/BusinessValidate.php b/app/validate/BusinessValidate.php new file mode 100644 index 0000000..ca97dfb --- /dev/null +++ b/app/validate/BusinessValidate.php @@ -0,0 +1,55 @@ + 'require|gt:0', + 'business_name|商家名称' => 'require|max:150', + 'business_subtitle|商家简称' => 'max:50', + 'business_license|营业执照' => 'require|max:250', + 'contact_name|联系人' => 'require|max:20', + 'contact_phone|联系电话' => 'require|max:20', + 'lat' => 'require|between:-90,90', + 'lng' => 'require|between:-180,180', + + 'province|所属省份' => 'max:100', + 'city|所属城市' => 'max:100', + 'county|所属区县' => 'max:100', + 'business_address|商家地址' => 'require|max:250', + + 'characteristic|商家特色' => 'max:250', + 'background|商家背景图' => 'max:250', + 'business_circle_id' => 'egt:0', + + 'agency_code' => 'require|length:32', + ]; + + protected $message = [ + 'type.require' => '请选择商家分类!', + 'type.gt' => '请选择商家分类!', + + 'lat.require' => '地址定位信息错误,请重新定位!', + 'lat.between' => '地址定位信息错误,请重新定位!', + 'lng.require' => '地址定位信息错误,请重新定位!', + 'lng.between' => '地址定位信息错误,请重新定位!', + + 'business_circle_id.egt' => '请选择所属商圈!', + 'agency_code.require' => '请选择需要加入的商家!', + 'agency_code.length' => '加入的商家参数错误!', + ]; + + protected $scene = [ + // 认证商家 + 'registerByNormal' => ['type', 'business_name', 'business_subtitle', 'business_license', 'contact_name', 'contact_phone', + 'lat', 'lng', 'province', 'city', 'county', 'business_address', 'business_circle_id'], + // 加入平台代理商 + 'registerByAgency' => ['type', 'business_name', 'business_subtitle', 'business_license', 'contact_name', 'contact_phone', + 'lat', 'lng', 'province', 'city', 'county', 'business_address', 'business_circle_id', 'agency_code'], + // 商家编辑可编辑资料(基本信息修改需要审核通过后才能变更) + 'apiEdit' => ['lat', 'lng', 'province', 'city', 'county', 'business_address', 'characteristic', 'background', 'business_circle_id'], + ]; +} \ No newline at end of file From 29e4c9629b56e04697432535da0657cfbbde0c61 Mon Sep 17 00:00:00 2001 From: zwesy Date: Wed, 1 Dec 2021 14:56:09 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=9B=B4=E6=96=B0=EF=BC=9A=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E7=94=A8=E6=88=B7=E4=B8=AA=E4=BA=BA=E8=B5=84=E6=96=99?= =?UTF-8?q?=E6=8E=A5=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