<?php
/**
 * mobile公共方法
 *
 * 公共方法
 *
 */
defined('InShopNC') or exit('Access Invalid!');


require_once(BASE_CORE_PATH . '/framework/libraries/model.php');
require_once(BASE_DATA_PATH . '/model/member.model.php');
require_once(BASE_MOBILE_PATH . '/util/errcode.php');

function output_data($datas, $extend_data = array(),$code = 200) 
{
    $data = array();
    $data['code'] = $code;

    if(!empty($extend_data)) {
        $data = array_merge($data, $extend_data);
    }

    $data['datas'] = $datas;

    if(!empty($_GET['callback'])) {
        echo $_GET['callback'].'('.json_encode($data).')';
    } else {
        echo json_encode($data);
    }
}

function output_error($message, $extend_data = array(),$code = 200) {
    $datas = array('error' => $message);
    output_data($datas, $extend_data,$code);
}

function joutput_data($datas,$type='')
{
    $data = array();

    $code = errcode::Success;
    $data['code'] = $code;
    $data['message'] = errcode::msg($code);
    $data['datas'] = $datas;

    if($_SESSION['client_type'] != 'ajax')
    {
        $contents = ob_get_clean();
        if(!empty($contents)) {
            Log::record($contents,Log::ERR);
        }
        ob_start();
    }

    if(!empty($type) && $type == 'web') {
        echo(json_encode($data,JSON_UNESCAPED_UNICODE));
    } else {
        echo(json_encode($data));
    }
}

function joutput_error($code,$message = '',$type='')
{
    if(empty($message)) {
        $message = errcode::msg($code);
    }

    $data = array();
    $data['code'] = $code;
    $data['message'] =  $message;
    $data['datas'] = null;

    if($_SESSION['client_type'] != 'ajax')
    {
        $contents = ob_get_clean();
        if(!empty($contents)) {
            Log::record($contents,Log::ERR);
        }
        ob_start();
    }
    Log::record("code = {$code} message = {$message}",Log::ERR);

    if(!empty($type) && $type == 'web') {
        echo(json_encode($data,JSON_UNESCAPED_UNICODE));
    } else {
        echo(json_encode($data));
    }
}

function mobile_page($page_count) 
{
    $extend_data = array();
    $current_page = intval($_GET['curpage']);
    if($current_page <= 0) {
        $current_page = 1;
    }
    if($current_page >= $page_count) {
        $extend_data['hasmore'] = false;
    } else {
        $extend_data['hasmore'] = true;
    }
    $extend_data['page_total'] = $page_count;
    return $extend_data;
}

/**
 * 过滤html标签,js代码,css样式标签
 * @param $str
 * @return mixed
 */
function remove_tags($str) {
    $str = preg_replace( "@<script(.*?)</script>@is", "", $str );
    $str = preg_replace( "@<iframe(.*?)</iframe>@is", "", $str );
    $str = preg_replace( "@<style(.*?)</style>@is", "", $str );
    $str = preg_replace( "@<(.*?)>@is", "", $str );

    return $str;
}