ctms/ctms-api/controllers/gm/v1/MemberController.php
fm453 314745edf8 优化ctms-api语法、修复已知BUG;
主要修复ctms-api、dacms对PHP新版本的支持问题
2025-04-10 23:19:15 +08:00

151 lines
4.4 KiB
PHP
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
# @Author: 嗨噜客(三亚) <fm453>
# @Date: 2022-05-22T07:35:53+08:00
# @Email: fm453@lukegzs.com
# @Last modified by: fm453
# @Last modified time: 2024-08-09T09:18:58+08:00
# @Copyright: www.hiluker.cn
//前端管理员入口
namespace api\controllers\gm\v1;
use Yii;
use common\models\Member as User;
use addons\models\AcUserExt;
use addons\models\AcEmployee;
use addons\models\AcStore;
class MemberController extends Common
{
public function beforeAction($action)
{
if (!$this->pid) {
$this->result('您正使用本系统内部接口,禁止非法链接使用!');
}
return parent::beforeAction($action);
}
public function actionIndex()
{
$apis = [
'login' => '登陆'
];
$this->result('您正使用CMTS系统用户管理接口', $apis, 200);
}
//用户登陆
public function actionLogin()
{
$pid = $this->pid;
$post = $this->postdata;
$search = $post['search'] ?? NULL;
$username = $search['username'] ?? NULL;
$username = $post['username'] ?? $username;
$passwd = $search['passwd'] ?? NULL;
$passwd = $post['passwd'] ?? $passwd;
$return = [];
if (!$username && !$passwd) {
$this->result('登陆参数错误!', NULL, 400);
}
if (!$username) {
$this->result('请输入您的账号!', NULL, 400);
}
if (!$passwd) {
$this->result('请输入您的密码!', NULL, 400);
}
$username = trim(htmlspecialchars_decode($username));
$passwd = trim(htmlspecialchars_decode($passwd));
if (!$username) {
$this->result('输入错误,请检查您的账号!', NULL, 400);
}
if (!$passwd) {
$this->result('输入有误,请检查您的密码!', NULL, 400);
}
$model = new User();
$user = $model->findOne(['mobile' => $username]);
if (!$user) {
$this->result('查无此人!', NULL, 404);
}
if ($user->status != User::STATUS_ACTIVE) {
$this->result('账户异常,禁止登陆!', NULL, 403);
}
//安全校验,登陆密码
$isPwdRight = $user->validatePassword($passwd);
if (!$isPwdRight) {
$this->result('密码错误,禁止登陆!', NULL, 400);
}
//用户主信息校验结束
//校验网点身份信息
$AcEmployeeModel = new AcEmployee();
$employee = $AcEmployeeModel->findOne(['mobile' => $user->mobile]);
if (!$employee) {
$this->result('未找到对应工号,请联系管理员!', NULL, 404);
}
if ($employee->deleted) {
$this->result('工号已清除,禁止登陆!', NULL, 403);
}
if (!$employee->status) {
$this->result('工号禁用,禁止登陆!', NULL, 403);
}
$this->employee_id = $employee->id;
$_user = $this->userLogin($user, $employee, TRUE);
$this->result('登陆成功!', ['user' => $_user], 200);
}
private function userLogin($user, $employee, $hasExt = FALSE)
{
// 使用指定用户名获取用户身份实例
$identity = $user;
// 登录用户有效期30d
$duration = 3600 * 24 * 30;
$_login = Yii::$app->user->login($identity, $duration);
if (!$_login) {
$this->result('登陆失败!', NULL, 400);
}
//登陆成功,返回用户信息
$_user = ['id' => $user->id, 'username' => $user->username, 'mobile' => $user->mobile, 'email' => $user->email, 'avatar' => $user->avatar];
$_user['employee_id'] = $this->employee_id;
$AcStoreModel = new AcStore();
$store = $AcStoreModel->findOne($employee->store_id);
$_store = [];
$citys = Yii::$app->params['citys'];
if ($store && !$store->deleted) {
$_store = [
'id' => $store->id,
'title' => $store->title,
'city' => isset($citys[$store->city]) ? $citys[$store->city]['name'] : '',
'addr' => $store->addr,
'longt' => $store->longt,
'lat' => $store->lat,
'status' => $store->status_code
];
}
$_user['store'] = $_store;
if ($hasExt) {
//查询关联用户的在当前系统中的扩展信息
$AcUserExt = new AcUserExt();
$exts = $AcUserExt->find()->where(['mid' => $user->id, 'mobile' => $user->mobile, 'pid' => $this->pid, 'deleted' => 0])->all();
if ($exts) {
foreach ($exts as $ext) {
$_user[$ext['key']] = $ext['value'];
}
}
}
//生成token用户数据是否强制更新过期时间
$isLong = $this->postdata['isLong'] ?? FALSE;
$expire = $isLong ? '+1 Month' : FALSE;
$_user = $this->userToken($_user, $refresh = TRUE, $expire);
return $_user;
}
}