<?php
/**
 * ajax接收端
 */
include("./includes/fun.global.php");

@header('Content-Type: application/json; charset=UTF-8');

switch ($_QET['act']) {
    case 'submit_input': #提交
        $user_data = login_data::user_data();
        if (!empty((int)$_QET['orders'])) {
            //启用新的订单缓存
            $DataOrder = $DB->get_row("SELECT * FROM `sky_journal` WHERE `count` = '{$_QET['orders']}' AND `name` = '订单缓存' AND `uid` = -1024 LIMIT 1");
            if (!$DataOrder) dies(-1, '订单已经失效！,请重新提交！');
            $data = config::common_unserialize($DataOrder['content'])['data'][0];
            $_QET['gid'] = $data['gid'];
            $_QET['data'] = $data['input'];
            $_QET['num'] = $data['num'];
        }
        //维持原有状态,防止第三方插件APP失效
        switch ($_QET['type']) {
            case 1:
                if ($user_data <> false) {
                    reward::user_grant($_QET, $user_data['id']);
                } else reward::user_grant($_QET, -1);
                break;
            case 2:
                if (reward::user_landing() <> 1) dies('-2', '请先完成登陆!');
                reward::user_grant($_QET, $user_data['id']);
                break;
            case 3:
                if (reward::user_landing() <> 1) dies('-2', '请先完成登陆!');
                reward::user_grant($_QET, $user_data['id']);
                break;
            default:
                dies(-1, '参数异常');
                break;
        }
        break;
    case 'order_details': #查询订单详情
        if (empty($_QET['id'])) die(json_encode(['code' => '-1', 'msg' => '请填写完整！']));
        $id = (int)$_QET['id'];
        /**
         * 仅给用户开放下列查询权限！
         */
        community::order_details($id, ',当前商品名称,商品下单份数,订单初始数量,订单当前数量,详细订单状态,订单处理状态,用户下单地区,本地系统订单,订单付款方式,订单付款金额,订单创建时间,订单修改时间,订单处理备注,购买商品说明');
        break;
    case 'OrderQuery': //查询订单（新）
        if (empty($_QET['type'])) dies(-1, '请填写完整！');
        switch ($_QET['type']) {
            case 'OrderAll': //查询全部订单
                query::OrderAll($_QET['msg']);
                break;
        }
        break;
    case 'orders_transit': #写入日志缓存！
        if (empty($_QET['conts']) || empty((int)$_QET['gid']) || empty($_QET['data'])) dies(-1, '请将参数提交完整！');
        if (empty((int)$_QET['num'])) {
            $_QET['num'] = 1;
        }
        $arr_blacklist = explode(',', $conf['blacklist']);
        foreach ($arr_blacklist as $v) {
            if (strpos(json_encode($_QET['data']), $v) !== false) dies(-1, '下单信息：<font color="red" size="5">' . $v . '</font><br>已被站长设置为下单违禁词！');
        }
        $IP = userip();
        $input = [];
        foreach ($_QET['data'] as $v) {
            $input[] = trim($v);
        }
        //订单列写入data[0]，为后续购物车做准备！
        $Data = serialize(['data' => [['gid' => $_QET['gid'], 'input' => $input, 'num' => $_QET['num'], 'conts' => $_QET['conts']]]]);
        $count = $DB->count("SELECT count(*) FROM `sky_journal` WHERE `name` = '订单缓存' ") + 1;
        $re = $DB->query("INSERT INTO `sky_journal` (`ip`, `uid`, `count`, `name`, `content`, `date`)
 VALUES ('$IP', '-1024', '$count', '订单缓存', '$Data', '$date');");
        if ($re) {
            if (template::isMobile() == true) {
                $typs = 1;
                $url = '/?mod=checkstandM&order=';
            } else {
                $typs = 2;
                $url = '/?mod=checkstandPC&order=';
            }
            dier(['code' => 1, 'msg' => '订单创建成功,点击前往付款！', 'url' => $url . $count, 'type' => $typs]);
        } else dies(-1, '订单创建失败！' . $DB->error());
        break;
    case 'getclass': #获取分类
        $re = $DB->query("SELECT * FROM `sky_class` WHERE `state` = 1  ORDER BY `sort` DESC ");
        $array = [];
        while ($res = $DB->fetch($re)) {
            $count = $DB->count("SELECT count(*) FROM `sky_goods` WHERE `cid` = '{$res['cid']}' AND `state` = '1' ");
            $res['number'] = $count;
            $array[] = $res;
        }
        dier([
            'code' => 0,
            'msg' => 'succ',
            'data' => $array
        ]);
        break;
    case 'SharePoster':
        if (empty((int)$_QET['gid'])) dies(-1, '请填写完整！');
        include_once(SYSTEM_ROOT . "extend/GoodsImage.php");
        $image = \extend\GoodsImage::mergePic($_QET['gid']);
        if ($image) {
            dier([
                'code' => 1,
                'msg' => '商品海报图生成成功！',
                'src' => $image . '?sr=' . time()
            ]);
        } else dies(-1, '商品海报图生成失败！');
        break;
    case 'prevent': #分享防洪调用
        if (empty($_SESSION['url_pr'])) dies(-1, '无权访问');
        $url_doem = str_replace("[url]", reward::prevent($_SESSION['url_pr'], 2), $conf['share_url']);
        dies(1, $url_doem);
        break;
    case 'getgoods': #获取分类下的商品
        $cid = (int)$_QET['cid'];
        if (empty($cid)) dies(-1, '请填写完整');
        if ($cid == '-1') {
            $data = $DB->get_row("SELECT cid FROM `sky_class` ORDER BY `sort` DESC LIMIT 1");
            if ($data) $cid = $data['cid'];
        }
        $user_data = login_data::user_data();
        $re = $DB->query("SELECT gid,name,image,quota,sort,cid,quantity,profits,money FROM `sky_goods` WHERE `state` = 1 AND `cid`= '$cid'  ORDER BY `sort` DESC ");
        $array = [];
        while ($res = $DB->fetch($re)) {
            $atl = $DB->count("SELECT sum(num) FROM `sky_buylog` WHERE `gid` = '{$res['gid']}' AND `date`> '$times' ");
            $res['quota'] = $res['quota'] - $atl;
            $price = price::price_uid(($user_data == false ? -1 : $user_data['grade']), $res['profits'], $res['money']);
            unset($res['profits'], $res['money']);
            $res['price'] = round($price['price'], 2);
            $res['points'] = round($price['points'], 0);
            $res['level'] = $price['level'];
            $array[] = $res;
        }
        dier([
            'code' => 0,
            'msg' => 'succ',
            'data' => $array
        ]);
        break;
    case 'getgoods_details': #获取详细商品信息
        $gid = (int)$_QET['gid'];
        if (empty($gid)) dies(-1, '请填写完整');
        $user_data = login_data::user_data();
        $res = $DB->get_row("SELECT freight,picture,alert,gid,name,image,value,quota,quantity,docs,method,cid,profits,money,min,max FROM `sky_goods` WHERE `gid`='$gid' AND `state` = 1  LIMIT 1");
        if (!$res) dies(-1, '商品不存在！');
        if (in_array(5, json_decode($res['method'], TRUE))) {
            /**
             * 价格监控
             * 1、玖伍测试无异常：检查时间2020/3/13
             * 2、亿乐测试无异常：检测时间2020/313
             * 3、同系统测试无异常：检测时间2020/313
             * 4、卡易信测试无异常：检测时间2020/313
             */
            $gidass = $DB->get_row("SELECT * FROM `sky_goods` WHERE `gid`='$gid' AND `state` = 1  LIMIT 1");
            $monitoring = new monitoring();
            $state_gid = $monitoring->Price($gidass);
            if ($state_gid == -1) dies(-4, '商品有新的调整,点击确定继续！');
        }
        $atl = $DB->count("SELECT sum(num) FROM `sky_buylog` WHERE `gid` = '{$res['gid']}' AND `date`> '$times'  ");
        $res['count_quota'] = $res['quota'] - $atl;
        $res['count'] = count(explode('|', $res['value']));
        $res['currency'] = $conf['currency'];
        $res['method'] = json_decode($res['method'], TRUE);
        $res['docs'] = htmlspecialchars_decode(htmlspecialchars_decode($res['docs']));
        $price = price::price_uid(($user_data == false ? -1 : $user_data['grade']), $res['profits'], $res['money']);
        unset($res['profits'], $res['money']);
        $res['price'] = round($price['price'], 2);
        $res['points'] = round($price['points'], 0);
        $res['level'] = $price['level'];
        $res['level_arr'] = $price['level_arr'];
        $res['pay'] = $conf['pay_qqapy'] . '_' . $conf['pay_wxpay'] . '_' . $conf['pay_alipay'];
        if ($res['min'] == null || $res['min'] == '') $res['min'] = 0;
        if ($res['max'] == null || $res['max'] == '') $res['max'] = 0;
        if ($res['freight'] <> -1) {
            $freight = $DB->get_row("SELECT money,region,nums,exceed,threshold FROM `sky_freight` WHERE `id` = '{$res['freight']}' LIMIT 1");
            if ($freight) {
                $res['freight'] = $freight;
            }
        }

        dier([
            'code' => 1,
            'msg' => 'succ',
            'data' => $res
        ]);
        break;
    case 'gethuoshan':
        $url = trim($_POST['url']);
        if (empty($url)) exit('{"code":-5,"msg":"url不能为空"}');
        $result = gethuoshan($url);
        dier($result);
        break;
    case 'getxiaohongshu':
        $url = trim($_POST['url']);
        if (empty($url)) exit('{"code":-5,"msg":"url不能为空"}');
        $result = getxiaohongshu($url);
        dier($result);
        break;
    case 'getshuoshuo':
        $uin = trim(daddslashes($_GET['uin']));
        $page = intval($_GET['page']);
        if (empty($uin)) exit('{"code":-5,"msg":"QQ号不能为空"}');
        $result = getshuoshuo($uin, $page);
        dier($result);
        break;
    case 'getkuaishou':
        $url = trim($_POST['url']);
        if (empty($url)) exit('{"code":-5,"msg":"url不能为空"}');
        $result = getkuaishou($url);
        dier($result);
        break;
    case 'getkuaishou2': //新快手链接
        $url = trim($_POST['url']);
        if (empty($url)) exit('{"code":-5,"msg":"url不能为空"}');
        $result = getkuaishou2($url);
        dier($result);
        break;
    case 'getshareid':
        $url = trim($_POST['url']);
        if (empty($url)) exit('{"code":-5,"msg":"url不能为空"}');
        $result = getshareid($url);
        dier($result);
        break;
    case 'query_goods': #查询商品
        $val = (string)$_QET['name'];
        if (empty($val)) dies(-1, '请填写完整');
        $user_data = login_data::user_data();
        $re = $DB->query("SELECT  gid,name,image,quota,sort,cid,quantity,profits,money FROM `sky_goods` WHERE `state` = 1 AND `name` like '%$val%' ORDER BY `sort` DESC ");
        $array = [];
        while ($res = $DB->fetch($re)) {
            $atl = $DB->count("SELECT sum(num) FROM `sky_buylog` WHERE `gid` = '{$res['gid']}' AND `date`> '$times' ");
            $res['quota'] = $res['quota'] - $atl;
            $price = price::price_uid(($user_data == false ? -1 : $user_data['grade']), $res['profits'], $res['money']);
            unset($res['profits'], $res['money']);
            $res['price'] = round($price['price'], 2);
            $res['points'] = round($price['points'], 0);
            $res['level'] = $price['level'];
            $array[] = $res;
        }
        dier([
            'code' => 0,
            'msg' => 'succ',
            'data' => $array
        ]);
        break;
}