<?php

namespace Content\Lib;

use Dever;

class Base
{
    # 定义返回数据
    protected $data;

    public function __construct($state = 1)
    {
        if ($state == 2) {
            return;
        }
        # 获取小程序id
        $id = Dever::input('appid');
        if (!$id || $id < 0) {
            Dever::alert('错误的小程序id');
        }
        # 获取小程序基本信息
        $this->data['info'] = Dever::load('set/info-one', $id);
        if (!$this->data['info']) {
            Dever::alert('小程序信息不存在');
        }

        $this->data['info'] = $this->handlePic($this->data['info']);
    }

    /**
     * 替换所有图片地址为https
     *
     * @return mixed
     */
    protected function handlePic($data)
    {
        $config = array
        (
            'pic', 'content', 'logo', 'top', 'img', 'link'
        );

        foreach ($config as $k => $v) {
            if (isset($data[$v]) && $data[$v]) {
                $data[$v] = $this->replacePic($data[$v]);
            }
        }

        return $data;
    }

    /**
     * 替换所有图片地址为https
     *
     * @return mixed
     */
    protected function replacePic($pic)
    {
        $pic = Dever::pic($pic);
        if (strstr($pic, 'http://')) {
            $pic = str_replace('http://', 'https://', $pic);
        }
        
        return $pic;
    }

    /**
     * 将数据中的图片地址进行替换
     *
     * @return mixed
     */
    protected function one($data, $state = 1)
    {
        $data = $this->handlePic($data);
        if ($state == 2 && isset($data['pic']) && $data['pic']) {
            //$data['pic'] = explode(',', $data['pic']);
        }
        if (isset($data['video']) && $data['video']) {
            $data['video'] = Dever::load('content/v1/video.mp4', $data['video']);
        }

        if (isset($data['sdate']) && $data['sdate']) {
            $data['sdate'] = date('m/d', $data['sdate']);
        }

        if (isset($data['link']) && $data['link']) {
            $data['link'] = base64_encode($data['link']);
            $data['link'] = 'https://cm.5dev.cn/applet/content/v1/share.url?link=' . $data['link'];
        }

        if (isset($data['edate']) && $data['edate']) {
            $data['edate'] = date('m/d', $data['edate']);
        }

        if (is_numeric($data['cdate'])) {
            $data['cdate'] = date('Y-m-d', $data['cdate']);
        }

        //$data['cdate'] = Dever::mdate($data['cdate'], 2);
        if (isset($data['author_id'])) {
            $data['author'] = $this->handlePic(Dever::load('set/author-one', $data['author_id']));
            //$data['author']['cdate'] = date('Y.m.d', $data['author']['cdate']);
            $data['author']['cdate'] = $data['cdate'];
        }
        
        return $data;
    }

    /**
     * 验证用户
     *
     * @return mixed
     */
    protected function check_user()
    {
        $uid = Dever::input('uid');
        $session = Dever::input('session');

        $state = $this->check_session($uid, $session);
        if (!$state) {
            Dever::alert('请重新登录');
        }

        return $state;
    }

    /**
     * 生成登录session串 先做个简单的吧
     *
     * @return mixed
     */
    protected function session($uid, $prefix = 'user')
    {
        $secret = md5($prefix . '_' . $uid . '_' . Dever::config('base')->secret);

        return $secret;
    }

    /**
     * 验证登录session串
     *
     * @return mixed
     */
    protected function check_session($uid, $session, $prefix = 'user')
    {
        $secret = $this->session($uid, $prefix);
        if ($secret != $session) {
            return false;
        }
        return true;
    }

    /**
     * 获取对应的类型
     *
     * @return mixed
     */
    public function type($type = 1)
    {
        $config = array
        (
            1 => 'content/news',
            2 => 'content/course',
            3 => 'content/meeting',
            4 => 'comment/review',
        );
        return $config[$type];
    }

    /**
     * 统计数据
     *
     * @return mixed
     */
    public function count($source_table, $source_id, $type, $set)
    {
        $table = $this->type($source_table);
        $count = Dever::db($type)->total(array('option_info_id' => $this->data['info']['id'], 'option_source_table' => $source_table, 'option_source_id' => $source_id));
        $id = Dever::db($table)->update(array('set_' . $set => $count, 'where_id' => $source_id));

        //Dever::db($table)->addReview($source_id);
    }

    /**
     * vote
     *
     * @return mixed
     */
    public function vote($type, $id)
    {
        $state = 2;
        $uid = Dever::input('uid');
        if ($uid > 0) {
            $data = Dever::load('comment/vote-one', array('option_info_id' => $this->data['info']['id'], 'option_source_table' => $type, 'option_source_id' => $id, 'option_uid' => $uid));

            if ($data) {
                $state = 1;
            }
        }
        
        return $state;
    }
}