控制器
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開啟事務(wù)->id無法獲取自增id 只能用insertGetId獲取自增id
//修改
$saveres = $user->allowField(true)->isUpdate(true)->save($post);
//過濾非表字段
->allowField(true)
//標(biāo)識更新操作
->isUpdate(true)
// 返回操作結(jié)果
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) {
// 回滾事務(wù)
$this->rollback();
return model_false(10001, $e->getMessage());
}
}
或者這種靜態(tài)調(diào)用
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;
/**
* 定義驗證規(guī)則
* 格式:'字段名' => ['規(guī)則1','規(guī)則2'...]
*
* @var array
*/
protected $rule = [
'group_id|用戶組編號' => 'require|number',
];
/**
* 定義錯誤信息
* 格式:'字段名.規(guī)則名' => '錯誤信息'
*
* @var array
*/
protected $message = [
];
//List控制器成校驗名稱list
public function sceneList()
{
return $this->only([
'person_phone',
])->remove('person_phone', 'require');
}
// 自定義驗證規(guī)則(第一個$value是被驗證的數(shù)據(jù)姻僧,第二個是驗證規(guī)則,第三個$data是所有的數(shù)據(jù))
protected function start_time_validate($value)
{
$time = strtotime($value);
if ($time) {
if ($time >= time()) {
$this->start_time = $time;
return true;
} else {
return '開始時間必須大于等于當(dāng)前時間';
}
} else {
return '不合法的時間格式';
}
}
protected function end_time_validate($value)
{
$time = strtotime($value);
if ($time) {
if ($time > $this->start_time) {
return true;
} else {
return '結(jié)束時間必須大于開始時間';
}
} else {
return '不合法的時間格式';
}
}
}
自定義助手函數(shù)
if (!function_exists('model_true')) {
/**
* @param string $msg
* @param array $data
* @return array
* model返回數(shù)據(jù)
*/
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返回數(shù)據(jù)
*/
function model_false(int $code, string $msg = '操作失敗', array $data = [])
{
return ['res' => false, 'msg' => $msg, 'code' => $code, 'data' => $data];
}
}