<?php

namespace App\Http\Middleware;

use App\Consts\ErrorConst;
use App\Libs\Utils;
use App\Models\Channel\Channel;
use App\Models\Channel\ChannelUser;
use Illuminate\Support\Facades\DB;

trait CheckTokenTrait
{
    public function checkTokenTrait($token)
    {
        // 获取用户信息
        $user = DB::table('mp_manage_users')->where('token', $token)->first();
        $uid  = (int)getProp($user, 'id');
        if (!$uid) {
            Utils::throwError(ErrorConst::NOT_LOGIN);
        }

        // 将数据绑定到全局
        $site           = app('siteData');
        $site->uid      = $uid;
        $site->role     = getProp($user, 'role');
        $site->mobile    = getProp($user, 'mobile');
        $site->nickname = getProp($user, 'nickname');
        $site->token    = $token;
    }
}