Index.php 32.2 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
<?php
/**
 * tpshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司,并保留所有权利。
 * 网站地址: http://www.tpshop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: IT宇宙人 2015-08-10 $
 *
 * TPshop为照顾新手,让新手们都能读懂代码,将代码写的比较通俗易懂,不写太过高深代码。
 */ 
namespace app\home\controller; 
use think\Controller;
use Think\Exception;
use think\Url;
use think\Config;
use think\Page;
use think\Verify;
use think\Db;
use think\Cookie;
use think\Cache;



class Index extends Base {

    public function _initialize() {
        $islogin=0;
        if (getAdminId()) {
            $islogin=1;
            $where = array('a.admin_id' => getAdminId());
            $res = M('admin')->alias('a')->join('store b', ' a.store_id=b.store_id', 'left')
                ->where($where)->field('b.isweb_reg,b.reg_mobile,b.ERPId,b.store_logo')->find();
            $this->assign("stoinfo",$res);
        }
        $this->assign("islogin",$islogin);

        define('MODULE_NAME',$this->request->module());  // 当前模块名称是
        define('CONTROLLER_NAME',$this->request->controller()); // 当前控制器名称
        define('ACTION_NAME',$this->request->action()); // 当前操作名称是
        define('PREFIX',C('database.prefix')); // 数据库表前缀
    }
    public function index(){
        $cururl=curHostURL()."/mobile/index/index/stoid";
        //置顶商家
        $storeinfo=M('store')->alias('a')->join('wx_user b',' a.store_id=b.store_id','left')
            ->where(array('a.isindex'=>1))->field('a.store_id,a.store_name,b.headerpic,b.signature')->limit(0,12)
            ->select();

        $this->assign("cururl",$cururl);
        $this->assign("storeinfo",$storeinfo);
        return $this->fetch('','',0);
    }
    //典型案例
    public  function caselist()
    {
        $cururl=curHostURL()."/mobile/index/index/stoid";

        //类型
        $typelist=M('store_industry')->select();
        $this->assign("typelist",$typelist);
        //商家

        $where['a.istop']=1;
        $gettypeid=I('typeid/d',0);
        if ($gettypeid)
        {
            $where['a.store_industry_id']=$gettypeid;
        }
        $count =M('store')->alias('a')->join('wx_user b',' a.store_id=b.store_id','left')
            ->where($where)->count('a.store_id');
        $pagenum=12;
        $Page = $pager = new Page($count,$pagenum);// 实例化分页类 传入总记录数和每页显示的记录数

        $storeinfo=M('store')->alias('a')->join('wx_user b',' a.store_id=b.store_id','left')
            ->where($where)->field('a.store_id,a.store_name,b.headerpic,b.signature')
            ->limit($Page->firstRow.','.$Page->listRows)
            ->select();
        $show = $Page->show();
        $this->assign('page',$show);
        $this->assign('total',$count);
        $this->assign("gettypeid",$gettypeid);
        $this->assign("cururl",$cururl);
        $this->assign("storeinfo",$storeinfo);
        return $this->fetch();
    }

//    public  function ajaxcaselist()
//    {
//
//        //商家
//        $type=I('type/d',0);
//        $where['a.istop']=1;
//        if ($type)
//        {
//            $where['a.store_industry_id']=$type;
//        }
//        $count =M('store')->alias('a')->join('wx_user b',' a.store_id=b.store_id','left')
//            ->where($where)->count('a.store_id');
//        $pagenum=4;
//        $Page = $pager = new Page($count,$pagenum);// 实例化分页类 传入总记录数和每页显示的记录数
//
//
//
//        $storeinfo=M('store')->alias('a')->join('wx_user b',' a.store_id=b.store_id','left')
//            ->where($where)->field('a.store_id,a.store_name,b.headerpic,b.signature')
//            ->limit($Page->firstRow.','.$Page->listRows)
//            ->select();
//        $show = $Page->show();
//        $this->assign('page',$show);
//        $this->assign('total',$count);
//        $this->assign("storeinfo",$storeinfo);
//        return $this->fetch();
//    }

    //商家注册
    public  function  register()
    {

        $act=I('act');
        $this->assign("act",$act);
        $xylist=M('store_industry')->order('ordid asc')->select();
        $this->assign("xylist",$xylist);
        $bussinessmodel=M('store_bussinessmodel')->order('ordid asc')->select();
        $this->assign("bussinessmodel",$bussinessmodel);
        $this->assign("QCLOUD_IMGURL",QCLOUD_IMGURL);

        return $this->fetch();
    }
    //验证码  校验
    public function first_reg()
    {
        $mobile = I('mobile');
        $sms_captcha = I('sms_captcha');
        if (empty($mobile) || empty($sms_captcha))
        {
            return json(array('code'=>-1,'msg'=>'手机或验证不能为空!'));
        }
        if (!$this->is_mobile($mobile))
        {
            return json(array('code'=>-1,'msg'=>'手机格式有误,请重新输入!'));
        }
        $res=M('reg_sms')->alias('a')->where(array('mobile'=>$mobile,'code'=>$sms_captcha,'type'=>1))->find();
        if (empty($res))
        {
            return json(array('code'=>-1,'msg'=>'验证码有误!'));
        }
        $row = M('reg_sms')->where(array('mobile' => $mobile, 'type' => 1, 'status' => 1))->delete();
        return json(array('code'=>1,'msg'=>'验证成功!'));

    }


    //修改新密码验证码  校验
    public function resetpass()
    {
        $mobile = I('mobile');
        $sms_captcha = I('sms_captcha');
        $password = I('password');
        $vertify = I('vertify');
        $getcodekey = I('hidecodekey');
        if (empty($mobile) || empty($sms_captcha) || empty($password))
        {
            return json(array('code'=>-1,'msg'=>'手机或验证或密码不能为空!'));
        }
        if (!$this->is_mobile($mobile))
        {
            return json(array('code'=>-1,'msg'=>'手机格式有误,请重新输入!'));
        }
        $true_codekey = md5($mobile."&".$vertify."&".getErpKey());
        if ($true_codekey != $getcodekey) {
            return json(array('code'=>-1,'msg'=>'图片验证码有错!'.$true_codekey));
        }

        $res=M('reg_sms')->alias('a')->where(array('mobile'=>$mobile,'code'=>$sms_captcha,'type'=>2))->find();
        if (empty($res))
        {
            return json(array('code'=>-1,'msg'=>'验证码有误!'));
        }
        $storeinfo=M('store')->where(array('reg_mobile'=>$mobile))->find();
        if (empty($storeinfo))
        {
            return json(array('code'=>-1,'msg'=>'找不到该手机注册信息!'));
        }

        $savedata['pwd']=md5($password);
        $modiinfo=M('admin')->where(array('store_id'=>$storeinfo['store_id'],'ERPUser'=>$mobile))->save($savedata);
        if ($modiinfo) {
            $row = M('reg_sms')->where(array('mobile' => $mobile, 'type' => 2, 'status' => 1))->delete();
            return json(array('code' => 1, 'msg' => '修改成功!'));
        }else
        {
            return json(array('code' => -1, 'msg' => '修改失败!'));
        }

    }

    //提交 完成
    public  function sumbit_reg()
    {
        $data=I('post.');
        if (empty($data))
        {
            return json(array('code'=>-1,'msg'=>'提交信息有误!'));
        }
        $vertify=$data['vertify'];
        $mobile=$data['mobile'];
        $getcodekey=$data['codekey'];
        $true_codekey = md5($mobile."&".$vertify."&".getErpKey());
        if ($true_codekey != $getcodekey) {
            return json(array('code'=>-1,'msg'=>'图片验证码有错!'));
        }

        $stoinfo['store_name']=$data['store_company'];
        $stoinfo['store_company']=$data['store_company'];
        $stoinfo['reg_mobile']=$data['mobile'];
        $stoinfo['store_industry_id']=$data['store_industry_id'];
        $stoinfo['business_model_id']=$data['business_model_id'];
        $stoinfo['sms_mobile']=$data['mobile'];
        $stoinfo['store_tel']=$data['mobile'];
        $stoinfo['store_contact']=$data['store_contact'];
        $stoinfo['store_address']=$data['store_city'].$data['store_address'];
        $stoinfo['business_license']=$data['business_license'];
        $stoinfo['business_licenseimg']=$data['business_licenseimg'];
        $stoinfo['is_audit']=0;
        $stoinfo['isweb_reg']=1;
        $stoinfo['BillDate']=time();
        $stoinfo['BillIp']=getIP();
        $sto_add=M('store')->add($stoinfo);

       if ($sto_add)
       {
           //添加admin角色
           $role['store_id'] = $sto_add;
           $role['role_name'] = '超级管理员';
           $role['act_list'] = 'all';
           $role['role_desc'] = '管理全站';
           $ro = D('role')->add($role);        //新增的角色ID

           if ($ro) {
               $admin['store_id'] = $sto_add;
               $admin['ERPUser'] = $data['mobile'];
               $admin['ERPName'] = $data['store_contact'];
               $admin['role_id'] = $ro;
               $admin['BillDate'] = time();
               $admin['BillIp'] = getIP();
               $admin['pwd'] = md5($data['password']);
               $admininfo = D('admin')->add($admin);
               return json(array('code'=>1,'msg'=>'提交成功!'));
           }
           else
           {
               return json(array('code'=>-1,'msg'=>'提交失败!'));
           }

       }
       else
       {
           return json(array('code'=>-1,'msg'=>'提交失败!'));
       }

    }

    //验证发送
    public function reg_sms()
    {
        $mobile = I('mobile');
        $vertify = I('vertify');
        $getcodekey =substr(I('codekey'),0,50);
        $type = I('type/d',1);
        if (empty($mobile))
        {
            return json(array('code'=>-1,'msg'=>'手机或验证不能为空!'));
        }
        if (!$this->is_mobile($mobile))
        {
            return json(array('code'=>-1,'msg'=>'手机格式有误,请重新输入!'));
        }
        if (empty($vertify) || strlen($vertify)!=4 || empty($getcodekey))
        {
            return json(array('code'=>-1,'msg'=>'图片验证为空或位数不足4位!'));
        }
        $true_codekey = md5($mobile."&".$vertify."&".getErpKey());
        if ($true_codekey != $getcodekey) {
            return json(array('code'=>-1,'msg'=>'图片验证码有错!'));
        }
        //注册是发送短信
        if ($type==1){
            $storeinfo=M('store')->where(array('reg_mobile'=>$mobile))->find();
            if($storeinfo){
                return json(array('code'=>-1,'msg'=>'该手机已经注册过用户!'));
            }
        }
        if ($type==2)
        {
            $storeinfo=M('store')->where(array('reg_mobile'=>$mobile))->find();
            if (empty($storeinfo))
            {
                return json(array('code'=>-1,'msg'=>'找不到该手机注册信息!'));
            }
        }

        //判断是否存在验证码
        $data = M('reg_sms')->where(array('mobile' => $mobile, 'type' => $type))->order('id DESC')->find();
        //获取时间配置
        $sms_time_out = 60;
        if ($data && (time() - $data['add_time']) < $sms_time_out) {
            $return_arr = array('status' => -1, 'msg' => $sms_time_out . '秒内不允许重复发送');
            return json($return_arr);
        } else {
            //随机一个验证码
            $code = rand(100000, 999999);
            $row = M('reg_sms')->add(array('mobile' => $mobile, 'code' => $code, 'add_time' => time(), 'type' => $type, 'status' => 0));

            $getparam = "&smsTemplateCode=SMS_117245087&parameters=['" . $code . "']";
            $poststr = "phone=" . $mobile . "&msgOrigin=WXD&smsFreeSignName=万信达软件" . $getparam;
            $resp = httpRequest("http://source-api.yolipai.com/external/sms/alidayu/send", "post", $poststr);
            $resp = json_decode($resp, true);
            if ($resp['code'] == 0) {
                //发送成功, 修改发送状态位成功
                M('reg_sms')->where(array('mobile' => $mobile, 'code' => $code, 'type' => $type, 'status' => 0))->save(array('status' => 1));
                return json(array('code'=>1,'msg'=>'发送成功,请注意查收'));
            }
            else
            {
                return json(array('code'=>-1,'msg'=>'发送失败'. $resp['msg']));
            }


        }


    }
    //商家登陆
    public  function  login()
    {

        $getcook_loginuser=cookie("home_loginuser");

        $this->assign('cookie_loginuser',$getcook_loginuser);
        return $this->fetch();
    }
    //登陆POST
    public function sumbitlogin()
    {
        $mobile = I('mobile');
        $password = I('password');
        $autologin = I('autologin');
        if (empty($mobile) || empty($password))
        {
            return json(array('code'=>-1,'msg'=>'用户名或密码不能为空!'));
        }
        $password_md5=md5($password);
        $mobile_arry=explode("@",$mobile);

        if (count($mobile_arry)==2)
        {
            $login_user=$mobile_arry[0];
            $login_mobile=$mobile_arry[1];
        }
        else{
            $login_user=$mobile;
            $login_mobile=$mobile;
        }

        $where="";
        if ($this->is_mobile($login_mobile))
        {
            $where=array('b.reg_mobile'=>$login_mobile,'a.ERPUser'=>$login_user,'a.pwd'=>$password_md5);
        }
        else{
            $where=array('b.ERPId'=>$login_mobile,'a.ERPUser'=>$login_user,'a.pwd'=>$password_md5);
        }


        $res=M('admin')->alias('a')->join('store b',' a.store_id=b.store_id','left')
            ->where($where)->find();
        if (empty($res))
        {
            return json(array('code'=>-1,'msg'=>'用户名或密码有误!'));
        }

        if ($res['is_audit']==0)
        {
            return json(array('code'=>-1,'msg'=>"该账户正在审核中,我们将在72小时内处理,请耐心等待!"));
        }
        else if ($res['is_audit']==-1)
        {
            return json(array('code'=>-1,'msg'=>$res['fail_remak']));
        }

        $fieldstr="";
        if ($this->is_mobile($login_mobile)) {
            $fieldstr = "mobile=" . $res['reg_mobile'] . "&ERPUser=" . $res['ERPUser'];
        }else
        {
            $fieldstr = "ERPId=" . $res['ERPId'] . "&ERPUser=" . $res['ERPUser'];
        }
        $gofieldstr = $fieldstr;

        $fieldstr .= "&Key=" . getErpKey();
        $fieldstr1 = strtolower($fieldstr);
        $key = md5($fieldstr1);
        $poststr = $gofieldstr . "&key=" . $key;
        $poststr = urlencode($poststr);


        //记住账号
        if ($autologin)
        {

            Cookie::set("home_loginuser",$mobile);
        }
        else{
            Cookie::set("home_loginuser","");
        }
        return json(array('code'=>1,'link'=>$poststr,'msg'=>'登陆成功!'));


    }

    function is_mobile( $text ) {
        $search = '/^0?1[2|3|4|5|6|7|8|9][0-9]\d{8}$/';
        if ( preg_match( $search, $text ) ) {
            return ( true );
        } else {
            return ( false );
        }
    }
    //找回密码
    public  function  reset_password()
    {
        return $this->fetch();
    }




    //ERP手店资料申请
    public function apply_shop(){

        $str = strtolower(I('url'));
        $str=str_replace('data=','',$str);
        $str=urldecode($str);
        $fieldstr="";
        if($str!="") {
            $sReqData1 = explode('&', $str);
            $ary = array();
            $arrlength = count($sReqData1);
            for ($i = 0; $i < $arrlength; $i++) {
                $ar = explode('=', $sReqData1[$i]);
                $k = $ar[0];
                $v = $ar[1];
                $ary[$k] = $v;
                if ($i<$arrlength-1)
                {
                    $fieldstr.=($i==0?"":"&").$k."=".strtolower(urlencode($v));
                }
            }

            $fieldstr.="&key=".getErpKey();
            if ($ary['key'] !=md5($fieldstr)) {
                goUrl('/err.html?msg=' . urlencode("Key值有误!"));
            }

           $info=M('store')->where(array('ERPId'=>$ary['erpid']))->find();
           if ($info)
           {
               $a = D('admin')->where(array('store_id'=>$info['store_id']))->where("ERPUser<>'admin'")->field('ERPUser')->select();
              // echo json_encode($info);
               $info['CORFRNAME']=$info['store_name'];
               $info['CORNAME']=$info['store_contact'];
               $info['CORFRTEL']=$info['store_tel'];

               $getconvertimg=explode(",",$info['convertimg']);

               $getERPUser="";
               foreach ($a as $v=>$value )
               {
                   if ($getERPUser=="")
                   {
                       $getERPUser=$value['ERPUser'];
                   }
                   else{
                       $getERPUser.=",".$value['ERPUser'];
                   }

               }
               $info['ERPUser']=$getERPUser;
               $wxuser=M('wx_user')->where('store_id',$info['store_id'])->find();

               $info['WX_appid_Pay']=$wxuser['appid'];
               $info['WX_secret_Pay']=$wxuser['appsecret'];
               $info['WX_MCHID']=$wxuser['mchid'];
               $info['WX_APIKey']=$wxuser['mchkey'];
               $this->assign('info', $info);
               $this->assign('convertimg', $getconvertimg);
           }
           else
           {

               try {
                   $oldinfo = httpRequest(IMPORT_URL . "/API/CustInfo.ashx?method=shopinfo&ERPDBAccID=" . $ary['erpid']);

                   if ($oldinfo) {
                       $oldinfo = json_decode($oldinfo, true);
                       if ($oldinfo['code'] == 0) {
                           $oldinfodata = $oldinfo['data']['item'][0];
                           $ary['WX_appid_Pay'] = $oldinfodata['WX_appid_Pay'];
                           $ary['WX_secret_Pay'] = $oldinfodata['WX_secret_Pay'];
                           $ary['WX_MCHID'] = $oldinfodata['WX_MCHID'];
                           $ary['WX_APIKey'] = $oldinfodata['WX_APIKey'];
                       }
                   }

                   if ($ary['erpid']) {
                       $accinfo = httpRequest(WXD_OPENURL . "api/wxd/customer/page?accdb=".$ary['erpid']);
                       if ($accinfo)
                       {
                           $accinfo=json_decode($accinfo,true);
                           if ($accinfo['data']['pageData'][0]) {
                               $accinfodata =$accinfo['data']['pageData'][0];
                               $ary['corfrname'] = $accinfodata['CustName'];
                               $ary['corname'] = $accinfodata['BossMan'];
                               $ary['corfrtel'] = $accinfodata['MobileTel'];
                               $ary['soft_cloud'] = $accinfodata['YunS'];

                           }
                       }

                   }
               }
               catch (Exception $ex)
               {

               }

               $this->assign('info', $ary);
           }

        }

        return $this->fetch();
    }
 
    /**
     *  公告详情页
     */
    public function notice(){
        return $this->fetch();
    }
    
    // 二维码
    public function qr_code(){
        // 导入Vendor类库包 Library/Vendor/Zend/Server.class.php
        //http://www.tp-shop.cn/Home/Index/erweima/data/www.99soubao.com
         require_once 'vendor/phpqrcode/phpqrcode.php';
          //import('Vendor.phpqrcode.phpqrcode');
            $errorCorrectionLevel = "L"; // 纠错级别:
            error_reporting(E_ERROR);            
            $url = urldecode(urldecode($_GET["data"]));
            ob_clean();
        ob_end_clean();
            \QRcode::png($url);

    }



    // 验证码
    public function verify()
    {
        //验证码类型
        $type = I('get.type') ? I('get.type') : '';
        $fontSize = I('get.fontSize') ? I('get.fontSize') : '40';
        $length = I('get.length') ? I('get.length') : '4';
        
        $config = array(
            'fontSize' => $fontSize,
            'length' => $length,
            'useCurve' => true,
            'useNoise' => false,
        );
        $Verify = new Verify($config);
        $Verify->entry($type);        
    }
    


    public function apply_shop_Handle(){
        try {
            $data = I('post.');
            $wxverfiy = $data['wxverfiy'];
            $wxverfiy=str_replace(".txt","",$wxverfiy);
            $wxverfiy=trim($wxverfiy);

            $file = $wxverfiy . ".txt";
            $wxverfiy1 = str_replace("MP_verify_", "", $wxverfiy);
            file_put_contents($file, $wxverfiy1);

            $server_url=serverIP();
            $url=explode(',',IP_ARRAY);
            $err=true;
            foreach ($url as $k => $v) {
                if ($v != $server_url) {
                    $url = 'http://' . $v . '/index.php/home/index/receiveStreamFile/file/' . $file;
                    $a = $this->sendStreamFile($url, $file);
                    if (empty($a) || $a === false) {
                        $err = false;
                    }
                }
            }
            if(!$err){
                $this->ajaxReturn(array('status' => -1, 'msg' => '申请失败!'));
                exit();
            }

            $staff_num = ($data['staff_num']);//员工编号
            unset($data['staff_num']);//过滤员工编号
            $data['BillDate'] = time();
            $data['BillIp'] = getIP();
            $data['is_audit'] = 0;//申请的商家默认未审核
            $data['ERPId']=strtolower($data['ERPId']);

            $erpres=httpRequest(WXDURL."?access_token=wxdoa.cust.all.list.get&accdb=".$data['ERPId'],'get');
            $erpres=json_decode($erpres,true);
            if ($erpres && $erpres['code']==1) {
                $api_token=$erpres['data'][0]['token'];
            }
            $data['api_token'] = $api_token;
            $info_store=D('store')->where("ERPId", $data['ERPId'])->find();
            if ($info_store) {
               // $this->error("此帐套已存在,请更换", U('manager/Admin/store_info'));
                $r = D('store')->where('store_id',$info_store['store_id'])->save($data);

                //默认给基本设置添加一条记录
                $ifo['default_storage']=100;   //默认库存
                $ifo['warning_storage']=10;    //预警库存
                $ifo['distribut_need']=100;    //需要多少提现
                $ifo['distribut_min']=100;     //最少提现多少钱
                $ifo['categoryset']=',1,2,3,'; //分类默认都显示
                $ifo['refund_type']=1;         //退款默认原路返回
                $ifo['is_sort_storage']=0;     //门店GPS默认关闭
                $ifo['switch_list']='{"jfcz_switch":1,"dhwz_switch":1}';  //默认显示积分,微券兑换
                $ifo['sales_rules']=1;         //默认线上库存
                $ifo['inv_type']=0;
                $ifo['invitation_rate']='{"inv_jf":"0","inv_coupon":""}';  //邀请规则
                $ifo['reg_type']=0;
                $ifo['reg_default']='{"reg_def_ty":"0","jf":"0","coupon":""}'; //注册规则
                $uo=M('store_config')->where('store_id',$info_store['store_id'])->find();
                if($uo)
                    $wxuser=M('store_config')->where('Id',$uo['Id'])->save($ifo);
                else{
                    $ifo['store_id']=$info_store['store_id']; //注册规则
                    $wxuser=M('store_config')->save($ifo);
                }

                /*--默认商家分销人员--*/
                $uo=M('store_distribut')->where('store_id',$info_store['store_id'])->find();
                $yy=M('distri_price')->where('type',0)->where('money',0)->find();
                if($uo){
                    $op['store_id']=$info_store['store_id'];   //默认库存
                    $op['distribut_num']=$yy['user_num'];
                    M('store_distribut')->save($op);
                }else{
                    $op['distribut_num']=$yy['user_num'];
                    M('store_distribut')->where('store_id',$info_store['store_id'])->save($op);
                }

                /*--清理缓存数据--*/
                $stoid=$info_store['store_id'];
                $arr = getCachaArr($stoid);
                foreach ($arr as $b) {
                    Cache::rm($b);
                }
                delFile(TEMP_PATH . "/" . $stoid);


                //修改到微信表
                $wx['wx_username'] = $data['wx_username'];
                $wx['wx_pwd'] = $data['wx_pwd'];
                $wx['appid'] = $data['appid'];
                $wx['appsecret'] = $data['appsecret'];
                $wx['mchid'] = $data['mchid'];
                $wx['mchkey'] = $data['mchkey'];
                $wx['token'] = $data['appid'].time();
                $wx_l = D('wx_user')->where('store_id',$info_store['store_id'])->save($wx);
                if ($wx_l && $r) {
                    $this->ajaxReturn(array('status' => 1, 'msg' => '提交成功!'));
                }else
                {
                    $this->ajaxReturn(array('status' => -1, 'msg' => '提交失败!'));
                }


            } else {
                //添加商家
                //$SupplyId = uuid();

                $r = D('store')->add($data);   //新增的商家ID
                ///后续操作
                if ($r) {

                    $mshop_role_info=M('role')->where(array('store_id'=>197,'role_name'=>'管理员'))->find();
                    //添加商家的admin角色
                    $role['store_id'] = $r;
                    $role['role_name'] = '超级管理员';
                    $role['act_list'] = 'all';
                    $role['role_desc'] = '管理全站';
                    $ro = D('role')->add($role);        //新增的角色ID


                    $role1['store_id'] = $r;
                    $role1['role_name'] = '管理员';
                    $role1['act_list'] = $mshop_role_info['act_list'];
                    $role1['role_desc'] = '管理员';
                    $ro1 = D('role')->add($role1);        //新增的角色ID

                    if ($ro) {
                        $staff_num = $staff_num . ",admin"; //添加默认admin用户

                        $staff_num=str_replace(",",",",$staff_num);
                        $array = explode(",", $staff_num);
                        //添加商家用户
                        for ($i = 0; $i < count($array); $i++) {
                            //添加用户
                            if ($array[$i] == "admin") {
                                $admin['role_id'] = $ro;
                                $admin['ERPName'] = '超级管理员';
                            }
                            else
                            {
                                $admin['role_id'] = $ro1;
                                $admin['ERPName'] = $array[$i];
                            }
                            $admin['store_id'] = $r;
                            $admin['ERPId'] = strtolower($data['ERPId']);
                            $admin['ERPUser'] = $array[$i];
                            $admin['BillIp'] = getIP();
                            $admin['BillDate'] = time();

                            $a = D('admin')->add($admin);
                            if (!$a) {
                                $this->ajaxReturn(array('status' => -1, 'msg' => '申请失败!'));
                            }
                        }
                    }

                    //默认给基本设置添加一条记录
                    $ifo['default_storage']=100;   //默认库存
                    $ifo['warning_storage']=10;    //预警库存
                    $ifo['distribut_need']=100;    //需要多少提现
                    $ifo['distribut_min']=100;     //最少提现多少钱
                    $ifo['categoryset']=',1,2,3,'; //分类默认都显示
                    $ifo['refund_type']=1;         //退款默认原路返回
                    $ifo['is_sort_storage']=0;     //门店GPS默认关闭
                    $ifo['switch_list']='{"jfcz_switch":1,"dhwz_switch":1}';  //默认显示积分,微券兑换
                    $ifo['sales_rules']=1;         //默认线上库存
                    $ifo['inv_type']=0;
                    $ifo['invitation_rate']='{"inv_jf":"0","inv_coupon":""}';  //邀请规则
                    $ifo['reg_type']=0;
                    $ifo['reg_default']='{"reg_def_ty":"0","jf":"0","coupon":""}'; //注册规则
                    $uo=M('store_config')->where('store_id',$r)->find();
                    if($uo)
                        $wxuser=M('store_config')->where('Id',$uo['Id'])->save($ifo);
                    else{
                        $ifo['store_id']=$r; //注册规则
                        $wxuser=M('store_config')->save($ifo);
                    }


                    /*--清理缓存数据--*/
                    $stoid=$r;
                    $arr = getCachaArr($stoid);
                    foreach ($arr as $b) {
                        Cache::rm($b);
                    }
                    delFile(TEMP_PATH . "/" . $stoid);


                    //添加到微信表
                    $wx['store_id'] = $r;
                    $wx['wx_username'] = $data['wx_username'];
                    $wx['wx_pwd'] = $data['wx_pwd'];
                    $wx['appid'] = $data['appid'];
                    $wx['appsecret'] = $data['appsecret'];
                    $wx['mchid'] = $data['mchid'];
                    $wx['mchkey'] = $data['mchkey'];
                    $wx['token'] = $data['appid'].time();
                    $wx_l = D('wx_user')->add($wx);
                    if ($wx_l) {
                        $this->ajaxReturn(array('status' => 1, 'msg' => '申请成功!'));
                    }
                } else {
                    //添加失败
                    $this->ajaxReturn(array('status' => -1, 'msg' => '申请失败!'));
                }
            }
        }
        catch (Exception $ex)
        {
            mlog($ex,"apply_shop");
            $this->ajaxReturn(array('status' => -1, 'msg' => '异常添加失败!'));
        }
    }

    /** php 发送流文件
     * @param  String $url 接收的路径
     * @param  String $file 要发送的文件
     * @return boolean
     */
    public function sendStreamFile($url, $file)
    {

        if (file_exists($file)) {

            $opts = array(
                'http' => array(
                    'method' => 'POST',
                    'header' => 'content-type:application/x-www-form-urlencoded',
                    'content' => file_get_contents($file)
                )
            );

            $context = stream_context_create($opts);
            $response = file_get_contents($url, false, $context);
            $ret = json_decode($response, true);
            return $ret;

        } else {
            return false;
        }
    }

    /** php 接收流文件
     * @param  String $file 接收后保存的文件名
     * @return boolean
     */
    public function receiveStreamFile()
    {
        $receiveFile = I('file');

        $streamData = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : '';

        if (empty($streamData)) {
            $streamData = file_get_contents('php://input');
        }

        if ($streamData != '') {
            $ret = file_put_contents($receiveFile, $streamData, true);
        } else {
            $ret = false;
        }
        return $ret;
    }

    // 二维码,64位
    public function qr_code_64($url){
        // 导入Vendor类库包 Library/Vendor/Zend/Server.class.php
        //http://www.tp-shop.cn/Home/Index/erweima/data/www.99soubao.com
        require_once 'vendor/phpqrcode/phpqrcode.php';
        //import('Vendor.phpqrcode.phpqrcode');
        $errorCorrectionLevel = "L"; // 纠错级别:
        error_reporting(E_ERROR);
        $url = urldecode(urldecode($url));
        //ob_clean();
        //ob_end_clean();
        //\QRcode::png($url);
        $QRcode = new \QRcode();
        ob_start();
        $QRcode->png($url);
        $imageString = base64_encode(ob_get_contents());
        ob_end_clean();
        return "data:image/jpg;base64,".$imageString;
    }

}