<?php

namespace app\api\controller;

use app\models\page\PageModel;
use app\Request;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

/**
 * 公共类
 * Class PublicController
 * @package app\api\controller
 */
class PublicController
{
    /**
     * @param Request $request
     * @return mixed
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function index(Request $request)
    {
        $banner = sys_data('home_banner', 0, 1) ?: [];//TODO 首页轮播图
        $menus = sys_data('home_menus', 0, 1) ?: [];//TODO 首页导航
        $pics = sys_data('home_pics', 0, 1) ?: [];//TODO 首页展示图
        $tengxun_map_key = sys_config('tengxun_map_key', '');
        return app('json')->successful(compact('banner', 'menus', 'pics', 'tengxun_map_key'));
    }

    public function page($unique)
    {
        $info = PageModel::where('is_del', 0)->where('unique', $unique)->find()->toArray();
        return app('json')->successful($info);
    }


    /**
     * 获取分享配置
     * @param Request $request
     * @return mixed
     */
    public function share(Request $request)
    {
        $mer_id = $request->mer_id();
        $data['img'] = sys_config('wechat_share_img', '', $mer_id);
        if (strstr($data['img'], 'http') === false) $data['img'] = sys_config('site_url', '', $mer_id) . $data['img'];
        $data['img'] = str_replace('\\', '/', $data['img']);
        $data['title'] = sys_config('wechat_share_title', '', $mer_id);
        $data['synopsis'] = sys_config('wechat_share_synopsis', '', $mer_id);
        return app('json')->successful(compact('data'));
    }
}