rcube_platform.php 2.7 KB
<?php


/**
 * 接入平台
 * @author:dc
 * @time 2022/7/22 14:10
 * Class rcube_email_server_address
 */
class rcube_platform {

    const STATUS_ACTIVE = 1;
    const STATUS_DISABLED = 0;

    private $db;

    /**
     * 表
     * @var string
     */
    public $table;


    /**
     * rcube_platform constructor.
     * @param null $db
     */
    public function __construct($db = null)
    {

        $this->db   =   $db ? $db : rcube::get_instance()->get_dbh();

        $this->table    =   $this->db->table_name('platform',true);
    }


    public function lists(){
        // 查询数据
        $result   =   $this->db->query("select * from ". $this->table);

        return $result->fetchAll(PDO::FETCH_ASSOC);
    }

    /**
     * 查询一条数据
     * @param $id
     * @return array|false
     * @author:dc
     * @time 2022/7/23 14:50
     */
    public function firstById($id)
    {
        // 查询数据
        $result   =   $this->db->query("select * from ". $this->table ." where `id` = ? limit 1",$id);

        return $this->db->fetch_assoc($result);
    }

    /**
     * 查询一条数据
     * @param $appid
     * @return array|false
     * @author:dc
     * @time 2022/7/23 14:50
     */
    public function firstByAppId($appid)
    {
        // 查询数据
        $result   =   $this->db->query("select * from ". $this->table ." where `appid` = ? limit 1",$appid);

        return $this->db->fetch_assoc($result);
    }

    /**
     * 根据appid获取id
     * @param $appid
     * @return int|mixed
     * @author:dc
     * @time 2022/7/23 15:14
     */
    public function getIdByAppId($appid){
        // 查询数据
        $result   =   $this->db->query("select `id` from ". $this->table ." where `appid` = ? limit 1",$appid);

        $row = $this->db->fetch_assoc($result);
        if($row){
            return $row['id'];
        }

        return 0;
    }


    /**
     * 创建验证规则
     * @param $user_id
     * @param $appid
     * @param $appkey
     * @return string
     * @author:dc
     * @time 2022/7/23 9:56
     */
    public static function create_token($user_id,$appid,$appkey)
    {
        // 有效时间一分钟
        $sign = md5("ui={$user_id}&ai={$appid}&ak={$appkey}&t=".date('ymdhi'));

        return strtoupper($sign);
    }

    /**
     * 验证token规则
     * @param $user_id
     * @param $appid
     * @param $sign
     * @return bool
     * @author:dc
     * @time 2022/7/23 10:00
     */
    public static function check_token($user_id,$appid,$sign):bool {
        if($sign){
            $row = (new static())->firstByAppId($appid);
            if($row){
                return self::create_token($user_id,$appid,$row['appkey']) === $sign;
            }
        }
        return false;
    }





}