控制器
public function postAuthorityDistribution()
{
$post = request()->param();
$permissionGroupUserModel = new \app\api\model\PermissionGroupUser();
$permissionGroupUserValidate = new \app\api\validate\PermissionGroupUser();
//校验
if (!$permissionGroupUserValidate->scene('list')->check($post)) {
return $this->result(10003, $permissionGroupUserValidate->getError(), $this->time);
}
$res = $permissionGroupUserModel->permissionGroupUserList($post);
return $this->result($res['code'], $res['msg'], $this->time, $res['data']);
}
模型
public function permissionGroupUserList($post){
$user = $this;
$res = $this->transaction(function () use ($user, $post) {
//保存
$saveres = $user->allowField(true)->save($post);
//$user->id 获取主键id 使用startTrans开启事务->id无法获取自增id 只能用insertGetId获取自增id
//修改
$saveres = $user->allowField(true)->isUpdate(true)->save($post);
//过滤非表字段
->allowField(true)
//标识更新操作
->isUpdate(true)
// 返回操作结果
return $saveres
});
if ($res) {
return model_true('用户编辑成功');
} else {
return model_false(10002, '用户编辑失败');
}
}
public function permissionGroupUserList($post){
$this->startTrans();
try {
$this->commit();
return model_true("message");
} catch (\Exception $e) {
// 回滚事务
$this->rollback();
return model_false(10001, $e->getMessage());
}
}
或者这种静态调用
public static function do_login_act($post)
{
$info = self::where('user_login', $post['username'])->find();
if (empty($info)) {
return ['code' => 0, 'msg' => '账号不存在'];
}
if ($info['status'] == 2) {
return ['code' => 0, 'msg' => '账号已禁用'];
}
if ($info['status'] == 4) {
return ['code' => 0, 'msg' => '账号未激活'];
}
if (!password_compare($post['password'], $info['user_pass'])) {
return ['code' => 0, 'msg' => '密码错误'];
}
$info['last_login_ip'] = get_client_ip(0, true);
$info['last_login_time'] = time();
$res = self::transaction(function () use ($info) {
$info->allowField(true)->isUpdate(true)->save($info);
return true;
});
if ($res) {
unset($info['user_pass']);
session('user', $info->toArray());
session("login_type", "staff");
return ['code' => 1, 'msg' => '登录成功', 'data' => ['url' => '/admin/index/index']];
} else {
return ['code' => 1, 'msg' => '登录失败,请重试。'];
}
}
校验
<?php
namespace app\api\validate;
use think\Validate;
class User extends Validate
{
protected $start_time;
/**
* 定义验证规则
* 格式:'字段名' => ['规则1','规则2'...]
*
* @var array
*/
protected $rule = [
'group_id|用户组编号' => 'require|number',
];
/**
* 定义错误信息
* 格式:'字段名.规则名' => '错误信息'
*
* @var array
*/
protected $message = [
];
//List控制器成校验名称list
public function sceneList()
{
return $this->only([
'person_phone',
])->remove('person_phone', 'require');
}
// 自定义验证规则(第一个$value是被验证的数据,第二个是验证规则,第三个$data是所有的数据)
protected function start_time_validate($value)
{
$time = strtotime($value);
if ($time) {
if ($time >= time()) {
$this->start_time = $time;
return true;
} else {
return '开始时间必须大于等于当前时间';
}
} else {
return '不合法的时间格式';
}
}
protected function end_time_validate($value)
{
$time = strtotime($value);
if ($time) {
if ($time > $this->start_time) {
return true;
} else {
return '结束时间必须大于开始时间';
}
} else {
return '不合法的时间格式';
}
}
}
自定义助手函数
if (!function_exists('model_true')) {
/**
* @param string $msg
* @param array $data
* @return array
* model返回数据
*/
function model_true(string $msg = '操作成功', array $data = [])
{
return ['res' => true, 'msg' => $msg, 'code' => 0, 'data' => $data];
}
}
if (!function_exists('model_false')) {
/**
* @param int $code
* @param string $msg
* @param array $data
* @return array
* model返回数据
*/
function model_false(int $code, string $msg = '操作失败', array $data = [])
{
return ['res' => false, 'msg' => $msg, 'code' => $code, 'data' => $data];
}
}