180 lines
5.0 KiB
PHP
Executable File
180 lines
5.0 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Seller;
|
|
|
|
use App\Enums\BoolEnum;
|
|
use App\Enums\SellerLevel;
|
|
use App\Http\Controllers\Api\Customer\BaseController;
|
|
use App\Http\RequestValidators\AuthValidator;
|
|
use App\Model\Lottery;
|
|
use App\Model\Seller\Seller;
|
|
use App\Model\Seller\Shop;
|
|
use App\Utils\Helps;
|
|
use App\Utils\ThrowException;
|
|
use Illuminate\Http\Request;
|
|
|
|
class IndexController extends BaseController
|
|
{
|
|
/**
|
|
* @api {POST} /api/seller/register auth-注册
|
|
* @apiVersion 0.1.0
|
|
* @apiGroup 店主
|
|
*
|
|
* @apiParam {String} phone 手机号码
|
|
* @apiParam {String} password 密码
|
|
* @apiParam {String} password_confirmation 密码确认
|
|
*
|
|
* @apiSuccessExample {json} 返回结果
|
|
* {
|
|
* "code": 200,
|
|
* "message": "注册成功,请登录...",
|
|
* "data": []
|
|
* }
|
|
*/
|
|
public function register(Request $request)
|
|
{
|
|
$error = AuthValidator::registerErrors($request);
|
|
ThrowException::isTrue($error, $error);
|
|
|
|
$phone = $request->input('phone');
|
|
$password = $request->input('password');
|
|
$sellerId = $request->input('sellid');
|
|
|
|
if (!Helps::validPhone($phone)) {
|
|
ThrowException::run('手机号码格式错误');
|
|
}
|
|
|
|
$seller = Seller::where('phone', $phone)
|
|
->first();
|
|
if ($seller) {
|
|
return $this->jsonSuccess([], '已注册,请登录...');
|
|
}
|
|
|
|
$shop = new Shop();
|
|
$shop->shop_sn = Shop::generateShopSn();
|
|
$shop->name = $shop->shop_sn;
|
|
$shop->status = 1;
|
|
|
|
if ($sellerId) {
|
|
/** @var Seller $platformAgentor */
|
|
$platformAgentor = Seller::find($sellerId);
|
|
if ($platformAgentor) {
|
|
$shop->agent_seller_id = $platformAgentor->id;
|
|
$shop->agent_shop_id = $platformAgentor->shop_id;
|
|
// $enableAgent = $platformAgentor->enablePlatformAgent();
|
|
// if ($enableAgent) {
|
|
//
|
|
// }
|
|
}
|
|
}
|
|
|
|
$shop->save();
|
|
|
|
$seller = new Seller();
|
|
$seller->level = SellerLevel::MASTER;
|
|
$seller->phone = $phone;
|
|
$seller->password = Seller::encryPassword($password);
|
|
$seller->shop_id = $shop->id;
|
|
$seller->save();
|
|
|
|
Lottery::openAllLottery($shop->id);
|
|
return $this->jsonSuccess([
|
|
'redirect' => Helps::appSellerDownloadUrl()
|
|
], '注册成功,请登录...');
|
|
}
|
|
|
|
/**
|
|
* @api {POST} /api/seller/login auth-登录
|
|
* @apiVersion 0.1.0
|
|
* @apiGroup 店主
|
|
*
|
|
* @apiParam {String} phone 手机号码
|
|
* @apiParam {String} password 密码
|
|
*
|
|
* @apiSuccessExample {json} 返回结果
|
|
* {
|
|
* "code": 200,
|
|
* "message": "",
|
|
* "data": {
|
|
* "access_token": "eyJ0eXA5z7uNNgL76GgVGFFMOuINwHJnG73s", // token
|
|
* "token_type": "bearer",
|
|
* "expires_in": 36000 // 有效时长(秒)
|
|
* }
|
|
* }
|
|
*/
|
|
public function login(Request $request)
|
|
{
|
|
$phone = $request->input('phone');
|
|
$password = $request->input('password');
|
|
|
|
ThrowException::isTrue(!$phone || !$password, '账号活密码不能为空');
|
|
|
|
if (!preg_match('/^1[3-9]\d{9}$/i', $phone)) {
|
|
ThrowException::run('手机号码格式错误');
|
|
}
|
|
|
|
/** @var Seller $seller */
|
|
$seller = Seller::where('phone', $phone)->first();
|
|
ThrowException::isTrue(!$seller, '手机号或密码错误');
|
|
|
|
ThrowException::isTrue(!Seller::checkPassword($password, $seller->password), '手机号或密码错误');
|
|
|
|
ThrowException::isTrue($seller->shop->status != BoolEnum::YES, '店铺未启用');
|
|
|
|
|
|
$token = auth('seller')->login($seller);
|
|
|
|
ThrowException::isTrue(!$token, '登录失败!');
|
|
|
|
return $this->respondWithToken($token);
|
|
}
|
|
|
|
protected function respondWithToken($token)
|
|
{
|
|
return $this->jsonSuccess([
|
|
'access_token' => $token,
|
|
'token_type' => 'bearer',
|
|
'expires_in' => auth('customer')->factory()->getTTL() * 60
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @api {POST} /api/seller/refresh_token auth-刷新token
|
|
* @apiVersion 0.1.0
|
|
* @apiGroup 店主
|
|
*
|
|
* @apiSuccessExample {json} 返回结果
|
|
* {
|
|
* "code": 200,
|
|
* "message": "",
|
|
* "data": {
|
|
* "access_token": "eyJ0eXA5z7uNNgL76GgVGFFMOuINwHJnG73s", // token
|
|
* "token_type": "bearer",
|
|
* "expires_in": 36000 // 有效时长(秒)
|
|
* }
|
|
* }
|
|
*/
|
|
public function refreshToken()
|
|
{
|
|
return $this->respondWithToken(auth('customer')->refresh());
|
|
}
|
|
|
|
/**
|
|
* @api {GET|POST} /api/seller/logout auth-退出
|
|
* @apiVersion 0.1.0
|
|
* @apiGroup 店主
|
|
*
|
|
* @apiSuccessExample {json} 返回结果
|
|
* {
|
|
* "code":200,
|
|
* "message":"",
|
|
* "data":[]
|
|
* }
|
|
*/
|
|
public function logout()
|
|
{
|
|
auth('seller')->logout();
|
|
return $this->jsonSuccess();
|
|
}
|
|
}
|