<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

function get_wx_openid($retmethod,$return=false){
    
    $returnUrl = urlencode(url($retmethod,'','',true));
    $mcaddr = config('ext.app_config');
    $mcaddr = $mcaddr['sx_mc']['addr'];
    if(!$mcaddr){
        exit('未安装管理中心，无法使用本系统。');
    }
    $redUrl = $mcaddr.'/getopenid?get_type=2&return_url='.$returnUrl;
    if(!$return){
       header('Location:'.$redUrl);
//        redirect($redUrl);
    }else{
        return $redUrl;
    }
    
    
}
// 清除静态页面
function cleanStaticHtml(){
    unlink(ROOT_PATH.'public/login.html');
}
/*
 * 用于处理第三方接口请求时参数为多维数组的情况
 * */
function getThirdParams($param,$input){
    if(strpos($param,'.')){
        $params = explode('.',$param);
        $retVal = $input;
        foreach($params as $v){
            $retVal = $retVal[$v];
        }
        return $retVal;
    }else{
        return $input[$param];
    }
}
/*
 * 废弃，暂时未删除
 * */
function changeMyPosition1($positionCode){
    if(!$positionCode){
        return false;
    }
    $mc = new \app\common\lib\mcInterface();
    $info = $mc->getposition(['position_code'=>$positionCode]);
    /*
    $url = '5a0b96b65c111';
    $info = sx_call($url,['position_code'=>$positionCode]);

    if(!$info || $info['code'] != 200){
        return false;
    }
    */
    if($info === false){
        return false;
    }
    session('accessModel',null);
    session('source',null);
    session('intention',null);
    session('areaSet',null);
    session('capacity',null);
    session('channel',null);
    cache('shopAllStaffs_'.session('belong_shop_code'),null);
    $b_id = session('brandclass_id');

    $position = $info[0];
    $shopInfo = $position['shop_code'] ? controller('crm/Customer','lib')->get_shop(['shop_code'=>$position['shop_code']]) : '';
    $cacheInfo = cache('staffInfo_'.session('staff_id'));
    // if(!empty($shopInfo)){
        $shopInfo = $shopInfo[0];
        session('shop_code',$shopInfo['shop_code'] ? $shopInfo['shop_code'] : null);
        session('shop_name',$shopInfo['shop_name'] ? $shopInfo['shop_name'] : null);
        session('belong_shop_code',$shopInfo['belong_shop_code'] ? $shopInfo['belong_shop_code'] : null);
        session('belong_shop_name',$shopInfo['belong_shop_name'] ? $shopInfo['belong_shop_name'] : null);
        session('shop_type',$shopInfo['shop_type'] ? $shopInfo['shop_type'] : null);
        session('is_sk',$shopInfo['is_sk'] ? $shopInfo['is_sk'] : null);
        session('is_marketing',$shopInfo['is_marketing'] ? $shopInfo['is_marketing'] : null);//是否是营销店
        // session('open_id', $shopInfo['open_id']);
        session('is_group', $shopInfo['is_group'] ? $shopInfo['is_group'] : null);//是不是集团店:1:是,2:否
        $cacheInfo['shop_type'] = $shopInfo['shop_type'] ? $shopInfo['shop_type'] : null;
        $cacheInfo['shop_code'] = $shopInfo['shop_code'] ? $shopInfo['shop_code'] : null;
        $cacheInfo['shop_name'] = $shopInfo['shop_name'] ? $shopInfo['shop_name'] : null;
        $cacheInfo['belong_shop_code'] = $shopInfo['belong_shop_code'] ? $shopInfo['belong_shop_code'] : null;
        $cacheInfo['belong_shop_name'] = $shopInfo['belong_shop_name'] ? $shopInfo['belong_shop_name'] : null;
        $cacheInfo['is_marketing'] = $shopInfo['is_marketing'] ? $shopInfo['is_marketing'] : null;
        $cacheInfo['is_sk'] = $shopInfo['is_sk'] ? $shopInfo['is_sk'] : null;
        $cacheInfo['is_group'] = $shopInfo['is_group'] ? $shopInfo['is_group'] : null;
    // }

    $positions = explode(',',session('position_code'));
    if(!in_array($position['position_code'], (array)$positions)){
        $positions[] = $position['position_code'];
    }
    session('position_code',implode(',',(array)$positions));
    session('main_position_code',$position['position_code']);
    session('main_position_name',$position['position_name']);
    session('department_id',$position['department_id']);
    session('department_name',$position['department_name']);
    session('brandclass_id',$position['brandclass_id']);
    session('brandclass',$position['brandclass']);
    session('main_work_type',$position['work_type']);
    session('is_belong',$position['grade_type']);
    //替换cache信息
    $cacheInfo = cache('staffInfo_'.session('staff_id'));
    $cacheInfo['brandclass_id'] = $position['brandclass_id'];
    $cacheInfo['brandclass'] = $position['brandclass'];
    $cacheInfo['department_name'] = $position['department_name'];
    $cacheInfo['department_id'] = $position['department_id'];
    $cacheInfo['main_position_name'] = $position['position_name'];
    $cacheInfo['main_position_code'] = $position['position_code'];
    $cacheInfo['position_code'] = $position['position_code'];
    $cacheInfo['main_work_type'] = $position['work_type'];
    $cacheInfo['active'] = time();
    cache('staffInfo_'.session('staff_id'),$cacheInfo);

    //替换为要切换的事业部的客资有效期
    if($b_id != $position['brandclass_id']){
        $url = '5a4216fe6b2b5';
        $info = sx_call($url,['id'=>$position['brandclass_id']]);
        if($info['code'] == 200){
            $brand = $info['data'][0];
            session('valid_date', (int)$brand['valid_date']);
        }
    }
    return true;
}
