| 
									
										
										
										
											2021-11-25 18:54:59 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | namespace app\controller\api; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  | use app\exception\RepositoryException; | 
					
						
							| 
									
										
										
										
											2021-12-02 18:34:44 +08:00
										 |  |  | use app\model\BusinessCircle; | 
					
						
							|  |  |  | use app\model\Category; | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  | use app\repository\AccountRepository; | 
					
						
							| 
									
										
										
										
											2021-12-13 15:57:27 +08:00
										 |  |  | use app\repository\BillRepository; | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  | use app\repository\BusinessRepository; | 
					
						
							| 
									
										
										
										
											2021-12-13 15:57:27 +08:00
										 |  |  | use app\repository\CouponRepository; | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  | use app\repository\DictionaryRepository; | 
					
						
							| 
									
										
										
										
											2021-12-09 13:36:47 +08:00
										 |  |  | use app\repository\RechargeRepository; | 
					
						
							|  |  |  | use app\service\wx\WechatPay; | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  | use app\validate\BusinessValidate; | 
					
						
							| 
									
										
										
										
											2021-12-13 15:57:27 +08:00
										 |  |  | use Endroid\QrCode\Builder\Builder; | 
					
						
							|  |  |  | use Endroid\QrCode\Encoding\Encoding; | 
					
						
							|  |  |  | use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh; | 
					
						
							|  |  |  | use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin; | 
					
						
							|  |  |  | use Endroid\QrCode\Writer\PngWriter; | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  | use think\Collection; | 
					
						
							| 
									
										
										
										
											2021-12-09 13:36:47 +08:00
										 |  |  | use think\Exception; | 
					
						
							|  |  |  | use think\facade\Db; | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  | use think\exception\ValidateException; | 
					
						
							| 
									
										
										
										
											2021-12-13 15:57:27 +08:00
										 |  |  | use app\model\{Account, | 
					
						
							|  |  |  |     Account as AccountModel, | 
					
						
							|  |  |  |     Activity as ActivityModel, | 
					
						
							|  |  |  |     ApplyStaff, | 
					
						
							|  |  |  |     Business as BusinessModel, | 
					
						
							|  |  |  |     CouponBill, | 
					
						
							|  |  |  |     CouponMain}; | 
					
						
							| 
									
										
										
										
											2021-12-02 15:51:47 +08:00
										 |  |  | use think\response\Json; | 
					
						
							| 
									
										
										
										
											2021-12-09 13:36:47 +08:00
										 |  |  | use function EasyWeChat\Kernel\Support\generate_sign; | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-25 18:54:59 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * 商家端:商家 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Class Business | 
					
						
							|  |  |  |  * @package app\controller\api | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class Business extends Base | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     protected $noNeedLogin = [ | 
					
						
							|  |  |  |         'couponDetail', | 
					
						
							|  |  |  |     ]; | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-12-02 10:49:50 +08:00
										 |  |  |      * 商家认证注册 | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  |      * | 
					
						
							|  |  |  |      * 重新编辑注册则覆盖之前的审核信息,并重新进行审核 | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-12-02 10:49:50 +08:00
										 |  |  |     public function registerBusiness() | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  |     { | 
					
						
							|  |  |  |         $params = [ | 
					
						
							| 
									
										
										
										
											2021-12-09 13:36:47 +08:00
										 |  |  |             'business_license'=> $this->request->param('business_license', ''), | 
					
						
							|  |  |  |             'background'=> $this->request->param('background', ''), | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  |             'business_name'=> $this->request->param('business_name', ''), | 
					
						
							|  |  |  |             'business_subtitle'=> $this->request->param('business_subtitle', ''), | 
					
						
							|  |  |  |             'province'=> $this->request->param('province', ''), | 
					
						
							|  |  |  |             'city'=> $this->request->param('city', ''), | 
					
						
							|  |  |  |             'county'=> $this->request->param('county', ''), | 
					
						
							|  |  |  |             'business_address' => $this->request->param('business_address', ''), | 
					
						
							| 
									
										
										
										
											2021-12-09 13:36:47 +08:00
										 |  |  |             'lat'=> $this->request->param('lat', ''), | 
					
						
							|  |  |  |             'lng'=> $this->request->param('lng', ''), | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  |             'business_circle_id' => $this->request->param('business_circle_id/d', 0), | 
					
						
							| 
									
										
										
										
											2021-12-02 10:49:50 +08:00
										 |  |  |             'agency_code'   => $this->request->param('agency_code', ''), | 
					
						
							| 
									
										
										
										
											2021-12-09 13:36:47 +08:00
										 |  |  |             'type'=> $this->request->param('type/d', 0), | 
					
						
							|  |  |  |             'contact_name'=> $this->request->param('contact_name', ''), | 
					
						
							|  |  |  |             'contact_phone'=> $this->request->param('contact_phone', ''), | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  |         ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $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('无效请求!'); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-12-09 13:36:47 +08:00
										 |  |  |             if ($account->type !== Account::type_consumer) { | 
					
						
							|  |  |  |                 throw new ValidateException('您已经是商家或者员工,不能认证'); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-10 18:25:14 +08:00
										 |  |  |             //检查有没有加入一个商家的申请
 | 
					
						
							|  |  |  |             $applyStaff = $busRepo->getApplyStaffByUserCode($account->user_code); | 
					
						
							|  |  |  |             if(!empty($applyStaff)){ | 
					
						
							|  |  |  |                 throw new ValidateException('您有申请加入一个商家,不能认证'); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 10:49:50 +08:00
										 |  |  |             if (!$validate->scene('apiRegister')->check($params)) { | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  |                 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']; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 10:49:50 +08:00
										 |  |  |             if (!empty($params['agency_code'])) { | 
					
						
							|  |  |  |                 $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('该代理商已被封禁!'); | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-12-09 13:36:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 $params['is_assign']            = 1; | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             $business = null; | 
					
						
							|  |  |  |             if (isset($account['business_code']) && !empty($account['business_code'])) { | 
					
						
							|  |  |  |                 $business = $busRepo->findOneByWhere(['code'=> $account['business_code']]); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-09 13:36:47 +08:00
										 |  |  |             $params['create_time']          = date('Y-m-d H:i:s'); | 
					
						
							|  |  |  |             $params['is_delete']            = 0; | 
					
						
							|  |  |  |             $params['state']                = BusinessModel::state_reviewing; | 
					
						
							|  |  |  |             $params['enable']               = 0; | 
					
						
							|  |  |  |             $params['total_recharge']       = 0; | 
					
						
							|  |  |  |             $params['balance']              = 0; | 
					
						
							|  |  |  |             $params['is_agency']            = 0; | 
					
						
							|  |  |  |             $params['type_name']            = $businessCategory['name']; | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             if ($business) { | 
					
						
							|  |  |  |                 if ($business['state'] == BusinessModel::state_reviewing) { | 
					
						
							|  |  |  |                     throw new ValidateException('商户认证审批中,请勿重复提交!'); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 // 更新审批信息,重新审批
 | 
					
						
							|  |  |  |                 $params['update_time']  = date('Y-m-d H:i:s'); | 
					
						
							| 
									
										
										
										
											2021-12-09 13:36:47 +08:00
										 |  |  |                 $business->save($params); | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |             } 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, '服务器繁忙!商户认证申请提交失败!'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-10 18:25:14 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * 认证失败之后  撤销认证  删除资料  用户还原身份成消费者 | 
					
						
							|  |  |  |      * */ | 
					
						
							|  |  |  |     public function revokeAuth() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $accountId = $this->request->user['user_id'] ?? 0; | 
					
						
							|  |  |  |         $account = AccountRepository::getInstance()->findById($accountId, [], function ($q) { | 
					
						
							|  |  |  |             return $q->with(['business'=>function($q){ | 
					
						
							|  |  |  |                 $q->lock(true); | 
					
						
							|  |  |  |             }, 'parent']); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         if(empty($account)){ | 
					
						
							|  |  |  |             return  $this->json(6001,"登录失效"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ($account->type != AccountModel::type_consumer) { | 
					
						
							|  |  |  |             return $this->json(4001, "用户状态错误"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!isset($account->business) || empty($account->business)) { | 
					
						
							|  |  |  |             return $this->json(4001, "商家信息错误"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ($account->business->state != BusinessModel::state_off ) { | 
					
						
							|  |  |  |             return $this->json(4001, "当前商家状态不能撤销"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         //开始撤销操作
 | 
					
						
							|  |  |  |         Db::startTrans(); | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $account->business->delete(); | 
					
						
							|  |  |  |             $account->save(["business_code"=>"","type"=>AccountModel::type_consumer]); | 
					
						
							|  |  |  |             Db::commit(); | 
					
						
							|  |  |  |             return $this->json(); | 
					
						
							|  |  |  |         }catch (RepositoryException $e){ | 
					
						
							|  |  |  |             Db::rollback(); | 
					
						
							|  |  |  |             return $this->json(5001, "撤销失败"); | 
					
						
							|  |  |  |         }catch (Exception $e){ | 
					
						
							|  |  |  |             Db::rollback(); | 
					
						
							|  |  |  |             return $this->json(5002, "撤销失败"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-13 15:57:27 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * 申请失败后 | 
					
						
							|  |  |  |      * */ | 
					
						
							|  |  |  |     public function revokeJoinStaffAuth() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $accountId = $this->request->user['user_id'] ?? 0; | 
					
						
							|  |  |  |         $account = AccountRepository::getInstance()->findById($accountId, [], function ($q) { | 
					
						
							|  |  |  |             return $q->with(['business'=>function($q){ | 
					
						
							|  |  |  |                 $q->lock(true); | 
					
						
							|  |  |  |             }, 'parent']); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         if(empty($account)){ | 
					
						
							|  |  |  |             return  $this->json(6001,"登录失效"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ($account->type != AccountModel::type_consumer) { | 
					
						
							|  |  |  |             return $this->json(4001, "用户状态错误"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         //检查有没有加入一个商家的申请
 | 
					
						
							|  |  |  |         $applyStaff = BusinessRepository::getInstance()->getApplyStaffByUserCode($account->user_code); | 
					
						
							|  |  |  |         if(empty($applyStaff)){ | 
					
						
							|  |  |  |             return $this->json(4001, "申请记录不存在"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($applyStaff->status == ApplyStaff::status_success) { | 
					
						
							|  |  |  |             return $this->json(4001, "该申请已通过"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // 待审核或者审核驳回  可以撤销 开始撤销操作
 | 
					
						
							|  |  |  |         Db::startTrans(); | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $applyStaff->delete(); | 
					
						
							|  |  |  |             Db::commit(); | 
					
						
							|  |  |  |             return $this->json(); | 
					
						
							|  |  |  |         }catch (RepositoryException $e){ | 
					
						
							|  |  |  |             Db::rollback(); | 
					
						
							|  |  |  |             return $this->json(5001, "撤销失败"); | 
					
						
							|  |  |  |         }catch (Exception $e){ | 
					
						
							|  |  |  |             Db::rollback(); | 
					
						
							|  |  |  |             return $this->json(5002, "撤销失败"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-12-02 15:51:47 +08:00
										 |  |  |      * 获取当前账号所属商家的信息 | 
					
						
							|  |  |  |      * 适用范围:商户账号 或 商户员工账号 | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-12-02 15:51:47 +08:00
										 |  |  |     public function getMyBusinessInfo() | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-12-02 15:51:47 +08:00
										 |  |  |         $accountId = $this->request->user['user_id'] ?? 0; | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         try { | 
					
						
							| 
									
										
										
										
											2021-12-02 15:51:47 +08:00
										 |  |  |             $account    = AccountRepository::getInstance()->findById($accountId, [], function ($q) { | 
					
						
							|  |  |  |                 return $q->with(['business', 'parent']); | 
					
						
							|  |  |  |             }); | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  |             if (empty($account)) { | 
					
						
							| 
									
										
										
										
											2021-12-02 15:51:47 +08:00
										 |  |  |                 return $this->json(4004, '无效请求!'); | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 15:51:47 +08:00
										 |  |  |             $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(); | 
					
						
							|  |  |  |                     } | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  |                 } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 15:51:47 +08:00
										 |  |  |                 $result = arrayKeysExcludeFilter($result, ['pay_account', 'total_recharge']); | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 15:51:47 +08:00
										 |  |  |             } elseif(!empty($account['business'])) { | 
					
						
							|  |  |  |                 // 商户认证信息
 | 
					
						
							|  |  |  |                 $result     = $account['business']->toArray(); | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 15:51:47 +08:00
										 |  |  |             if (count($result)) { | 
					
						
							|  |  |  |                 $result     = DictionaryRepository::getInstance()->parseAreaText($result); | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 15:51:47 +08:00
										 |  |  |             $result = arrayKeysExcludeFilter($result, ['update_time']); | 
					
						
							|  |  |  |             return $this->json(0, 'success', $result); | 
					
						
							|  |  |  |         } catch (\Exception $e) { | 
					
						
							|  |  |  |             return $this->json(5001, '商家信息查询失败!'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 15:51:47 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * 获取商家信息(已认证通过的商户) | 
					
						
							|  |  |  |      * @return Json | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function getBusinessInfo() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $businessCode   = $this->request->param('businessCode/s', ''); | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 15:51:47 +08:00
										 |  |  |         $busRepo        = BusinessRepository::getInstance(); | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $business   = $busRepo->findOneByWhere(['code' => $businessCode, 'state' => BusinessModel::state_on]); | 
					
						
							|  |  |  |             if (empty($business)) { | 
					
						
							|  |  |  |                 return $this->json(4004, '没有相关的商户!'); | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 15:51:47 +08:00
										 |  |  |             $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); | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  |             return $this->json(0, 'success', $result); | 
					
						
							| 
									
										
										
										
											2021-12-02 15:51:47 +08:00
										 |  |  |         } catch (\Exception $e) { | 
					
						
							|  |  |  |             return $this->json(5004, '商家信息查询失败!'); | 
					
						
							| 
									
										
										
										
											2021-11-30 18:52:02 +08:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 18:40:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 18:34:44 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * 获取商家类型 | 
					
						
							|  |  |  |      * */ | 
					
						
							|  |  |  |     public function getBusinessTypeList() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $data = Category::getList(); | 
					
						
							|  |  |  |         return $this->json(1,"ok",$data); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 获取商家类型 | 
					
						
							|  |  |  |      * */ | 
					
						
							|  |  |  |     public function getBusinessCircle() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $data = BusinessCircle::getList(); | 
					
						
							|  |  |  |         return $this->json(1,"ok",$data); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 商家优惠券详情 | 
					
						
							|  |  |  |      * */ | 
					
						
							|  |  |  |     public function couponDetail() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $accountId = $this->request->user['user_id'] ?? 0; | 
					
						
							|  |  |  |         $account = AccountRepository::getInstance()->findById($accountId, [], function ($q) { | 
					
						
							|  |  |  |             return $q->with(['business', 'parent']); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $couponMainId = input("id/d"); | 
					
						
							|  |  |  |         $couponMain = CouponMain::findOne(["id"=>$couponMainId]); | 
					
						
							|  |  |  |         if(empty($couponMain)){ | 
					
						
							|  |  |  |             return $this->json(4001,"优惠券不存在"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if(!$business = BusinessRepository::getInstance()->findOneByWhere(["code"=>$couponMain["business_code"]])){ | 
					
						
							|  |  |  |             return $this->json(4001,"优惠券商家信息不存在"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $list       = $business->toArray(); | 
					
						
							|  |  |  |         $list       = DictionaryRepository::getInstance()->parseAreaText($list); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $data       = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $data["businessAddress"]        = $list['province_text'] . $list['city_text'] . $list['county_text'] . $business['business_address']; | 
					
						
							|  |  |  |         $data["businessName"]           = $list['business_name']; | 
					
						
							| 
									
										
										
										
											2021-12-21 11:38:57 +08:00
										 |  |  |         $data["businessCode"]           = $list['code']; | 
					
						
							| 
									
										
										
										
											2021-12-17 11:41:27 +08:00
										 |  |  |         $data["businessSubtitle"]       = $business['business_subtitle']; | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  |         $data["count"]                  = $couponMain->count; | 
					
						
							|  |  |  |         $data["couponName"]             = $couponMain->name; | 
					
						
							|  |  |  |         $data["deductionMoney"]         = $couponMain->deduction_money; | 
					
						
							|  |  |  |         $data["startTime"]              = $couponMain->start_time; | 
					
						
							|  |  |  |         $data["endTime"]                = $couponMain->end_time; | 
					
						
							|  |  |  |         $data["id"]                     = $couponMain->id; | 
					
						
							|  |  |  |         $data["imageUrl"]               = $this->request->domain(). $couponMain->image_url; | 
					
						
							|  |  |  |         $data["intro"]                  = $couponMain->intro;//简介
 | 
					
						
							| 
									
										
										
										
											2021-12-23 15:53:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  |         $data["lat"]                    = $couponMain->lat; | 
					
						
							|  |  |  |         $data["lng"]                    = $couponMain->lng; | 
					
						
							|  |  |  |         $data["money"]                  = $couponMain->money; | 
					
						
							|  |  |  |         $data["usingRule"]              = DictionaryRepository::getInstance()->getUsingRuleByCouponMainId($couponMain->id); | 
					
						
							|  |  |  |         $data["punishRule"]             = '';//处罚规则 已取消
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-23 15:53:43 +08:00
										 |  |  |         $data["content"]                = $couponMain->content; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-06 18:56:23 +08:00
										 |  |  |         //未登录可以领取 0可领取 1已领取
 | 
					
						
							|  |  |  |         if (empty($account)) { | 
					
						
							|  |  |  |             $data["receiveStatus"]  = CouponMain::COMMON_OFF;//领取状态
 | 
					
						
							|  |  |  |         }else{ | 
					
						
							|  |  |  |             $data["receiveStatus"]  = AccountRepository::getInstance()->getCouponReceiveStatus($account->user_code,$couponMain);//领取状态
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $data["receivedCount"] =  $couponMain->received_count;//已领取数量
 | 
					
						
							|  |  |  |         $data["typeName"] =  $couponMain->type_name; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this->json(0,"success",$data); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-09 13:36:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-12-10 18:25:14 +08:00
										 |  |  |      * 检查是否已经有商家审核了 | 
					
						
							| 
									
										
										
										
											2021-12-09 13:36:47 +08:00
										 |  |  |      * */ | 
					
						
							|  |  |  |     public function checkAuth() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $accountId = $this->request->user['user_id'] ?? 0; | 
					
						
							|  |  |  |         $account = AccountRepository::getInstance()->findById($accountId, [], function ($q) { | 
					
						
							|  |  |  |             return $q->with(['business', 'parent']); | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2021-12-10 18:25:14 +08:00
										 |  |  |         $showSubmitBtn = true; | 
					
						
							| 
									
										
										
										
											2021-12-09 13:36:47 +08:00
										 |  |  |         if(empty($account)){ | 
					
						
							| 
									
										
										
										
											2021-12-10 18:25:14 +08:00
										 |  |  |             return  $this->json(6001,"登录失效",["showSubmitBtn"=>$showSubmitBtn]); | 
					
						
							| 
									
										
										
										
											2021-12-09 13:36:47 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-13 15:57:27 +08:00
										 |  |  |         //如果有商家 并且审核失败或者待审核
 | 
					
						
							| 
									
										
										
										
											2021-12-09 13:36:47 +08:00
										 |  |  |         if (isset($account->business) && !empty($account->business) && in_array($account->business->state, [BusinessModel::state_off, BusinessModel::state_reviewing,])) { | 
					
						
							|  |  |  |             if ($account->business->state == BusinessModel::state_off) { | 
					
						
							| 
									
										
										
										
											2021-12-13 15:57:27 +08:00
										 |  |  |                 return $this->json(4001, "认证被退回,请重新填写资料:" . $account->business->reason, | 
					
						
							|  |  |  |                     ["business" => $account->business, "showSubmitBtn" => $showSubmitBtn]); | 
					
						
							| 
									
										
										
										
											2021-12-09 13:36:47 +08:00
										 |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-12-10 18:25:14 +08:00
										 |  |  |             $showSubmitBtn = false; | 
					
						
							|  |  |  |             return $this->json(4001, " 正在认证中请耐心等待", ["business" => $account->business, "showSubmitBtn" => $showSubmitBtn]); | 
					
						
							| 
									
										
										
										
											2021-12-09 13:36:47 +08:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-13 15:57:27 +08:00
										 |  |  |         if($account->type == AccountModel::type_staff){ | 
					
						
							|  |  |  |             $showSubmitBtn = false; | 
					
						
							|  |  |  |             return $this->json(4001, "您已经是员工,无需认证", ["showSubmitBtn" => $showSubmitBtn]); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-09 13:36:47 +08:00
										 |  |  |         //检查有没有加入一个商家的申请
 | 
					
						
							| 
									
										
										
										
											2021-12-10 18:25:14 +08:00
										 |  |  |         $applyStaff = BusinessRepository::getInstance()->getApplyStaffByUserCode($account->user_code); | 
					
						
							|  |  |  |         if(!empty($applyStaff)){ | 
					
						
							|  |  |  |             $showSubmitBtn = false; | 
					
						
							| 
									
										
										
										
											2021-12-13 15:57:27 +08:00
										 |  |  |             if($applyStaff->status == ApplyStaff::status_default){ | 
					
						
							|  |  |  |                 return $this->json(4001, "您有申请加入一个商家待审核,不能认证", [ "showSubmitBtn" => $showSubmitBtn ,"applyStaff"=>$applyStaff]); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if($applyStaff->status == ApplyStaff::status_fail){ | 
					
						
							|  |  |  |                 $showSubmitBtn = true; | 
					
						
							|  |  |  |                 return $this->json(4001, "您申请加入商家,商家驳回了,请先撤销申请", [ "showSubmitBtn" => $showSubmitBtn ,"applyStaff"=>$applyStaff]); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if($applyStaff->status == ApplyStaff::status_success){ | 
					
						
							|  |  |  |                 return $this->json(4001, "您申请加入商家已通过,请不要重复认证", [ "showSubmitBtn" => $showSubmitBtn ,"applyStaff"=>$applyStaff]); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             return $this->json(4001, "状态错误", [ "showSubmitBtn" => $showSubmitBtn ,"applyStaff"=>$applyStaff]); | 
					
						
							| 
									
										
										
										
											2021-12-10 18:25:14 +08:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-12-09 13:36:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-10 18:25:14 +08:00
										 |  |  |         return $this->json(0,"success",[ "showSubmitBtn" => $showSubmitBtn]); | 
					
						
							| 
									
										
										
										
											2021-12-09 13:36:47 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 充值 | 
					
						
							|  |  |  |      * */ | 
					
						
							|  |  |  |     public function recharge() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $accountId  = $this->request->user['user_id'] ?? 0; | 
					
						
							|  |  |  |         $money      = input("money/f",1); | 
					
						
							|  |  |  |         $account = AccountRepository::getInstance()->findById($accountId, [], function ($q) { | 
					
						
							|  |  |  |             return $q->with(['business', 'parent']); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         if(empty($account)||empty($account->business)){ | 
					
						
							|  |  |  |             return  $this->json(6001,"登录失效"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ($money <= 0) { | 
					
						
							|  |  |  |             return  $this->json(4001,"充值金额有误"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $money = floor($money*100)/100; | 
					
						
							|  |  |  |         Db::startTrans(); | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             //创建充值订单
 | 
					
						
							|  |  |  |             if (!$order = RechargeRepository::getInstance()->createOrder($account->business_code, $account->open_id,$money)) { | 
					
						
							|  |  |  |                 throw new RepositoryException('订单创建失败,请稍后重试'); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             //生成支付
 | 
					
						
							|  |  |  |             $time = time(); | 
					
						
							|  |  |  |             $res = WechatPay::getInstance()->order->unify([ | 
					
						
							|  |  |  |                 'body' => '商家充值', | 
					
						
							|  |  |  |                 'openid' => $account->open_id, | 
					
						
							|  |  |  |                 'out_trade_no' => $order->order_num, | 
					
						
							|  |  |  |                 'total_fee' => $money * 100, | 
					
						
							|  |  |  |                 'trade_type' => 'JSAPI', | 
					
						
							|  |  |  |                 'timeStamp' => $time, | 
					
						
							|  |  |  |                 'product_id' => $order->id, | 
					
						
							|  |  |  |                 'sign_type' => "MD5", | 
					
						
							|  |  |  |                 'notify_url' => $this->request->domain() . "/api/recharge/notify.html", | 
					
						
							|  |  |  |             ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (!isset($res['prepay_id'])) { | 
					
						
							|  |  |  |                 throw new RepositoryException('订单创建失败,请稍后重试'); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $payData = [ | 
					
						
							|  |  |  |                 "appId" =>config("wechat.applets_appId"), | 
					
						
							|  |  |  |                 "timeStamp" =>$time."", | 
					
						
							|  |  |  |                 'nonceStr'  => randomStr(0,16), | 
					
						
							|  |  |  |                 'package'   => 'prepay_id='.$res['prepay_id'], | 
					
						
							|  |  |  |                 "signType"  =>'MD5', | 
					
						
							|  |  |  |             ]; | 
					
						
							| 
									
										
										
										
											2021-12-09 13:44:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-09 13:36:47 +08:00
										 |  |  |             $payData['sign'] = generate_sign($payData, config("wechat.key")); | 
					
						
							| 
									
										
										
										
											2021-12-09 13:58:21 +08:00
										 |  |  |             $payData['order_num'] = $order->order_num; | 
					
						
							| 
									
										
										
										
											2021-12-09 14:14:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-09 13:36:47 +08:00
										 |  |  |             Db::commit(); | 
					
						
							|  |  |  |             return $this->json(0, "success", [ | 
					
						
							|  |  |  |                 "payData" => $payData | 
					
						
							|  |  |  |             ]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         }catch (RepositoryException $e){ | 
					
						
							|  |  |  |             Db::rollback(); | 
					
						
							|  |  |  |             return  $this->json(4001,"发起充值失败"); | 
					
						
							|  |  |  |         }catch (Exception $e){ | 
					
						
							|  |  |  |             Db::rollback(); | 
					
						
							|  |  |  |             return  $this->json(5001,"发起充值失败"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-09 18:26:25 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * 商家详情 | 
					
						
							|  |  |  |      * */ | 
					
						
							|  |  |  |     public function getShopInfo() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $accountId = $this->request->user['user_id'] ?? 0; | 
					
						
							|  |  |  |         $account = AccountRepository::getInstance()->findById($accountId, [], function ($q) { | 
					
						
							|  |  |  |             return $q->with(['business', 'parent']); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         if(empty($account)){ | 
					
						
							|  |  |  |             return  $this->json(6001,"登录失效"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if ($account->type == Account::type_consumer) { | 
					
						
							|  |  |  |             return $this->json(4001, "您不是商家"); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-12-10 18:25:14 +08:00
										 |  |  |         $account->business->business_license = $this->request->domain().$account->business->business_license; | 
					
						
							|  |  |  |         $account->business->background = $this->request->domain().$account->business->background; | 
					
						
							| 
									
										
										
										
											2021-12-09 18:26:25 +08:00
										 |  |  |         if (isset($account->business) && $account->business) { | 
					
						
							|  |  |  |             return $this->json(0, "success",["business"=>$account->business]); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         return $this->json(4001, "获取商家信息错误"); | 
					
						
							| 
									
										
										
										
											2021-12-10 18:25:14 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 商家的列表 | 
					
						
							|  |  |  |      * */ | 
					
						
							|  |  |  |     public function getBusinessList() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $accountId      = $this->request->user['user_id'] ?? 0; | 
					
						
							|  |  |  |         $page           = $this->request->param('page/d', 1); | 
					
						
							|  |  |  |         $size           = $this->request->param('size/d', 10); | 
					
						
							|  |  |  |         $keyword        = input("keyWord/s"); | 
					
						
							|  |  |  |         $accountRepo = AccountRepository::getInstance(); | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $account = $accountRepo->findById($accountId, [], function ($q) { | 
					
						
							|  |  |  |                 return $q->with(['business', 'parent']); | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |             if (empty($account)) { | 
					
						
							|  |  |  |                 throw new ValidateException('用户无效!'); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $where = [ | 
					
						
							|  |  |  |                 ["business_name|business_subtitle", "like", "%{$keyword}%"], | 
					
						
							|  |  |  |                 ["state", "=", BusinessModel::state_on],//审核状态 0 审核中 1 审核通过 2 拒绝
 | 
					
						
							|  |  |  |                 ["enable", "=", BusinessModel::COMMON_OFF],// 启用状态 0 正常 1禁用
 | 
					
						
							|  |  |  |             ]; | 
					
						
							|  |  |  |             $data = BusinessRepository::getInstance()->findList($where,[],$page,$size,null,["id"=>"desc"]); | 
					
						
							|  |  |  |             $data["list"]->each(function ($item){ | 
					
						
							|  |  |  |                 $item->businessCover = $this->request->domain() . $item->background; | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |             return $this->json(0,"success",$data["list"]); | 
					
						
							|  |  |  |         }catch (ValidateException $e) { | 
					
						
							|  |  |  |             return $this->json(4001, $e->getError()); | 
					
						
							|  |  |  |         } catch (Exception $e) { | 
					
						
							|  |  |  |             return $this->json(5001, '服务器繁忙!获取用户个人信息失败'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 加入一个商家成为员工 | 
					
						
							|  |  |  |      * */ | 
					
						
							|  |  |  |     public function joinBusiness(){ | 
					
						
							|  |  |  |         $accountId      = $this->request->user['user_id'] ?? 0; | 
					
						
							|  |  |  |         $businessCode   = input("business_code/s",""); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $accountRepo = AccountRepository::getInstance(); | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $account = $accountRepo->findById($accountId,[],function ($q){ | 
					
						
							|  |  |  |                 return $q->with(["business"]); | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |             if (empty($account)) { | 
					
						
							|  |  |  |                 throw new RepositoryException('用户无效!'); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $business = BusinessRepository::getInstance()->findOneByWhere(["code"=>$businessCode]); | 
					
						
							|  |  |  |             if(empty($business)){ | 
					
						
							|  |  |  |                 throw new RepositoryException('商家不存在'); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if($business->state !=BusinessModel::state_on){ | 
					
						
							|  |  |  |                 throw new RepositoryException('商家审核未通过'); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if($business->enable !=BusinessModel::COMMON_OFF){ | 
					
						
							|  |  |  |                 throw new RepositoryException('商家已禁用'); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             //检查有没有加入一个商家的申请
 | 
					
						
							|  |  |  |             $applyStaff = BusinessRepository::getInstance()->getApplyStaffByUserCode($account->user_code); | 
					
						
							|  |  |  |             if(!empty($applyStaff)){ | 
					
						
							|  |  |  |                 throw new RepositoryException('您已经有申请加入一个商家,不能重复申请'); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-13 15:57:27 +08:00
										 |  |  |             //是否有认证商家的申请
 | 
					
						
							| 
									
										
										
										
											2021-12-10 18:25:14 +08:00
										 |  |  |             if(isset($account->business)&&$account->business){ | 
					
						
							|  |  |  |                 if($account->business->state==businessModel::state_on){ | 
					
						
							|  |  |  |                     throw new RepositoryException('您已经是商家'); | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 if($account->business->state==businessModel::state_reviewing){ | 
					
						
							|  |  |  |                     throw new RepositoryException('您有审核中商家认证申请'); | 
					
						
							|  |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-12-13 15:57:27 +08:00
										 |  |  |                 throw new RepositoryException('您有被驳回商家认证申请,请先撤销申请'); | 
					
						
							| 
									
										
										
										
											2021-12-10 18:25:14 +08:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             //验证通过  写入申请
 | 
					
						
							|  |  |  |             BusinessRepository::getInstance()->createApplyStaff($account->user_code,$businessCode); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return $this->json(); | 
					
						
							|  |  |  |         }catch (RepositoryException $e) { | 
					
						
							|  |  |  |             return $this->json(4001, $e->getMessage()); | 
					
						
							|  |  |  |         } catch (Exception $e) { | 
					
						
							|  |  |  |             return $this->json(5001, '服务器繁忙!'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 绑定的员工列表  查询所有 | 
					
						
							|  |  |  |      * */ | 
					
						
							|  |  |  |     public function getBindUserList() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $accountId      = $this->request->user['user_id'] ?? 0; | 
					
						
							|  |  |  |         $keyword        = input("key/s",""); | 
					
						
							|  |  |  |         $type           = input("type/s","staff"); | 
					
						
							|  |  |  |         $accountRepo    = AccountRepository::getInstance(); | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $account = $accountRepo->findById($accountId,[],function ($q){ | 
					
						
							|  |  |  |                 return $q->with(["business"]); | 
					
						
							|  |  |  |             }); | 
					
						
							|  |  |  |             if (empty($account)) { | 
					
						
							|  |  |  |                 throw new RepositoryException('用户无效!'); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if ($account->type != AccountModel::type_business) { | 
					
						
							|  |  |  |                 throw new RepositoryException('您没有权限查看'); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             if (!isset($account->business) || empty($account->business)) { | 
					
						
							|  |  |  |                 throw new RepositoryException('商家信息无效'); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if($type == "staff"){ | 
					
						
							|  |  |  |                 //查看列表  员工列表
 | 
					
						
							|  |  |  |                 $list = $accountRepo->getStaffListByBusinessCode($account->business->code,$account->user_code,$keyword); | 
					
						
							|  |  |  |             }else{ | 
					
						
							|  |  |  |                 //申请列表
 | 
					
						
							|  |  |  |                 $list = $accountRepo->getApplyStaffListByBusinessCode($account->business->code,$keyword); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return $this->json(0,"success",["list"=>$list]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         }catch (RepositoryException $e) { | 
					
						
							|  |  |  |             return $this->json(4001, $e->getMessage()); | 
					
						
							|  |  |  |         } catch (Exception $e) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return $this->json(5001, '服务器繁忙!'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 审核加入商家 | 
					
						
							|  |  |  |      * */ | 
					
						
							|  |  |  |     public function examineApplyStaff() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $status = input("status/d", -1); | 
					
						
							|  |  |  |         $userCode = input("user_code/s", ''); | 
					
						
							|  |  |  |         $accountId = $this->request->user['user_id'] ?? 0; | 
					
						
							|  |  |  |         $accountRepo = AccountRepository::getInstance(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $account = $accountRepo->findById($accountId, [], function ($q) { | 
					
						
							|  |  |  |             return $q->with(["business"]); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         if(!in_array($status,[ApplyStaff::status_success,ApplyStaff::status_fail])){ | 
					
						
							|  |  |  |             return $this->json(4001, "状态错误"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (empty($account)) { | 
					
						
							|  |  |  |             return $this->json(6001, "用户无效!"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($account->type != AccountModel::type_business) { | 
					
						
							|  |  |  |             return $this->json(4001, "您没有权限"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (!isset($account->business) || empty($account->business)) { | 
					
						
							|  |  |  |             return $this->json(4001, "商家信息无效"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $applyStaff = BusinessRepository::getInstance()->getApplyStaffByUserCode($userCode); | 
					
						
							|  |  |  |         if(empty($applyStaff)){ | 
					
						
							|  |  |  |             return $this->json(4001,"申请记录不存在"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if(!isset($applyStaff->account)||empty($applyStaff->account)){ | 
					
						
							|  |  |  |             return $this->json(4001,"申请人信息不存在"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if($applyStaff->business_code!=$account->business->code){ | 
					
						
							|  |  |  |             return $this->json(4001,"信息无效"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if($applyStaff->status != ApplyStaff::status_default){ | 
					
						
							|  |  |  |             return $this->json(4001,"审核过了"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         //开始数据操作
 | 
					
						
							|  |  |  |         Db::startTrans(); | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $applyStaff->save(["status"=>$status]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             //如果是审核通过 修改员工状态
 | 
					
						
							|  |  |  |             if($status==ApplyStaff::status_success){ | 
					
						
							|  |  |  |                 $applyStaff->account->save(["type"=>AccountModel::type_staff,"business_code"=>$account->business->code,"main_code"=>$account->user_code]); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             Db::commit(); | 
					
						
							|  |  |  |             return $this->json(); | 
					
						
							|  |  |  |         } catch (RepositoryException $e) { | 
					
						
							|  |  |  |             Db::rollback(); | 
					
						
							|  |  |  |             return $this->json(5001,"审核失败"); | 
					
						
							|  |  |  |         }catch (Exception $e){ | 
					
						
							|  |  |  |             Db::rollback(); | 
					
						
							|  |  |  |             return $this->json(5002,"审核失败"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 解除员工关系 | 
					
						
							|  |  |  |      * */ | 
					
						
							|  |  |  |     public function relieveApplyStaff() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $userCode = input("user_code/s", ''); | 
					
						
							|  |  |  |         $accountId = $this->request->user['user_id'] ?? 0; | 
					
						
							|  |  |  |         $accountRepo = AccountRepository::getInstance(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $account = $accountRepo->findById($accountId, [], function ($q) { | 
					
						
							|  |  |  |             return $q->with(["business"]); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (empty($account)) { | 
					
						
							|  |  |  |             return $this->json(6001, "用户无效!"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($account->type != AccountModel::type_business) { | 
					
						
							|  |  |  |             return $this->json(4001, "您没有权限"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if (!isset($account->business) || empty($account->business)) { | 
					
						
							|  |  |  |             return $this->json(4001, "商家信息无效"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $staff = $accountRepo->findOneByWhere(["user_code"=>$userCode]); | 
					
						
							|  |  |  |         if(empty($staff)){ | 
					
						
							|  |  |  |             return $this->json(4001, "员工信息不存在"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if($staff->type != AccountModel::type_staff){ | 
					
						
							|  |  |  |             return $this->json(4001, "员工信息不正确1"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if($staff->main_code != $account->user_code){ | 
					
						
							|  |  |  |             return $this->json(4001, "员工信息不正确2"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         if($staff->business_code != $account->business_code){ | 
					
						
							|  |  |  |             return $this->json(4001, "员工信息不正确3"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $applyStaff = BusinessRepository::getInstance()->getApplyStaffByUserCodeAndBusinessCode($userCode,$account->business_code); | 
					
						
							|  |  |  |         if(empty($applyStaff)){ | 
					
						
							|  |  |  |             return $this->json(4001,"申请记录不存在"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         //确定是员工后  修改信息
 | 
					
						
							|  |  |  |         Db::startTrans(); | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $staff->save(["main_code"=>"","business_code"=>"","type"=>AccountModel::type_consumer]); | 
					
						
							|  |  |  |             $applyStaff->delete(); | 
					
						
							|  |  |  |             Db::commit(); | 
					
						
							|  |  |  |             return $this->json(); | 
					
						
							|  |  |  |         }catch (RepositoryException $e){ | 
					
						
							|  |  |  |             Db::rollback(); | 
					
						
							|  |  |  |             return $this->json("服务器错误,解除关系失败"); | 
					
						
							|  |  |  |         }catch (Exception $e){ | 
					
						
							|  |  |  |             Db::rollback(); | 
					
						
							|  |  |  |             return $this->json("服务器错误,解除关系失败2"); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-12-09 18:26:25 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-12-13 15:57:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 商家查看 | 
					
						
							|  |  |  |      * */ | 
					
						
							|  |  |  |     public function businessBill() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $page           = $this->request->param('page/d', 1); | 
					
						
							|  |  |  |         $size           = $this->request->param('size/d', 10); | 
					
						
							|  |  |  |         $startTime      = $this->request->param('startTime/s'); | 
					
						
							|  |  |  |         $endTime        = $this->request->param('endTime/s'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $accountId = $this->request->user['user_id'] ?? 0; | 
					
						
							|  |  |  |         $accountRepo = AccountRepository::getInstance(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $account = $accountRepo->findById($accountId, [], function ($q) { | 
					
						
							|  |  |  |             return $q->with(["business"]); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!isset($account->business) || empty($account->business)) { | 
					
						
							|  |  |  |             return $this->json(4001, "商家信息无效"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $businessCode = $account->business->code; | 
					
						
							|  |  |  |         $orders = ['id' => 'desc']; | 
					
						
							|  |  |  |         $list = BillRepository::getInstance()->apiBillList($page, $size, $businessCode, $startTime, $endTime, $orders); | 
					
						
							|  |  |  |         $bill_money_sum = BillRepository::getInstance()->getBillMoneySum($businessCode, $startTime, $endTime ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $this->json(0,"success",["list"=>$list,"bill_money_sum"=>$bill_money_sum]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * 下载商家核销二维码 | 
					
						
							|  |  |  |      * */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function downloadWriteOffCode() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $accountId = $this->request->user['user_id'] ?? 0; | 
					
						
							|  |  |  |         $accountRepo = AccountRepository::getInstance(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $account = $accountRepo->findById($accountId, [], function ($q) { | 
					
						
							|  |  |  |             return $q->with(["business"]); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!isset($account->business) || empty($account->business)) { | 
					
						
							|  |  |  |             return $this->json(4001, "商家信息无效"); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $businessCode = $account->business->code; | 
					
						
							|  |  |  |         $w = 3000;//尺寸
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $logoImg    = app()->getRootPath().'public/static/images/icon-logo.jpg'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $result = Builder::create() | 
					
						
							|  |  |  |             ->writer(new PngWriter()) | 
					
						
							|  |  |  |             ->writerOptions([]) | 
					
						
							|  |  |  |             ->data($businessCode) | 
					
						
							|  |  |  |             ->encoding(new Encoding('UTF-8')) | 
					
						
							|  |  |  |             ->errorCorrectionLevel(new ErrorCorrectionLevelHigh()) | 
					
						
							|  |  |  |             ->size($w) | 
					
						
							|  |  |  |             ->margin(10) | 
					
						
							|  |  |  |             ->roundBlockSizeMode(new RoundBlockSizeModeMargin()) | 
					
						
							|  |  |  |             ->logoPath($logoImg) | 
					
						
							|  |  |  |             ->logoResizeToHeight(ceil($w/6)) | 
					
						
							|  |  |  |             ->logoResizeToWidth(ceil($w/6)) | 
					
						
							|  |  |  |             ->logoPunchoutBackground(true) | 
					
						
							|  |  |  |             ->build(); | 
					
						
							| 
									
										
										
										
											2021-12-13 19:09:16 +08:00
										 |  |  |         header( | 
					
						
							|  |  |  |             "Content-type: image/jpg" | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |         $path = "/storage/business/" . $businessCode . ".jpg"; | 
					
						
							|  |  |  |         $result->saveToFile( public_path() . $path); | 
					
						
							|  |  |  |         return $this->json(0,"success",["url"=>$this->request->domain().$path]); | 
					
						
							| 
									
										
										
										
											2021-12-13 15:57:27 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-12-02 18:34:44 +08:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2021-12-02 18:40:22 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 |