<?php
/**
 * 微信小程序第三方平台管理控制器
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaoq <570035208@qq.com>
 */
namespace Admin\Controller;
class WxsmallController extends BaseController
{
    public function _initialize(){
        parent::_initialize();
        $this->wx = new \Common\Vendor\Weixinopen;
        $morecode=I("morecode");
       $template= require_once ROOT_PATH."/Public/data/wxsmall_template.php";
        //print_r($template);
        $this->template_array = $template[$morecode];
        $siteid=C('appid');
        $cachename_moresmall='config_moresmall.php';
        $config_morelist=$this->wx->wxcache($cachename_moresmall);
       
        $this->appid=$config_morelist[$siteid.'_'.$morecode]; 
        //file_put_contents('uploads/PAYinit_'.time().'.txt',var_export($this->appid,1));
    }
    /**
     * 小程序一键部署信息
     */
    public function domain(){
        $wxsmall = new \Common\Vendor\Wxsmallopen;
        $param = array('action'=>'get');
        $morecode=I("morecode");
        $domain_list=$wxsmall->modify_domain($param);
        
        
        //file_put_contents('uploads/PAYdomain_get_'.time().'.txt',var_export($domain_list,1));
        if($domain_list['errcode']==0&&$domain_list['errmsg']=='ok'){
            foreach($domain_list as $k=>$v){
                $config[$k] = is_array($v)?implode('|',$v):$v;
            }
        }else{
            $this->error('小程序参数配置错误');
        }
        $SUBMIT_DATA = unserialize(C('WXSMALL_SUBMIT_DATA'));
        $wxsmall_data=array();
        if($SUBMIT_DATA['extAppid']){
            $wxsmall_data=$SUBMIT_DATA;
        }else{
            $wxsmall_data=$SUBMIT_DATA[$this->appid];
        }
        if($wxsmall->small_appid && $wxsmall->small_appid!=$wxsmall_data['extAppid']){$SUBMIT_DATA['extAppid'] = $wxsmall->small_appid;}
       if(empty($wxsmall_data['extAppid'])){
           $wxsmall_data['extAppid']=$this->appid;
       }
        $this->get_qrcode();
        $this->assign('config',$config);
        $this->assign('morecode',$morecode);
        $this->assign('template_array',$this->template_array);
        $this->assign('SUBMIT_DATA',$wxsmall_data);
        $this->assign('VERSION',HANCHAO_WXSMALL_VERSION);
        $this->display('domain');
    }
    /**
     * 小程序上传代码
     */
    public function domain_save(){
        
        $param = I('param.');
        if(empty($param['extAppid'])||empty($param['template_id'])){
            $this->error('必要项不能为空！');
        }
        $wxsmall = new \Common\Vendor\Wxsmallopen;        
        $commit=$wxsmall->commit($param);
        if($commit['errcode']==0&&$commit['errmsg']=='ok'){

            if(empty($param['requestdomain'])){
                $param = array(
                    'action'=>'add',
                    "requestdomain"=>array("https://".BASE_DOMAIN,"https://apis.map.qq.com"),
                    "wsrequestdomain"=>array("wss://".BASE_DOMAIN,"wss://".BASE_DOMAIN),
                    "uploaddomain"=>array("https://".BASE_DOMAIN,"https://".BASE_DOMAIN),
                    "downloaddomain"=>array("https://".BASE_DOMAIN,"https://".BASE_DOMAIN),
                );
                $wxsmall->modify_domain($param);
                $param = array(
                    'action'=>'add',
                    "webviewdomain"=>array("https://".BASE_DOMAIN,"https://".BASE_DOMAIN),
                );
                $wxsmall->modify_webviewdomain($param);
            }
            
            $map = $save = array();
            $map['name'] = 'WXSMALL_SUBMIT_DATA';
            $conf = idtoname('WXSMALL_SUBMIT_DATA','Config','value','name',false);
            $conf = unserialize($conf);
            $other_config=array();
            $other_config['extAppid'] = $param['extAppid'];
            $other_config['template_id'] = $param['template_id'];
            $other_config['tester_list'] = $param['tester_list'];
            $data_config=array();
            if($conf['extAppid']){
                $data_config[$conf['extAppid']]=$conf;
            }else{
                $data_config=$conf;
            }
            
            $data_config[$other_config['extAppid']]=$other_config;
            $save['value'] = serialize($data_config);
            M('Config')->where($map)->save($save);
            $this->success('配置成功！',U("Admin/Wxsmall/domain",array('morecode'=>$param['morecode'])));
        }else{
            $this->error('小程序参数配置错误'.$commit['errmsg']);
        }
    }
    /**
     * 获取体验版二维码
     */
    public function get_qrcode(){
        $wxsmall = new \Common\Vendor\Wxsmallopen;
        $get_qrcode=$wxsmall->get_qrcode($param);
        $this->assign("qrcode",$get_qrcode);
    }
    /**
     * 将第三方提交的代码包提交审核
     */
    public function submit_audit(){
        $wxsmall = new \Common\Vendor\Wxsmallopen;
        $morecode=I("morecode");
        $submit_audit=$wxsmall->submit_audit();
        if($submit_audit['status']==1){
            $this->success('提交成功，请耐心等待审核！');
        }else{
            $this->error($submit_audit['msg']);
        }
    }
    /**
     * 查看审核状态
     */
    public function audit(){
        $wxsmall = new \Common\Vendor\Wxsmallopen;
        $latest=$wxsmall->get_latest_auditstatus();
        $morecode=I('morecode');
        $this->assign('morecode',$morecode);
        $this->assign("data",$latest);
        $data=unserialize(idtoname('WXSMALL_SUBMIT_DATA','Config','value','name',false));
       $wxsmall_data=array();
        if($data['extAppid']){
            $wxsmall_data=$data;
        }else{
            $wxsmall_data=$data[$this->appid];
        }
        if(empty($wxsmall_data['extAppid'])){
           $wxsmall_data['extAppid']=$this->appid;
        }
        $this->assign('SUBMIT_DATA',$wxsmall_data);
        $this->assign('VERSION',HANCHAO_WXSMALL_VERSION);
        $this->assign('template_array',$this->template_array);
        $this->display();
    }
    /**
     * 发布上线
     */
    public function release(){
        $morecode=I('morecode');
        $wxsmall = new \Common\Vendor\Wxsmallopen;
        $release=$wxsmall->release();
        if($release['errcode']==0&&$release['errmsg']=='ok'){
            $this->success('发布成功！');
        }else{
            $this->error($release['info']);
        }
    }
    /**
     * 检验文件下载
     */
    public function qrcodejumpdownload(){
        $wxsmall = new \Common\Vendor\Wxsmallopen;
        $result=$wxsmall->qrcodejumpdownload();
        if($result['errcode']==0&&$result['errmsg']=='ok'){
            $map = $save = array();
            $conf = idtoname('WXSMALL_SUBMIT_DATA','Config','value','name',false);
            $conf = unserialize($conf);
            $other_config=array();
            $conf['qrcodeauth']['file_name'] = $result['file_name'];
            $conf['qrcodeauth']['file_content'] = $result['file_content'];
            $data_config=array();
            if($conf['extAppid']){
                $data_config[$conf['extAppid']]=$conf;
            }else{
                $data_config=$conf;
            }
            $data_config[$other_config['extAppid']]=$other_config;
            $save['value'] = serialize($data_config);
            $map['name'] = 'WXSMALL_SUBMIT_DATA';
            M('Config')->where($map)->save($save);
            file_put_contents($result['file_name'],$result['file_content']);
            $this->success('获取成功');
        }else{
            $this->error('获取失败 '.$result['msg']);
        }
    }
    /**
     * 添加规则
     */
    public function qrcodejumpadd(){
        $wxsmall = new \Common\Vendor\Wxsmallopen;
        $result=$wxsmall->qrcodejumpadd();
        if($result['errcode']==0&&$result['errmsg']=='ok'){
            $map = $save = array();
            $conf = idtoname('WXSMALL_SUBMIT_DATA','Config','value','name',false);
            $conf = unserialize($conf);
            $other_config=array();
            $other_config['qrcodeconfig'] = $result['qrcodeconfig'];
            $data_config=array();
            if($conf['extAppid']){
                $data_config[$conf['extAppid']]=$conf;
            }else{
                $data_config=$conf;
            }
            $data_config[$other_config['extAppid']]=$other_config;
            $save['value'] = serialize($data_config);
            $map['name'] = 'WXSMALL_SUBMIT_DATA';
            M('Config')->where($map)->save($save);
            $this->success('获取成功');
        }else{
            $this->error('获取失败 '.$result['msg']);
        }
    }
    /**
     * 发布规则
     */
    public function qrcodejumppublish(){
        $param = I('param.');
        if(empty($param['data'])){
            $this->error('参数错误');
        }
        $wxsmall = new \Common\Vendor\Wxsmallopen;
        $result=$wxsmall->qrcodejumppublish($param);
        if($result['errcode']==0&&$result['errmsg']=='ok'){
            $this->success('发布成功');
        }else{
            $this->error('发布失败 '.$result['msg']);
        }
    }
    /**
     * 成员管理主页
     */
    public function tester(){
        $site_id=C("appid");
        $morecode=I("morecode");
        if(empty($morecode)){$morecode="SHOP";}
        $this->assign('morecode',$morecode);
        $wxsmall_code = require_once ROOT_PATH."/Public/data/wxsmall_code.php";
        $wx = new \Common\Vendor\Weixinopen;
        $cachename_moresmall='config_moresmall.php';
        $config_morelist=$wx->wxcache($cachename_moresmall);
        $small_list=array();
        /*foreach($wxsmall_code as $key=>$val){
            if($config_morelist[$site_id.'_'.$key]){
                $small_list[$key]['value']=$config_morelist[$site_id.'_'.$key];
                $small_list[$key]['name']=$val;
            }
        }*/
        $small_list['value']=$config_morelist[$site_id.'_'.$morecode];
        $small_list['name']=$wxsmall_code[$morecode];
        $conf = idtoname('WXSMALL_SUBMIT_DATA','Config','value','name',false);
        $conf = unserialize($conf);
        $this->assign('list',$conf[$config_morelist[$site_id.'_'.$morecode]]['tester_list']);
        $this->assign("config_morelist",$config_morelist);
        $this->assign("small_list",$small_list);
        $this->display();
    }
    /**
     * 成员管理-绑定管理员
     */
    public function tester_bind(){
        $param = I('param.');
        if(empty($param['wechatid'])){
            $this->error('参数错误');
        }
        $wxsmall = new \Common\Vendor\Wxsmallopen;
        $result=$wxsmall->bind_tester($param['wechatid']);
        $map = $save = array();
        $conf = idtoname('WXSMALL_SUBMIT_DATA','Config','value','name',false);
        $conf = unserialize($conf);
        if(($result['errcode']==0&&$result['errmsg']=='ok')||($result['errcode']=="85004"&&!in_array($param['wechatid'],$conf['tester_list']))){
           // $conf['tester_list'][] = $param['wechatid'];
            if($conf['extAppid']){
                $conf['tester_list'][] = $param['wechatid'];
            }else{
                $conf[$param['rank_id']]['tester_list'][] = $param['wechatid'];
            }
            $save['value'] = serialize($conf);
            $map['name'] = 'WXSMALL_SUBMIT_DATA';
            M('Config')->where($map)->save($save);
            $this->success('添加成功');
        }else{
            $this->error('添加失败 '.$result['msg']);
        }
    }
    /**
     * 成员管理-解除绑定
     */
    public function tester_unbind(){
        $param = I('param.');
        if(empty($param['wechatid'])){
            $this->error('参数错误');
        }
        $wxsmall = new \Common\Vendor\Wxsmallopen;
        $result=$wxsmall->unbind_tester($param['wechatid']);
        if($result['errcode']==0&&$result['errmsg']=='ok'){
            $map = $save = array();
            $conf = idtoname('WXSMALL_SUBMIT_DATA','Config','value','name',false);
            $conf = unserialize($conf);
            if($conf['extAppid']){
                unset($conf['tester_list'][array_search($param['wechatid'] , $conf['tester_list'])]);
            }else{
                unset($conf[$this->appid]['tester_list'][array_search($param['wechatid'] , $conf['tester_list'])]);
            }
            $save['value'] = serialize($conf);
            $map['name'] = 'WXSMALL_SUBMIT_DATA';
            M('Config')->where($map)->save($save);
            $this->success('解除成功');
        }else{
            $this->error('解除失败 '.$result['msg']);
        }
    }
    public function index(){
        $this->small_list();
    }
    public function doindex(){
        $morecode=I('morecode');
        $siteid=C('appid');
        $wx = new \Common\Vendor\Weixinopen;
        $cachename_moresmall='config_moresmall.php';
        $config_morelist=$wx->wxcache($cachename_moresmall);
        $appid=$config_morelist[$siteid.'_'.$morecode];
        //$res = $wx->wxconfig(C('appid'),'',1);
        if(empty($appid)) $is_author = 0;
        if($appid) {
            $res = $wx->wxconfig($appid, '', 1);
            $data = array();
            $is_author = 0;//是否已经授权(0:否,1:是)
            if (!empty($res)) {
                $is_author = 1;
            }
            $SUBMIT_DATA = unserialize(C('WXSMALL_SUBMIT_DATA'));
            $wxsmall = new \Common\Vendor\Wxsmallopen;
            if ($wxsmall->small_appid && $wxsmall->small_appid != $SUBMIT_DATA['extAppid']) $SUBMIT_DATA['extAppid'] = $wxsmall->small_appid;
            $this->get_qrcode();
        }
        $name=C("WEB_SITE_NAME");
        $this->assign("web_name",$name);
        $this->assign("is_author",$is_author);
        $this->assign('SUBMIT_DATA',$SUBMIT_DATA);
        $this->assign('morecode',$morecode);
        $this->display('doindex');
    }

    /**
     * 小程序列表
     */
    public function small_list(){
        $wxsmall_code = require_once ROOT_PATH."/Public/data/wxsmall_code.php";
        $wx = new \Common\Vendor\Weixinopen;
        $cachename_moresmall='config_moresmall.php';
        $site_id=C('appid');
        $config_morelist=$wx->wxcache($cachename_moresmall);
        $real_code=array();
        if($site_id==636 || $site_id==665){
            $real_code=$wxsmall_code;
        }else{
            $real_code=array("SHOP"	=>'商城小程序');
        }
        $this->assign("config_morelist",$config_morelist);
        $this->assign("list",$real_code);
        $this->assign("site_id",$site_id);
        $this->display('small_list');
    }
    //更新小程序配置
    public function save_wxsmall(){
        $wx = new \Common\Vendor\Weixinopen;
        $cachename='config_listsmall.php';
        $config_listsmall=$wx->wxcache($cachename);
        print_r($config_listsmall);exit;
    }

    /**
     * 获取仓库小程序二维码
     */
    public function get_code(){
        $param=I('param.');
        $param['morecode']="SELFBUY";
        $param['shop_id']=1;
        $param['width']=150;
        $wxsmall = new \Common\Vendor\Wxsmallopen;
        $result=$wxsmall->get_location_small_code($param);
        echo "<img src='".$result."'>";
        print_r($result);
    }
    public function small_index(){
        $wxsmall_code = require_once ROOT_PATH."/Public/data/wxsmall_code.php";
        $wx = new \Common\Vendor\Weixinopen;
        $cachename_moresmall='config_moresmall.php';
        $site_id=C('appid');
        $config_morelist=$wx->wxcache($cachename_moresmall);
        $real_code=array();
        if($site_id==636 || $site_id==665){
            $real_code=$wxsmall_code;
        }else{
            $real_code=array("SHOP"	=>'商城小程序');
        }
        $this->assign("config_morelist",$config_morelist);
        $this->assign("list",$real_code);
        $this->assign("site_id",$site_id);
        $this->display();
    }
}