MobileBase.php 29.1 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
<?php
/**
 * tpshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司,并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: IT宇宙人 2015-08-10 $
 */
namespace app\mobile\controller;
use app\home\logic\UsersLogic;
use think\Controller;
use think\Session;
use think\Db;
use think\Cookie;

class MobileBase extends Controller {
    public $session_id;
    public $weixin_config;
    public $cateTrre = array();
    public  $pm_erpid;

    public  $getopenid_num=0;
    public  $tpshop_config=null;
    
    /*
     * 初始化操作
     */
    public function _initialize() {

        //Session::start();
        if(!session_id()) Session::start();
        //是否有带商家值
        if (getMobileStoId()=="" || !is_numeric(getMobileStoId()))
        {
            $this->redirect('/err_mobile.html?y=1');
        }
        //商家是否审核或存在
        //$resstore=M('store')->where(array('store_id'=>getMobileStoId(),'is_audit'=>1))->find();
        $resstore=tpCache("shop_info.is_audit",getMobileStoId());
        if(empty($resstore)){
            $this->redirect('/err_mobile.html?y=2');
        }
        if ($resstore!=1)
        {
            $this->redirect('/err_mobile.html?y=3');
        }

        //是否开启维护状态
        $r=getManagerConf();

        $arr= explode(',',$r['storelist']);
        if($r['site_status']==1 && !in_array(getMobileStoId(),$arr)){
            $this->redirect('/up_mobile.html');
        }

        session('user');
        header("Cache-control: private");
        $this->session_id = session_id(); // 当前的 session_id
        define('SESSION_ID',$this->session_id); //将当前的session_id保存为常量,供其它方法调用
        // 判断当前用户是否手机                
        if(isMobile())
            cookie('is_mobile','1',3600); 
        else 
            cookie('is_mobile','0',3600);
         //是否用户推荐USERID
        if (I('first_leader'))
        {
            Cookie::set("first_leader",I('first_leader'));
        }

        $this->request->isAjax() ? define('IS_AJAX',true) : define('IS_AJAX',false);  //
        ($this->request->method() == 'GET') ? define('IS_GET',true) : define('IS_GET',false);  //
        ($this->request->method() == 'POST') ? define('IS_POST',true) : define('IS_POST',false);  //

        define('MODULE_NAME',$this->request->module());  // 当前模块名称是
        define('CONTROLLER_NAME',$this->request->controller()); // 当前控制器名称
        define('ACTION_NAME',$this->request->action()); // 当前操作名称是

       if(ACTION_NAME!="ajaxGetMore" && ACTION_NAME!="cart3"  && ACTION_NAME!="getStorageList" ) {
            $nowexin = ['goodsInfo','getNum','getGoodslimit','getStorageList','ajaxComment','ajaxdetail','template'];
            $arr_debug= explode(',',$r['storelist_debug']);
            if(!in_array(getMobileStoId(),$arr_debug) &&  !in_array(ACTION_NAME, $nowexin)){
                if (!strstr($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger')) {
                    echo " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0\" /><p style='width: 100%; margin: 0 auto;max-width: 720px; font-size: 18px; padding-top: 10px;  text-align:center'>请在微信浏览器中打开!<p>";
                    die;

                }
            }
            //微信浏览器
            if(strstr($_SERVER['HTTP_USER_AGENT'],'MicroMessenger')){
                if(empty($_SESSION['wx_stoid'])|| $_SESSION['wx_stoid']!=getMobileStoId()){

                    $this->wei_xin_set();

                }else{

                    if ($_SESSION['openid']) {

                        /*--如果有存在session('?user')--*/
                        if (session('?user')) {

                            $user=session('user');
                            if($user['openid']!=$_SESSION['openid'] || $user['store_id']!=getMobileStoId()){

                                $this->wei_xin_set();

                            }
                        } else {

                            $this->wei_xin_set();

                        }

                    }else {

                        $this->wei_xin_set();

                    }
                }
            }
            $user_id=session('user')['user_id'];
            $erpvipid=session('user')['erpvipid'];
            $user_mobile=session('user')['mobile'];
            //
            setcookie('erpvipid',$erpvipid,null,'/');
            setcookie('user_mobile',$user_mobile,null,'/');
            //获取页面地址
            getlasturl();
            $this->public_assign();

            /*--更新最后登录时间--*/
            $user00=session('user');
            $a_date=date('Y-m-d',$user00['last_login']);
            $n_date=date('Y-m-d');
            if($a_date!=$n_date){
                M('users')->where('store_id',getMobileStoId())->where('user_id',$user00['user_id'])
                    ->save(['last_login'=>time()]);
                $user00['last_login']=time();
                session('user',$user00);  //更新会员的登录时间
            }
        }




    }

    /*--微信设置--*/
    public  function wei_xin_set(){

            $stoid = getMobileStoId();
            $this->weixin_config = M('wx_user')->where('store_id=' . $stoid)->find(); //获取微信配置
            $this->assign('wechat_config', $this->weixin_config);
            if (is_array($this->weixin_config) && $this->weixin_config['wait_access'] == 1) {
                $wxuser = $this->getwxuser(); //授权获取openid以及微信用户信息

                if (!empty($wxuser['errcode'])) {
                    $this->redirect('/noauthorize.html?stoid=' . $stoid . '&er=' . $wxuser['errcode']);
                }

                if (empty($wxuser['subscribe'])) {
                    $wxuser['subscribe'] = 0;
                }
                //微信授权是否有关注,没有插入记录或更新关注
                if ($wxuser['subscribe'] == 1) {
                    $wxopenlist = M('wx_openlist')->where(array('store_id' => $stoid, 'wxopenid' => $wxuser['openid']))->find();
                    if ($wxopenlist) {
                        if ($wxopenlist['issubscribe'] == 0) {
                            $userdata['issubscribe'] = 1;
                            M('wx_openlist')->where('store_id', $stoid)->where('wxopenid', $wxuser['openid'])->save($userdata);
                        }
                    } else {
                        $wx_users = M('wx_user')->where('store_id', $stoid)->field('wxid')->find();
                        $userdata['store_id'] = getMobileStoId();
                        $userdata['wxid'] = $wx_users['wxid'];
                        $userdata['wxopenid'] = $wxuser['openid'];
                        $userdata['issubscribe'] = 1;
                        $userdata['subscribe_date'] = time();
                        M('wx_openlist')->insertGetId($userdata);
                    }
                }

                //是否关注了公众号
                session('subscribe', $wxuser['subscribe']);// 当前这个用户是否关注了微信公众号
                $this->assign('is_sub', $wxuser['subscribe']);

                //微信自动登录
                $logic = new UsersLogic();
                $data = $logic->thirdLogin($wxuser, getMobileStoId());


                if ($data['status'] == 1) {
                    session('user', $data['result']);
                    setcookie('user_id', $data['result']['user_id'], null, '/');
                    session('get_vip_base', get_vip_base());//日志
                    $is_d = $data['result']['is_distribut'];
                    if (empty($is_d)) {
                        $is_d = "0";
                    }
                    setcookie('is_distribut', $is_d, null, '/');
                    setcookie('uname', $data['result']['nickname'], null, '/');
                    // 登录后将购物车的商品的 user_id 改为当前登录的id( 暂时没有用,不要启用)
//                $cartLogic = new \app\home\logic\CartLogic();
//                $cartLogic->login_cart_handle($this->session_id,$data['result']['user_id']);  //用户登录后 需要对购物车 一些操作
                }
            }

    }

    
    /**
     * 保存公告变量到 smarty中 比如 导航 
     */   
    public function public_assign()
    {

            $tpshop_config = array();
            /*---
            $tp_config = M('store')->alias('a')->join('store_config b','b.store_id= a.store_id','left')->join('store_distribut c',' c.store_id=a.store_id','left') ->join('wx_user d',' d.store_id=a.store_id','left')
                ->field('b.hot_keywords,a.store_qqlist,a.store_name,a.store_keyword,a.store_desc,c.switch,a.store_tel,c.name,c.first_name,c.second_name,c.third_name,d.qrcode_url,a.store_logo,a.ERPId,b.categoryset,b.refund_type,b.switch_list')
                ->cache("mobilebase_".getMobileStoId(),3600)->where("a.store_id",getMobileStoId())->find();
            ---*/
            $tp_config = tpCache('shop_info', getMobileStoId());
            $tp_config1 = tpCache('basic', getMobileStoId());
            if ($tp_config1)
                $tp_config = array_merge($tp_config, $tp_config1);
            $tp_config2 = tpCache('distribut', getMobileStoId());
            if ($tp_config2)
                $tp_config = array_merge($tp_config, $tp_config2);
            $tp_config3 = M('wx_user')->cache("mobilebase_" . getMobileStoId(), 3600)->where("store_id", getMobileStoId())->find();
            if ($tp_config3)
                $tp_config = array_merge($tp_config, $tp_config3);

            $tpshop_config['hot_keywords'] = explode('|', $tp_config['hot_keywords']);
            $tpshop_config['qqlist'] = $tp_config['store_qqlist'];
            $tpshop_config['user_id'] = session('user')['user_id'];
            $tpshop_config['is_distribut'] = session('user')['is_distribut'];
            $tpshop_config['shop_info_store_title'] = $tp_config['store_name'];
            $tpshop_config['shop_info_store_keyword'] = $tp_config['store_keyword'];
            $tpshop_config['shop_info_store_desc'] = $tp_config['store_desc'];
            $tpshop_config['shop_info_phone'] = $tp_config['store_tel'];
            $tpshop_config['qrcode_url'] = $tp_config['qrcode_url'];
            $tpshop_config['distribut_switch'] = $tp_config['switch'];
            $tpshop_config['distribut_name'] = $tp_config['name'];
            $tpshop_config['distribut_first_name'] = $tp_config['first_name'];
            $tpshop_config['distribut_second_name'] = $tp_config['second_name'];
            $tpshop_config['distribut_third_name'] = $tp_config['third_name'];

            $tpshop_config['shop_info_store_logo'] = $tp_config['store_logo'];
            $tpshop_config['categoryset'] = $tp_config['categoryset'];
            $tpshop_config['refund_type'] = $tp_config['refund_type'];
            if ($tp_config['switch_list']) {
                $switch_list = json_decode($tp_config['switch_list'], true);
                $tpshop_config['jfcz_switch'] = $switch_list['jfcz_switch'];
                $tpshop_config['dhwz_switch'] = $switch_list['dhwz_switch'];
                $tpshop_config['yckcz_switch'] = $switch_list['yckcz_switch'];
                $tpshop_config['rank_switch'] = $switch_list['rank_switch'];
                $tpshop_config['is_closecoupon'] = $switch_list['is_closecoupon'];
            }
            if ($tp_config['mzprice_list']) {
                $mz = json_decode($tp_config['mzprice_list'], true);
                $tpshop_config['is_beauty'] = $mz['is_beauty'];
                $tpshop_config['mz_con'] = $mz['mz_con'];
                $tpshop_config['mz_tran'] = $mz['mz_tran'];
                $tpshop_config['mz_money'] = $mz['mz_money'];
                $tpshop_config['mz_goods_id'] = $mz['mz_goods_id'];
                $tpshop_config['mz_goods_name'] = $mz['mz_goods_name'];
                $tpshop_config['mz_time'] = $mz['mz_time'];
            }
            $tpshop_config['service_bz'] = $tp_config1['service_bz'];

            /*--制作自定义导航--*/
            $ms = M('store_footer')->where('store_id', getMobileStoId())->field('id')
                ->where('is_use', 1)->find();
            if ($ms) {
                $pa = '/public/template/footer/' . getMobileStoId() . '/' . $ms['id'] . '.html';
                if (!file_exists(ROOT_PATH . $pa)) {
                    $pa = "public/empty";
                } else {
                    $pa = "." . $pa;
                }
                $this->assign('fcustom_footer', $pa);
            } else {
                $this->assign('fcustom_footer', 'public/empty');
            }

            $this->pm_erpid = tpCache('shop_info.ERPId', getMobileStoId());
            $is_sort_storage = tpCache("basic.is_sort_storage", getMobileStoId());
            $this->assign('is_sort_storage', $is_sort_storage);
            $this->assign('tpshop_config', $tpshop_config);
            $this->assign('pm_erpid', $this->pm_erpid);
            $this->assign('baseImgHost', QCLOUD_IMGURL);

            $this->assign('mini_host', Mini_Host());

    }      

    // 网页授权登录获取 OpendId
    public function GetOpenid()
    {

        if($_SESSION['openid'] && $_SESSION['wx_stoid'] && $_SESSION['wx_stoid']==getMobileStoId())
            return $_SESSION['openid'];
        //通过code获得openid
        if (!isset($_GET['code'])){
            //触发微信返回code码
            //$baseUrl = urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']);
            $baseUrl = urlencode($this->get_url());
            $url = $this->__CreateOauthUrlForCode($baseUrl); // 获取 code地址
            Header("Location: $url"); // 跳转到微信授权页面 需要用户确认登录的页面
            exit();
        } else {
            //上面获取到code后这里跳转回来
            $code = $_GET['code'];
            $data = $this->getOpenidFromMp($code);//获取网页授权access_token和用户openid
            $data2 = $this->GetUserInfo($data['access_token'],$data['openid']);//获取微信用户信息
            $data['nickname'] = empty($data2['nickname']) ? '微信用户' : trim($data2['nickname']);
            $data['sex'] = $data2['sex'];
            $data['head_pic'] = $data2['headimgurl']; 
            $data['subscribe'] = $data2['subscribe'];
            $_SESSION['openid'] = $data['openid'];
            $_SESSION['wx_stoid'] = getMobileStoId();
            $data['oauth'] = 'weixin';
            if(isset($data['unionid'])){
            	$data['unionid'] = $data['unionid'];
            }
            return $data;
        }
    }

    /*--获取微信信息--*/
    public function getwxuser()
    {
        $stoid = getMobileStoId();
        $cookie_code = Cookie::get("code" . $stoid);
        $cookie_data=Cookie::get("code_data" . $stoid);
        mlog('cookiedata值:' . json_encode($cookie_data), 'geturl/' . getMobileStoId());
        mlog('cookie值:' . json_encode($cookie_code), 'geturl/' . getMobileStoId());
        if (empty($cookie_code) || empty($cookie_data)) {
            //通过code获得openid
            if (!isset($_GET['code'])) {
                //触发微信返回code码
                //$baseUrl = urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING']);
                $baseUrl = urlencode($this->get_url());
                $url = $this->__CreateOauthUrlForCode($baseUrl); // 获取 code地址
                Header("Location: $url"); // 跳转到微信授权页面 需要用户确认登录的页面
                exit();
            } else {
                //上面获取到code后这里跳转回来
                $code = $_GET['code'];
                mlog($code . '-' . date('Y-m-d H:i:s'), 'code/' . getMobileStoId());
                $data = $this->getOpenidFromMp($code);//获取网页授权access_token和用户openid
                //如果授权失败,去掉code,重新授权
                if(empty($data) || $data['errcode']=='40029') {
                    $url1=$this->get_newurl();
                    $baseUrl = urlencode($url1);
                    $url = $this->__CreateOauthUrlForCode($baseUrl); // 获取 code地址
                    Header("Location: $url"); // 跳转到微信授权页面 需要用户确认登录的页面
                    exit();
                }
                Cookie::set("code" . $stoid,$code);
                $data2 = $this->GetUserInfo($data['access_token'], $data['openid']);//获取微信用户信息
                $data['nickname'] = empty($data2['nickname']) ? '微信用户' : trim($data2['nickname']);
                $data['sex'] = $data2['sex'];
                $data['head_pic'] = $data2['headimgurl'];
                $data['subscribe'] = $data2['subscribe'];
                $_SESSION['openid'] = $data['openid'];
                $_SESSION['wx_stoid'] = getMobileStoId();
                $data['oauth'] = 'weixin';
                if (isset($data['unionid'])) {
                    $data['unionid'] = $data['unionid'];
                }
                mlog('unionid:' . json_encode($data['unionid']), 'getusers/' . getMobileStoId());
                mlog('subscribe:' . json_encode($data['subscribe']), 'getusers/' . getMobileStoId());
            }
            Cookie::set("code_data" . $stoid,$data);
            return $data;
        }else{
            $data=Cookie::get("code_data" . $stoid);
            Cookie::delete("code_data" . $stoid);
            Cookie::delete("code" . $stoid);
            return $data;
        }

    }

    /**
     * 获取当前的url 地址
     * @return type
     */
    private function get_url() {
        $sys_protocal =((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
        $php_self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
        $path_info = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
        $relate_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $php_self.(isset($_SERVER['QUERY_STRING']) ? '?'.$_SERVER['QUERY_STRING'] : $path_info);
        return $sys_protocal.(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '').$relate_url;
    }    
    
    /**
     *
     * 通过code从工作平台获取openid机器access_token
     * @param string $code 微信跳转回来带上的code
     *
     * @return openid
     */
    public function GetOpenidFromMp($code)
    {
        //通过code获取网页授权access_token 和 openid 。网页授权access_token是一次性的,而基础支持的access_token的是有时间限制的:7200s。
    	//1、微信网页授权是通过OAuth2.0机制实现的,在用户授权给公众号后,公众号可以获取到一个网页授权特有的接口调用凭证(网页授权access_token),通过网页授权access_token可以进行授权后接口调用,如获取用户基本信息;
    	//2、其他微信接口,需要通过基础支持中的“获取access_token”接口来获取到的普通access_token调用。
        $url = $this->__CreateOauthUrlForOpenid($code);       
        $ch = curl_init();//初始化curl        
        curl_setopt($ch, CURLOPT_TIMEOUT, 300);//设置超时
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,FALSE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);         
        $res = curl_exec($ch);//运行curl,结果以jason形式返回
        mlog('获取授权信息:' . json_encode($res), 'getusers/' . getMobileStoId());
        $data = json_decode($res,true);         
        curl_close($ch);
        return $data;
    }
    
    
        /**
     *
     * 通过access_token openid 从工作平台获取UserInfo      
     * @return openid
     */
    public function GetUserInfo($access_token,$openid)
    {
        // 获取用户 信息
        $url = $this->__CreateOauthUrlForUserinfo($access_token, $openid);
        $ch = curl_init();//初始化curl        
        curl_setopt($ch, CURLOPT_TIMEOUT, 300);//设置超时
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $res = curl_exec($ch);//运行curl,结果以jason形式返回
        $data = json_decode($res, true);
        curl_close($ch);
        //获取用户是否关注了微信公众号, 再来判断是否提示用户 关注
        mlog('unionid01:' . $res . '-nk' . $data['nickname'] . '-img' . $data['headimgurl'], 'getusers/' . getMobileStoId());
        //if(!isset($data['unionid'])){
        //$access_token2 = $this->get_access_token();//获取基础支持的access_token
        $access_token2 = m_get_access_token($this->weixin_config);

        $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token2&openid=$openid&lang=zh_CN";
        mlog('unionid011:' . $url, 'getusers/' . getMobileStoId());
        $subscribe_info = httpRequest($url, 'GET');
        mlog('unionid02:' . $subscribe_info, 'getusers/' . getMobileStoId());
        if ($subscribe_info) {
            $subscribe_info = json_decode($subscribe_info, true);
            if ($subscribe_info['subscribe']) {
                $data['subscribe'] = $subscribe_info['subscribe'];
            }
        }
        //}
        if (empty($data['subscribe'])) {
            $data['subscribe'] = 0;
        }
        return $data;
    }
    
    
    public function get_access_token(){

        return m_get_access_token($this->weixin_config);
    }    

    /**
     *
     * 构造获取code的url连接
     * @param string $redirectUrl 微信服务器回跳的url,需要url编码
     *
     * @return 返回构造好的url
     */
    private function __CreateOauthUrlForCode($redirectUrl)
    {
        $urlObj["appid"] = $this->weixin_config['appid'];
        $urlObj["redirect_uri"] = "$redirectUrl";
        $urlObj["response_type"] = "code";
//      $urlObj["scope"] = "snsapi_base";
        $urlObj["scope"] = "snsapi_userinfo";
        $urlObj["state"] = "STATE";
        $urlObj["component_appid"]=open_appId;
        $bizString = $this->ToUrlParams($urlObj);
        mlog("网址1:https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString."#wechat_redirect","geturl/".getMobileStoId());
        return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString."#wechat_redirect";
    }

    /**
     *
     * 构造获取open和access_toke的url地址
     * @param string $code,微信跳转带回的code
     *
     * @return 请求的url
     */
    private function __CreateOauthUrlForOpenid($code)
    {
        $urlObj["appid"] = $this->weixin_config['appid'];
        $urlObj["code"] = $code;
        $urlObj["grant_type"] = "authorization_code";
        $urlObj["component_appid"] = open_appId;
        $urlObj["component_access_token"] = getcomponent_access_token();

        $bizString = $this->ToUrlParams($urlObj);
        mlog("网址2:https://api.weixin.qq.com/sns/oauth2/component/access_token?".$bizString,"geturl/".getMobileStoId());
        return "https://api.weixin.qq.com/sns/oauth2/component/access_token?".$bizString;
    }

    /**
     *
     * 构造获取拉取用户信息(需scope为 snsapi_userinfo)的url地址
     * @return 请求的url
     */
    private function __CreateOauthUrlForUserinfo($access_token,$openid)
    {
        $urlObj["access_token"] = $access_token;
        $urlObj["openid"] = $openid;
        $urlObj["lang"] = 'zh_CN';
        $bizString = $this->ToUrlParams($urlObj);
        mlog("网址3:https://api.weixin.qq.com/sns/userinfo?".$bizString,"geturl/".getMobileStoId());
        return "https://api.weixin.qq.com/sns/userinfo?".$bizString;
    }


    /**
     *
     * 拼接签名字符串
     * @param array $urlObj
     *
     * @return 返回已经拼接好的字符串
     */
    private function ToUrlParams($urlObj)
    {
        $buff = "";
        foreach ($urlObj as $k => $v)
        {
            if($k != "sign"){
                $buff .= $k . "=" . $v . "&";
            }
        }

        $buff = trim($buff, "&");
        return $buff;
    }
    public function ajaxReturn($data){
        exit(json_encode($data));
    }



    /*--线上与线下绑定,调用线下接口进行取ERPVIPID--*/
    public function  get_erp_vipid(){

        /*----
        $list = array("access_token"=>"wxd.vip.vipinfo.list.get");
        $where['MobileTel']=array('=',"13599727122");
        $tk=M("store")->where("store_id",getAdmStoId())->field("api_token")->find();
        $array =array(
            'token'=>$tk['api_token'],
            'data'=>"",
            'map'=>$where,
            'order'=>"",
        );
        $json=json_encode($array);
        $url=GetSignUrl($list);
        if(empty($url)){
            return json(["code"=>"-1","msg"=>"生成签名错误"]);
        }
        $rs=http_post_json($url,$json);---*/
        $tk=M("store")->where("store_id",getMobileStoId())->field("api_token")->find();
        $where['MobileTel']=array('=',"13599727122");
        $rs1=getApiData("wxd.vip.vipinfo.list.get",$tk['api_token'],null,$where);
        $rs=[$rs1,'ok'];
        if($rs){
             //return urldecode($rs[0]);
             return json_encode($rs);
        }else{
            return json(["code"=>"-1","msg"=>"获取接口数据错误"]);
        }
        //mlog($res);
    }

    /*--线上与线下绑定,调用线下接口进行取ERPVIPID--*/
    public function  add_erp_vip(){
        /*----
        $list = array("access_token"=>"wxd.vip.vipinfo.add");
        $tk=M("store")->where("store_id",getAdmStoId())->field("api_token")->find();
        $data =array(
            'VIPName'=>'minghai',
            'VIPNo'=>'13599727122',
            'Id'=>'qjhtest',
            'MobileTel'=>'13599727122',
        );
        $jsonre=array(
            'token'=>$tk['api_token'],
            'data'=> array($data),
            'map'=>'',
            'order'=>'',
        );
        $json=json_encode($jsonre);
        //var_dump($jsonre);die;
        $url=GetSignUrl($list);
        var_dump($url);die;
        if(empty($url)){
            return json(["code"=>"-1","msg"=>"生成签名错误"]);
        }
        $rs=http_post_json($url,$json);---*/

        $tk=M("store")->where("store_id",getMobileStoId())->field("api_token")->find();
        $data =array(
            'VIPName'=>'minghai',
            'VIPNo'=>'13599727122',
            'Id'=>'qjhtest',
            'MobileTel'=>'13599727122',
        );

        $rs1=getApiData("wxd.vip.vipinfo.list.get",$tk['api_token'],array($data));
        $rs=[$rs1,"1"];

        if($rs){
             //return urldecode($rs[0]);
             return json_encode($rs);
        }else{
            return json(["code"=>"-1","msg"=>"获取接口数据错误"]);
        }
        //mlog($res);
    }



    /*--获取先的地址--*/
    public function get_newurl() {
        $url = $this->get_url();
        $arr = parse_url($url);
        $arr_query = $this->convertUrlQuery($arr['query']);
        $newurl1="";
        if ($this->getUrlQuery($arr_query)){
           $newurl1="&".$this->getUrlQuery($arr_query);
        }
        $newport="";
        if ($arr['port']!=="80" && $arr['port'])
        {
           $newport=":".$arr['port'];
        }
        $newurl=$arr['scheme']."://".$arr['host'].$newport.$arr['path']."?1=1".$newurl1;
        return $newurl;
    }
    //获取参数
    public  function convertUrlQuery($query)
    {
        $queryParts = explode('&', $query);
        $params = array();
        foreach ($queryParts as $param) {
            $item = explode('=', $param);
            $params[$item[0]] = $item[1];
        }
        return $params;
    }
    /**
     * 将参数变为字符串
     * @param $array_query
     * @return string string 'm=content&c=index&a=lists&catid=6&area=0&author=0&h=0ion=0&s=1&page=1' (length=73)
     */
    public function getUrlQuery($array_query)
    {
        $tmp = array();
        foreach($array_query as $k=>$param)
        {
            if ($k==="code" || $k==="state"){}
            else
            {
                $tmp[] = $k.'='.$param;
            }

        }
        $params = implode('&',$tmp);
        return $params;
    }


    public function buy_page_assign($p_dis_type=0)
    {
        if (session('user')['head_pic']) {
            $qc = new \app\home\controller\Index();
            $s_head_pic = mb_img_curl_url(session('user')['head_pic'], 2);
            $this->assign('share_head_pic', $s_head_pic['data']);
        }
        /*---
        if($p_dis_type) {
            $pk = mb_get_user_sto($p_dis_type);
            $this->assign("de_pick", $pk);
        }--*/
    }


}