jingcai-php/app/Http/Controllers/Api/Seller/SellerController.php

584 lines
20 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Http\Controllers\Api\Seller;
use App\Enums\BoolEnum;
use App\Enums\CooperateState;
use App\Enums\LottState;
use App\Enums\MaterialScene;
use App\Enums\OrderType;
use App\Enums\PayState;
use App\Enums\SellerLevel;
use App\Enums\WithdrawState;
use App\Http\RequestValidators\AuthValidator;
use App\Model\Customer\Customer;
use App\Model\Customer\CustomerWithdraw;
use App\Model\Order;
use App\Model\Seller\Seller;
use App\Model\Seller\SellerFeedback;
use App\Model\Seller\Shop;
use App\Model\Seller\ShopCooperate;
use App\Model\Seller\ShopCooperateLottery;
use App\Service\MaterialService;
use App\Utils\Helps;
use App\Utils\ThrowException;
use Illuminate\Http\Request;
class SellerController extends BaseController
{
/**
* @api {GET} /api/seller/seller/info 我的-店主信息
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": {
* "seller": { // 登录店主/店员信息
* "id": 1,
* "shop_id": 1,
* "level": 1,
* "phone": "",
* "name": "超级大大",
* "nickname": null,
* "email": "fefdsa",
* "balance": "89.44", // 店主余额
* "avatar": "da",
* "email_verified_at": null,
* "created_at": null,
* "updated_at": "2023-04-22T14:24:55.000000Z",
* "deleted_at": null
* },
* "shop": { // 店铺信息
* "id": 1,
* "name": "天下第一点",
* "seller_name": "天下第一点",
* "seller_phone": "111",
* "seller_wechat": "111",
* "addr": "天下第一点",
* "announcement": "天下第一点",
* "created_at": null,
* "updated_at": null,
* "deleted_at": null
* },
* "orders": {
* "draft_num": 0, // 待出票数
* "win_num": 0, // 待派奖数
* "withdraw_num": 0 // 提现数
* },
* "customers": {
* "balance": "21550.92" // 托管余额
* }
* }
* }
*
*/
public function info()
{
$seller = Seller::find($this->sellerId());
$seller->has_pay_password = $seller->password_pay ? true : false;
unset($seller->password_pay);
unset($seller->password);
$shop = Shop::with('shopExtra:id,shop_id,state')->find($seller->shop_id);
$shopId = $this->shopId();
$draftNum = Order::where(function($query) use ($shopId) {
$query->where(function($query) use ($shopId) {
$query->where('cooperate_show', BoolEnum::NO)
->where('shop_id', $shopId);
})->orWhere(function($query) use ($shopId) {
$query->where('cooperate_show', BoolEnum::YES)
->where('cooperate_id', $shopId);
});
})
->where('lottery_state', LottState::DRAFT)
->where(function ($query) {
$query->whereIn('type', [OrderType::NORMAL, OrderType::FADAN, OrderType::GENDAN])
->orWhere(function ($query) {
$query->where('type', OrderType::UNION)
->whereRaw('id = pid');
});
})->count();
$winNum = Order::where('draft_shop_id', $this->shopId())
->where('lottery_state', LottState::WIN)
->where(function ($query) {
$query->whereIn('type', [OrderType::NORMAL, OrderType::FADAN, OrderType::GENDAN])
->orWhere(function ($query) {
$query->where('type', OrderType::UNION)
->whereRaw('id = pid');
});
})->count();
$pendingNum = Order::where('lottery_state', LottState::PENDING)
->where('pay_state', PayState::SUCCESS)
->where(function ($query) {
$query->whereIn('type', [OrderType::NORMAL, OrderType::FADAN, OrderType::GENDAN])
->orWhere(function ($query) {
$query->where('type', OrderType::UNION)
->whereRaw('id = pid');
});
})
->where(function($query) use ($shopId) {
$query->where(function($query) use ($shopId) {
$query->where('cooperate_show', BoolEnum::NO)
->where('shop_id', $shopId);
})->orWhere(function($query) use ($shopId) {
$query->where('cooperate_show', BoolEnum::YES)
->where('cooperate_id', $shopId);
});
})
->count();
$withdrawNum = CustomerWithdraw::where('shop_id', $this->shopId())
->where('state', WithdrawState::PENDING)->count();
$allCustomerBalance = Customer::where('shop_id', $this->shopId())->sum('balance');
$shopAuditingNum = ShopCooperate::where('cooperate_id', $this->shopId())
->where('audit_state', CooperateState::PENDING)->count();
$lotteryAuditingNum = ShopCooperateLottery::where('cooperate_id', $this->shopId())
->where('audit_state', CooperateState::PENDING)->count();
return $this->jsonSuccess([
'seller' => $seller,
'shop' => $shop,
'cooperate' => [
'shop_auditing_num' => $shopAuditingNum,
'lottery_auditing_num' => $lotteryAuditingNum,
],
'orders' => [
'draft_num' => $draftNum,
'win_num' => $winNum,
'withdraw_num' => $withdrawNum,
'pending_num' => $pendingNum,
],
'customers' => [
'balance' => $allCustomerBalance
],
'buyer' => [
'domain' => config('buyer.domain')
],
'release' => Helps::release($shopId)
]);
}
/**
* @api {POST} /api/seller/seller/change_password 我的-修改密码
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiParam {String} password_old 旧密码
* @apiParam {String} password 新密码
* @apiParam {String} password_confirmation 新密码确认
*
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": []
* }
*
*/
public function changePassword(Request $request)
{
$old = $request->input('password_old');
$password = $request->input('password');
$err = AuthValidator::changePasswordErrors($request);
ThrowException::isTrue($err, $err);
$seller = Seller::find($this->sellerId());
ThrowException::isTrue(!Seller::checkPassword($old, $seller->password), '密码错误');
$seller->password = Seller::encryPassword($password);
$seller->save();
return $this->jsonSuccess();
}
/**
* @api {POST} /api/seller/seller/change_info 我的-修改信息
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiParam {Int} [notice_order_pay_success] 订单支付成功提醒
*
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": []
* }
*
*/
public function changeInfo(Request $request)
{
$seller = Seller::find($this->sellerId());
$notice_order_pay_success = $request->input('notice_order_pay_success');
$notice_order_receive = $request->input('notice_order_receive');
if ($notice_order_receive !== null) {
$seller->notice_order_receive = $notice_order_receive == BoolEnum::YES ? BoolEnum::YES : BoolEnum::NO;
}
if ($notice_order_pay_success !== null) {
$seller->notice_order_pay_success = $notice_order_pay_success == BoolEnum::YES ? BoolEnum::YES : BoolEnum::NO;
}
$seller->save();
return $this->jsonSuccess();
}
/**
* @api {POST} /api/seller/seller/change_pay_password 我的-设置支付密码
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiParam {String} [password_old] 旧密码,第一次设置时,可以不传
* @apiParam {String} password 新密码
* @apiParam {String} password_confirmation 新密码确认
*
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": []
* }
*
*/
public function changePayPassword(Request $request)
{
$old = $request->input('password_old');
$password = $request->input('password');
$err = AuthValidator::changePasswordErrors($request, true);
ThrowException::isTrue($err, $err);
/** @var Seller $seller */
$seller = Seller::find($this->sellerId());
if ($seller->password_pay) {
ThrowException::isTrue(!Seller::checkPassword($old, $seller->password_pay), '密码错误');
}
$seller->password_pay = Seller::encryPassword($password);
$seller->save();
return $this->jsonSuccess();
}
/**
* @api {POST} /api/seller/seller/reset_pay_password 我的-忘记支付密码
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiParam {String} real_name 姓名
* @apiParam {String} real_identity 身份证号
* @apiParam {String} password_pay 新密码
* @apiParam {String} password_pay_confirmation 新密码确认
*
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": []
* }
*
*/
public function resetPayPassword(Request $request)
{
$real_name = $request->input('real_name');
$real_identity = $request->input('real_identity');
$password = $request->input('password_pay');
$passwordConfirm = $request->input('password_pay_confirmation');
ThrowException::isTrue(!$password, '密码不能为空');
ThrowException::isTrue(strlen($password) < 6 || strlen($password) > 20, '密码长度为6-20位');
ThrowException::isTrue($password != $passwordConfirm, '两次密码不一致');
/** @var Seller $seller */
$seller = Seller::find($this->sellerId());
if (!$seller->real_name || !$seller->real_identify) {
ThrowException::run('尚未实名认证');
}
if ($seller->real_name != $real_name || $seller->real_identify != $real_identity) {
ThrowException::run('身份证或姓名不对');
}
$seller->password_pay = Seller::encryPassword($password);
$seller->save();
return $this->jsonSuccess();
}
/**
* @api {POST} /api/seller/seller/real_verify 我的-实名认证
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiParam {String} real_identity 身份证
* @apiParam {String} real_name 真实姓名
*
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": []
* }
*/
public function realVerify(Request $request)
{
$real_identity = $request->input('real_identity');
$real_name = $request->input('real_name');
ThrowException::isTrue(!$real_name, '姓名不能为空');
ThrowException::isTrue(!Helps::validIdCard($real_identity), '身份证格式有误');
ThrowException::isTrue(!Helps::validIdCardIsAdult($real_identity), '您尚未未成年');
$seller = $this->seller();
ThrowException::isTrue($seller->real_identity, '不可重复认证');
$seller->real_identity = $real_identity;
$seller->real_name = $real_name;
$seller->save();
return $this->jsonSuccess();
}
/**
* @api {POST} /api/seller/seller/upload 我的-上传头像
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiParam {String} file avatar // filename
*
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": {
* "path": "xxxxx", // 头像地址
* }
* }
*
*/
public function upload(Request $request, MaterialService $materialService)
{
$file = $request->file('avatar');
$material = $materialService->upload($file, $this->sellerId(), MaterialScene::AVATAR);
$seller = $this->seller();
$seller->avatar = $material->path;
$seller->save();
return $this->jsonSuccess([
'avatar' => $seller->avatar
]);
}
/**
* @api {POST} /api/seller/seller/store 员工管理-创建账号
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiParam {String} name 姓名
* @apiParam {String} phone 手机号
* @apiParam {String} password 密码
* @apiParam {String} password_confirmation 确认密码
*
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": []
* }
*
*/
public function store(Request $request)
{
$this->checkMasterNext();
$name = $request->input('name');
$phone = $request->input('phone');
$password = $request->input('password');
$passwordConfirm = $request->input('password_confirmation');
ThrowException::isTrue(!$name, '员工姓名不能为空');
ThrowException::isTrue(!Helps::validPhone($phone), '手机号格式有误');
ThrowException::isTrue(strlen($password) < 6, '密码长度不能小于6位');
ThrowException::isTrue($password != $passwordConfirm, '密码输入不一致,请重新输入');
$exist = Seller::where('shop_id', $this->shopId())
->where('phone', $phone)
->first();
ThrowException::isTrue($exist, '手机号为' . $phone . '的用户已经存在');
$sel = new Seller();
$sel->shop_id = $this->shopId();
$sel->name = $name;
$sel->phone = $phone;
$sel->password = Seller::encryPassword($password);
$sel->level = SellerLevel::STAFF;
$sel->save();
return $this->jsonSuccess();
}
/**
* @api {POST} /api/seller/seller/delete 员工管理-删除
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiParam {Int} id 员工id
*
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": []
* }
*
*/
public function delete(Request $request)
{
$this->checkMasterNext();
$id = $request->input('id');
$seller = Seller::find($id);
ThrowException::isTrue(!$seller, '员工不存在');
ThrowException::isTrue($seller->shop_id != $this->shopId(), '无权操作');
$seller->delete();
return $this->jsonSuccess();
}
/**
* @api {POST} /api/seller/seller/list 员工管理-列表
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": [
* {
* "id": 3,
* "shop_id": 1,
* "level": 2, // 1店主2店员
* "phone": "13411111111", // 手机号
* "name": "xxxx", // 姓名
* "nickname": null, // 昵称
* "email": "",
* "avatar": "", // 头像
* "email_verified_at": null,
* "enable_send_prize": 1, // 是否允许派奖1是0否
* "enable_ie": 1,// 是否允许加扣款1是0否
* "enable_work": 1, // 是否允许接单1是0否
* "created_at": "2023-05-17 21:23:54",
* "updated_at": "2023-05-17 21:23:54",
* "deleted_at": null
* }
* ]
* }
*/
public function list()
{
$this->checkMasterNext();
$data = Seller::where('shop_id', $this->shopId())
->where('level', SellerLevel::STAFF)
->get();
return $this->jsonSuccess($data);
}
/**
* @api {POST} /api/seller/seller/update 员工管理-更新
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiParam {Int} id 员工id
* @apiParam {String} [name] 姓名
* @apiParam {String} [phone] 手机号
* @apiParam {String} [password] 密码
* @apiParam {Int} [enable_send_prize] 开启派奖1开启0关闭
* @apiParam {Int} [enable_ie] 开启加扣款1开启0关闭
* @apiParam {Int} [enable_work] 开启接单1开启0关闭
*
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": []
* }
*
*/
public function update(Request $request)
{
$this->checkMasterNext();
$id = $request->input('id');
$seller = Seller::find($id);
ThrowException::isTrue(!$seller, '员工不存在');
ThrowException::isTrue($seller->shop_id != $this->shopId(), '无权操作');
$name = $request->input('name');
$phone = $request->input('phone');
$password = $request->input('password');
$enable_send_prize = $request->input('enable_send_prize');
$enable_ie = $request->input('enable_ie');
$enable_work = $request->input('enable_work');
if ($name !== null) {
$seller->name = $name;
}
if ($phone !== null) {
ThrowException::isTrue(!Helps::validPhone($phone), '手机号格式有误');
$exist = Seller::where('shop_id', $this->shopId())
->where('id', '<>', $id)
->where('phone', $phone)
->first();
ThrowException::isTrue($exist, '手机号为' . $phone . '的用户已经存在');
$seller->phone = $phone;
}
if ($password !== null) {
ThrowException::isTrue(strlen($password) < 6, '密码长度不能小于6位');
$seller->password = Seller::encryPassword($password);
}
if ($enable_ie !== null && BoolEnum::hasValue($enable_ie, false)) {
$seller->enable_ie = $enable_ie;
}
if ($enable_send_prize !== null && BoolEnum::hasValue($enable_send_prize, false)) {
$seller->enable_send_prize = $enable_send_prize;
}
if ($enable_work !== null && BoolEnum::hasValue($enable_work, false)) {
$seller->enable_work = $enable_work;
}
$seller->save();
return $this->jsonSuccess();
}
/**
* @api {POST} /api/customer/seller/feedback 我的-反馈
* @apiVersion 0.1.0
* @apiGroup 店主
*
* @apiParam {String} content 内容
*
* @apiSuccessExample {json} 返回结果
* {
* "code": 200,
* "message": "",
* "data": []
* }
*/
public function feedback(Request $request)
{
$content = $request->input('content');
ThrowException::isTrue(!$content, '内容不能为空');
$feedback = new SellerFeedback();
$feedback->seller_id = $this->sellerId();
$feedback->shop_id = $this->shopId();
$feedback->content = $content;
$feedback->save();
return $this->jsonSuccess();
}
}