<?php
namespace Wechat\Controller;
use Think\Controller;
header("Content-Type:text/html;charset=utf-8");
class StoreController extends OAuthController {
	
    public function index(){
        $signPackage = $this->getJssdkConfig();
        $this->assign("signPackage",$signPackage);
        $this->display();
    }
    //入驻申请
    public function storeEnter(){ 
    	$openid = cookie("openid");
        $result = $this->isApply($openid);//查询是否申请入驻
        if($result['status'] == 1){//待审核
            echo "审核中";
        }elseif ($result['status'] == 2) {//已通过
            redirect("storeCenter");//商家中心
        }elseif ($result['status'] == -1){//未通过
            echo "入驻申请未通过，请重新申请，原因:".$result['reason'];
            $signPackage = $this->getJssdkConfig();
            $this->assign("signPackage",$signPackage);   
            $this->display(); 
            //redirect("storeEnter");
        }else{
            $signPackage = $this->getJssdkConfig();
            $this->assign("signPackage",$signPackage);   
            $this->display(); 
        }  
    }
    //商家中心
    public function storeCenter(){
        $openid = cookie("openid");
    	$result = $this->isStore($openid);
        if(!$result){
            redirect("storeEnter");
        }
        $signPackage = $this->getJssdkConfig();
        $this->assign("signPackage",$signPackage);   
    	$this->display();
    }
    //订单管理
    public function storeOrder(){
    	$openid = cookie("openid");
        $result = $this->isStore($openid);
        if(!$result){
            redirect("storeEnter");
        }
    	$this->display();
    }
    //商家申请入驻查询
    public function isApply($openid){
        $apply  = M('stores_enter')->where(array('openid'=>$openid))->find();
        $result = array("status" => $apply['status'],"reason" => $apply['reason']);
        return $result;
    }
    //商家查询
    public function isStore($openid){
        $result = M('stores')->where(array('openid'=>$openid))->find();     
        return $result; 
    }
    //jssdk config 参数
    public function getJssdkConfig(){
        vendor('Jssdk.Jssdk');
        $appid = "wx6af8fae629334990"; //sm: wx79072a671d933458
        $appsecret = "896bfa0f37825d6907b9c421c2121bff";//sm: d00816ccd2d1b6f4afcdb1b2e7b57360
        $jssdk = new \JSSDK($appid,$appsecret);
        $signPackage = $jssdk->getSignPackage();
        return $signPackage;
    }
}