feat:完成后台客户列表接口、前台客户登录模块接口
This commit is contained in:
parent
102a4763c6
commit
cc5f45657e
112
app/admin/controller/Customer/Customer.php
Normal file
112
app/admin/controller/Customer/Customer.php
Normal file
@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\Customer;
|
||||
|
||||
use app\BaseController;
|
||||
use app\common\model\Customer\Customer as ModelCustomer;
|
||||
use app\Request;
|
||||
use think\Validate;
|
||||
use think\exception\ValidateException;
|
||||
use think\facade\Filesystem;
|
||||
use app\common\arw\adjfut\src\Excel;
|
||||
use app\common\arw\adjfut\src\UploadFile;
|
||||
use app\common\exception\Tool;
|
||||
use think\facade\Db;
|
||||
use think\facade\Env;
|
||||
|
||||
|
||||
class Customer extends BaseController
|
||||
{
|
||||
/**
|
||||
* 获取客户列表
|
||||
*/
|
||||
public function getCustomerList(Request $request): array
|
||||
{
|
||||
$con = Tool::getOptionalQuery(
|
||||
['customer_name', 'LIKE'],
|
||||
['customer_account', 'LIKE'],
|
||||
['customer_phone', 'LIKE'],
|
||||
['customer_email', 'LIKE'],
|
||||
['customer_sex', '='],
|
||||
);
|
||||
|
||||
$query = ModelCustomer::where($con)
|
||||
->field([
|
||||
'customer_id',
|
||||
'customer_guid',
|
||||
'customer_name',
|
||||
'customer_account',
|
||||
'customer_phone',
|
||||
'customer_email',
|
||||
'customer_sex'
|
||||
])
|
||||
->order('customer_update_time', 'desc');
|
||||
|
||||
return msg("获取客户列表成功!", $query);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加客户
|
||||
*/
|
||||
public function addCustomer(Request $request): array
|
||||
{
|
||||
$params = $request->param();
|
||||
$this->validate($params, [
|
||||
'customer_name|客户昵称' => 'require',
|
||||
'customer_account|客户账号' => 'require|alphaNum|min:10',
|
||||
]);
|
||||
$model = ModelCustomer::create($params, [
|
||||
'customer_name',
|
||||
'customer_account',
|
||||
'customer_password',
|
||||
'customer_phone',
|
||||
'customer_email',
|
||||
'customer_sex',
|
||||
'customer_guid',
|
||||
'customer_create_user_guid',
|
||||
'customer_update_user_guid'
|
||||
]);
|
||||
return msg('添加成功!');
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑客户
|
||||
*/
|
||||
public function editCustomer(Request $request): array
|
||||
{
|
||||
$params = $request->param();
|
||||
$this->validate($params, [
|
||||
'customer_name|客户昵称' => 'require',
|
||||
'customer_account|客户账号' => 'require',
|
||||
]);
|
||||
|
||||
$model = ModelCustomer::where('customer_guid', $params['customer_guid'])->find();
|
||||
if (!$model) throwErrorMsg("该客户不存在", 1);
|
||||
$model->allowField([
|
||||
'customer_name',
|
||||
'customer_account',
|
||||
'customer_password',
|
||||
'customer_phone',
|
||||
'customer_email',
|
||||
'customer_sex',
|
||||
'customer_update_user_guid'
|
||||
])->save($params);
|
||||
return msg('编辑成功!');
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除客户
|
||||
*/
|
||||
public function deleteCustomer(Request $request): array
|
||||
{
|
||||
$params = $request->param();
|
||||
$this->validate($params, [
|
||||
'customer_guid' => 'require',
|
||||
]);
|
||||
$customer = ModelCustomer::where([
|
||||
'customer_guid' => explode(',', $params['customer_guid'])
|
||||
])->select();
|
||||
$customer->delete();
|
||||
return msg('删除成功!');
|
||||
}
|
||||
}
|
@ -4,11 +4,13 @@ namespace app\api\controller;
|
||||
|
||||
use app\common\arw\adjfut\src\Validate;
|
||||
use app\BaseController;
|
||||
use app\common\logic\Login as LogicLogin;
|
||||
use app\common\logic\Login\CustomerLogin as LogicCustomerLogin;
|
||||
use app\common\model\Token as ModelToken;
|
||||
use app\Request;
|
||||
use think\captcha\facade\Captcha;
|
||||
use think\middleware\SessionInit;
|
||||
use think\Response;
|
||||
use think\facade\Db;
|
||||
|
||||
class Login extends BaseController
|
||||
{
|
||||
@ -28,117 +30,98 @@ class Login extends BaseController
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证token
|
||||
* 客户账号注册接口
|
||||
*
|
||||
* @param Request $request
|
||||
* @return void
|
||||
*/
|
||||
public function validateToken(Request $request)
|
||||
{
|
||||
$token = $request->getCurrentToken();
|
||||
return [
|
||||
'code' => 0,
|
||||
'data' => [
|
||||
'exp_time' => $token->token_exp_time,
|
||||
],
|
||||
'msg' => 'ok'
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 西北政法大学单点登陆
|
||||
*
|
||||
* @param Request $request
|
||||
* @return Response
|
||||
* @date 2023-01-04
|
||||
* @example
|
||||
* @author admin
|
||||
* @return array
|
||||
* @date 2023-06-25
|
||||
* @author xjh
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function casOauthLogin(Request $request): Response
|
||||
public function accountRegistration(Request $request): array
|
||||
{
|
||||
$url = $request->param('url');
|
||||
return LogicLogin::casOauthLogin(
|
||||
LogicLogin::casOauthLoginHandle($url)
|
||||
);
|
||||
$params = Validate::param([
|
||||
'customer_name|客户昵称' => 'require',
|
||||
'customer_account|客户账号' => 'require|alphaNum|min:10',
|
||||
'customer_password|客户密码' => 'require|alphaNum|min:8',
|
||||
// 'captcha|验证码' => $request->isProd() ? 'require|captcha' : false
|
||||
]);
|
||||
try {
|
||||
Db::startTrans();
|
||||
$token = LogicCustomerLogin::accountRegistration($params);
|
||||
Db::commit();
|
||||
return [
|
||||
'code' => 0,
|
||||
'data' => [
|
||||
'token' => $token->token_content,
|
||||
'exp_time' => $token->token_exp_time,
|
||||
],
|
||||
'msg' => '注册成功!'
|
||||
];
|
||||
} catch (\Throwable $th) {
|
||||
Db::rollback();
|
||||
throwErrorMsg($th->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 西北政法大学单点登出
|
||||
* 客户账号登录接口
|
||||
*
|
||||
* @param Request $request
|
||||
* @return Response
|
||||
* @date 2023-01-04
|
||||
* @example
|
||||
* @author admin
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function casOauthLogout(Request $request): Response
|
||||
{
|
||||
$url = $request->param('url');
|
||||
return LogicLogin::casOauthLogout($url);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户账号登录
|
||||
*
|
||||
* @param Request $request
|
||||
* @date 2022-03-05
|
||||
* @example
|
||||
* @author admin
|
||||
* @return array
|
||||
* @date 2023-06-25
|
||||
* @author xjh
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function accountLogin(Request $request): array
|
||||
{
|
||||
$param = Validate::param([
|
||||
'account|账号' => 'require',
|
||||
'password|密码' => 'require',
|
||||
'captcha|验证码' => $request->isProd() ? 'require|captcha' : false
|
||||
$params = Validate::param([
|
||||
'customer_account|客户账号' => 'require|alphaNum|min:10',
|
||||
'customer_password|客户密码' => 'require|alphaNum|min:8',
|
||||
// 'captcha|验证码' => $request->isProd() ? 'require|captcha' : false
|
||||
]);
|
||||
$token = LogicLogin::accountLogin(
|
||||
$param['account'],
|
||||
$param['password']
|
||||
);
|
||||
try {
|
||||
Db::startTrans();
|
||||
$token = LogicCustomerLogin::accountLogin($params);
|
||||
Db::commit();
|
||||
return [
|
||||
'code' => 0,
|
||||
'data' => [
|
||||
'token' => $token->token_content,
|
||||
'exp_time' => $token->token_exp_time,
|
||||
],
|
||||
'msg' => '登录成功!'
|
||||
];
|
||||
} catch (\Throwable $th) {
|
||||
Db::rollback();
|
||||
throwErrorMsg($th->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'code' => 0,
|
||||
'data' => [
|
||||
'token' => $token->token_content,
|
||||
'exp_time' => $token->token_exp_time,
|
||||
],
|
||||
'msg' => 'ok'
|
||||
];
|
||||
/**
|
||||
* 客户登出接口
|
||||
*
|
||||
* @param Request $request
|
||||
* @return array
|
||||
* @date 2023-06-25
|
||||
* @author xjh
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function logout(Request $request): array
|
||||
{
|
||||
ModelToken::logout();
|
||||
return msg('登出成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成验证码
|
||||
*
|
||||
* @date 2022-03-05
|
||||
* @example
|
||||
* @author admin
|
||||
* @date 2023-06-25
|
||||
* @author xjh
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function getCaptcha()
|
||||
{
|
||||
return Captcha::create();
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户登出
|
||||
*
|
||||
* @param Request $request
|
||||
* @date 2022-03-09
|
||||
* @example
|
||||
* @author admin
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public function userLogout(Request $request): array
|
||||
{
|
||||
$token = $request->getCurrentToken();
|
||||
$token->logout();
|
||||
return [
|
||||
'code' => 0,
|
||||
'msg' => '登出成功'
|
||||
];
|
||||
}
|
||||
}
|
||||
|
62
app/common/logic/Customer/Customer.php
Normal file
62
app/common/logic/Customer/Customer.php
Normal file
@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\logic\Customer;
|
||||
|
||||
use app\common\model\Token;
|
||||
use app\common\model\Customer\Customer as ModelCustomer;
|
||||
use app\common\model\Menu\Menu as ModelMenu;
|
||||
use app\common\model\Menu\MenuApi as ModelMenuApi;
|
||||
use think\facade\Config;
|
||||
use think\facade\Request;
|
||||
use think\helper\Arr;
|
||||
use think\Response;
|
||||
use think\response\Redirect;
|
||||
use app\common\arw\adjfut\src\Curl;
|
||||
use app\common\arw\adjfut\src\Tool;
|
||||
|
||||
class Customer
|
||||
{
|
||||
/**
|
||||
* 获取客户菜单
|
||||
*
|
||||
* @return array
|
||||
* @date 2023-06-25
|
||||
* @author xjh
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function getCustomerMenu(): array
|
||||
{
|
||||
$result = [];
|
||||
$data = ModelMenu::field([
|
||||
'menu.menu_guid',
|
||||
'menu.menu_parent_guid',
|
||||
'menu.menu_name',
|
||||
'menu.menu_url',
|
||||
'menu.menu_show',
|
||||
'menu.menu_icon',
|
||||
])->find(ModelMenu::CUSTOMER_MENU)->toArray();
|
||||
$menu_api_url = ModelMenuApi::where([
|
||||
'menu_guid' => $data['menu_guid'],
|
||||
'menu_api_status' => 1
|
||||
])->column('menu_api_url');
|
||||
$data['menu_api_url'] = join(',', $menu_api_url);
|
||||
$result[] = $data;
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 密码加密
|
||||
*
|
||||
* @param string $password
|
||||
* @return string
|
||||
* @date 2023-06-25
|
||||
* @author xjh
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function encryptPassword(string $password): string
|
||||
{
|
||||
return md5($password);
|
||||
}
|
||||
}
|
88
app/common/logic/Login/CustomerLogin.php
Normal file
88
app/common/logic/Login/CustomerLogin.php
Normal file
@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\logic\Login;
|
||||
|
||||
use app\common\model\Token;
|
||||
use app\common\model\Customer\Customer as ModelCustomer;
|
||||
use app\common\model\Token as ModelToken;
|
||||
use app\BaseModel;
|
||||
use app\common\logic\Customer\Customer as LogicCustomer;
|
||||
use think\facade\Config;
|
||||
use think\facade\Request;
|
||||
use think\helper\Arr;
|
||||
use think\Response;
|
||||
use think\response\Redirect;
|
||||
use app\common\arw\adjfut\src\Curl;
|
||||
use app\common\arw\adjfut\src\Tool;
|
||||
|
||||
class CustomerLogin
|
||||
{
|
||||
/**
|
||||
* 客户注册
|
||||
*
|
||||
* @param array $params 客户数据
|
||||
* @return ModelToken
|
||||
* @date 2023-06-25
|
||||
* @author xjh
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function accountRegistration(array $params): ModelToken
|
||||
{
|
||||
BaseModel::setUserGuid(false);
|
||||
$customer_create = ModelCustomer::create([
|
||||
'customer_name' => $params['customer_name'],
|
||||
'customer_account' => $params['customer_account'],
|
||||
'customer_password' => $params['customer_password'],
|
||||
]);
|
||||
return self::handleTokenData($customer_create->customer_guid);
|
||||
}
|
||||
|
||||
/**
|
||||
* 客户登录
|
||||
*
|
||||
* @param array $params 客户数据
|
||||
* @return ModelToken
|
||||
* @date 2023-06-25
|
||||
* @author xjh
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function accountLogin(array $params): ModelToken
|
||||
{
|
||||
BaseModel::setUserGuid(false);
|
||||
$customer = ModelCustomer::where('customer_account', $params['customer_account'])->find();
|
||||
if (!$customer) {
|
||||
throwErrorMsg("账号不存在!");
|
||||
}
|
||||
if ($customer->customer_password != LogicCustomer::encryptPassword($params['customer_password'])) {
|
||||
throwErrorMsg("密码错误!");
|
||||
}
|
||||
return self::handleTokenData($customer->customer_guid);
|
||||
}
|
||||
|
||||
/**
|
||||
* 客户Token数据处理
|
||||
*
|
||||
* @param string $customer_guid 客户guid
|
||||
* @return ModelToken
|
||||
* @date 2023-06-25
|
||||
* @author xjh
|
||||
* @since 1.0.0
|
||||
*/
|
||||
private static function handleTokenData(string $customer_guid): ModelToken
|
||||
{
|
||||
$menus = ModelCustomer::getCustomerMenu();
|
||||
$api = [];
|
||||
foreach ($menus as $menu) {
|
||||
$api = array_merge($api, explode(',', $menu['menu_api_url']));
|
||||
}
|
||||
$api = array_values(array_filter(array_unique($api)));
|
||||
if (!$api) {
|
||||
throwErrorMsg("无权登录");
|
||||
}
|
||||
return ModelToken::login(
|
||||
$customer_guid,
|
||||
ModelToken::CUSTOMER_TYPE,
|
||||
['menu' => $menus, 'api' => $api,]
|
||||
);
|
||||
}
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\common\logic;
|
||||
namespace app\common\logic\Login;
|
||||
|
||||
use app\common\model\Token;
|
||||
use app\common\model\User\User;
|
||||
@ -14,7 +14,7 @@ use think\response\Redirect;
|
||||
use app\common\arw\adjfut\src\Curl;
|
||||
use app\common\arw\adjfut\src\Tool;
|
||||
|
||||
class Login
|
||||
class UserLogin
|
||||
{
|
||||
/**
|
||||
* 账号登陆
|
||||
@ -24,7 +24,7 @@ class Login
|
||||
* @return Token
|
||||
* @date 2023-01-03
|
||||
* @example
|
||||
* @author admin
|
||||
* @author adminss
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function accountLogin(string $account, string $password = ''): Token
|
112
app/common/model/Customer/Customer.php
Normal file
112
app/common/model/Customer/Customer.php
Normal file
@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model\Customer;
|
||||
|
||||
use app\common\arw\adjfut\src\Validate;
|
||||
use app\common\logic\Customer\Customer as LogicCustomer;
|
||||
use app\common\logic\Customer\Customer as CommonLogicCustomer;
|
||||
use app\BaseModel;
|
||||
use think\model\concern\SoftDelete;
|
||||
use app\common\arw\adjfut\src\Excel;
|
||||
use app\Request;
|
||||
use app\common\exception\Tool;
|
||||
use think\facade\Db;
|
||||
|
||||
class Customer extends BaseModel
|
||||
{
|
||||
use SoftDelete;
|
||||
// 删除字段
|
||||
protected $deleteTime = 'customer_delete_time';
|
||||
// 设置主键名
|
||||
protected $pk = 'customer_guid';
|
||||
// 设置废弃字段
|
||||
protected $disuse = [];
|
||||
// 设置字段信息
|
||||
protected $schema = [
|
||||
'customer_id' => 'int',
|
||||
'customer_guid' => 'string',
|
||||
'customer_name' => 'string',
|
||||
'customer_account' => 'string',
|
||||
'customer_password' => 'string',
|
||||
'customer_phone' => 'string',
|
||||
'customer_email' => 'string',
|
||||
'customer_sex' => 'string',
|
||||
'customer_create_time' => 'datetime',
|
||||
'customer_create_user_guid' => 'string',
|
||||
'customer_update_time' => 'datetime',
|
||||
'customer_update_user_guid' => 'string',
|
||||
'customer_delete_time' => 'datetime',
|
||||
'customer_delete_user_guid' => 'string',
|
||||
|
||||
];
|
||||
// 设置json类型字段
|
||||
protected $json = [''];
|
||||
// 开启自动写入时间戳字段
|
||||
protected $autoWriteTimestamp = 'datetime';
|
||||
// 创建时间
|
||||
protected $createTime = 'customer_create_time';
|
||||
// 修改时间
|
||||
protected $updateTime = 'customer_update_time';
|
||||
|
||||
/**
|
||||
* 新增前
|
||||
*/
|
||||
public static function onBeforeInsert(self $model): void
|
||||
{
|
||||
$model->customer_password = CommonLogicCustomer::encryptPassword($model->customer_password);
|
||||
self::checkRepeatData($model);
|
||||
$model->completeCreateField();
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新前
|
||||
*/
|
||||
public static function onBeforeUpdate(self $model): void
|
||||
{
|
||||
$model->customer_password = CommonLogicCustomer::encryptPassword($model->customer_password);
|
||||
self::checkRepeatData($model);
|
||||
$model->completeUpdateField();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除前
|
||||
*/
|
||||
public static function onBeforeDelete(self $model): void
|
||||
{
|
||||
$model->completeDeleteField();
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据查重
|
||||
*
|
||||
* @param self $model
|
||||
* @return void
|
||||
* @date 2022-03-11
|
||||
* @example
|
||||
* @author admin
|
||||
* @since 1.0.0
|
||||
*/
|
||||
private static function checkRepeatData(self $model)
|
||||
{
|
||||
Validate::unique(
|
||||
self::class,
|
||||
$model->customer_guid,
|
||||
$model->getData(),
|
||||
['customer_account' => '账号',],
|
||||
['customer_account' => "账号已存在!"]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取客户菜单
|
||||
*
|
||||
* @return array
|
||||
* @date 2023-06-25
|
||||
* @author xjh
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function getCustomerMenu(): array
|
||||
{
|
||||
return LogicCustomer::getCustomerMenu();
|
||||
}
|
||||
}
|
@ -43,6 +43,8 @@ class Menu extends BaseModel
|
||||
protected $createTime = 'menu_create_time';
|
||||
// 修改时间
|
||||
protected $updateTime = 'menu_update_time';
|
||||
// 客户菜单
|
||||
const CUSTOMER_MENU = '6cd0fd60-c99c-38a0-7217-e88789b30a17';
|
||||
/**
|
||||
* 状态 启用
|
||||
*/
|
||||
|
@ -28,6 +28,7 @@ class Token extends BaseModel
|
||||
protected $schema = [
|
||||
'token_id' => 'int',
|
||||
'token_guid' => 'string',
|
||||
'token_type' => 'int',
|
||||
'user_guid' => 'string',
|
||||
'token_menu' => 'json',
|
||||
'token_api' => 'json',
|
||||
@ -46,6 +47,10 @@ class Token extends BaseModel
|
||||
protected $createTime = 'token_create_time';
|
||||
// 修改时间
|
||||
protected $updateTime = 'token_update_time';
|
||||
// 用户类型
|
||||
const USER_TYPE = 1;
|
||||
// 客户用户
|
||||
const CUSTOMER_TYPE = 2;
|
||||
/**
|
||||
* 当前用户
|
||||
*
|
||||
@ -138,6 +143,7 @@ class Token extends BaseModel
|
||||
* 如果不存在token 新增数据
|
||||
*
|
||||
* @param string $guid 用户id
|
||||
* @param int $type 登录类型
|
||||
* @param array $options 配置项
|
||||
* @param int $options[expTime] 过期时间(时间戳) 不传默认两小时
|
||||
* @param array $options[menu] 菜单
|
||||
@ -147,7 +153,7 @@ class Token extends BaseModel
|
||||
* @author admin
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public static function login(string $guid, array $options = []): self
|
||||
public static function login(string $guid, int $type, array $options = []): self
|
||||
{
|
||||
$expTime = 0;
|
||||
$menu = [];
|
||||
@ -174,16 +180,18 @@ class Token extends BaseModel
|
||||
*/
|
||||
$model = self::where([
|
||||
'user_guid' => $guid,
|
||||
'token_type' => $type
|
||||
])->find();
|
||||
// 数据不存在
|
||||
if (!$model) {
|
||||
return self::create($data + [
|
||||
'user_guid' => $guid,
|
||||
'token_type' => $type,
|
||||
]);
|
||||
}
|
||||
// token已过期
|
||||
if ($model->is_exp) {
|
||||
$data['token_content'] = $model->generateContent($model);
|
||||
$data['token_content'] = $model->generateContent();
|
||||
}
|
||||
$model->save($data);
|
||||
return $model;
|
||||
@ -332,6 +340,7 @@ class Token extends BaseModel
|
||||
return md5(join('-', [
|
||||
$this->token_exp_time,
|
||||
$this->user_guid,
|
||||
$this->token_type,
|
||||
]) . time());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user