Template.php 41.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司,并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: IT宇宙人     
 * Date: 2015-09-09
 */
namespace app\manager\controller;
use think\AjaxPage;
use think\Page;

class Template extends Base {
    /*模板列表*/
    public function templateList()
    {
        $pagenum=I('pagenum/d',10);
        $type=I('type');
        $key_word = I('key_word') ? trim(I('key_word')) : ''; // 关键词搜索
        $cur_page=I('p/d',0);//当前页数
        $typelist=M('module_type')->order('ordid asc')->select();
        $this->assign("typelist",$typelist);
        $this->assign('key_word',$key_word);
        $this->assign('pagenum',$pagenum);
        $this->assign('type',$type);
        $this->assign('cur_page',$cur_page);
        return $this->fetch();
    }
    public function ajaxtemplateList(){
        $type=I('type');
        $type_id=I('type_id');
        $key_word=I('key_word');
        $key_word = I('key_word') ? trim(I('key_word')) : ''; // 关键词搜索
        $cur_page=I('p/d',0);//当前页数
        if ($cur_page<=0) { $cur_page=1; }
        $pagenum=I('pagenum/d',10);
        $where = ' a.type <> 0 and a.type<>3 and a.type<>4'; // 搜索条件
        if($type){
            $where.=" and a.type=".$type;
        }
        if($type_id){
            $where.=" and a.type_id=".$type_id;
        }
        if($key_word)
        {
            $where .= " and (a.page_title like '%$key_word%' or a.page_describe like '%$key_word%' or a.template_sn like '%$key_word%')" ;
        }

        $model = M('store_module');
        $count = $model->alias('a')->join('module_type b','a.type_id=b.id','left')
            ->where($where)->count();

        $Page  = new AjaxPage($count,$pagenum);
        $show = $Page->show();
        $List = $model->alias('a')->join('module_type b','a.type_id=b.id','left')
            ->join('(select count(1) as countbuynum,module_id from wxd_store_renew_module group by module_id) c','a.id=c.module_id','left')
            ->join('(select count(1) as usercount,from_id from wxd_store_module group by from_id) d','a.id=d.from_id','left')
            ->where($where)->order("a.billdate desc")

            ->field('a.*,b.typename,ifnull(c.countbuynum,0) as countbuynum,ifnull(d.usercount,0) as usercount ')
            ->limit($Page->firstRow.','.$Page->listRows)->select();

        $oldurl=U('manager/template/templatelist',array(
            "type"=>$type,
            "key_word"=>$key_word,
            "pagenum"=>$pagenum,
            "p"=>$cur_page,
        ));

        $this->assign('oldurl',urlencode($oldurl));
        $this->assign('key_word',$key_word);
        $this->assign('pagenum',$pagenum);
        $this->assign('type',$type);
        $this->assign('type_id',$type_id);
        $this->assign('cur_page',$cur_page);
        $this->assign('temList',$List);
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('pager',$Page);
        return $this->fetch();
    }

    /*--模板添加和修改页--*/
    public function templatehandle(){
        /*-------模块读取-------*/
        $rs=M("module")->select();
        //元素  {"content":citem,"ename":data.data.eng_name,"name":"","icon":};
        foreach ($rs as $k=>$v){
            $str=file_get_contents(ROOT_PATH.$v['controljson']);
            $jdata=json_decode($str,true);
            $data[]=[
                "content"=>$jdata['content'],
                "ename"=>$v['eng_name'],
                "name"=>$v['name'],
                "icon"=>getimg($v['module_icon'],NOIMG),
                "type"=>0
            ];
        }

        $typelist=M('module_type')->order('ordid asc')->select();
        $this->assign("typelist",$typelist);
        if(empty($data)) $data=-1;
        $this->assign("module",json_encode($data));

        $id=I('id');

        if(!empty($id)){
            /*--模板读取--*/
            $temdata=M("store_module")->where('id',$id)->find();
            /*--要读取content--*/
            $p=ROOT_PATH."/public/template/feature/".$temdata['template_sn'].".json";

            if(file_exists($p)) {
                $str = file_get_contents($p);
                $jjdata = json_decode($str, true);
            }else{
                $this->assign("err","未找到模板文件");
                return $this->fetch();
            }

              $this->assign("temdata1",$temdata);
            $this->assign("temdata",json_encode($jjdata));
            /*--config--{"title":"","descr":"","istop":0,"isright":0,"money":0};*/
            $data=[
                "title"=>$temdata['page_title'],
                "descr"=>$temdata['page_describe'],
                "istop"=>$temdata['istop'],
                "isright"=>$temdata['isright'],
                "isshow"=>$temdata['isshow'],
                "type_id"=>$temdata['type_id'],
                "money"=>$temdata['money'],
                "days"=>$temdata['days'],
                "bkcolor"=>$temdata['bkcolor'],
            ];
            $this->assign("config",json_encode($data));
        }else{
            $this->assign("temdata",-1);
            $this->assign("config",-1);
        }

        $this->initEditor();
        return $this->fetch();
    }

    /*-------模板的保存------------*/
    public function temlatesave(){

        mlog("0","temlatesave");
       /*--删除手机展示缓存--*/
       delFile(TEMP_PATH."/manager_template");

       $data=I("post.");
       $id=I('temid');
       /*---创建模板存放目录---*/
       if (!is_dir(ROOT_PATH.'/public/template/feature/'))
            mkdir(ROOT_PATH.'/public/template/feature/');
       /*----页面基础配置----*/
       $conf = $data[1];
       /*----json文件保存----*/
       $str = json_encode($data[0]);

       mlog("1".$id,"temlatesave");

       /*----------------新增-----------------*/
       if(empty($id)) {
           mlog("2","temlatesave");
           $mr=get_total_millisecond();
           $sn = date('YmdHis').$mr.rand(1000, 9999);
           /*--json文件操作--*/
           $filename='/public/template/feature/'.$sn.'.json';
           if(file_exists(ROOT_PATH.$filename) && $filename){
               mdelFile(ROOT_PATH.$filename);
           }
           file_put_contents(ROOT_PATH.$filename,$str);

           /*---调用风格表存储手机端显示文件---*/
           $filename2='/public/template/feature/'.$sn.'.html';
           if(file_exists(ROOT_PATH.$filename2) && $filename2){
               mdelFile(ROOT_PATH.$filename2);
           }
           /*---手机前端要显示保存的内容---*/
           $htm="";
           foreach ($data[0] as $k=>$v){
              /*--风格类型--*/
              $type=$v['content']['style'];
              $r0=M("module")->where('eng_name',$v['ename'])->find();
              $r1=M("module_list")->where('parent_id',$r0['id'])->where('type',$type)->find();
              /*--获取手机前端要显示html--*/
              $htmlfile="/public/template/html/". $r0['eng_name'] . "/mobliehtm".$r1['class_eng_name'].".html";
              if(file_exists(ROOT_PATH.$htmlfile)){
                   $cstr=file_get_contents(ROOT_PATH.$htmlfile);
              }
              else{
                   return json(['code'=>-1,'msg'=>'未找到手机端展示文件']);
              }

              /*--根据字段替换--*/
              $fields=explode(",",$r1['fields']);
              foreach ($fields as $kk=>$vv) {
                   $val=$v['content'][$vv];
                   if($vv=='no'){
                       $val=uniqid();
                   }
                   if($vv=='scoll_length'){
                      $val=$v['content']['length'];
                      if(count($v['content']['data'])<$val){
                          $val=count($v['content']['data']);
                      }
                   }

                   if (gettype($val)=='boolean'){
                      $val=(int)$val;
                   }

                   $replacestr="{{".$vv."}}";
                   $cstr=str_replace($replacestr,$val."",$cstr);
              }

              /*--如果有循环体--*/
              if(!empty($v['content']['data'])) {
                  $rhtm="";
                  $width=100/count($v['content']['data']);
                  /*--截取循环里面的循环体--*/
                  $start1=strpos($cstr,'<foreach>',0);

                  /*--如果有进行循环--*/
                  if($start1) {
                      $end1 = strpos($cstr, '</foreach>', 0);
                      /* $str = "<foreach><span>{{name}}</span></foreach>"; */
                      $repeat = substr($cstr, $start1 + 9, $end1 - $start1 - 9);
                      $cstr = str_replace('<foreach>', "", $cstr);
                      $cstr = str_replace('</foreach>', "", $cstr);
                      $cstr = str_replace($repeat, "{{the_repeat}}", $cstr);

                      foreach ($v['content']['data'] as $kk => $vv) {
                          $vv['width'] = $width;
                          $rp = $repeat;
                          foreach ($vv as $mk => $mv) {
                              $replacestr = "{{" . $mk . "}}";
                              $rp = str_replace($replacestr, $mv . "", $rp);
                          }
                          $rhtm .= $rp;
                      }
                      $cstr = str_replace("{{the_repeat}}", $rhtm, $cstr);
                  }else{
                      /*--如果有按数组替换的--*/
                      if(strpos($cstr,'{{data[0]',0)) {
                          /*--根据字段替换--*/
                          foreach ($v['content']['data'] as $kk => $vv) {
                              foreach ($vv as $km => $vm) {
                                  $replacestr = "{{data[" . $kk . "]." . $km . "}}";
                                  $cstr = str_replace($replacestr, $vm . "", $cstr);
                              }
                          }
                      }else{
                          /*---如果是商品分组的手动选择商品---*/
                           if(
                               $v['content']['classstyle']==1
                               || $v['content']['moudletype']==2
                               || $v['content']['moudletype']==3
                               || $v['content']['moudletype']==4
                             ){
                              $idlist="";
                              foreach ($v['content']['data'] as $kk => $vv) {
                                  $idlist.=$vv['goodsid'].",";
                              }
                              $idlist=substr($idlist,0,strlen($idlist)-1);
                              $replacestr = "{{idlist}}";
                              $val0="".$idlist."";
                              $cstr = str_replace($replacestr, $val0, $cstr);
                          }
                      }
                  }
              }
              $htm.=$cstr;
           }
           /*---写入文件---*/
           file_put_contents(ROOT_PATH.$filename2,$htm);

           $type=1;
           if(empty($conf['money'])) $conf['money']=0;
           if($conf['money']>0) $type=2;

           /*--存储数据--*/
           $sdata = [
               "type" => $type,//默认免费模板
               "page_title" => $conf['title'],
               "page_describe" => $conf['descr'],
               "bkcolor" => $conf['bkcolor'],
               "istop" => $conf['istop'],
               "isright" => $conf['isright'],
               "isshow" => $conf['isshow'],
               "type_id" => $conf['type_id'],
               "money" => $conf['money'],
               "days" => $conf['days'],
               "template_sn" => $sn,
               "billdate" => time(),
           ];
           $u=M('store_module')->save($sdata);
           if($u)
              return json(['code'=>1]);
           else
              return json(['code'=>-1,'msg'=>'保存失败']);

       }
       /*----------------编辑-----------------*/
       else{

           mlog("3","temlatesave");

           $rs=M('store_module')->where('id',$id)->find();
           $sn=$rs['template_sn'];
           /*--json文件操作--*/
           $filename='/public/template/feature/'.$sn.'.json';
           if(file_exists(ROOT_PATH.$filename) && $filename){
               mdelFile(ROOT_PATH.$filename);
           }
           file_put_contents(ROOT_PATH.$filename,$str);

           /*---调用风格表存储手机端显示文件---*/
           $filename2='/public/template/feature/'.$sn.'.html';
           if(file_exists(ROOT_PATH.$filename2) && $filename2){
               mdelFile(ROOT_PATH.$filename2);
           }
           /*---手机前端要显示保存的内容---*/
           $htm="";
           foreach ($data[0] as $k=>$v){
               /*--风格类型--*/
               $type=$v['content']['style'];
               $r0=M("module")->where('eng_name',$v['ename'])->find();
               $r1=M("module_list")->where('parent_id',$r0['id'])->where('type',$type)->find();
               /*--获取手机前端要显示html--*/
               $htmlfile="/public/template/html/". $r0['eng_name'] . "/mobliehtm".$r1['class_eng_name'].".html";
               if(file_exists(ROOT_PATH.$htmlfile)){
                   $cstr=file_get_contents(ROOT_PATH.$htmlfile);
               }
               else{
                   return json(['code'=>-1,'msg'=>'未找到手机端展示文件']);
               }
               /*--根据字段替换--*/
               $fields=explode(",",$r1['fields']);
               foreach ($fields as $kk=>$vv) {
                   $val=$v['content'][$vv];
                   if($vv=='no'){
                       $val=uniqid();
                   }
                   if($vv=='scoll_length'){
                       $val=$v['content']['length'];
                       if(count($v['content']['data'])<$val){
                           $val=count($v['content']['data']);
                       }
                   }
                   if (gettype($val)=='boolean'){
                       $val=(int)$val;
                   }

                   $replacestr="{{".$vv."}}";
                   $cstr=str_replace($replacestr,$val."",$cstr);
               }

               /*--如果有循环体--*/
               if(!empty($v['content']['data'])) {
                   $rhtm="";
                   $width=100/count($v['content']['data']);
                   /*--截取循环里面的循环体--*/
                   $start1=strpos($cstr,'<foreach>',0);
                   /*--如果有进行循环--*/
                   if($start1) {
                       $end1 = strpos($cstr, '</foreach>', 0);
                       /* $str = "<foreach><span>{{name}}</span></foreach>"; */
                       $repeat = substr($cstr, $start1 + 9, $end1 - $start1 - 9);

                       $cstr = str_replace('<foreach>', "", $cstr);
                       $cstr = str_replace('</foreach>', "", $cstr);
                       $cstr = str_replace($repeat, "{{the_repeat}}", $cstr);

                       foreach ($v['content']['data'] as $kk => $vv) {
                           $vv['width'] = $width;
                           $rp = $repeat;
                           foreach ($vv as $mk => $mv) {
                               $replacestr = "{{" . $mk . "}}";
                               $rp = str_replace($replacestr, $mv . "", $rp);
                           }
                           $rhtm .= $rp;
                       }
                       $cstr = str_replace("{{the_repeat}}", $rhtm, $cstr);
                   }else{
                       /*--如果有按数组替换的--*/
                       if(strpos($cstr,'{{data[0]',0)) {
                           /*--根据字段替换--*/
                           foreach ($v['content']['data'] as $kk => $vv) {
                               foreach ($vv as $km => $vm) {
                                   $replacestr = "{{data[" . $kk . "]." . $km . "}}";
                                   $cstr = str_replace($replacestr, $vm . "", $cstr);
                               }
                           }
                       }else{
                           /*---如果是商品分组的手动选择商品---*/
                           if(
                               $v['content']['classstyle']==1
                               || $v['content']['moudletype']==2
                               || $v['content']['moudletype']==3
                               || $v['content']['moudletype']==4
                           ){
                               $idlist="";
                               foreach ($v['content']['data'] as $kk => $vv) {
                                   $idlist.=$vv['goodsid'].",";
                               }
                               $idlist=substr($idlist,0,strlen($idlist)-1);
                               $replacestr = "{{idlist}}";
                               $val0="".$idlist."";
                               $cstr = str_replace($replacestr, $val0, $cstr);
                           }
                       }

                   }
               }

               $htm.=$cstr;
           }
           /*---写入文件---*/
           file_put_contents(ROOT_PATH.$filename2,$htm);

           $mid= session('manager_id');
           $type=1;
           if(empty($conf['money'])) $conf['money']=0;
           if($conf['money']>0) $type=2;

           $rr=M("manager_admin")->where("manager_id",$mid)->find();
           /*--存储数据--*/
           $sdata = [
               "type" => $type,//默认免费模板
               "page_title" => $conf['title'],
               "page_describe" => $conf['descr'],
               "bkcolor" => $conf['bkcolor'],
               "istop" => $conf['istop'],
               "isright" => $conf['isright'],
               "isshow" => $conf['isshow'],
               "type_id" => $conf['type_id'],
               "money" => $conf['money'],
               "days" => $conf['days'],
               "editdate" => time(),
               "editip"=>getIP(),
               "editman"=>$rr['manager_name'],
           ];
           $u=M('store_module')->where('id',$id)->save($sdata);
           if($u)
               return json(['code'=>1]);
           else
               return json(['code'=>-1]);
       }
    }

    /*--模板拷贝--*/
    public function copytemplate(){
        delFile(TEMP_PATH."/manager");

        $mr=get_total_millisecond();
        $id=I("id");
        $rs=M('store_module')->where('id',$id)->find();
        $oldsn= $rs['template_sn'];
        $newsn=date('YmdHis').$mr.rand(1000, 9999);
        /*--复制json--*/
        $file=ROOT_PATH.'/public/template/feature/'.$oldsn.'.json';
        $newfile=ROOT_PATH.'/public/template/feature/'.$newsn.'.json';

        if(file_exists($file)) {
            $str = file_get_contents($file);
            file_put_contents($newfile,$str);
        }else{
            return json(['code'=>-1,"msg"=>'未找到原文件']);
        }

        /*--复制html--*/
        $file=ROOT_PATH.'/public/template/feature/'.$oldsn.'.html';
        $newfile=ROOT_PATH.'/public/template/feature/'.$newsn.'.html';
        if(file_exists($file)) {
            $str = file_get_contents($file);
            file_put_contents($newfile,$str);
        }else{
            return json(['code'=>-1,"msg"=>'未找到原文件']);
        }

        $rs['template_sn']=$newsn;
        $rs['billdate']=time();

        unset($rs['id']);
        unset($rs['editdate']);
        unset($rs['editip']);
        unset($rs['editman']);

        $u=M('store_module')->save($rs);
        if($u)
            return json(['code'=>1]);
        else
            return json(['code'=>-1,"msg"=>'复制失败']);

    }

    /*--删除模板--*/
    public function deltemplate(){
        delFile(TEMP_PATH."/manager");

        $id=I("id");
        $r=M("store_module")->where('id',$id)->find();
        $sn=$r['template_sn'];
        $file=ROOT_PATH.'/public/template/feature/'.$sn.'.json';
        $file2=ROOT_PATH.'/public/template/feature/'.$sn.'.html';
        mdelFile($file);
        mdelFile($file2);
        $r=M("store_module")->where('id',$id)->delete();
        if($r)
            return json(['code'=>1]);
        else
            return json(['code'=>-1]);
    }

    /*--------------------------模块------------------------*/
    public function moduleList(){
        $key_word = I('key_word') ? trim(I('key_word')) : ''; // 关键词搜索
        $cur_page=I('p/d',0);//当前页数
        if ($cur_page<=0) { $cur_page=1; }
        $pagenum=I('pagenum/d');

        $this->assign('key_word',$key_word);
        $this->assign('pagenum',$pagenum);
        $this->assign('cur_page',$cur_page);
        return $this->fetch();
    }
    public function ajaxmoduleList(){
        $key_word = I('key_word') ? trim(I('key_word')) : ''; // 关键词搜索
        $cur_page=I('p/d',0);//当前页数
        if ($cur_page<=0) { $cur_page=1; }
        $pagenum=I('pagenum/d');

        $where = ' 1 = 1 '; // 搜索条件
        if($key_word)
        {
            $where .= " and (name like '%$key_word%' or eng_name like '%$key_word%')" ;
        }
        $model = M('module');
        $count = $model->where($where)->count();
        $Page  = new AjaxPage($count,$pagenum);
        $show = $Page->show();
        $List = $model->where($where)->order("sort")->limit($Page->firstRow.','.$Page->listRows)->select();
        $oldurl=U('manager/template/modulelist',array(
            "key_word"=>$key_word,
            "pagenum"=>$pagenum,
            "p"=>$cur_page,
        ));

        $this->assign('oldurl',urlencode($oldurl));
        $this->assign('key_word',$key_word);
        $this->assign('pagenum',$pagenum);
        $this->assign('cur_page',$cur_page);
        $this->assign('List',$List);
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('pager',$Page);
        return $this->fetch();
    }

    /*--模块处理--*/
    public function modulehandle(){
        $isjax=I("is_ajax");
        $id=I('id');

        if(!empty($id)){
            $info=M('module')->where('id',$id)->find();
        }
        if($isjax){
            $data=I("post.");
            /*--如果原文件存在,要删除原文件--*/
            $filename='/public/template/html/'.$data['eng_name'].'/controlhtm.html';
            $filename2='/public/template/html/'.$data['eng_name'].'/controljson.json';
            $filename3='/public/template/html/'.$data['eng_name'].'/showhtm.html';

            if(file_exists(ROOT_PATH.$filename) && $filename){
                mdelFile(ROOT_PATH.$filename);
            }
            if(file_exists(ROOT_PATH.$filename2) && $filename2){
                mdelFile(ROOT_PATH.$filename2);
            }
            if(file_exists(ROOT_PATH.$filename3) && $filename3){
                mdelFile(ROOT_PATH.$filename3);
            }

            if($id){
               $where="name='".$data['name']."' or  eng_name='".$data['eng_name']."'";
               $rs=M('module')->where('id','neq',$id)->where($where)->find();
               if($rs){
                   return json(['code'=>-1,'msg'=>'模块名称,英文名称不能重复']);
               }
                /*---创建目录---*/
                if (!is_dir(ROOT_PATH.'/public/template/html/'.$data['eng_name'].'/'))
                    mkdir(ROOT_PATH.'/public/template/html/'.$data['eng_name'].'/');

                $str=$data['controlhtm'];
                file_put_contents(ROOT_PATH.$filename,$str);
                $data['controlhtm']=$filename;

                $str=$data['controljson'];
                file_put_contents(ROOT_PATH.$filename2,$str);
                $data['controljson']=$filename2;

                $str=$data['showhtm'];
                file_put_contents(ROOT_PATH.$filename3,$str);
                $data['showhtm']=$filename3;

                $mid= session('manager_id');
                $rr=M("manager_admin")->where("manager_id",$mid)->find();
                $data['editdate']=time();
                $data['editip']=getIP();
                $data['editman']=$rr['manager_name'];
                unset($data['id']);
                unset($data['is_ajax']);

                M('module')->where('id',$id)->save($data);

           }else{
               $where="name='".$data['name']."' or  eng_name='".$data['eng_name']."'";
               $rs=M('module')->where($where)->find();
               if($rs){
                   return json(['code'=>-1,'msg'=>'模块名称,英文名称不能重复']);
               }
               /*---创建目录---*/
               if (!is_dir(ROOT_PATH.'/public/template/html/'.$data['eng_name'].'/'))
                    mkdir(ROOT_PATH.'/public/template/html/'.$data['eng_name'].'/');

                $str=$data['controlhtm'];
                file_put_contents(ROOT_PATH.$filename,$str);
                $data['controlhtm']=$filename;

                $str=$data['controljson'];
                file_put_contents(ROOT_PATH.$filename2,$str);
                $data['controljson']=$filename2;

                $str=$data['showhtm'];
                file_put_contents(ROOT_PATH.$filename3,$str);
                $data['showhtm']=$filename3;

                $data['billdate']=time();
                M('module')->save($data);
           }
           return json(['code'=>1]);
        }

        /*----*/
        if(!empty($info)) {
            $filename = $info['controlhtm'];
            $filename2 = $info['controljson'];
            $filename3 = $info['showhtm'];
            if(file_exists(ROOT_PATH.$filename) && $filename) {
                $str = file_get_contents(ROOT_PATH.$filename);//将整个文件内容读入到一个字符串中
                $info['controlhtm']=$str;
            }else{
               $info['controlhtm'] = "";
            }

            if(file_exists(ROOT_PATH.$filename2) && $filename2) {
                $str = file_get_contents(ROOT_PATH.$filename2);//将整个文件内容读入到一个字符串中
                $info['controljson']=$str;
            }else{
                $info['controljson'] = "";
            }

            if(file_exists(ROOT_PATH.$filename3) && $filename3) {
                $str = file_get_contents(ROOT_PATH.$filename3);//将整个文件内容读入到一个字符串中
                $info['showhtm']=$str;
            }else{
                $info['showhtm'] = "";
            }

        }else{
              $info['controlhtm'] = "";
              $info['controljson'] = "";
              $info['showhtm'] = "";
        }
        $this->assign('info',$info);
        return $this->fetch();
    }

    /*--获取MODULE--*/
    public function getmodule(){
        $id=I("id");
        $rs=M("module")->where('id',$id)->find();
        $molist=M("module_list")->where('parent_id',$id)->select();
        $rs[$k]['molist']=$molist;
        return json(['code'=>1,"data"=>$rs]);
    }

    /*---模块删除---*/
    public function delmodule(){
        $id=I("id");
        $mr=M("module")->where('id',$id)->find();
        $rs=M("store_module")->select();
        foreach ($rs as $kl=>$vl) {
            $p = ROOT_PATH . "/public/template/feature/" . $vl['template_sn'] . ".json";
            if (file_exists($p)) {
                $str = file_get_contents($p);
                $jjdata = json_decode($str, true);
                foreach ($jjdata as $k=>$v){
                   if($mr['eng_name']==$v['ename']){
                       return json(['code'=>-1,"msg"=>'有模板使用了该模块,不能删除']);
                   }
                }
            }
        }
        //模块删除
        delFile(ROOT_PATH."/public/template/html/".$mr['eng_name']."/");

        $r0=M("module_list")->where('parent_id',$id)->delete();
        $r=M("module")->where('id',$id)->delete();
        if($r)
            return json(['code'=>1]);
        else
            return json(['code'=>-1,"msg"=>'删除失败']);
    }

    /*--------------模块风格--------------*/
    public function moduletypeList(){
        $key_word = I('key_word') ? trim(I('key_word')) : ''; // 关键词搜索
        $cur_page=I('p/d',0);//当前页数
        $pid=I('pid/d',0);   //父ID
        if ($cur_page<=0) { $cur_page=1; }
        $pagenum=I('pagenum/d',10);

        $this->assign('cur_page', $cur_page);
        $this->assign('key_word',urlencode($key_word));
        $this->assign('pagenum',$pagenum);
        $this->assign('pid',$pid);
        return $this->fetch();
    }

    public function ajaxmoduletypeList(){
        $key_word = I('key_word') ? trim(I('key_word')) : ''; // 关键词搜索
        $cur_page=I('p/d',0);//当前页数
        $pid=I('pid/d',0);   //父ID
        if ($cur_page<=0) { $cur_page=1; }
        $pagenum=I('pagenum/d',10);

        $where = 'parent_id='.$pid; // 搜索条件
        if($key_word)
        {
            $where .= " and (class_name like '%$key_word%' or class_eng_name like '%$key_word%')";
        }
        $model = M('module_list');
        $count = $model->where($where)->count();
        $Page  = new AjaxPage($count,$pagenum);
        $show = $Page->show();
        $List = $model->where($where)->order("sort")->limit($Page->firstRow.','.$Page->listRows)->select();

        $oldurl=U('manager/template/moduletypelist',array(
            "key_word"=>$key_word,
            "pagenum"=>$pagenum,
            "p"=>$cur_page,
            "pid"=>$pid,
        ));

        $this->assign('oldurl',urlencode($oldurl));
        $this->assign('key_word',$key_word);
        $this->assign('pagenum',$pagenum);
        $this->assign('cur_page',$cur_page);

        $this->assign('List',$List);
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('pager',$Page);
        return $this->fetch();
    }

    /*--------模板风格处理--------*/
    public function moduletypehandle(){
        $isjax=I("is_ajax");
        $id=I('id');
        $pid=I('pid');

        $mo=M('module_list')->where('id',$id)->find();
        $pmo=M('module')->where('id',$pid)->find();
        if(empty($pmo)){
            $this->error("未找到模板", U('manager/template/moduletypeList',array("pid"=>$pid)));
        }

        /*---创建目录---*/
        if (!is_dir(ROOT_PATH.'/public/template/html/'.$pmo['eng_name'].'/'))
            mkdir(ROOT_PATH.'/public/template/html/'.$pmo['eng_name'].'/');

        if($isjax){
            $data=I("post.");
            $data['parent_id']=$pid;
            $filename2='/public/template/html/'.$pmo['eng_name'].'/mobliehtm'.$data['class_eng_name'].'.html';
            if(file_exists(ROOT_PATH.$filename2) && $filename2){
                mdelFile(ROOT_PATH.$filename2);
            }
            if($id){
                $where="class_name='".$data['class_name']."' or  class_eng_name='".$data['class_eng_name']."'";
                $rs=M('module_list')->where('id','neq',$id)->where($where)->find();
                if($rs){
                    return json(['code'=>-1,'msg'=>'风格名称,英文名称不能重复']);
                }
                /*---生成手机端展示html---*/
                $str=$data['mobilehtm'];
                file_put_contents(ROOT_PATH.$filename2,$str);
                $data['mobilehtm']=$filename2;

                $mid= session('manager_id');
                $rr=M("manager_admin")->where("manager_id",$mid)->find();
                $data['editdate']=time();
                $data['editip']=getIP();
                $data['editman']=$rr['manager_name'];
                unset($data['id']);
                unset($data['is_ajax']);
                M('module_list')->where('id',$id)->save($data);
            }else{
                $where="class_name='".$data['class_name']."' or  class_eng_name='".$data['class_eng_name']."'";
                $rs=M('module_list')->where('parent_id',$pid)->where($where)->find();
                if($rs){
                    return json(['code'=>-1,'msg'=>'风格名称,英文名称不能重复']);
                }

                /*---生成手机端展示html---*/
                $str=$data['mobilehtm'];
                file_put_contents(ROOT_PATH.$filename2,$str);
                $data['mobilehtm']=$filename2;
                $data['billdate']=time();
                M('module_list')->save($data);
            }
            return json(['code'=>1]);
        }

        /*--编辑展示--*/
        if(!empty($mo)) {
            $info=$mo;
            $filename = $info['mobilehtm'];
            if(file_exists(ROOT_PATH.$filename) && $filename) {
                $str = file_get_contents(ROOT_PATH.$filename);//将整个文件内容读入到一个字符串中
                $info['mobilehtm']=$str;
            }else{
                $info['mobilehtm'] = "";
            }
        }else{
            $info['showhtm'] = "";
            $info['mobilehtm'] = "";
        }
        $this->assign('info',$info);
        return $this->fetch();
    }

    /*--------模板风格删除--------*/
    public function delmoduletype(){
        $id=I("id");
        $mr=M("module_list")->where('id',$id)->find();
        $r=M("module")->where('id',$mr['parent_id'])->find();
        /*--删除风格文件--*/
        $p = ROOT_PATH . "/public/template/html/" . $r['eng_name'] . "/mobliehtm".$mr['class_eng_name'].".html";
        mdelFile($p);
        $rs=M('module_list')->where("id",$id)->delete();
        if($rs)
            return json(['code'=>1]);
        else
            return json(['code'=>-1,"msg"=>"删除失败"]);

    }

    /*---编辑默认导航,读取默认导航的json---*/
    public function defnav(){
        $path="/public/template/footer/footer.json";
        if(file_exists(ROOT_PATH.$path)){
            $cstr=file_get_contents(ROOT_PATH.$path);
            $mdata = json_decode($cstr, true);
            $this->assign("temdata",json_encode($mdata));
        }else{
            $this->assign("temdata","-1");
            $this->assign("err","未找到导航json文件");
        }

        return $this->fetch();
    }

    /*---编辑默认导航保存事件---*/
    public function defnavsave(){
        $data=I("post.");
        /*---创建模板存放目录---*/
        if (!is_dir(ROOT_PATH.'/public/template/footer/'))
            mkdir(ROOT_PATH.'/public/template/footer/');
        /*----json文件保存----*/
        $ccstr = json_encode($data);
        /*--json文件操作重新保存--*/
        $filename='/public/template/footer/footer.json';
        if(file_exists(ROOT_PATH.$filename) && $filename){
                mdelFile(ROOT_PATH.$filename);
        }
        file_put_contents(ROOT_PATH.$filename,$ccstr);
        /*--获取手机前端要显示html--*/
        $htmlfile="/public/template/footer/footer.html";
        if(file_exists(ROOT_PATH.$htmlfile)){
            $cstr=file_get_contents(ROOT_PATH.$htmlfile);
        }
        else{
            return json(['code'=>-1,'msg'=>'未找到手机端展示文件']);
        }

        /*---调用风格表存储手机端显示文件---*/
        $filename2='/public/template/footer/sfooter.html';
        if(file_exists(ROOT_PATH.$filename2) && $filename2){
            mdelFile(ROOT_PATH.$filename2);
        }

        $htm="";
        /*--根据字段替换--*/
        foreach ($data as $kk=>$vv) {
            if($kk=="data") continue;
            if($vv=='no'){
                $val=uniqid();
            }
            $replacestr="{{".$kk."}}";
            $cstr=str_replace($replacestr,$vv."",$cstr);
        }

        /*--如果有循环体--*/
        if(!empty($data['data'])) {
            $rhtm = "";
            $width = 100 / count($data['data']);
            $cstr = str_replace('{{width}}', $width . "", $cstr);

            /*--截取循环里面的循环体--*/
            $start1 = strpos($cstr, '<foreach>', 0);
            /*--如果有进行循环--*/
            if ($start1) {
                $end1 = strpos($cstr, '</foreach>', 0);
                /* $str = "<foreach><span>{{name}}</span></foreach>"; */
                $repeat = substr($cstr, $start1 + 9, $end1 - $start1 - 9);
                $cstr = str_replace('<foreach>', "", $cstr);
                $cstr = str_replace('</foreach>', "", $cstr);
                $cstr = str_replace($repeat, "{{the_repeat}}", $cstr);

                foreach ($data['data'] as $kk => $vv) {
                    $ind=$kk+1;
                    $rp = $repeat;
                    $rp = str_replace("{{index}}", $ind . "", $rp);
                    foreach ($vv as $mk => $mv) {
                        $replacestr = "{{" . $mk . "}}";
                        $rp = str_replace($replacestr, $mv . "", $rp);
                    }
                    $rhtm .= $rp;
                }
                $cstr = str_replace("{{the_repeat}}", $rhtm, $cstr);
            }
            $htm.=$cstr;
        }
        /*---写入文件---*/
        file_put_contents(ROOT_PATH.$filename2,$htm);
        return json(['code'=>1,'msg'=>'保存成功']);
    }


    /*----添加图片---*/
    public function img(){
        return $this->fetch();
    }


    /**
     * 模板类型列表
     * @return wxsendtype
     */
    public function templatetype(){
        $model = M('module_type');
        $list = array();
        $pagenum=20;//每页显示多少条
        if ((int)I('pagenum/s')>0)
        {
            $pagenum=I('pagenum/s');
        }

        $keywords = I('keywords/s');

        if ($keywords) {
            $where['typename']=array(['like','%'.$keywords.'%']);;
        }

        $count = $model->where($where)->count();
        $Page = $pager = new Page($count,$pagenum);
        $list = $model->where($where)->limit($Page->firstRow.','.$Page->listRows)->order('ordid asc')->select();
        $show = $Page->show();
        $this->assign('keywords',$keywords);
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->assign('pager',$pager);
        $this->assign('pagenum',$pagenum);
        $this->assign('oldurl',urlencode(curPageURL()));
        return $this->fetch();
    }
    //添加微信模板
    public function addtemplatetype(){

        $oldurl = I('oldurl/s');
        if($oldurl){
            $oldurl = urldecode(urldecode($oldurl));
        }

        $getstoid=getAdmStoId();
        $id = I('id');

        $wxsend = M('module_type')->where(array('id' => $id))->find();
        if (empty($wxsend))
        {
            $getmaxordid=1;
            $maxordid=M('module_type')->max('ordid');
            if ($maxordid)
            {
                $getmaxordid=$maxordid+1;
            }
            $wxsend['ordid']=$getmaxordid;
        }

        if(IS_POST){
            $data = input('post.');
            if ($id) {
                $data['id']=$id;
                M("module_type")->update($data);
            }
            else {
                $sendlist = M('module_type')->where(array('id' => $id))->find();
                if ($sendlist) {
                    $this->error('该分类已存在!', U('Manager/Template/addtemplatetype'));
                    exit();
                } else {
                    M("module_type")->insert($data);
                }
            }
            if ($oldurl) {

                $this->success("操作成功!!!", $oldurl);
            } else {
                $this->success("操作成功!!!", U('Manager/Template/templatetype'));
            }
            exit;
        }

        $this->assign('wxsend',$wxsend);
        return $this->fetch('',getAdmStoId());

    }
    //删除推送消息
    public function deltemplatetype()
    {

        $getid=I('id/d');
        $modelist=M('store_module')->where(array('type_id' => $getid))->count();

        if ($modelist>0)
        {
            $return_arr = array('status' => -1,'msg' => '删除失败,此类型已存在记录','data'  =>'',);   //$return_arr = array('status' => -1,'msg' => '删除失败','data'  =>'',);
            $this->ajaxReturn($return_arr);
        }
        else {

            $deltype = M("module_type")->where(array('id' => $getid))->delete();
            if ($deltype) {
                $return_arr = array('status' => 1, 'msg' => '操作成功', 'data' => '',);   //$return_arr = array('status' => -1,'msg' => '删除失败','data'  =>'',);

            } else {
                $return_arr = array('status' => -1, 'msg' => '删除失败', 'data' => '',);
            }
            $this->ajaxReturn($return_arr);
        }
    }

    /**
     * 初始化编辑器链接
     * 本编辑器参考 地址 http://fex.baidu.com/ueditor/
     */
    private function initEditor()
    {
        $this->assign("URL_upload", U('admin/Ueditor/imageUp',array('savepath'=>'goods','savepath1'=>getERPId()))); // 图片上传目录
        $this->assign("URL_imageUp", U('admin/Ueditor/imageUp',array('savepath'=>'goods','savepath1'=>getERPId()))); //  不知道啥图片
        $this->assign("URL_fileUp", U('admin/Ueditor/fileUp',array('savepath'=>'goods','savepath1'=>getERPId()))); // 文件上传s
        $this->assign("URL_scrawlUp", U('admin/Ueditor/scrawlUp',array('savepath'=>'goods','savepath1'=>getERPId())));  //  图片流
        $this->assign("URL_getRemoteImage", U('admin/Ueditor/getRemoteImage',array('savepath'=>'goods','savepath1'=>getERPId()))); // 远程图片管理
        $this->assign("URL_imageManager", U('admin/Ueditor/imageManager',array('savepath'=>'goods','savepath1'=>getERPId()))); // 图片管理
        $this->assign("URL_getMovie", U('admin/Ueditor/getMovie',array('savepath'=>'goods','savepath1'=>getERPId()))); // 视频上传
        $this->assign("URL_Home", "");
    }

}