<?php
/**
 * api模块
 */

include 'includes/fun.global.php';
include_once(SYSTEM_ROOT . "fun.admin.php");
@header('Content-Type: application/json; charset=UTF-8');
switch ($_QET['act']) {
    case 'ip':
        dies(1, userip());
        break;
    case 'operation':
        admin::operation($_QET);
        break;
    case 'WebsiteData': #获取网站基础数据
        dier(['code' => 1, 'msg' => '数据获取成功', 'data' => [
            'sitename' => $conf['sitename'], //网站名称
            'kfqq' => $conf['kfqq'], //客服QQ
            'notice_top' => base64_decode($conf['notice_top']), //首页公告
            'notice_check' => base64_decode($conf['notice_check']), //查单公告
            'notice_bottom' => base64_decode($conf['notice_bottom']), //底部公告
            'notice_user' => base64_decode($conf['notice_user']), //分站后台公告
            'currency' => $conf['currency'], //货币名称
        ], 'time' => time()]);
        break;
    case 'Docking_get':
        $Docking = new Docking();
        $verify = $Docking->verify($_QET);
        if ($verify['code'] == 1) {
            $goods = $DB->query("SELECT gid,cid,sort,name,image,quota,quantity,alert,docs,method,value,min,max,picture,alert,profits,money FROM `sky_goods` WHERE `state` = 1 ");
            $array = [];
            while ($re = $DB->fetch($goods)) {
                $method = json_decode($re['method'], true);
                unset($re['method']);
                if (!in_array(4, $method)) continue;
                $price = price::price_uid($verify['data']['grade'], $re['profits'], $re['money']);
                unset($re['profits'], $re['money']);
                $re['price'] = $price['price'];
                if ($re['price'] == 0) continue;
                $re['points'] = $price['points'];
                $re['level'] = $price['level'];
                $re['level_arr'] = $price['level_arr'];
                if ($re['min'] == null || $re['min'] == '') $re['min'] = 0;
                if ($re['max'] == null || $re['max'] == '') $re['max'] = 0;
                $array[] = $re;
                unset($method);
            }
            dier([
                'code' => 1,
                'msg' => '商品列表获取成功',
                'data' => $array,
                'time' => time(),
                'money' => $verify['data']['money'],
            ]);
        } else dies(-1, '验证失败');
        break;
    case 'Docking_buy':
        $Docking = new Docking();
        $verify = $Docking->verify($_QET);
        if ($verify['code'] == 1) {
            $Docking->buy($_QET, $verify['data']);
        } else dies(-1, '验证失败');
        break;
    case 'Docking_query':
        $Docking = new Docking();
        $verify = $Docking->verify($_QET);
        if ($verify['code'] == 1) {
            $id = (int)$_QET['order'];
            $or = $DB->get_row("SELECT * FROM `sky_order` WHERE `order` = '$id' LIMIT 1");
            if (!$or) dies(-1, '订单不存在!');
            community::order_details($or['id'], ',当前商品名称,商品下单份数,订单初始数量,订单当前数量,详细订单状态,订单处理状态,用户下单地区,本地系统订单,订单付款方式,订单付款金额,订单创建时间,订单修改时间,订单处理备注,购买商品说明');
        } else dies(-1, '验证失败');
        break;
    case 'Docking_goods':
        $Docking = new Docking();
        $verify = $Docking->verify($_QET);
        if ($verify['code'] == 1) {
            $gid = (int)$_QET['gid'];
            $data = $DB->get_row("SELECT * FROM `sky_goods` WHERE `gid` = $gid LIMIT 1");
            $price = price::price_uid($verify['data']['grade'], $data['profits'], $data['money']);
            unset($data['profits'], $data['money']);
            $price['price'] = (float)$price['price'] / $data['quantity'];
            $price['state'] = $data['state'];
            dier(['code' => 1, 'msg' => '获取成功!', 'data' => $price]);
        } else dies(-1, '验证失败');
        break;
    case 'cloning_print_data':
        if (empty((string)$_QET['token'])) dies(-1, '克隆密钥有误！');
        $cloning = new cloning();
        $cloning->print_data($_QET);
        break;
    case 'UserMoneyApi': //用户加款/扣钱
        if (empty((string)$_QET['token']) || empty((int)$_QET['uid']) || empty((int)$_QET['type']) || empty((float)$_QET['money'])) dies(-1, '请填写完整！');
        if ((string)$_QET['token'] <> $conf['secret']) dies(-1, 'API对接密钥有误！');
        $uid = (int)$_QET['uid'];
        $money = (float)$_QET['money'];
        $dr = $DB->get_row("SELECT money FROM `sky_user` WHERE `id` = $uid LIMIT 1");
        if (!$dr) dies(-1, '用户不存在！');
        switch ((int)$_QET['type']) {
            case 1: #充值
                $m = $dr['money'] + $money;
                $re = $DB->query("UPDATE `sky_user` SET `money`= $m WHERE (`id`='$uid')");
                break;
            case 2: #扣款
                $m = $dr['money'] - $money;
                if ($m < 0) dies(-1, '用户余额低于0,无法扣款！');
                $re = $DB->query("UPDATE `sky_user` SET `money`= $m WHERE (`id`='$uid')");
                break;
        }
        if ($re) {
            $msg = '成功通过api为用户(' . $uid . ')' . ($_QET['type'] == 1 ? '加款' : '扣款') . $money . '元,操作后余额为:' . $m . '元!';
            userlog(($_QET['type'] == 1 ? '后台加款' : '后台扣款'), $msg, $uid, $money);
            dies(1, $msg);
        } else dies(-1, ($_QET['type'] == 1 ? '加款' : '扣款') . '失败！');
        break;
    case 'OrdersTesting': //漏单监控(目前只支持易支付！)
        price_monitoring::pay_order();
        break;
    default:
        dies(-1, '403');
        break;
}