<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
use app\common\model\Config;
use tools\Qxsms;

/**
 * 校验手机格式
 * @param $phone
 * @return int
 */
function check_mobile($phone){
    return preg_match("/1\d{10}$/",$phone);
}

/**
 * [check_email 校验邮箱格式]
 */
function check_email($email){
    $pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";
    return preg_match($pattern,$email);
}

/**
 * 字符串截取,支持中文和其他编码
 * @static
 * @access public
 * @param string $str 需要转换的字符串
 * @param string $start 开始位置
 * @param string $length 截取长度
 * @param string $charset 编码格式
 * @param string $suffix 截断显示字符
 * @return string
 */
function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=false) {
    if(function_exists("mb_substr"))
        $slice = mb_substr($str, $start, $length, $charset);
    elseif(function_exists('iconv_substr')) {
        $slice = iconv_substr($str,$start,$length,$charset);
        if(false === $slice) {
            $slice = '';
        }
    }else{
        $re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
        $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
        $re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
        $re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
        preg_match_all($re[$charset], $str, $match);
        $slice = join("",array_slice($match[0], $start, $length));
    }
    return $suffix ? $slice.'...' : $slice;
}

/**
 * 系统加密方法
 * @param string $data 要加密的字符串
 * @param string $key  加密密钥
 * @param int $expire  过期时间 单位 秒
 * @return string
 */
function think_encrypt($data, $key = '', $expire = 0) {
    $key  = md5(empty($key) ? config('app.encryption_key') : $key);
    $data = base64_encode($data);
    $x    = 0;
    $len  = strlen($data);
    $l    = strlen($key);
    $char = '';

    for ($i = 0; $i < $len; $i++) {
        if ($x == $l) $x = 0;
        $char .= substr($key, $x, 1);
        $x++;
    }

    $str = sprintf('%010d', $expire ? $expire + time():0);

    for ($i = 0; $i < $len; $i++) {
        $str .= chr(ord(substr($data, $i, 1)) + (ord(substr($char, $i, 1)))%256);
    }
    return str_replace(array('+','/','='),array('-','_',''),base64_encode($str));
}

/**
 * 系统解密方法
 * @param  string $data 要解密的字符串 (必须是think_encrypt方法加密的字符串)
 * @param  string $key  加密密钥
 * @return string
 */
function think_decrypt($data, $key = ''){
    $key    = md5(empty($key) ? config('app.encryption_key') : $key);
    $data   = str_replace(array('-','_'),array('+','/'),$data);
    $mod4   = strlen($data) % 4;
    if ($mod4) {
        $data .= substr('====', $mod4);
    }
    $data   = base64_decode($data);
    $expire = substr($data,0,10);
    $data   = substr($data,10);

    if($expire > 0 && $expire < time()) {
        return '';
    }
    $x      = 0;
    $len    = strlen($data);
    $l      = strlen($key);
    $char   = $str = '';

    for ($i = 0; $i < $len; $i++) {
        if ($x == $l) $x = 0;
        $char .= substr($key, $x, 1);
        $x++;
    }

    for ($i = 0; $i < $len; $i++) {
        if (ord(substr($data, $i, 1))<ord(substr($char, $i, 1))) {
            $str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1)));
        }else{
            $str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1)));
        }
    }
    return base64_decode($str);
}

/**
 * 根据省份证号获取生日和性别
 * @param $idcard
 * @return array
 */
function get_birthday_sex_by_idcard($idcard){
    if(mb_strlen($idcard) != 18){
        return ['birthday'=>null,'gender'=>0];
    }
    $birthday = msubstr($idcard, 6, 8);
    $birthday = date('Y-m-d',strtotime($birthday));
    $gender = msubstr($idcard,16,1)%2;
    $gender = $gender?1:2;
    return ['birthday'=>$birthday,'gender'=>$gender];
}

/**
 * ajax 请求正确返回
 * @param string $msg
 * @param array $data
 * @return json
 */
function ajax_return_ok($data = array(),$msg = ''){
    $result['code'] = 0;
    $result['data'] = $data;
    $result['message'] = $msg ;
    header('Content-Type:application/json; charset=utf-8');
    if(version_compare(PHP_VERSION,'5.4.0','<')){
        exit(json_encode($result));
    }else{
        exit(json_encode($result,JSON_UNESCAPED_UNICODE)); //显示中文
    }
}

/**
 * ajax 请求错误返回
 * @param string $msg
 * @param string $code
 * @return json
 */
function ajax_return_error($msg = null,$code = 1){
    if ($msg == null){
        $msgDefault = config ( 'e_msg_default' );
        $result['msg'] = $msgDefault [$code];
    }else{
        $result['msg'] = $msg ;
    }
    $result['status'] = 0;
    $result['code'] = $code;
    header('Content-Type:application/json; charset=utf-8');
    if(version_compare(PHP_VERSION,'5.4.0','<')){
        exit(json_encode($result));
    }else{
        exit(json_encode($result,JSON_UNESCAPED_UNICODE)); //显示中文
    }
}

/**
 * AES 128 ecb 加密 与java加密保持一致
 * @param $data 加密字符串
 * @param $key 加密key
 * @return string 加密串
 */
function aes_encrypt($data) {
    $key = config('app.encryption_key');
    $data =  openssl_encrypt($data, 'aes-128-ecb', $key, OPENSSL_RAW_DATA);
    return base64_encode($data);
}

/**
 * AES 128 ecb 解密 与java加密保持一致
 * @param $data 解密字符串
 * @param $key 加密key
 * @return string 解密串
 */
function aes_decrypt($data) {
    $key = config('app.encryption_key');
    $encrypted = base64_decode($data);
    return openssl_decrypt($encrypted, 'aes-128-ecb', $key, OPENSSL_RAW_DATA);
}

/**
 * 下划线转驼峰
 * @param $str
 * @return string|string[]|null
 */
function line_to_hump($str){
    return preg_replace_callback('/(_[a-z])/', function ($match) {
        return ucfirst(trim($match[0], '_'));
    }, $str);
}

/**
 * 驼峰转下划线
 * @param $str
 * @return string|string[]|null
 */
function hump_to_line($str){
    return preg_replace_callback('/([A-Z])/', function ($match) {
        return '_' . lcfirst($match[0]);
    }, $str);
}

/**
 * 多维数组键值下划线转换为驼峰
 * @param $arr
 * @return array
 */
function array_change_line_to_hump($arr){
    $tem = [];
    foreach ($arr as $k=>$v){
        if(is_array($v)){
            $tem[line_to_hump($k)] = array_change_line_to_hump($v);
        }else{
            $tem[line_to_hump($k)] = $v;
        }
    }
    return $tem;
}

/**
 * 多维数组取某一字段
 * @param array $arr
 * @param string $field 要删除字段
 * @param string $child
 * @return array
 */
function array_delete_char($arr,$field,$child = 'depAndJobDtos'){
    $tem = [];
    foreach ($arr as $k=>$v){
        unset($v[$field]);
        if(!empty($v[$child])){
            $v[$child] = array_delete_char($v[$child],$field);
        }
        $tem[$k] = $v;
    }
    return $tem;
}

/**
 * 把返回的数据集转换成Tree  主要组织部门岗位人员树使用
 * @param array $list 要转换的数据集
 * @param string $pid parent标记字段
 * @param string $child 子孙键名
 * @param int $root 父级值
 * @return array
 */
function list_to_tree($list, $pid = 'pid', $child = '_child', $root = 0)
{
    $tree = [];
    foreach($list as $k => $v){
        if($v[$pid] == $root){
            $children = list_to_tree($list, $pid, $child, $v['id']);
            if(!empty($v[$child])){
                $children = array_merge($children,$v[$child]);
            }
            $v[$child] = $children;
            $tree[] = $v;
        }
    }
    return $tree;
}

/**
 * 获取编号
 * @param $prefix
 * @return string
 */
function get_unique_id($prefix=''){
    return $prefix.date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 12), 1))), 0, 8);
}

/**
 * 获取编号
 * @param string $prefix
 * @return string
 */
function get_unique_sn($prefix=''){
    return $prefix.date('YmdHis').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 12), 1))), 0, 6);
}

/**
 * flowjson排序
 * @param $flowJson
 * @param string $nodeid
 * @param array $result
 * @return array
 */
function sort_flow_json($flowJson,$nodeid='',&$result=[]){
    foreach ($flowJson as $k=>$v){
        if((!$nodeid && $v['type'] == 1) || $nodeid && $v['id'] == $nodeid){
            $result[] = $v;
            if(count($result) != count($flowJson)){
                sort_flow_json($flowJson,$v['nextId'],$result);
                break;
            }
        }
    }
    return $result;
}

/**
 *
 * @param $start
 * @param $end
 * @param $type
 * @return float|int
 */
function calculate_leave($start,$end,$type){
    $starts = explode(' ',$start);
    $ends = explode(' ',$end);
    $start = date('Ymd',strtotime($starts[0]));
    $stxt = $starts[1];
    $end = date('Ymd',strtotime($ends[0]));
    $etxt = $ends[1];
    if($start > $end){
        return 0;
    }

    $bxday = \think\Db::name('holiday')
        ->where('holiday',0)
        ->where('day','>=',date('Y-m-d',strtotime($starts[0])))
        ->where('day','<=',date('Y-m-d',strtotime($ends[0])))
        ->column('day');
    $bxday = $bxday?$bxday:[];

    $holiday = \think\Db::name('holiday')
        ->where('holiday',1)
        ->where('day','>=',date('Y-m-d',strtotime($starts[0])))
        ->where('day','<=',date('Y-m-d',strtotime($ends[0])))
        ->column('day');
    $holiday = $holiday?$holiday:[];

    $days = 0;
    $cday = (strtotime($end) - strtotime($start))/86400 + 1;
    for ($i=1;$i<=$cday;$i++){
        if($type == 1){ // 减去节假日
            $cur = strtotime($start) + ($i-1)*86400;
            $w = date('w',$cur);
            $itxt = date('Y-m-d',$cur);
            if(($w == 6 || $w == 0) && in_array($itxt,$bxday)){ // 检查是否是补休日
                if($i == 1 && $stxt == '下午'){ // 开始日期
                    $days += 0.5;
                }else if($i == $cday && $etxt == '上午'){
                    $days += 0.5;
                }else{
                    $days++;
                }
            } else if($w >0 && $w < 6 && !in_array($itxt,$holiday)){
                if($i == 1&&$stxt == '下午'){ // 开始日期
                    $days += 0.5;
                } else if($i == $cday && $etxt == '上午'){
                    $days += 0.5;
                }else{
                    $days++;
                }
            }
        } else { // 不减节假日
            $days++;
        }
    }
    return $days;

}

/**
 * 计算社保比例金额
 * @param $money
 * @param $bl
 * @param $bl_type
 * @param $bl_extra
 * @param $free_bl
 * @return false|float
 */
function calculate_money($money,$bl,$bl_type,$bl_extra,$free_bl){
    $gjjp = $money*$bl;
    if($bl_extra > 0){
        if($bl_type == 1){ // 比例
            $gjjp += $money*$bl_extra;
        } else { // 整数
            $gjjp += $bl_extra;
        }
    }
    $gjjp = $gjjp*(1 - $free_bl);
    return round($gjjp,2);
}

/**
 * 延迟任务
 * @param $delay 延迟时间(s)
 * @param $data 数据
 * @param int $type 0=推送 1=短信
 * @return bool true=成功
 */
function queue_later($delay,$data,$type = 0){
    $connector = strtolower(config('queue.connector'));
    try{
        if($type == 0){ //取消订单
            $job = "app\queue\OrderJob@cancel";
            $queue = "cancel";
        }else if($type == 1){ //处理拼团单
            $job = "app\queue\OrderJob@group";
            $queue = "group";
        }else{
            return false;
        }
        if($connector == 'redis'){
            \think\Queue::later($delay, $job, $data, $queue);
            return true;
        }else{
            $res = \think\Queue::later($delay, $job, $data, $queue);
            return $res?true:false;
        }
    }catch (Exception $e){
        trace($e->getMessage());
    }
    return false;
}

/**
 * 推送队列任务
 * @param string $data 业务数据
 * @param int $type 0=推送 1=短信
 * @param int $delay 延迟时间(s)
 * @return bool true=成功
 */
function queue_push($data,$type = 0,$delay = 0){
    try{
        if($type == 0){ //推送
            $job = "app\queue\Jobs@jpush";
            $queue = "jpush";
        }else if($type == 1){ //短信
            $job = "app\queue\Jobs@qxsms";
            $queue = "qxsms";
        }else if($type == 3){ //自动派单
            $job = "app\queue\Jobs@autoSend";
            $queue = "autoSend";
        }else{
            return false;
        }
        if($delay > 0){
            \think\Queue::later($delay, $job, $data, $queue);
        }else{
            \think\Queue::push($job, $data,$queue);
        }

        return true;
    }catch (Exception $e){
        trace('队列添加失败:'.$e->getMessage());
        return false;
    }
}

/**
 * 验证短信发送类
 * @param $mobile 手机号
 * @return bool
 */
function send_verify_sms($mobile){
    \think\Db::startTrans();
    try{
        $curTime = time();
        $endTime = $curTime + 10*60; //有效期10分钟
        $code = mt_rand(100000,999999);
        $data = array(
            'mobile' => $mobile,
            'code' => $code,
            'status' => 0,
            'create_yyyymmdd' => date('Ymd',$curTime),
            'create_time' => date('Y-m-d H:i:s',$curTime),
            'end_time' => date('Y-m-d H:i:s',$endTime)
        );

        $ret = \think\Db::name('sms_record')->insertGetId($data);
        if (!$ret) {
            exception('短信记录失败');
        }
        $content = \app\common\util\AppMsg::getSmsMsg(\app\common\util\AppMsg::SMS_VERIFY,['code'=>$code]);
        $json = [
            'orgId' => 0,
            'mobiles' => [$mobile],
            'msg' => $content,
            'fromId' => 0,
            'type' => 1
        ];

        $ret = queue_push(json_encode($json),1);
        if(!$ret){
            exception('添加短信队列失败');
        }

        \think\Db::commit();
        return true;
    }catch (Exception $e){
        trace('短信发送失败:'.$e->getMessage());
        trace('短信发送号码:'.$mobile);
        \think\Db::rollback();
        return false;
    }
}

/**
 * 验证短信验证码
 * @param $mobile 手机号
 * @param $code 验证码
 * @return bool
 */
function verify_sms($mobile,$code){
    if(!$code){
        return false;
    }
    $curTime = date('Y-m-d H:i:s');
    $info = \think\Db::name('sms_record')
        ->where('code',$code)
        ->where('mobile',$mobile)
        ->where('status',0)
        ->where('end_time','>=',$curTime)
        ->find();
    if(!$info){
        return false;
    }
    \think\Db::name('sms_record')->where('id',$info['id'])->setField('status',1);
    return true;
}

/**
 * 营销短信发送
 * @param array $mobiles 手机号
 * @param string $msg 内容
 * @return bool
 */
function send_sms($mobiles,$msg='',$orgId=0,$fromId=0){
    if(!$mobiles){
        return true;
    }
    try{
        $json = [
            'orgId' => $orgId,
            'mobiles' => $mobiles,
            'msg' => $msg,
            'fromId' => $fromId,
            'type' => 2
        ];

        $ret = queue_push(json_encode($json),1);
        if(!$ret){
            exception('添加短信队列失败');
        }
        return true;
    }catch (Exception $e){
        trace('短信添加失败:'.$e->getMessage());
        trace('短信添加号码:'.implode(',',$mobiles));
        \think\Db::rollback();
        return false;
    }
}

/**
 * 极光推送
 * @param array $userids
 * @param string $msg
 * @param array $extras
 */
function send_jpush($userids,$type,$msg='',$extras=array()){
    if(!$userids){
        return true;
    }
    try {
        // {"users":[1,2,3],"type":1,"msg":"有新订单需要你的处理","extra"=>[]}
        $json = [
            'users' => $userids,
            'type' => $type,
            'msg' => $msg,
            'extra' => $extras
        ];

        $ret = queue_push(json_encode($json),0);
        if(!$ret){
            exception('添加推送队列失败');
        }
        return true;
    } catch (Exception $e) {
        trace('push-error:'.$e->getMessage().':'.json_encode($json));
        return false;
    }
}

/**
 * 计算年龄/工龄
 * @param $date
 * @return false|mixed|string
 */
function calculate_age($date){
    list($year,$month,$day) = explode("-",$date);
    $year_diff = date("Y") - $year;
    $month_diff = date("m") - $month;
    $day_diff  = date("d") - $day;
    if ($day_diff < 0 || $month_diff < 0)
        $year_diff--;
    return $year_diff;
}

// 过滤以逗号分隔的内容,去掉空项
function check_exp_imp($data){
    if(!$data){
        return '';
    }
    $vals = explode(',',$data);
    $nr = [];
    foreach ($vals as $v){
        if($v && !in_array($v,$nr)){
            $nr[] = $v;
        }
    }
    return empty($nr)?'':implode(',',$nr);
}
/**
 * [check_wechat 校验微信号格式]
 */
    function check_wechat($wechat){
        return preg_match('/^[_a-zA-Z0-9]{5,19}+$/isu',$wechat);
    }

/**
 * [check_link 校验url格式]
 */
function check_url($link){
    return preg_match("/http[s]?:\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*/is", $link);
}
/**
 * 数据签名认证
 * @param  array  $data 被认证的数据
 * @return string       签名
 */
function data_auth_sign($data) {
    //数据类型检测
    if(!is_array($data)){
        $data = (array)$data;
    }
    ksort($data); //排序
    $code = http_build_query($data); //url编码并生成query字符串
    $sign = sha1($code); //生成签名
    return $sign;
}
function education($key){
    $arr = [
        '暂无',
        '初中及以下',
        '高中',
        '中专/中技',
        '大专',
        '本科',
        '硕士',
        '博士',
    ];
    return isset($arr[$key])?$arr[$key]:'暂无';
}

function politics($val){
    $arr = [
        '暂无',
        '中共党员',
        '中共预备党员',
        '共青团员',
        '民革会员',
        '民盟盟员',
        '民建会员',
        '民进会员',
        '农工党党员',
        '致公党党员',
        '九三学社社员',
        '台盟盟员',
        '无党派人士',
        '群众',
    ];
    return isset($arr[$val])?$arr[$val]:'暂无';
}

/**
 * 加载小部件
 * @param $template 模板
 * @param $data 参数
 * @return string
 */
function widget_view($template,$data){
    return \think\facade\View::fetch($template,$data);
}
/**
 * 加载小部件
 * @param $template 模板
 * @param $data 参数
 * @return string
 */
function widget($template,$data){
    return \think\facade\View::fetch($template,$data);
}
/**
 * 创建token
 * @param $userid
 * @return string
 */
function create_token($userid){
    //十进制转八进制
    $token =  decoct($userid).'9'.decoct(intval(time()));
    return $token;
}

/**
 * select返回的数组进行整数映射转换
 *
 * @param array $map  映射关系二维数组  array(
 *                                          '字段名1'=>array(映射关系数组),
 *                                          '字段名2'=>array(映射关系数组),
 *                                           ......
 *                                       )
 * @author 朱亚杰 <zhuyajie@topthink.net>
 * @return array
 *
 *  array(
 *      array('id'=>1,'title'=>'标题','status'=>'1','status_text'=>'正常')
 *      ....
 *  )
 *
 */
function int_to_string(&$data,$map=array('status'=>array(1=>'正常',0=>'禁用'))) {
    if($data === false || $data === null ){
        return $data;
    }
    $data = (array)$data;
    foreach ($data as $key => $row){
        foreach ($map as $col=>$pair){
            if(isset($row[$col]) && isset($pair[$row[$col]])){
                $data[$key][$col.'_text'] = $pair[$row[$col]];
            }
        }
    }
    return $data;
}

//生成guid
function new_guid()
{
    if (function_exists('com_create_guid')) {
        return com_create_guid();
    } else {
        mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.
        $charid = strtoupper(md5(uniqid(rand(), true)));
        $hyphen = chr(45);// "-"
        $uuid = chr(123)// "{"
            .substr($charid, 0, 8).$hyphen
            .substr($charid, 8, 4).$hyphen
            .substr($charid, 12, 4).$hyphen
            .substr($charid, 16, 4).$hyphen
            .substr($charid, 20, 12)
            .chr(125);// "}"
        return $uuid;
    }
}

/**
 * 获取二维码加密串(urlencode是避免生成的字符被转码)
 * @param $type 二维码类型
 * @param $id 二维码ID
 * @return string 加密后的字符串
 */
function get_qrcode_str($type,$id){
    $data = array(
        'type' => (string)$type,
        'id' => (int)$id,
        'ucode' => config('app.ucode') //正式使用后不可修改
    );
    $strs = aes_encrypt(json_encode($data),config('app.encryption_key'));
    return str_ireplace('+','_',$strs);
}

/**
 * 获取二维码加密串(urlencode是避免生成的字符被转码)
 * @param $str 内容
 * @return array 解密后的数组
 */
function get_qrcode_arr($str){
    $str = str_ireplace('_','+',$str);
    $data = json_decode(aes_decrypt($str, config('app.encryption_key')),true);
    return $data ? $data : array();
}
//本周日期
function get_week_date(){
    $week = date('W');
    $time = strtotime(date('Y-m-d'));
    $arr = array();
    for ($i=6; $i>=0; $i--){
        $t = $time - $i*24*60*60;
        $w = date('W',$t);
        if($w == $week) {
            $arr[] = date('Ymd', $t);
        }
    }
    return $arr;
}
function getTime(){
    return date('Y-m-d H:i:s');
}

function getSite(){
//    $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
//    return $http_type.$_SERVER['HTTP_HOST'];
    return config("app.app_host");
}

/**
 * 检查字符串是否为空
 * @param $str
 * @return bool
 */
function check_val_empty($str){
    if (empty($str)) {
        return false;
    }
    return true;
}

/**
 * 对查询结果集进行排序
 * @access public
 * @param array $list 查询结果
 * @param string $field 排序的字段名
 * @param array $sortby 排序类型
 * asc正向排序 desc逆向排序 nat自然排序
 * @return array
 */
function list_sort_by($list,$field, $sortby='asc') {
    if(is_array($list)){
        $refer = $resultSet = array();
        foreach ($list as $i => $data)
            $refer[$i] = &$data[$field];
        switch ($sortby) {
            case 'asc': // 正向排序
                asort($refer);
                break;
            case 'desc':// 逆向排序
                arsort($refer);
                break;
            case 'nat': // 自然排序
                natcasesort($refer);
                break;
        }
        foreach ( $refer as $key=> $val)
            $resultSet[] = &$list[$key];
        return $resultSet;
    }
    return false;
}
// 格式化以符号分隔的字符串,去空值
function format_str($strs,$glup = ','){
    if(!$strs){
        return '';
    }
    $arr = explode($glup,$strs);
    $news = [];
    foreach ($arr as $k=>$v){
        if($v){
            $news[] = $v;
        }
    }
    return !empty($news)?implode($glup,$news):'';
}
//判断是否是二级调度 1是0否
function check_two_dispatch($userId){
    $is = 0;
    $roles = \think\Db::name('user_roles')
        ->alias('a')
        ->join('roles b','a.roles_id=b.id')
        ->where('a.user_id',$userId)
        ->field('b.level,b.parent_id')
        ->find();
    if($roles && $roles['level']==2 && $roles['parent_id']==9){
        $is = 1;
    }
    return $is;

}
//二级调度开关
function two_dispatch_off($orgId){
    $res = (new \app\common\model\Config())
        ->getConfig('org_two_dispatch',$orgId);
    return $res?(int)$res:0;
}
//判断是否是一级调度 1是0否
function check_is_dispatch($userId){
    $is = 0;
    $roles = \think\Db::name('user_roles')
        ->alias('a')
        ->join('roles b','a.roles_id=b.id')
        ->where('a.user_id',$userId)
        ->field('b.*')
        ->find();
    if($roles && $roles['parent_id']==9 && $roles['level']==1){
        $is = 1;
    }
    return $is;

}

//获取调度权限
function get_dispatch_auth($userId){
    $ids = [];
    $roles = \think\Db::name('user_roles')
        ->alias('a')
        ->join('roles b','a.roles_id=b.id')
        ->where('a.user_id',$userId)
        ->field('b.*')
        ->find();
    if($roles){
        $ids = $roles['work_type_mode']?explode(',',$roles['work_type_mode']):[];
    }
    return $ids;
}
//array
function formatArray($array1,$array2,$type=0){
    if($type==0){//保留$array1
        foreach ($array2 as $k=>$v){
            if(!in_array($k,$array1)){
                unset($array2[$k]);
            }
        }
    }else{//剔除$array1
        foreach ($array2 as $k=>$v){
            if(in_array($k,$array1)){
                unset($array2[$k]);
            }
        }
    }
    return $array2;
}

// 近一个月
function get_one_month(){
    $time = strtotime(date('Y-m-d'));
    $arr = array();
    for ($i=29; $i>=0; $i--){
        $arr[] = date('Ymd',$time - $i*24*60*60);
    }
    return $arr;
}
function get_unique_id2 ($prefix='')
{
    return $prefix . date ( 'Ymd' ) .
        substr ( implode ( NULL, array_map ( 'ord', str_split ( substr ( uniqid (), 7, 13 ), 1 ) ) ), 0, 6 );
}

function get_config($name){
    $info = \think\Db::name('config')->where('name',$name)->find();
    if($info){
        return config_parse($info['type'], $info['value']);
    }
}

function config_parse($type, $value){
    switch ($type) {
        case 3: //解析数组
            $array = preg_split('/[,;\r\n]+/', trim($value, ",;\r\n"));
            if(strpos($value,':')){
                $value  = array();
                foreach ($array as $val) {
                    list($k, $v) = explode(':', $val);
                    $value[$k]   = $v;
                }
            }else{
                $value =    $array;
            }
            break;
    }
    return $value;
}

function birthday($birthday){
    $age = strtotime($birthday);
    if($age === false){
        return false;
    }
    list($y1,$m1,$d1) = explode("-",date("Y-m-d",$age));
    $now = strtotime("now");
    list($y2,$m2,$d2) = explode("-",date("Y-m-d",$now));
    $age = $y2 - $y1;
    if((int)($m2.$d2) < (int)($m1.$d1))
        $age -= 1;
    return $age;
}
function diffBetweenTwoDays ($day1, $day2) {
    $second1 = strtotime($day1);
    $second2 = strtotime($day2);
    if ($second1 < $second2) {
        $tmp = $second2;
        $second2 = $second1;
        $second1 = $tmp;
    }
    return intval(($second1 - $second2) / 86400)+1;
}


function curl_post($url , $data=array()){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    // POST数据
    curl_setopt($ch, CURLOPT_POST, 1);
    // 把post的变量加上
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;

}

function nDate($time) {
    $tag = 'Y-m-d H:i:s';
    $a = substr($time, 0, 10);
    $b = substr($time, 10);//毫秒
    $date = date($tag, $a - 28800);//.$b
    return $date;
}
function getDay($time){
    $days =date("t",$time);

    for ($i = 0;$i <intval($days);$i++)
    {
        $day[] =date('Ymd',strtotime("+" .$i." day",strtotime(date("Y-m-01",$time))));

    }
    return $day;
}

//本年所有月份        开始和结束
function get_month($time){
    $year = $time;
    $yeararr = [];
    for ($i=1; $i <=12 ; $i++) {
        $yeararr[$i] = [
            'key'=>$year.'年'.$i.'月份',
            'date'=>date('Ym',strtotime($year.'-'.$i))
        ];
    }
    return $yeararr;
}

//本年所有月份        开始和结束
function get_month1($time){
    $year = $time;
    $yeararr = [];
    for ($i=1; $i <=12 ; $i++) {
        $yeararr[$i-1] = [
            'key'=>$year.'年'.$i.'月份',
            'date'=>date('Ym',strtotime($year.'-'.$i))
        ];
    }
    return $yeararr;
}

/**
 * 本月的开始日期
 *
 * @param bool $His     是否展示时分秒 默认true
 *
 * @return false|string
 */
  function beginMonth($His = true)
{
    $timestamp = mktime(0, 0, 0, date('m'), 1, date('Y'));
    return $His ? date('Y-m-d H:i:s', $timestamp) : date('Y-m-d', $timestamp);
}

/**
 * 本月的结束日期
 *
 * @param bool $His     是否展示时分秒 默认true
 *
 * @return false|string
 */
  function endMonth($His = true)
{
    $timestamp = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
    return $His ? date('Y-m-d H:i:s', $timestamp) : date('Y-m-d', $timestamp);
}

/**
 * 几年的开始日期
 *
 * @param bool $His     是否展示时分秒 默认true
 *
 * @return false|string
 */
  function beginYear($His = true)
{
    $timestamp = mktime(0, 0, 0, 1, 1, date('Y'));
    return $His ? date('Y-m-d H:i:s', $timestamp) : date('Y-m-d', $timestamp);
}

/**
 * 本月的结束日期
 *
 * @param bool $His     是否展示时分秒 默认true
 *
 * @return false|string
 */
  function endYear($His = true)
{
    $timestamp = mktime(23, 59, 59, 12, 31, date('Y'));
    return $His ? date('Y-m-d H:i:s', $timestamp) : date('Y-m-d', $timestamp);
}

/**
 * 生成二维码
 * @param $value 二维码内容
 * @param $filepath 保存路径
 * @return bool
 */
function create_qrcode($value,$filepath)
{
    include_once env('root_path').'/extend/phpqrcode/phpqrcode.php';
    if (empty($value)) {
        return false;
    }

    $dirfile = dirname($filepath);
    if(!file_exists($dirfile)){
        @mkdir($dirfile,0777);
    }

    //创建一个临时二维码的地址
    $dir   = getcwd().DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.'qrcodeimg'.DIRECTORY_SEPARATOR;
    if (!file_exists($dir)) {
        @mkdir($dir);
    }
    //临时二维码地址
    $QRprefix = $dir.DIRECTORY_SEPARATOR.substr(md5($value), 8, 16);
    $QRTemp = $QRprefix.'_temp.png';

    //判断本地是否存在临时的先删了
    if (file_exists($QRTemp)) {
        @unlink($QRTemp);
    }
    $errorCorrectionLevel = 'H';//容错级别
    $matrixPointSize = 6;//生成图片大小
    //生成二维码图片
    QRcode::png(
        $value,
        $QRTemp,
        $errorCorrectionLevel,
        $matrixPointSize,
        2
    );
    $orgId = cur_org_id();
    if($orgId == 17){
        $logo = env('root_path').'/public/hb.png';//准备好的logo图片
    }else{
        $logo = env('root_path').'/public/logo.png';//准备好的logo图片
    }

    if (!empty($logo)) {
        $QRBuffer = add_logo_2qrcode($QRTemp, $logo);
    } else {
        $QRBuffer = imagecreatefromstring(print_72dip_300dip(file_get_contents($QRTemp)));
    }

    //删除临时文件
    if (file_exists($QRTemp)) {
        @unlink($QRTemp);
    }

    imagejpeg($QRBuffer,$filepath,100);
    ImageDestroy($QRBuffer);
}

//添加LOGO
function add_logo_2qrcode($QRTempPath, $logoPath)
{
    $QRBuffer = imagecreatefromstring(print_72dip_300dip(file_get_contents($QRTempPath)));
    $logo = imagecreatefromstring(print_72dip_300dip(file_get_contents($logoPath)));
    $QR_width = imagesx($QRBuffer);//二维码图片宽度
    $QR_height = imagesy($QRBuffer);//二维码图片高度
    $logo_width = imagesx($logo);//logo图片宽度
    $logo_height = imagesy($logo);//logo图片高度
    $logo_qr_width = $QR_width / 5;
    $scale = $logo_width/$logo_qr_width;
    $logo_qr_height = $logo_height/$scale;
    $from_width = ($QR_width - $logo_qr_width) / 2;
    //重新组合图片并调整大小
    imagecopyresampled(
        $QRBuffer,
        $logo,
        $from_width,
        $from_width,
        0,
        0,
        $logo_qr_width,
        $logo_qr_height,
        $logo_width,
        $logo_height
    );
    return $QRBuffer;
}

//浏览器默认输出为72DPI,打印要求为300DPI
function print_72dip_300dip($QRBufferStr)
{
    //数据块长度为9
    $len = pack("N", 9);
    //数据块类型标志为pHYs
    $sign = pack("A*", "pHYs");
    //X方向和Y方向的分辨率均为300DPI(1像素/英寸=39.37像素/米),单位为米(0为未知,1为米)
    $data = pack("NNC", 300 * 39.37, 300 * 39.37, 0x01);
    //CRC检验码由数据块符号和数据域计算得到
    $checksum = pack("N", crc32($sign . $data));
    $phys = $len . $sign . $data . $checksum;
    $pos = strpos($QRBufferStr, "pHYs");
    if ($pos > 0) {
        //修改pHYs数据块
        $QRBufferStr = substr_replace($QRBufferStr, $phys, $pos - 4, 21);
    } else {
        //IHDR结束位置(PNG头固定长度为8,IHDR固定长度为25)
        $pos = 33;
        //将pHYs数据块插入到IHDR之后
        $QRBufferStr = substr_replace($QRBufferStr, $phys, $pos, 0);
    }
    return $QRBufferStr;
}

/**
 * 压缩文件/目录成zip
 * @param $path
 * @param $zip
 */
function add_file_to_zip($path,$zip){
    $handler=opendir($path); //打开当前文件夹由$path指定。
    while(($filename=readdir($handler))!==false){
        if($filename != "." && $filename != ".."){//文件夹文件名字为'.'和‘..’,不要对他们进行操作
            if(is_dir($path."/".$filename)){// 如果读取的某个对象是文件夹,则递归
                add_file_to_zip($path."/".$filename, $zip);
            }else{ //将文件加入zip对象
                $pp = $path."/".$filename;
                $pps = explode('//',$pp);
                $zip->addFile($pp,$pps[1]);
            }
        }
    }
    @closedir($path);
}

/**
 * 清空文件夹及文件夹下的所有文件
 * @param $path "./code/"
 */
function deldir($path){
    //如果是目录则继续
    if(is_dir($path)){
        //扫描一个文件夹内的所有文件夹和文件并返回数组
        $p = scandir($path);
        foreach($p as $val){
            //排除目录中的.和..
            if($val !="." && $val !=".."){
                //如果是目录则递归子目录,继续操作
                if(is_dir($path.$val)){
                    //子目录中操作删除文件夹和文件
                    deldir($path.$val.'/');
                    //目录清空后删除空文件夹
                    @rmdir($path.$val.'/');
                }else{
                    //如果是文件直接删除
                    @unlink($path.$val);
                }
            }
        }
        @rmdir($path);
    }
}

/*
 * $userId 用户id
 * $delUrl 删除路径(权限标识)
 * */
function btnAuth($userId,$delUrl){
    if(is_admin($userId)){
        return true;
    }
    $roles = \think\Db::name('user_roles')
        ->alias('a')
        ->join('roles b','a.roles_id=b.id')
        ->where('a.user_id',$userId)
        ->field('b.*')
        ->find();
    if(!empty($roles)){
        $authS = $roles['auths']?explode(',',$roles['auths']):[];
        if($roles['org_id'] > 0){
            $orgauths = \think\Db::name('org')->where('id',$roles['org_id'])->value('auths');
            $orgauths = $orgauths?explode(',',$orgauths):[];
            $authS = array_intersect($authS,$orgauths);
        }
        $menu = \think\Db::name('menu')
            ->where('url',$delUrl)
            ->where('enable',1)
            ->where('del',0)
            ->find();
        if(!empty($menu) && in_array($menu['id'],$authS)){
            return true;
        }
    }

    return false;
}

/**
 * 二维数组按照指定字段进行排序
 * @params array $array 需要排序的数组
 * @params string $field 排序的字段
 * @params string $sort 排序顺序标志 SORT_DESC 降序;SORT_ASC 升序
 */
function arraySequence($array, $field, $sort = 'SORT_ASC') {
    $arrSort = array();
    foreach ($array as $uniqid =>$row) {
        foreach ($row as $key =>$value) {
            $arrSort[$key][$uniqid] = $value;
        }
 }
 array_multisort($arrSort[$field], constant($sort), $array);
 return $array;
}
 function getM1($do,$cof){
    $diff = strtotime($do) - strtotime($cof);
    $days = floor(($diff )/ (60*60*24));
    $hours = floor(($diff - $days*60*60*24)  / (60*60));
    $minutes = floor(($diff - $days*60*60*24  - $hours*60*60)/ 60);
    $seconds = floor(($diff - $days*60*60*24  - $hours*60*60 - $minutes*60));
    if ($diff < 60) {
        $time_cost = $seconds."秒";
    } else if ($diff >= 60 && $diff < 60*60) {
        $time_cost = $minutes."分钟".$seconds."秒";
    } else if ($diff >= 60*60 && $diff < 60*60*24) {
        $time_cost = $hours."小时".$minutes."分钟".$seconds."秒";
    } else {
        $time_cost = $days."天".$hours."小时".$minutes."分钟".$seconds."秒";
    }
    return $time_cost;
}

function getOrgGrabOrder($userId,$orgId){
    $rolesId = \think\Db::name('user_roles')
        ->where('user_id', $userId)
        ->value('roles_id');
    $rolesInfo =  \think\Db::name('roles')
        ->where('id', $rolesId)
        ->find();
    if ($rolesInfo['parent_id'] > 0) {
        $rolesId = $rolesInfo['parent_id'];
    }
    $workType =  \think\Db::name('work_type_mode')
        ->where('type', 1)
        ->where('roles', 'like', '%' . $rolesId . '%')
        ->column('id');
    $is = 0;
    if(empty($workType)){
        return $is;
    }
    foreach ($workType as $k=>$v){
        $orgGrabOrder = (new Config())->getConfig('org_grab_order'.$v,$orgId);
        if($orgGrabOrder && $orgGrabOrder>0){
            $is = 1;
        }
    }
    return $is;

}

//获取近12个月 0=包含本月 1=不包含本月
function monthlater($type=0){
    $str = array();
    for($i=0;$i<12;$i++){
        if($type == 0){
            $str[$i] =date('Y-m',strtotime('-'.$i.'month'));//包含本月
        }else{
            $str[$i] =date('Y-m',strtotime('-1month-'.$i.'month'));//不包含本月
        }
    }
    sort($str);
    return $str;
}

function get_sort_user($orgId,$work=-1,$type_id=-1){
    $rolesId = \think\Db::name('roles')
        ->where('parent_id',7)
        ->where('org_id',$orgId)
        ->where('del',0)
        ->column('id');
    $userRolesId =  \think\Db::name('user_roles')
        ->where('roles_id','in',$rolesId)
        ->column('user_id');
    $map[] = ['u.id','in',$userRolesId];
    $map[] = ['u.del','=',0];
    $map[] = ['u.type','=',0];
    $map[] = ['u.enable','=',1];
    $map[] = ['u.enable_sorts','=',1];
    $map[] = ['uo.org_id','=',$orgId];
    if($work >0){
        $map[] = ['u.work','=',$work];
    }
    if($type_id >0){
        $map[] = ['u.type_id','=',$type_id];

    }
    $map= empty($map) ? true: $map;
    $lists =  \think\Db::name('user')
        ->alias('u')
        ->join('user_org uo','u.id=uo.user_id')
        ->where($map)
        ->field('u.id,uo.org_id')
        ->order(['u.sorts'=>'desc','u.id'=>'asc'])
        ->column('u.id');
    return $lists;
}

function get_sort_user_list($orgId,$work=-1){
    $rolesId =  \think\Db::name('roles')
        ->where('parent_id',7)
        ->where('org_id',$orgId)
        ->where('del',0)
        ->column('id');
    $userRolesId =  \think\Db::name('user_roles')
        ->where('roles_id','in',$rolesId)
        ->column('user_id');
    $map[] = ['u.id','in',$userRolesId];
    $map[] = ['u.del','=',0];
    $map[] = ['u.type','=',0];
    $map[] = ['u.enable','=',1];
    $map[] = ['u.enable_sorts','=',1];
    $map[] = ['uo.org_id','=',$orgId];
    if($work >0){
        $map[] = ['u.work','=',$work];

    }
    $map= empty($map) ? true: $map;
    $lists = \think\Db::name('user')
        ->alias('u')
        ->join('user_org uo','u.id=uo.user_id')
        ->where($map)
        ->field('u.id,uo.org_id,u.sorts')
        ->order(['u.sorts'=>'desc','u.id'=>'asc'])
        ->select();
    return $lists;
}

function getDateFromRange($startdate, $enddate){
    $stimestamp = strtotime($startdate);
    $etimestamp = strtotime($enddate);
    // 计算日期段内有多少天
    $days = ($etimestamp-$stimestamp)/86400+1;
    // 保存每天日期
    $date = array();
    for($i=0; $i<$days; $i++){
        $date[] = date('Y-m-d', $stimestamp+(86400*$i));
    }
    return $date;
}

/**
 * 加密cookie
 * @param $name
 * @param $val
 * @param $expire
 */
function encodecookie($name,$val,$expire=100*365*24*60*60){
    cookie($name,think_encrypt($val),$expire);
}

/**
 * 解密cookie
 * @param $name
 */
function decodecookie($name){
    return think_decrypt(cookie($name));
}

/**
 * 获取点餐微信公众号配置
 * @param $orgId  组织
 * @return array|bool  false=该组织未配置微信账号
 */
function get_pay_wechat($orgId){
    $wechat = \think\Db::name('dinner_wechat')->where('org_id',$orgId)->order('id','desc')->find();
    if(!$wechat){
        return [];
    }
    $option = [
        'user_type' => '3'.$wechat['id'],
        'app_id' => trim($wechat['app_id']),
        'secret' => trim($wechat['secret']),
        'mch_id' => trim($wechat['mch_id']), // 商户号
        'key' => trim($wechat['key']),   // API 密钥
        // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
        'cert_path' => env('root_path').'public'.$wechat['cert_path'],
        'key_path' => env('root_path').'public'.$wechat['key_path'],

        // 下面为可选项
        // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
        'response_type' => 'array',

        'log' => [
            'level' => 'error',
            'file' => env('runtime_path').'/log/wechat.log',
        ],
    ];
    return $option;
}

/**
 * 获取某周的周一和周末日期
 * @param int $index //0则代表本周 1则代表下一周
 * @return array
 */
function get_week_days($index = 0,$format="Y-m-d"){ //0则代表本周
    $week = date('w') == 0 ? 7 : date('w');
    $start = strtotime('today ' . (- ($week - 1) + (7 * $index)) . 'day');
    $end = strtotime('today ' . ((8 - $week) + (7 * $index)) . 'day -1second');
    return [date($format,$start), date($format,$end)];
}


/**
 * 获取某天是星期几
 * @param $day 例如:2023-03-30
 * @return string
 */
function get_week_txt($day){
    $da = date("w",strtotime($day));
    if( $da == "1" ){
        return '周一';
    }else if( $da == "2" ){
        return '周二';
    }else if( $da == "3" ){
        return '周三';
    }else if( $da == "4" ){
        return '周四';
    }else if( $da == "5" ){
        return '周五';
    }else if( $da == "6" ){
        return '周六';
    }else if( $da == "0" ){
        return '周日';
    }
    return '';
}
/**
 * 秒数格式化
 * @param $diff
 * @return string
 */
function second_to_str($diff){
    if($diff <= 0){
        return '0秒';
    }
    $days = floor(($diff )/ (60*60*24));
    $hours = floor(($diff - $days*60*60*24)  / (60*60));
    $minutes = floor(($diff - $days*60*60*24  - $hours*60*60)/ 60);
    $seconds = floor(($diff - $days*60*60*24  - $hours*60*60 - $minutes*60));
    if ($diff < 60) {
        $time_cost = $seconds."秒";
    } else if ($diff >= 60 && $diff < 60*60) {
        $time_cost = $minutes."分钟".$seconds."秒";
    } else if ($diff >= 60*60 && $diff < 60*60*24) {
        $time_cost = $hours."小时".$minutes."分钟".$seconds."秒";
    } else {
        $time_cost = $days."天".$hours."小时".$minutes."分钟".$seconds."秒";
    }
    return $time_cost;
}
//字符串换行;
 function force_black_string_ex($str, $num){

    $str_length = strlen($str);

    $strNum = $str_length/$num>0 ? ($str_length/$num) : ($str_length/$num)+1;
    $resultStr = '';

    for($i=0; $i<$strNum; $i++){
        $resultStr .= '<br>'.mb_substr($str, $i*$num,$num);
    }

    $resultStr = mb_substr($resultStr, 4);
    return $resultStr;
}

//根据分页截取数组
function array_page_list($list=[],$page=1,$size=10){

    $offset = ($page - 1) * $size;

    $arr = array_slice($list,$offset,$size);

    return $arr;

}

//二维数组去重去空
function array_remove_empty($array, $keys_to_check = []) {
    if (!is_array($array)) {
        return [];
    }

    if(!$array){
        return [];
    }

    $filtered_array = [];
    foreach ($array as $key => $value) {
        if (!is_array($value)) {
            if (!empty($value) || (is_numeric($value) && $value !== 0)) {
                $filtered_array[$key] = $value;
            }
        } else {
            $filtered_array[$key] = array_remove_empty($value, $keys_to_check);
        }
    }

    return $filtered_array;
}

//使用htmlpurifier防范xss攻击

function remove_xss($str){
    $cfg = HTMLPurifier_Config::createDefault();
    $cfg->set('Core.Encoding','UTF-8');
    $cfg->set('HTML.Allowed','div,b,strong,p[style],i,em,a[href|title],ul,ol,li,br,span[style],img[width|height|alt|src]');
    $cfg->set('CSS.AllowedProperties','font,font-size,font-weight,font-style,font-family,text-decoration,padding-left,color,background-color,text-align');
    $cfg->set('HTML.TargetBlank',TRUE);

    //    $cfg->set('Core.RemoveJavaScript', true);
    $obj = new HTMLPurifier($cfg);
    return $obj->purify($str);
}