mirror of
				https://gitee.com/technical-laohu/mpay.git
				synced 2025-11-04 16:53:44 +08:00 
			
		
		
		
	删除文件 app
This commit is contained in:
		@@ -1,44 +0,0 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace app\controller\api;
 | 
			
		||||
 | 
			
		||||
use think\Request;
 | 
			
		||||
use app\model\Order;
 | 
			
		||||
 | 
			
		||||
class ConsoleController
 | 
			
		||||
{
 | 
			
		||||
    public function orderinfo(Request $request)
 | 
			
		||||
    {
 | 
			
		||||
        $date = (int)$request->get('time') ?: 0;
 | 
			
		||||
        $time = match ($date) {
 | 
			
		||||
            0 => [date('Y') . '-01-01 00:00:00', date('Y-m-d 23:59:59')],
 | 
			
		||||
            1 => [date('Y-m-d H:i:s', strtotime('-30 days')), date('Y-m-d 23:59:59')],
 | 
			
		||||
            2 => [date('Y-m-d H:i:s', strtotime('-6 months')), date('Y-m-d 23:59:59')],
 | 
			
		||||
            3 => [date('Y-m-d H:i:s', strtotime('-1 year')), date('Y-m-d 23:59:59')],
 | 
			
		||||
            default => []
 | 
			
		||||
        };
 | 
			
		||||
        if (!$time) {
 | 
			
		||||
            return json(['code' => 400, 'msg' => '参数错误']);
 | 
			
		||||
        }
 | 
			
		||||
        $orders = Order::whereBetweenTime('create_time', $time[0], $time[1])->where('state', 1)->field('id,type,really_price')->select();
 | 
			
		||||
        $data = [
 | 
			
		||||
            'ordernum' => count($orders),
 | 
			
		||||
            'totalmoney' => \number_format(array_sum(array_column($orders->toArray(), 'really_price')), 2),
 | 
			
		||||
            'wxpay' => [
 | 
			
		||||
                'num' => count($orders->where('type', 'wxpay')),
 | 
			
		||||
                'money' => \number_format(array_sum(array_column($orders->where('type', 'wxpay')->toArray(), 'really_price')), 2)
 | 
			
		||||
            ],
 | 
			
		||||
            'alipay' => [
 | 
			
		||||
                'num' => count($orders->where('type', 'alipay')),
 | 
			
		||||
                'money' => \number_format(array_sum(array_column($orders->where('type', 'alipay')->toArray(), 'really_price')), 2)
 | 
			
		||||
            ],
 | 
			
		||||
            'unionpay' => [
 | 
			
		||||
                'num' => count($orders->where('type', 'unionpay')),
 | 
			
		||||
                'money' => \number_format(array_sum(array_column($orders->where('type', 'unionpay')->toArray(), 'really_price')), 2)
 | 
			
		||||
            ]
 | 
			
		||||
        ];
 | 
			
		||||
        return json($data);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,187 +0,0 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace app\controller\api;
 | 
			
		||||
 | 
			
		||||
use app\BaseController;
 | 
			
		||||
use app\model\Order;
 | 
			
		||||
use app\model\User;
 | 
			
		||||
 | 
			
		||||
class OrderController extends BaseController
 | 
			
		||||
{
 | 
			
		||||
    // 查询订单
 | 
			
		||||
    public function getOrders()
 | 
			
		||||
    {
 | 
			
		||||
        $query = $this->request->get();
 | 
			
		||||
        $orders = Order::serchOrders($query)->order('id', 'desc')->paginate(['list_rows' => $query['limit'], 'page' => $query['page']]);
 | 
			
		||||
        if ($orders) {
 | 
			
		||||
            return json(['code' => 0, 'msg' => 'OK', 'count' => $orders->total(), 'data' => $orders->items()]);
 | 
			
		||||
        } else {
 | 
			
		||||
            return json(['code' => 1, 'msg' => '无数据记录', 'count' => 0, 'data' => []]);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // 修改订单支付状态
 | 
			
		||||
    public function changeOrderState()
 | 
			
		||||
    {
 | 
			
		||||
        $info = $this->request->post();
 | 
			
		||||
        $uporder_res = Order::update(['state' => $info['state'], 'id' => $info['id']]);
 | 
			
		||||
        if ($uporder_res) {
 | 
			
		||||
            return json(\backMsg(0, '修改成功'));
 | 
			
		||||
        } else {
 | 
			
		||||
            return json(\backMsg(1, '修改失败'));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // 手动补单
 | 
			
		||||
    public function doPayOrder()
 | 
			
		||||
    {
 | 
			
		||||
        $info = $this->request->post();
 | 
			
		||||
        // 修改支付状态
 | 
			
		||||
        $order = Order::find($info['id']);
 | 
			
		||||
        $order->state = $info['state'];
 | 
			
		||||
        $res = $order->save();
 | 
			
		||||
        if ($res) {
 | 
			
		||||
            // 创建通知
 | 
			
		||||
            $notify = self::crateNotify($order);
 | 
			
		||||
            // 字符串签名
 | 
			
		||||
            $user_key = User::where('pid', $order->pid)->value('secret_key');
 | 
			
		||||
            $sign = self::getSign($notify, $user_key);
 | 
			
		||||
            $notify['sign'] = $sign;
 | 
			
		||||
            // 异步通知
 | 
			
		||||
            $res_notify = self::getHttpResponse($order->notify_url . '?' . http_build_query($notify));
 | 
			
		||||
            if ($res_notify === 'success') {
 | 
			
		||||
                return json(\backMsg(0, '订单通知成功'));
 | 
			
		||||
            } else {
 | 
			
		||||
                return json(\backMsg(1, '异步通知失败'));
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            return json(\backMsg(1, '支付状态修改失败'));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // 重新通知
 | 
			
		||||
    public function redoPayOrder()
 | 
			
		||||
    {
 | 
			
		||||
        $id = $this->request->post('id');
 | 
			
		||||
        // 修改支付状态
 | 
			
		||||
        $order = Order::find($id);
 | 
			
		||||
        if ($order) {
 | 
			
		||||
            // 创建通知
 | 
			
		||||
            $notify = self::crateNotify($order);
 | 
			
		||||
            // 字符串签名
 | 
			
		||||
            $user_key = User::where('pid', $order->pid)->value('secret_key');
 | 
			
		||||
            $sign = self::getSign($notify, $user_key);
 | 
			
		||||
            $notify['sign'] = $sign;
 | 
			
		||||
            // 异步通知
 | 
			
		||||
            $res_notify = self::getHttpResponse($order->notify_url . '?' . http_build_query($notify));
 | 
			
		||||
            if ($res_notify === 'success') {
 | 
			
		||||
                return json(\backMsg(0, '订单通知成功'));
 | 
			
		||||
            } else {
 | 
			
		||||
                return json(\backMsg(1, '异步通知失败'));
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            return json(\backMsg(1, '订单不存在'));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // 删除订单
 | 
			
		||||
    public function deleteOrder()
 | 
			
		||||
    {
 | 
			
		||||
        $id = $this->request->post('id');
 | 
			
		||||
        $del_res = Order::destroy($id);
 | 
			
		||||
        if ($del_res) {
 | 
			
		||||
            return json(\backMsg(0, '删除成功'));
 | 
			
		||||
        } else {
 | 
			
		||||
            return json(\backMsg(1, '删除失败'));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // 批量删除订单
 | 
			
		||||
    public function batchRemove()
 | 
			
		||||
    {
 | 
			
		||||
        $ids = $this->request->post('ids');
 | 
			
		||||
        if (!$ids) {
 | 
			
		||||
            return json(\backMsg(1, '参数错误'));
 | 
			
		||||
        }
 | 
			
		||||
        $del_res = Order::destroy($ids);
 | 
			
		||||
        if ($del_res) {
 | 
			
		||||
            return json(\backMsg(0, '删除成功'));
 | 
			
		||||
        } else {
 | 
			
		||||
            return json(\backMsg(1, '删除失败'));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // 清空超时订单
 | 
			
		||||
    public function batchTimeout()
 | 
			
		||||
    {
 | 
			
		||||
        $ids = Order::scope('timeoutOrder')->column('id');
 | 
			
		||||
        if (!$ids) {
 | 
			
		||||
            return json(\backMsg(1, '无过期订单'));
 | 
			
		||||
        }
 | 
			
		||||
        $batch_del_res = Order::destroy($ids);
 | 
			
		||||
        if ($batch_del_res) {
 | 
			
		||||
            return json(\backMsg(0, '清理成功'));
 | 
			
		||||
        } else {
 | 
			
		||||
            return json(\backMsg(1, '清理失败'));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // 签名方法
 | 
			
		||||
    private static function getSign(array $param = [], string $key = ''): string
 | 
			
		||||
    {
 | 
			
		||||
        if (!$param)
 | 
			
		||||
            return '参数错误';
 | 
			
		||||
        if (!$key)
 | 
			
		||||
            return '密钥错误';
 | 
			
		||||
        ksort($param);
 | 
			
		||||
        reset($param);
 | 
			
		||||
        $signstr = '';
 | 
			
		||||
        foreach ($param as $k => $v) {
 | 
			
		||||
            if ($k != "sign" && $k != "sign_type" && $v != '') {
 | 
			
		||||
                $signstr .= $k . '=' . $v . '&';
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        $signstr = substr($signstr, 0, -1);
 | 
			
		||||
        $signstr .= $key;
 | 
			
		||||
        $sign = md5($signstr);
 | 
			
		||||
        return $sign;
 | 
			
		||||
    }
 | 
			
		||||
    // 构建通知参数
 | 
			
		||||
    private static function crateNotify($param): array
 | 
			
		||||
    {
 | 
			
		||||
        $notify = [
 | 
			
		||||
            'pid' => $param->pid,
 | 
			
		||||
            'trade_no' => $param->order_id,
 | 
			
		||||
            'out_trade_no' => $param->out_trade_no,
 | 
			
		||||
            'type' => $param->type,
 | 
			
		||||
            'name' => $param->name,
 | 
			
		||||
            'money' => $param->money,
 | 
			
		||||
            'trade_status' => 'TRADE_SUCCESS',
 | 
			
		||||
            'sign_type' => 'MD5',
 | 
			
		||||
        ];
 | 
			
		||||
        // 添加扩展参数
 | 
			
		||||
        $notify = array_merge($notify, unserialize($param->param));
 | 
			
		||||
        return $notify;
 | 
			
		||||
    }
 | 
			
		||||
    // 请求外部资源
 | 
			
		||||
    private static function getHttpResponse($url, $header = [], $post = null, $timeout = 10)
 | 
			
		||||
    {
 | 
			
		||||
        $ch = curl_init($url);
 | 
			
		||||
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
 | 
			
		||||
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
 | 
			
		||||
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
 | 
			
		||||
        if ($header) {
 | 
			
		||||
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
 | 
			
		||||
        } else {
 | 
			
		||||
            $httpheader[] = "Accept: */*";
 | 
			
		||||
            $httpheader[] = "Accept-Language: zh-CN,zh;q=0.8";
 | 
			
		||||
            $httpheader[] = "Connection: close";
 | 
			
		||||
            curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
 | 
			
		||||
        }
 | 
			
		||||
        curl_setopt($ch, CURLOPT_HEADER, false);
 | 
			
		||||
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 | 
			
		||||
        if ($post) {
 | 
			
		||||
            curl_setopt($ch, CURLOPT_POST, true);
 | 
			
		||||
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
 | 
			
		||||
        }
 | 
			
		||||
        $response = curl_exec($ch);
 | 
			
		||||
        curl_close($ch);
 | 
			
		||||
        return $response;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,135 +0,0 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace app\controller\api;
 | 
			
		||||
 | 
			
		||||
use app\BaseController;
 | 
			
		||||
use app\model\PayAccount;
 | 
			
		||||
use app\model\PayChannel;
 | 
			
		||||
use app\model\Platform;
 | 
			
		||||
use app\model\User;
 | 
			
		||||
use think\facade\View;
 | 
			
		||||
 | 
			
		||||
class PayManageController extends BaseController
 | 
			
		||||
{
 | 
			
		||||
    // 获取账号列表
 | 
			
		||||
    public function getPayAccount()
 | 
			
		||||
    {
 | 
			
		||||
        $query = $this->request->get();
 | 
			
		||||
        $accounts = PayAccount::serchAccount($query)->order('id', 'desc')->paginate(['list_rows' => $query['limit'], 'page' => $query['page']]);
 | 
			
		||||
        if ($accounts) {
 | 
			
		||||
            return json(['code' => 0, 'msg' => 'OK', 'count' => $accounts->total(), 'data' => $accounts->items()]);
 | 
			
		||||
        } else {
 | 
			
		||||
            return json(['code' => 1, 'msg' => '无数据记录', 'count' => 0, 'data' => []]);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // 编辑账号
 | 
			
		||||
    public function editAccount()
 | 
			
		||||
    {
 | 
			
		||||
        $info = $this->request->post();
 | 
			
		||||
        $up_res = PayAccount::update($info);
 | 
			
		||||
        if ($up_res) {
 | 
			
		||||
            $acc = PayAccount::find($info['id']);
 | 
			
		||||
            $this->createAccountConfig($acc);
 | 
			
		||||
            return json(\backMsg(0, '修改成功'));
 | 
			
		||||
        } else {
 | 
			
		||||
            return json(\backMsg(1, '修改失败'));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // 账号状态
 | 
			
		||||
    public function accountEnable()
 | 
			
		||||
    {
 | 
			
		||||
        $info = $this->request->post();
 | 
			
		||||
        $up_res = PayAccount::update($info);
 | 
			
		||||
        if ($up_res) {
 | 
			
		||||
            return json(\backMsg(0, '成功'));
 | 
			
		||||
        } else {
 | 
			
		||||
            return json(\backMsg(1, '失败'));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // 删除账号
 | 
			
		||||
    public function delAccount()
 | 
			
		||||
    {
 | 
			
		||||
        $ids = $this->request->post('ids');
 | 
			
		||||
        $res = PayAccount::destroy($ids);
 | 
			
		||||
        $res2 = PayChannel::destroy($ids);
 | 
			
		||||
        if ($res && $res2) {
 | 
			
		||||
            return \json(\backMsg(0, '已删除'));
 | 
			
		||||
        } else {
 | 
			
		||||
            return \json(\backMsg(1, '失败'));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // 添加账号
 | 
			
		||||
    public function addAccount()
 | 
			
		||||
    {
 | 
			
		||||
        $info = $this->request->post();
 | 
			
		||||
        $pid = $this->request->session('pid');
 | 
			
		||||
        $info['pid'] = $pid;
 | 
			
		||||
        $check_acc = PayAccount::where(['account' => $info['account'], 'pid' => $pid])->find();
 | 
			
		||||
        if ($check_acc) {
 | 
			
		||||
            return \json(\backMsg(1, '账号已存在'));
 | 
			
		||||
        }
 | 
			
		||||
        $acc = PayAccount::create($info);
 | 
			
		||||
        if ($acc) {
 | 
			
		||||
            $this->createAccountConfig($acc);
 | 
			
		||||
            return \json(\backMsg(0, '添加成功'));
 | 
			
		||||
        } else {
 | 
			
		||||
            return \json(\backMsg(1, '添加失败'));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // 添加收款终端
 | 
			
		||||
    public function addChannel()
 | 
			
		||||
    {
 | 
			
		||||
        $info = $this->request->post();
 | 
			
		||||
        $res = PayChannel::create($info);
 | 
			
		||||
        if ($res) {
 | 
			
		||||
            return \json(\backMsg(0, '添加成功'));
 | 
			
		||||
        } else {
 | 
			
		||||
            return \json(\backMsg(1, '添加失败'));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // 编辑收款终端
 | 
			
		||||
    public function editChannel()
 | 
			
		||||
    {
 | 
			
		||||
        $info = $this->request->post();
 | 
			
		||||
        $up_res = PayChannel::update($info);
 | 
			
		||||
        if ($up_res) {
 | 
			
		||||
            return json(\backMsg(0, '修改成功'));
 | 
			
		||||
        } else {
 | 
			
		||||
            return json(\backMsg(1, '修改失败'));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // 收款终端列表
 | 
			
		||||
    public function getChannelList()
 | 
			
		||||
    {
 | 
			
		||||
        $aid = $this->request->post('aid');
 | 
			
		||||
        $res = PayChannel::where(['account_id' => $aid])->order('last_time', 'desc')->select();
 | 
			
		||||
        if ($res) {
 | 
			
		||||
            return \json(\backMsg(0, '获取成功', $res));
 | 
			
		||||
        } else {
 | 
			
		||||
            return \json(\backMsg(1, '失败'));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // 生成账号配置
 | 
			
		||||
    private function createAccountConfig($acc)
 | 
			
		||||
    {
 | 
			
		||||
        $platform = Platform::where('platform', $acc->getData('platform'))->find();
 | 
			
		||||
        $user = User::where('pid', $acc->pid)->find();
 | 
			
		||||
        $query = \unserialize($platform->query);
 | 
			
		||||
        $data = [
 | 
			
		||||
            'pid'       =>  $user->pid,
 | 
			
		||||
            'key'       =>  $user->secret_key,
 | 
			
		||||
            'aid'       =>  $acc->id,
 | 
			
		||||
            'platform'  =>  $acc->getData('platform'),
 | 
			
		||||
            'account'   =>  $acc->account,
 | 
			
		||||
            'password'  =>  $acc->password,
 | 
			
		||||
            'payclass'  =>  $platform->class_name,
 | 
			
		||||
            'query'     =>  \var_export($query, \true)
 | 
			
		||||
        ];
 | 
			
		||||
        $config = View::fetch('tpl/account_config', $data);
 | 
			
		||||
        $name = "{$data['pid']}_{$data['aid']}";
 | 
			
		||||
        $path = "../config/payconfig/{$name}.php";
 | 
			
		||||
        \file_put_contents($path, $config);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,63 +0,0 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace app\controller\api;
 | 
			
		||||
 | 
			
		||||
use app\BaseController;
 | 
			
		||||
use app\model\Platform;
 | 
			
		||||
use think\facade\View;
 | 
			
		||||
 | 
			
		||||
class PluginController extends BaseController
 | 
			
		||||
{
 | 
			
		||||
    // 插件列表
 | 
			
		||||
    public function getPluginList()
 | 
			
		||||
    {
 | 
			
		||||
        $query = $this->request->get();
 | 
			
		||||
        $data = Platform::order('id', 'desc')->paginate(['list_rows' => $query['limit'], 'page' => $query['page']]);
 | 
			
		||||
        if ($data) {
 | 
			
		||||
            return json(['code' => 0, 'msg' => 'OK', 'count' => $data->total(), 'data' => $data->items()]);
 | 
			
		||||
        } else {
 | 
			
		||||
            return json(['code' => 1, 'msg' => '无数据记录', 'count' => 0, 'data' => []]);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // 插件启用
 | 
			
		||||
    public function pluginEnable()
 | 
			
		||||
    {
 | 
			
		||||
        $info = $this->request->post();
 | 
			
		||||
        $up_res = Platform::update($info);
 | 
			
		||||
        if ($up_res) {
 | 
			
		||||
            return json(\backMsg(0, '成功'));
 | 
			
		||||
        } else {
 | 
			
		||||
            return json(\backMsg(1, '失败'));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    // 插件选项
 | 
			
		||||
    public function pluginOption()
 | 
			
		||||
    {
 | 
			
		||||
        // 加载平台配置
 | 
			
		||||
        $platform = \think\facade\Config::load("extendconfig/platform", 'extendconfig');
 | 
			
		||||
        $option = [];
 | 
			
		||||
        foreach ($platform as $key => $value) {
 | 
			
		||||
            $option[] = ['platform' => $key, 'name' => $value];
 | 
			
		||||
        }
 | 
			
		||||
        return json($option);
 | 
			
		||||
    }
 | 
			
		||||
    // 生成插件配置
 | 
			
		||||
    public function crtPlfConfig()
 | 
			
		||||
    {
 | 
			
		||||
        $info = Platform::where('state', 1)->field('platform, name')->select()->toArray();
 | 
			
		||||
        $data = [];
 | 
			
		||||
        foreach ($info as $value) {
 | 
			
		||||
            $data[$value['platform']] = $value['name'];
 | 
			
		||||
        }
 | 
			
		||||
        $config = View::fetch('tpl/platform_config', $data);
 | 
			
		||||
        $path = "../config/extendconfig/platform.php";
 | 
			
		||||
        $res = \file_put_contents($path, $config);
 | 
			
		||||
        if ($res) {
 | 
			
		||||
            return \json(\backMsg(msg: '创建成功'));
 | 
			
		||||
        } else {
 | 
			
		||||
            return \json(\backMsg(1, '创建成功'));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,79 +0,0 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace app\controller\api;
 | 
			
		||||
 | 
			
		||||
use app\BaseController;
 | 
			
		||||
use think\facade\Session;
 | 
			
		||||
use app\model\User;
 | 
			
		||||
 | 
			
		||||
class UserController extends BaseController
 | 
			
		||||
{
 | 
			
		||||
    protected $middleware = ['Auth' => ['except' => ['login']]];
 | 
			
		||||
 | 
			
		||||
    public function login()
 | 
			
		||||
    {
 | 
			
		||||
        $login_info = $this->request->post();
 | 
			
		||||
        $userinfo = self::checkUser($login_info);
 | 
			
		||||
        if ($userinfo['code'] === 0) {
 | 
			
		||||
            Session::set('userid', $userinfo['data']->id);
 | 
			
		||||
            Session::set('pid', $userinfo['data']->pid);
 | 
			
		||||
            Session::set('nickname', $userinfo['data']->nickname);
 | 
			
		||||
            Session::set('userrole', $userinfo['data']->role);
 | 
			
		||||
            Session::set('islogin', true);
 | 
			
		||||
            return json(\backMsg(0, 'ok'));
 | 
			
		||||
        } else {
 | 
			
		||||
            return json($userinfo);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    public function logout()
 | 
			
		||||
    {
 | 
			
		||||
        Session::clear();
 | 
			
		||||
        return json(\backMsg(0, '注销成功'));
 | 
			
		||||
    }
 | 
			
		||||
    public function editUser()
 | 
			
		||||
    {
 | 
			
		||||
        $userid = \session('userid');
 | 
			
		||||
        $info = $this->request->post();
 | 
			
		||||
        $res = User::update($info, ['id' => $userid]);
 | 
			
		||||
        if (!$res) {
 | 
			
		||||
            return json(\backMsg(1, '修改失败'));
 | 
			
		||||
        }
 | 
			
		||||
        return json(\backMsg(0, '重置成功'));
 | 
			
		||||
    }
 | 
			
		||||
    public function resetKey()
 | 
			
		||||
    {
 | 
			
		||||
        $userid = \session('userid');
 | 
			
		||||
        $res = User::update(['secret_key' => $this->generateKey()], ['id' => $userid]);
 | 
			
		||||
        if (!$res) {
 | 
			
		||||
            return json(\backMsg(1, '重置失败'));
 | 
			
		||||
        }
 | 
			
		||||
        return json(\backMsg(0, '重置成功'));
 | 
			
		||||
    }
 | 
			
		||||
    private function checkUser(array $login_info): array
 | 
			
		||||
    {
 | 
			
		||||
        $username = $login_info['username'];
 | 
			
		||||
        $password = $login_info['password'];
 | 
			
		||||
        $userinfo = User::where('username', $username)->find();
 | 
			
		||||
        if ($userinfo) {
 | 
			
		||||
            if ($password === $userinfo->password) {
 | 
			
		||||
                return ['code' => 0, 'data' => $userinfo];
 | 
			
		||||
            } else {
 | 
			
		||||
                return \backMsg(1, '登陆密码错误');
 | 
			
		||||
            }
 | 
			
		||||
        } else {
 | 
			
		||||
            return \backMsg(2, '用户不存在');
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    private function generateKey()
 | 
			
		||||
    {
 | 
			
		||||
        $bytes = openssl_random_pseudo_bytes(16, $strong);
 | 
			
		||||
        if ($strong) {
 | 
			
		||||
            $key = bin2hex($bytes);
 | 
			
		||||
            return md5($key);
 | 
			
		||||
        } else {
 | 
			
		||||
            return false;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user