<?php

namespace app\admin\controller;

use think\Db;
use app\api\model\Gateway;

/**
 * 通用设置页
 */
class Common extends Base
{
    /**
     * 通用设置页
     * @return mixed
     */
    public function index()
    {
        $setting = Db::table('setting')->find();

        $this->assign('setting', $setting);
        $this->assign('section', 'common');
        return $this->fetch();
    }

    /**
     * 通用设置页
     * @return mixed
     */
    public function system()
    {
        $setting = Db::table('setting')->find();

        $this->assign('setting', $setting);
        $this->assign('section', 'system');
        return $this->fetch();
    }

    /**
     * 保存通用设置
     */
    public function save()
    {
        $post = $this->request->post();
        Db::table('setting')->where('id','>', 0)->update($post);
        $setting = Db::table('setting')->find();
        if ($setting) {
            unset($setting['appsecret']);
            unset($setting['api_address']);
            unset($setting['dirty_words']);
            unset($setting['default_friends']);
            unset($setting['default_groups']);
            unset($setting['getui_appid']);
            unset($setting['getui_appkey']);
            unset($setting['getui_master_secret']);
            unset($setting['getui_api_address']);
                        Gateway::sendToGroup('global', 'settingChange', $setting);
        }
        return $this->json(0);
    }
}
