glhcp/server/app/admin/logic/user/AppLogic.php

105 lines
3.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace app\admin\logic\user;
use app\common\basics\Logic;
use app\common\model\user\User;
use app\common\model\user\UserApi;
use think\facade\Db;
class AppLogic extends Logic
{
public static function lists($get)
{
$count = UserApi::count();
$lists = UserApi::alias('ua')->leftJoin('user u', 'u.id = ua.user_id')
->order('ua.id', 'desc')
->page($get['page'], $get['limit'])
->field('ua.*,u.nickname')
->select()
->toArray();
return ['count' => $count, 'lists' => $lists];
}
public static function add($post)
{
try{
if (empty($post['user_id']) || empty($post['app_id']) || empty($post['app_secret'])) {
throw new \Exception('参数错误');
}
$userLevel = UserApi::where(['app_id'=>trim($post['app_id'])])->findOrEmpty();
if(!$userLevel->isEmpty()) {
throw new \Exception('APP_ID已被使用请更换后重试');
}
$data = [
'user_id' => trim($post['user_id']),
'app_id' => trim($post['app_id']),
'app_secret' => trim($post['app_secret']),
'remarks' => trim($post['remarks']),
'status' => 1,
];
UserApi::create($data);
User::where('id', $post['user_id'])->save([
'is_api' => 1
]);
return true;
}catch(\Exception $e) {
self::$error = $e->getMessage();
return false;
}
}
public static function edit($post)
{
try{
$userLevel = UserApi::where([
['app_id', '=', trim($post['app_id'])],
['id', '<>', $post['id']]
])->findOrEmpty();
if(!$userLevel->isEmpty()) {
throw new \think\Exception('名称已被使用,请更换后重试');
}
$data = [
'id' => $post['id'],
'user_id' => trim($post['user_id']),
'app_id' => trim($post['app_id']),
'app_secret' => trim($post['app_secret']),
'remarks' => trim($post['remarks']),
];
UserApi::update($data);
return true;
}catch(\Exception $e) {
self::$error = $e->getMessage();
return false;
}
}
public static function del($id)
{
try{
UserApi::where('id', $id)->delete();
return true;
}catch(\Exception $e) {
self::$error = $e->getMessage();
return false;
}
}
public static function getUserApiList()
{
$levelArr = UserApi::field('id,name,phone')
->order('id desc')
->select()
->toArray();
return $levelArr;
}
public static function getUserApi($id){
$detail = UserApi::alias('ua')->leftJoin('user u', 'u.id=ua.user_id')->field('ua.*,u.nickname,u.mobile')->where(['ua.id'=>$id])->findOrEmpty();
if($detail->isEmpty()) {
return [];
}
$detail = $detail->toArray();
return $detail;
}
}