master
wangxinglong 2021-12-10 18:25:14 +08:00
parent 49ba74b186
commit b5986ae653
7 changed files with 460 additions and 14 deletions

View File

@ -4,10 +4,8 @@ namespace app\controller\api;
use app\exception\RepositoryException;
use app\model\BusinessCircle;
use app\model\Category;
use app\model\Recharge as RechargeModel;
use app\repository\AccountRepository;
use app\repository\BusinessRepository;
use app\repository\CouponRepository;
use app\repository\DictionaryRepository;
use app\repository\RechargeRepository;
use app\service\wx\WechatPay;
@ -16,7 +14,7 @@ use think\Collection;
use think\Exception;
use think\facade\Db;
use think\exception\ValidateException;
use app\model\{Account, Business as BusinessModel, Account as AccountModel, CouponMain};
use app\model\{Account, ApplyStaff, Business as BusinessModel, Account as AccountModel, CouponMain};
use think\response\Json;
use function EasyWeChat\Kernel\Support\generate_sign;
@ -74,6 +72,12 @@ class Business extends Base
throw new ValidateException('您已经是商家或者员工,不能认证');
}
//检查有没有加入一个商家的申请
$applyStaff = $busRepo->getApplyStaffByUserCode($account->user_code);
if(!empty($applyStaff)){
throw new ValidateException('您有申请加入一个商家,不能认证');
}
if (!$validate->scene('apiRegister')->check($params)) {
throw new ValidateException($validate->getError());
}
@ -146,6 +150,48 @@ class Business extends Base
}
}
/**
* 认证失败之后 撤销认证 删除资料 用户还原身份成消费者
* */
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, "撤销失败");
}
}
/**
* 获取当前账号所属商家的信息
* 适用范围:商户账号 商户员工账号
@ -291,7 +337,7 @@ class Business extends Base
/**
* 检查是已经有商家审核了
* 检查是已经有商家审核了
* */
public function checkAuth()
{
@ -299,22 +345,28 @@ class Business extends Base
$account = AccountRepository::getInstance()->findById($accountId, [], function ($q) {
return $q->with(['business', 'parent']);
});
$showSubmitBtn = true;
if(empty($account)){
return $this->json(6001,"登录失效");
return $this->json(6001,"登录失效",["showSubmitBtn"=>$showSubmitBtn]);
}
//如果有商家 并且审核失败或者
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) {
return $this->json(4001, "认证被退回,请重新填写资料:" . $account->business->reason,["business"=>$account->business]);
return $this->json(4001, "认证被退回,请重新填写资料:" . $account->business->reason, ["business" => $account->business, "showSubmitBtn" => $showSubmitBtn]);
}
return $this->json(4001, " 正在认证中请耐心等待",["business"=>$account->business]);
$showSubmitBtn = false;
return $this->json(4001, " 正在认证中请耐心等待", ["business" => $account->business, "showSubmitBtn" => $showSubmitBtn]);
}
//检查有没有加入一个商家的申请
//TODO....
$applyStaff = BusinessRepository::getInstance()->getApplyStaffByUserCode($account->user_code);
if(!empty($applyStaff)){
$showSubmitBtn = false;
return $this->json(4001, "您有申请加入一个商家,不能认证", [ "showSubmitBtn" => $showSubmitBtn]);
}
return $this->json();
return $this->json(0,"success",[ "showSubmitBtn" => $showSubmitBtn]);
}
@ -379,7 +431,6 @@ class Business extends Base
Db::rollback();
return $this->json(4001,"发起充值失败");
}catch (Exception $e){
echo $e->getMessage();
Db::rollback();
return $this->json(5001,"发起充值失败");
}
@ -401,11 +452,266 @@ class Business extends Base
if ($account->type == Account::type_consumer) {
return $this->json(4001, "您不是商家");
}
$account->business->business_license = $this->request->domain().$account->business->business_license;
$account->business->background = $this->request->domain().$account->business->background;
if (isset($account->business) && $account->business) {
return $this->json(0, "success",["business"=>$account->business]);
}
return $this->json(4001, "获取商家信息错误");
}
/**
* 商家的列表
* */
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('您已经有申请加入一个商家,不能重复申请');
}
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('您有审核中商家认证申请');
}
throw new RepositoryException('您有被驳回商家认证申请');
}
//验证通过 写入申请
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");
}
}
}

View File

@ -391,14 +391,16 @@ class Coupon extends Base
}
$field = ["is_verificated","received_time","verificate_time","consumer_code"];
$data = CouponRepository::getInstance()->findList($whereMap,$field,$page,$size,function($q){
return $q->with(["account"=>function($q){
return $q->withjoin(["account"=>function($q){
$q->field(["nick_name","avatar_url","gender","user_code"]);
}]);
},$sortOrder);
//所有
$data["allNum"] = CouponRepository::getInstance()->getModel()->where(["coupon_id"=>$couponMainId])->count();
//未使用
$data["allNum"] = $data["total"];
$data["unUsedNum"] = CouponRepository::getInstance()->getModel()->where(['is_verificated' => self::BOOL_FALSE,"coupon_id"=>$couponMainId])->count();
//已使用
$data["usedNum"] = CouponRepository::getInstance()->getModel()->where(['is_verificated' => self::BOOL_TRUE ,"coupon_id"=>$couponMainId])->count();
return $this->json(0,"success",$data);

View File

@ -34,7 +34,7 @@ class Recharge extends Base
return $this->json("4001", "订单不存在");
}
if ($recharge['state'] == RechargeModel::state_on) {
return $this->json();
return $this->json(0,"该订单已支付成功");
}
$business = BusinessRepository::getInstance()->getModel()->where(["code" => $recharge['business_code']])->lock(true)->find();
if (empty($business)) {

27
app/model/ApplyStaff.php Normal file
View File

@ -0,0 +1,27 @@
<?php
namespace app\model;
class ApplyStaff extends Base
{
public const status_default = 0;//待审核
public const status_success = 1;//通过
public const status_fail = 2;//驳回
/**
* 关联的商户信息
*/
public function business()
{
return $this->hasOne(Business::class, 'code', 'business_code');
}
/**
* 关联的用户
*/
public function account()
{
return $this->hasOne(Account::class, 'user_code', 'user_code');
}
}

View File

@ -5,6 +5,7 @@ namespace app\repository;
use app\exception\RepositoryException;
use app\service\Repository;
use app\traits\account\ApplyStaffTrait;
use app\traits\account\BusinessFlowTrait;
use app\traits\CommentTrait;
use app\traits\CouponBillTrait;
@ -28,6 +29,7 @@ class AccountRepository extends Repository
use BusinessFlowTrait;
use CouponTrait;
use CouponBillTrait;
use ApplyStaffTrait;
/**
* 获取指定账户记录By手机号
*

View File

@ -8,6 +8,7 @@ use app\model\CouponMain;
use app\model\Deduction;
use app\model\Recharge;
use app\service\Repository;
use app\traits\account\ApplyStaffTrait;
use app\traits\CouponBillTrait;
use app\traits\CouponMainTrait;
use think\Collection;
@ -24,6 +25,7 @@ class BusinessRepository extends Repository
{
use CouponBillTrait;
use CouponMainTrait;
use ApplyStaffTrait;
/**
* 根据条件查询列表

View File

@ -0,0 +1,107 @@
<?php
namespace app\traits\account;
use app\model\Account;
use app\model\ApplyStaff;
trait ApplyStaffTrait
{
/**
* 获取申请员工记录
* @param $userCode
* @return ApplyStaff|array|\think\Model|null
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getApplyStaffByUserCode($userCode)
{
return ApplyStaff::with(["business","account"])->where("user_code",$userCode)->find();
}
/**
* 获取指定商家申请员工记录
* @param $userCode
* @return ApplyStaff|array|\think\Model|null
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getApplyStaffByUserCodeAndBusinessCode($userCode,$businessCode)
{
return ApplyStaff::with(["business","account"])->where("user_code",$userCode)->where("business_code",$businessCode)->find();
}
/**
* 创建一个加入员工申请
* @param $userCode
* @param $businessCode
*/
public function createApplyStaff($userCode,$businessCode)
{
$data = [
"user_code"=>$userCode,
"business_code"=>$businessCode,
"status"=>ApplyStaff::status_default,
"create_time"=>date("Y-m-d H:i:s"),
];
ApplyStaff::create($data);
}
/**
* 获取商家的员工列表
* @param $businessCode
* @param $mainCode
* @return Account
*/
public function getStaffListByBusinessCode($businessCode,$mainCode,$keyword)
{
$where = [
["type","=",Account::type_staff],
["main_code","=",$mainCode],
["business_code","=",$businessCode],
];
if(!empty($keyword)){
$where [] = ["nick_name","like","%{$keyword}%"] ;
}
$field = [
"nick_name as nickName",
"avatar_url as avatarUrl",
"gender",
"user_code",
];
return Account::where($where)->field($field)->select()->toArray();
}
/**
* 获取商家的员工申请列表
* @param $businessCode
* @param $mainCode
* @return Account
*/
public function getApplyStaffListByBusinessCode($businessCode,$keyword)
{
$where = [
["a.business_code","=",$businessCode],
["a.status","=",ApplyStaff::status_default],
];
if(!empty($keyword)){
$where[] = ["b.nick_name","like","%{$keyword}%"] ;
}
$field = [
"b.nick_name as nickName",
"b.avatar_url as avatarUrl",
"b.gender",
"b.user_code",
"a.create_time",
];
return ApplyStaff::alias("a")
->join("account b","a.user_code = b.user_code")
->where($where)
->field($field)
->select()
->toArray();
}
}