<?php
/**
* @author 龙虾 <15108148@qq.com>
* @version 2.0 2020-08-18
* @copyright 睿谷信息 <www.rgxx.com>
*/
namespace app\admin\controller;
use think\Controller;
use Request;

class Robots extends Base
{
	public function index()
	{
		if(Request::isPost()) {
			$action = Request::post('action');
			switch ($action) {
				case 'viewlocation':
					if(!file_exists('robots.txt')) $this->error('robots.txt文件不存在！');
					$robots_all = file_get_contents('robots.txt');
					return json(['code' => 1,'msg' =>'获取成功', 'result' => $robots_all]);
					break;
				
				case 'create':
					$robots_txt = explode("\n",Request::post('result'));
					if(empty($robots_txt))  $this->error('内容为空！');
					$robots_all = "";
					foreach ($robots_txt as $v) {
						$robots_all .= $v."\r\n";
					}
					file_put_contents("robots.txt",$robots_all);
					$this->success('设置成功');
					
					break;
			}
		}
		return $this->fetch();
	}
}