<repo_name>callme119/thinkphp-guide<file_sep>/Application/Wjy/Controller/SampleController.class.php
<?php
namespace Wjy\Controller;

use Think\Controller;
use Wjy\Model\UserModel;

Class SampleController extends Controller
{
	public function indexAction()
	{
		$UserM = new UserModel();
		$users = $UserM->getLists();
		dump($users);
		$this->display();
	}

}
<file_sep>/Application/User/Model/Index/addModel.class.php
<?php
namespace User\Model\Index;

class addModel
{
	protected $id = 0;
	protected $userName = "";
	protected $password = "";
	protected $user = array();

	public function setUser($user)
	{
		$this->user = $user;
	}

	public function getId()
	{
		return $this->user['id'];
	}
	public function getUserName()
	{
		return $this->user['username'];
	}
	public function getPassword()
	{
		return $this->user['password'];
	}
}
<file_sep>/Application/User/Logic/UserLogic.class.php
<?php
namespace User\Logic;
use User\Model\UserModel;
class UserLogic extends UserModel
{
	protected $pageShow = 0;
	protected  $errors = array();

	public function getErrors()
	{
		return $this->errors;
	}

	public function getListByName($name)
	{
		//判断是否是字符串
		if (is_string($name)!==true) {
			$this->errors[] = "传入变量类型非string";
			return false;
		}

		//去空格
		$name=trim((string)$name, " ");

		
		$map['name'] = $name;
		$status=$this->create($map,4);

		//判断状态
		if (!$status)
		{
			$this->errors[]=$this->getError();
			return false;
		}

		$data = $this->where($map)->find();
		return $data;
	}

	public function getListById($id)
	{
		$map['id'] = $id;
		$data = $this->where($map)->find();
		return $data;
	}


	public function getPageShow(){
		return $this->pageShow;
	}
	public function deleteInfo($id)
	{
		$map['id'] = $id;
		$datas=$this->where($map)->delete();
		return $datas;
	}
	public function addList($list)
	{
		try{
			if($this->create($list))
			{
				$id=$this->add();
				return $id;
			}
			else
			{
				$this->errors[]=$this->getError();
				return false;
			}
		}
		catch(\Think\Exception $e)
		{
			$this->errors[]=$e->getMessage();
			return false;
		}
	}
	public function saveList($list){
		try{
			if($this->create($list))
			{
				$id=$this->save();
				return $id;
			}
			else
			{
				$this->errors[]=$this->getError();
				return false;
			}
		}
		catch(\Think\Exception $e)
		{
			$this->errors[]=$e->getMessage();
			return false;
		}
	}
}<file_sep>/Application/Login/Controller/IndexController.class.php
<?php
namespace Login\Controller;
use Think\Controller;
use User\Logic\UserLogic;		//用户表
class IndexController extends Controller {
    public function indexAction(){
   		$this->display();
    }

    public function loginAction()
    {
    	//取用户名
    	$userName = I('post.name');

    	//抓取用户名
    	$UserL = new UserLogic();
    	$user = $UserL->getListByName($userName);

        //判断用户名是否合法
        if($user===false){
            $errors = $UserL->getErrors();
            $this->error("操作错误".'<br/>'.implode('<br/>', $errors));
            //数组变字符串 implode()
        }

    	//判断是否有此用户
    	if($user == null)
    	{
    		echo "the username is not ";
    		exit();
    	}
    	//取用户传入密码
    	$userPassword = I('<PASSWORD>');

    	//判断密码正确性
    	if($userPassword != $user['password'])
    	{
    		echo "wrong password";
    		exit();
    	}

    	//seesion userId
    	session('userId',$user['id']);  

    	//跳转至首页
    	$this->success("操作成功" , U('Home/Index/index'));
    }

    public function logoutAction()
    {
    	session('userId',null); // 删除name
    	$this->success("注销成功",U('Login/Index/index'));
    }
}<file_sep>/Application/User/Controller/SampleController.class.php
<?php
namespace User\Controller;

use Think\Controller;
use Wjy\Model\UserModel;
use User\Logic\UserLogic;		//用户表
use User\Model\Index\indexModel;  

Class SampleController extends Controller
{
	public function indexAction()
	{
		//获取列表
        $UserM = new UserModel();
		$users = $UserM->getLists();
		echo $UserM->getLastSql();
		dump($users);

		

        $IndexModel = new indexModel();
     
        $IndexModel->setUsers($users);

        //传入列表
    	$this->assign('M',$IndexModel);
        
		$this->display();
	}

}
<file_sep>/Application/Home/Controller/IndexController.class.php
<?php
namespace Home\Controller;
use Think\Controller;
use User\Logic\UserLogic;		//
class IndexController extends Controller
{
	public function indexAction()
	{
		//判断用户是否登陆
		$userId = I('session.userId');
		if($userId == '')
		{
			$this->success("请登录",U('Login/Index/index'));
			exit();
		}
		
		//取用户信息
		$UserL = new UserLogic();
		$user = $UserL->getListById($userId);
		
		//传值 
		$this->assign("user",$user);


		$this->display();
	}
}<file_sep>/Application/Deng/Logic/wechatMenuapiLogic.class.php
<?php
namespace Admin\Logic;

use Admin\Controller\IndexController;

class wechatMenuapiLogic extends IndexController {
	 private $access_token;
    
    public function setAccessToken($access_token){
    	$this->access_token = $access_token;
    }

    public function createMenu(){        
        $jsonmenu = 
        '{
     "button":[
     {	
          "type":"click",
          "name":"我要住店",
          "key":"hotel"
      },
      {	
          "type":"click",
          "name":"我要旅游",
          "key":"tour"
      },
      {
           "name":"看这里",
           "sub_button":[
           {	
               "type":"click",
               "name":"客房环境",
               "key" : "environment"
            },
            {
              "type":"click",
               "name":"酒店介绍",
               "key" : "description"
            },
            {
               "type":"view",
               "name":"一键导航",
               "url":"https://www.baidu.com"
            },
            {
               "type":"view",
               "name":"全景360",
               "url":"https://www.baidu.com"
            }]
      	 }]
 		}';
        $url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$this->access_token;
        echo "hello";
        $result = $this->https_request($url, $jsonmenu);
        var_dump($result);
        
    }
    
    private function https_request($url,$data = null){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if(!empty($data)){
        	curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }
}<file_sep>/Application/Wjy/Model/UserModel.class.php
<?php
namespace Wjy\Model;

use Wjy\Model\YunzhiModel;

class UserModel extends YunzhiModel
{

}<file_sep>/Application/User/Model/Index/indexModel.class.php
<?php
namespace User\Model\Index;

use User\Model\UserModel;
use UserPost\Model\UserPostModel;
use Post\Model\PostModel;
use UserPost\Model\UserPostViewModel;

class indexModel
{
	protected $pageShow = ""; //xxxxx
	protected $users = array();//xxxxxx
	public function setPageShow($pageShow)
	{
		$this->pageShow = (string)$pageShow;
	}

	public function getPageShow()
	{
		$pageShow = "<a href=" . U('?p=2');
	}

	public function setUsers($users)
	{
		$this->users = $users;
	}

	public function getUsers()
	{
		return $this->users;
	}

	public function getPostsByUserId($userId)
	{
		$userId = (int)$userId;

		$UserPostViewM = new UserPostViewModel();
		$lists = $UserPostViewM->where("user_id = $userId")->select();
		// echo $UserPostM->getLastSql();
		return $lists;
	}

	public function getNameByPostId($postId)
	{
		$postId = (int)$postId;

		$PostM = new PostModel();
		$list = $PostM->where("id = $postId")->find();
		return $list['name'];
	}
}<file_sep>/Application/Deng/Controller/IndexController.class.php
<?php
namespace Deng\Controller;

use Think\Controller;
use Deng\Logic\wechatInterfaceapiLogic;
use Deng\Logic\wechatMenuapiLogic;
use Deng\Logic\CustomMenuLogic;
use Deng\Model\CustomMenuModel;

class IndexController extends Controller {
   public function createMenu(){
   	//调用微信接口获取access_token等基本信息
		$appid = C("APPID");
		$appsecret = C("APPSECRET");
		$wechatInterface = new wechatInterfaceapiLogic();
		$access_token = $wechatInterface->getAccessToken($appid,$appsecret);

		//创建菜单
		$wechatMenu = new wechatMenuapiLogic();
		$wechatMenu->setAccessToken($access_token);
		$wechatMenu->createMenu();
   }

   //自定义菜单查询
   public function indexAction(){
   		$MenuL = new CustomMenuLogic();

        //获取列表
        $menu = $MenuL->getLists();

        $MenuM = new CustomMenuModel();
        $MenuM->setMenus($menu);

        //传入列表
    	$this->assign('M',$MenuM);
        
    	//调用V层
   		$this->display();
   }

   //自定义菜单修改
   public function editAction(){
   	 	//获取菜单ID
        $menuId = I('get.id');

        //取菜单信息，并且将pid换位上级菜单名称
        $MenuL = new CustomMenuLogic();
		$menu = $MenuL->getListById($menuId);
		$menu['pid'] = $MenuL->getListById($menu['pid'])['title'];

        $MenuM = new CustomMenuModel();
        $MenuM->setMenu($menu);

        //传入列表
    	$this->assign('M',$MenuM);

        //显示 display('add')
        $this->display('add'); 
   }

   //自定义菜单更新
   public function updateAction(){
   		 //取用户信息
        $data = I('post.');

        //传给M层
        $MenuL = new CustomMenuLogic();
        $MenuL->saveList($data);

        //判断异常
        if(count($errors=$MenuL->getErrors())!==0)
        {
            //数组变字符串
            $error =implode('<br/>', $errors);
            
            
            //显示错误
             $this->error("添加失败，原因：".$error,U('User/Index/index'));

             return false;
            
        }
            $this->success("操作成功" , U('User/Index/index'));
   }
   //自定义菜单添加
   public function addAction(){
   		$MenuL = new CustomMenuLogic();
   		$titles = $MenuL->getTitles();

        //传入列表
   		$MenuM = new CustomMenuModel();
   		$MenuM->setTitles($titles);
    	$this->assign('M',$MenuM);

   		//显示 display
        $this->display();
   }

   //自定义菜单保存
   public function saveAction(){
   		 //取用户信息
        $menu = I('post.');

        //添加 add()
        $MenuL = new CustomMenuLogic();
        $MenuL->addList($menu);

        //判断异常
        if(count($errors=$MenuL->getErrors())!==0)
        {
            //数组变字符串
            $error =implode('<br/>', $errors);
            
            
            //显示错误
            $this->error("添加失败，原因：".$error,U('User/Index/index'));
            
        }
        $this->success("操作成功" , U('index'));
   }

   //自定义菜单删除
   public function deleteAction(){
   		$menuId = I('get.id');

        $MenuL = new CustomMenuLogic();
        $status = $MenuL->deleteInfo($menuId);

        if($status！==false){
           $this->success("删除成功", U('index')); 
        }
        else{
            $this->error("删除失败" , U('index'));
        }
   }
}<file_sep>/Application/UserPost/Model/UserPostViewModel.class.php
<?php 
namespace UserPost\Model;
use Think\Model\ViewModel;
class UserPostViewModel extends ViewModel {
	public $viewFields = array(
		'UserPost'=>array('id', 'user_id', 'post_id'),
		'User'=>array('name'=>"user_name", '_on'=>'UserPost.user_id=User.id'),
		'Post'=>array('name'=>"post_name", '_on'=>'UserPost.post_id=Post.id'),
		);
}<file_sep>/Application/UserPost/Model/UserPostModel.class.php
<?php
namespace UserPost\Model;

use Think\Model;

class UserPostModel extends Model{
	
}<file_sep>/Application/Deng/Logic/CustomMenuLogic.class.php
<?php
namespace Deng\Logic;

use Deng\Model\CustomMenuModel;

class CustomMenuLogic extends CustomMenuModel
{
	public function getErrors()
	{
		return $this->errors;
	}

	public function getListById($id)
	{
		$map['id'] = $id;
		$data = $this->where($map)->find();
		return $data;
	}

	public function getListByName($name)
	{
		//判断是否是字符串
		if (is_string($name)!==true) {
			$this->errors[] = "传入变量类型非string";
			return false;
		}

		//去空格
		$name=trim((string)$name, " ");

		
		$map['title'] = $name;
		$data = $this->where($map)->find();
		return $data;
	}

	public function getLists($status=0)
	{
		try
		{
			$lists=$this->select();

			return $lists;
		}
		catch(\Think\Exception $e)
		{
			$this->errors[]=$e->getMessage();
			return false;
		}

	}

	public function deleteInfo($id)
	{
		$map['id'] = $id;
		$datas = $this->where($map)->delete();
		return $datas;
	}

	public function addList($list)
	{
		try{
			//将ptitle转换为pid
			$ptitle = $list['pid'];
			$list['pid'] = $this->getListByName($ptitle)['id'];

			if($this->create($list))
			{
				$data = $this->add();

				return $data;
			}
			else
			{
				$this->errors[]=$this->getError();
				return false;
			}
		}
		catch(\Think\Exception $e)
		{
			$this->errors[]=$e->getMessage();
			return false;
		}
	}
	public function saveList($list){
		try{
			if($this->create($list))
			{
				$id=$this->save();
				return $id;
			}
			else
			{
				$this->errors[]=$this->getError();
				return false;
			}
		}
		catch(\Think\Exception $e)
		{
			$this->errors[]=$e->getMessage();
			return false;
		}
	}

	public function getTitles(){
		try 
		{
			$titles = array();
			$data = $this->getLists();
			foreach ($data as $key => $value) {
				$titles[] = $value['title'];
			}
			return $titles;
		}
		catch(\Think\Exception $e)
		{
			$this->errors[]=$e->getMessage();
			return false;
		}
	}
}<file_sep>/Application/User/Controller/IndexController.class.php
<?php
namespace User\Controller;

use Think\Controller;
use User\Logic\UserLogic;		//用户表
use User\Model\Index\indexModel;        //

class IndexController extends Controller
{
    public function indexAction(){   	

        //获取列表
        $UserL = new UserLogic();
        $users = $UserL->getLists();

        //获取分页信息
        $page = $UserL->__construct();

        //判断异常
        if(count($errors=$UserL->getErrors())!==0)
        {
            //数组变字符串
            $error = implode('<br/>', $errors);
             
            //显示错误
             $this->error("查找失败，原因：".$error,U('Home/Index/index'));

             return false;
        }

        $IndexModel = new indexModel();
        $IndexModel->setPageShow($page);
        $IndexModel->setUsers($users);

        //传入列表
    	$this->assign('M',$IndexModel);
        
    	//调用V层
   		$this->display();
    }
    // public function detailAction(){
    // 	//取用户ID
    // 	$userId = I('get.id');

    // 	//抓取用户信息
    // 	$UserL = new UserLogic();
    // 	$user = $UserL->getListById($userId);

    //     //传值
    //     $this->assign('user',$user);

    // 	$this->display();
    // }
    // public function addAction(){
    //     //显示 display
    //     $this->display();
    // }
    // public function saveAction(){
    //     //取用户信息
    //     $user = I('post.');

    //     //添加 add()
    //     $UserL = new UserLogic();
    //     $UserL->addList($user);

    //     //判断异常
    //     if(count($errors=$UserL->getErrors())!==0)
    //     {
    //         //数组变字符串
    //         $error =implode('<br/>', $errors);
            
            
    //         //显示错误
    //          $this->error("添加失败，原因：".$error,U('User/Index/index?p='.I('get.p')));
            
    //     }
    //     $this->success("操作成功" , U('User/Index/index?p='.I('get.p')));    
    // }
    // public function editAction(){
    //     //获取用户ID
    //     $userId = I('get.id');

    //     //取用户信息 getListById()
    //     $UserL = new UserLogic();
    //     $user = $UserL->getListById($userId);

    //     //传给前台
    //     $this->assign('user',$user);
        
    //     //显示 display('add')
    //     $this->display('add'); 
    // }
    // public function updateAction(){
    //     //取用户信息
    //     $data = I('post.');

    //     //传给M层
    //     $UserL = new UserLogic();
    //     $UserL->saveList($data);

    //     //判断异常
    //     if(count($errors=$UserL->getErrors())!==0)
    //     {
    //         //数组变字符串
    //         $error =implode('<br/>', $errors);
            
            
    //         //显示错误
    //          $this->error("添加失败，原因：".$error,U('User/Index/index?p='.I('get.p')));

    //          return false;
            
    //     }
    //         $this->success("操作成功" , U('User/Index/index?id=', I('get.')));
    // }
    // public function deleteAction(){

    //     $userId = I('get.id');

    //     $UserL = new UserLogic();
    //     $status = $UserL->deleteInfo($userId);

    //     if($status！==false){
    //        $this->success("删除成功", U('User/Index/index?p='.I('get.p'))); 
    //     }
    //     else{
    //         $this->error("删除失败" , U('User/Index/index?p='.I('get.p')));
    //     }
    // }
}<file_sep>/Application/Post/Model/PostModel.class.php
<?php
namespace Post\Model;

use Think\Model;

class PostModel extends Model{
	
}