<?php

namespace app\admin\controller;

use app\admin\common\Base;
use think\Request;
use think\Db;
use think\Log;
header("Content-type: text/html; charset=utf-8"); 

class Wechatset extends Base
{
	/**
	*初始化
	*/
	protected function _initialize(){
		parent::_initialize();
		/*if(session('admin_user_type') != 1){
	    		$result['erro']=-1;
	    		$result['msg']='权限不足';
	    		Response::create($result,'json')->send();
	    		exit;
	    	}*/
	}
	 /**
     * 小程序资讯信息页面
     */
	public function wechatRealTimeInfo()
    {
        return $this->view->fetch('realTimeInfo');
    }
     /**
     * 小程序添加资讯信息页面
     */
    public function wechatRealTimeInfoAdd()
    {
        return $this->view->fetch('realTimeInfoAdd');
    }
    /**
     * 小程序添加资讯信息页面
     */
    public function wechatRealTimeInfoUpdate(Request $request)
    {
    	$id=$request->get('id');
    	$this->assign('id',$id);
        return $this->view->fetch('realTimeInfoUpdate');
    }
     /**
     * 查询小程序资讯信息
     * @author hjc
     * @date 2024-05-14
     */
    public function getWechatRealTimeInfoList(Request $request){
        $post=$request->param();
        
        $page = isset($post['page']) && !empty($post['page']) ? $post['page'] : 1;//页数
    	$page_size = isset($post['page_size']) && !empty($post['page_size']) ? $post['page_size'] : 30;//每页条数
    	//搜索
    	$where=[];
    	if(isset($post['title']) && !empty($post['title'])){
    		$where['title_plain']=['like','%'.$post['title'].'%'];
    	}
        //升序获取菜单列表
        $res = Db::name('wechat_real_time_info')
        		->field('id,title_plain,thumbnail,excerpt_plain,url,create_time')
        		->where($where)
        		->order('create_time desc')
		    	->paginate($page_size,false,['page'=>$page]);
        $lists=$res->items();
    	$start=($page-1)*$page_size;
    	foreach($lists as  $k=>$v){
    		$start+=1;
    		$lists[$k]['no_id']=$start;
    	}
       $result['lists'] =$lists;
    	$result['lastPage'] = $res->lastPage();//总页数
	$result['currentPage'] = $res->currentPage();//当前页
//	$result['total'] = $res->total();//数据总条数
	$result['erro'] = 0;
	$result['msg'] = '查询成功';
	return json($result);
    }
    public function wechatRecordList()
    {
        return $this->view->fetch('wechatRecordList');
    }
   /**
     * 删除小程序资讯信息
     * @author hjc
     * @date 2024-05-14
     */
    public function delWechatRealTimeInfo(Request $request){
        $post=$request->param();
        if(!isset($post['id']) || empty($post['id']) ){
			$result['erro'] = -1;
		        $result['msg'] = "请选择要删除的记录";
			return json($result);
		}
		$id=Db::name('wechat_real_time_info')->where('id',$post['id'])->value('id');
		if(empty($id)){
			$result['erro'] = -1;
		        $result['msg'] = "该记录异常，无法删除";
			return json($result);
		}
    		$res=Db::name('wechat_real_time_info')->where('id',$id)->delete();
    		if($res > 0){
			$result['erro'] = 0;
			$result['msg'] = '删除成功';
		}else{
			$result['erro'] = -1;
			$result['msg'] = '删除失败';
		}
		return json($result);
    }
    /***
	*保存小程序资讯信息
	**/
	public function saveWechatRealTimeInfo(Request $request){
		$post=$request->param();
		
		if(!isset($post['title_plain']) || empty($post['title_plain']) ){
			$result['erro'] = -1;
		        $result['msg'] = "未传入标题";
			return json($result);
		}
		if(!isset($post['thumbnail']) || empty($post['thumbnail']) ){
			$result['erro'] = -1;
		        $result['msg'] = "未传入图片";
			return json($result);
		}
		if(!isset($post['excerpt_plain']) || empty($post['excerpt_plain']) ){
			$result['erro'] = -1;
		        $result['msg'] = "未传入简介";
			return json($result);
		}
		if(!isset($post['url']) || empty($post['url'])){
			$result['erro'] = -1;
		        $result['msg'] = "未传入资讯跳转链接";
			return json($result);
		}
		//提交数据
		$data=[];
		$data['title_plain']=$post['title_plain'];//标题
		$data['thumbnail']=$post['thumbnail'];//图片
		$data['excerpt_plain']=$post['excerpt_plain'];//介绍
		$data['url']=$post['url'];//跳转连接
		if(isset($post['id']) && !empty($post['id'])){
			//修改数据
			$res=Db::name('wechat_real_time_info')->where('id',$post['id'])->update($data);
		}else{
			//保存记录数据
			$data['id']=md5(time().rand(100000,999999));
			$data['create_time']=date('Y-m-d H:i:s');
			$res=Db::name('wechat_real_time_info')->insert($data);
		}
		if($res===false){
			$result['erro'] = -1;
			$result['msg'] = "添加失败";
			return json($result);
		}else{
			$result['erro'] = 0;
			$result['msg'] = "添加成功";
			return json($result);
		}
	}
	/**
	*查询资讯信息详情
	*/
	public function getRealTimeInfoDetail(Request $request){
		$post=$request->param();
		if(!isset($post['id']) || empty($post['id'])){
			$result['erro'] = -1;
		        $result['msg'] = "未传入资讯id";
			return json($result);
		}
		//修改数据
		$result['infro']=Db::name('wechat_real_time_info')->where('id',$post['id'])->find();
		$result['erro'] = 0;
		$result['msg'] = "查询成功";
		return json($result);
	}
}
