<?php

namespace web\ambap;

class me {
    public static function json_invoicetitle_get() {
        global $db;
        $rsuser = verifyfast();
        $csql = new \ciy\sql('ap_usr_invoicetitle');
        $csql->where('vuser', $rsuser['id']);
        $csql->order('id desc');
        $ret['list'] = $db->get($csql);
        return succjson($ret);
    }
    public static function json_invoicetitle_edit() {
        global $db;
        $rsuser = verifyfast();
        if ($rsuser === null)
            return errjson('您还未登陆', 2);
        $post = new \ciy\post();
        $id = $post->getint('id');
        if ($id > 0) {
            $csql = new \ciy\sql('ap_usr_invoicetitle');
            $csql->where('vuser', $rsuser['id']);
            $csql->where('id', $id);
            $shiprow = $db->getone($csql);
            if (!is_array($shiprow))
                return errjson('抬头不存在' . $id);
        }
        $name = $post->get('name');
        $addr = $post->get('addr');
        $phone = $post->get('phone');
        $taxnumber = $post->get('taxnumber');
        $bankname = $post->get('bankname');
        $bankaccount = $post->get('bankaccount');
        try {
            $db->begin();
            $updata = array();
            $updata['name'] = $name;
            $updata['addr'] = $addr;
            $updata['taxnumber'] = $taxnumber;
            $updata['phone'] = $phone;
            $updata['bankname'] = $bankname;
            $updata['bankaccount'] = $bankaccount;
            $updata['uptimes'] = tostamp();
            $csql = new \ciy\sql('ap_usr_invoicetitle');
            if ($id > 0) {
                $csql->where('id', $id);
                if ($db->update($csql, $updata) === false)
                    throw new \Exception('更新失败:' . $db->error);
            } else {
                $updata['vuser'] = $rsuser['id'];
                if ($db->insert($csql, $updata) === false)
                    throw new \Exception('新增失败:' . $db->error);
                $id = $db->insert_id();
            }
            $updata['id'] = $id;
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            return errjson($ex->getMessage());
        }
        $ret['data'] = $updata;
        return succjson($ret);
    }
    public static function json_invoicetitle_del() {
        global $db;
        $rsuser = verifyfast();
        if ($rsuser === null)
            return errjson('您还未登陆', 2);
        $post = new \ciy\post();
        $id = $post->getint('id');
        $csql = new \ciy\sql('ap_usr_invoicetitle');
        $csql->where('vuser', $rsuser['id']);
        $csql->where('id', $id);
        if ($db->delete($csql) === false)
            return errjson('删除失败:' . $db->error);
        return succjson();
    }
    public static function json_shipaddr_get() {
        global $db;
        $rsuser = verifyfast();
        $csql = new \ciy\sql('ap_usr_shipaddr');
        $csql->where('vuser', $rsuser['id']);
        $csql->order('id desc');
        $ret['list'] = $db->get($csql);
        return succjson($ret);
    }
    public static function json_shipaddr_edit() {
        global $db;
        $rsuser = verifyfast();
        if ($rsuser === null)
            return errjson('您还未登陆', 2);
        $post = new \ciy\post();
        $id = $post->getint('id');
        if ($id > 0) {
            $csql = new \ciy\sql('ap_usr_shipaddr');
            $csql->where('vuser', $rsuser['id']);
            $csql->where('id', $id);
            $shiprow = $db->getone($csql);
            if (!is_array($shiprow))
                return errjson('地址不存在' . $id);
        }
        $name = $post->get('name');
        $addr = $post->get('addr');
        $phone = $post->get('phone');
        $areacode = $post->getint('areacode');
        try {
            $db->begin();
            $updata = array();
            $updata['name'] = $name;
            $updata['addr'] = $addr;
            $updata['areacode'] = $areacode;
            $updata['phone'] = $phone;
            $updata['uptimes'] = tostamp();
            $csql = new \ciy\sql('ap_usr_shipaddr');
            if ($id > 0) {
                $csql->where('id', $id);
                if ($db->update($csql, $updata) === false)
                    throw new \Exception('更新失败:' . $db->error);
            } else {
                $updata['vuser'] = $rsuser['id'];
                if ($db->insert($csql, $updata) === false)
                    throw new \Exception('新增失败:' . $db->error);
                $id = $db->insert_id();
            }
            $updata['id'] = $id;
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            return errjson($ex->getMessage());
        }
        $ret['data'] = $updata;
        return succjson($ret);
    }
    public static function json_shipaddr_del() {
        global $db;
        $rsuser = verifyfast();
        if ($rsuser === null)
            return errjson('您还未登陆', 2);
        $post = new \ciy\post();
        $id = $post->getint('id');
        $csql = new \ciy\sql('ap_usr_shipaddr');
        $csql->where('vuser', $rsuser['id']);
        $csql->where('id', $id);
        if ($db->delete($csql) === false)
            return errjson('删除失败:' . $db->error);
        return succjson();
    }
    public static function json_problechat_close() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $id = $post->getint('id');
        $updata = array();
        $updata['closetimes'] = tostamp();
        $csql = new \ciy\sql('ap_usr_problem');
        $csql->where('id', $id);
        if ($db->update($csql, $updata) === false)
            return errjson('更新失败:' . $db->error);
        return succjson();
    }
    public static function json_problechat_send() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $content = $post->get('content');
        $id = $post->getint('id');
        $updata = array();
        $updata['problemid'] = $id;
        $updata['content'] = $content;
        $updata['sendtype'] = 1;
        $updata['addtimes'] = tostamp();
        $csql = new \ciy\sql('ap_usr_problechat');
        if ($db->insert($csql, $updata) === false)
            return errjson('更新失败:' . $db->error);
        $updata['id'] = $db->insert_id();
        $ret['data'] = $updata;
        $updata = array();
        $updata['replytimes'] = 0;
        $updata['closetimes'] = 0;
        $csql = new \ciy\sql('ap_usr_problem');
        $csql->where('id', $id);
        if ($db->update($csql, $updata) === false)
            return errjson('更新失败:' . $db->error);
        return succjson($ret);
    }
    public static function json_problechat_get() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $id = $post->getint('id');
        $csql = new \ciy\sql('ap_usr_problechat');
        $csql->where('problemid', $id);
        $mid = $post->getint('mid');
        $pagecount = $post->getint('pagecount', 20);
        if ($post->get('op') == 'max') {
            $csql->where('id>', $mid);
            $csql->order('id');
        } else {
            $csql->where('id<', $mid);
            $csql->order('id desc');
            $csql->limit(1, $pagecount);
        }
        $rows = $db->get($csql);
        $ret['list'] = $rows;
        $ret['pagecount'] = $pagecount;
        if ($post->getbool('once')) {
            $ret['once'] = array();
            $csql = new \ciy\sql('ap_usr_problem');
            $csql->where('id', $id);
            $ret['once']['data'] = $db->getone($csql);
        }
        return succjson($ret);
    }
    public static function json_problem_get() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $query = $post->get('query');
        $csql = new \ciy\sql('ap_usr_problem');
        $liid = objint($query, 'liid');
        if ($liid == 3)
            $csql->where('closetimes=0')->where('replytimes>0');
        else if ($liid == 9)
            $csql->where('closetimes>0');
        else
            $csql->where('closetimes=0')->where('replytimes=0');
        $csql->where('vuser', $rsuser['id']);
        $csql->order('id desc');
        $pageno = $post->getint('pageno', 1);
        $pagecount = $post->getint('pagecount', 10);
        $csql->limit($pageno, $pagecount);
        $rows = $db->get($csql);
        $ret = array('pageno' => $pageno, 'pagecount' => $pagecount, 'list' => $rows);
        return succjson($ret);
    }
    public static function json_problem_add() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $content = $post->get('content');
        $imgs = $post->get('imgs');
        $contract = $post->get('contract');
        $updata = array();
        $updata['content'] = $content;
        $updata['imgs'] = $imgs;
        $updata['contract'] = $contract;
        $updata['vuser'] = $rsuser['id'];
        $updata['addtimes'] = tostamp();
        $csql = new \ciy\sql('ap_usr_problem');
        if ($db->insert($csql, $updata) === false)
            return errjson('更新失败:' . $db->error);
        $updata['id'] = $db->insert_id();
        $ret['data'] = $updata;
        return succjson($ret);
    }
    public static function json_suggest_get() {
        global $db;
        $rsuser = verifyuser();
        $ret['list'] = array();
        if ($rsuser == null)
            return succjson($ret);
        $csql = new \ciy\sql('ap_usr_suggest');
        $csql->where('vuser', $rsuser['id']);
        $csql->order('id desc');
        $csql->limit(1, 10);
        $ret['list'] = $db->get($csql);
        return succjson($ret);
    }
    public static function json_suggest_submit() {
        global $db;
        $rsuser = verifyuser();
        $post = new \ciy\post();
        $content = $post->get('content');
        $imgs = $post->get('imgs');
        $contract = $post->get('contract');
        $updata = array();
        $updata['content'] = $content;
        $updata['imgs'] = $imgs;
        $updata['contract'] = $contract;
        if ($rsuser === null)
            $updata['vuser'] = 0;
        else
            $updata['vuser'] = $rsuser['id'];
        $updata['addtimes'] = tostamp();
        $csql = new \ciy\sql('ap_usr_suggest');
        if ($db->insert($csql, $updata) === false)
            return errjson('更新失败:' . $db->error);
        return succjson();
    }
    public static function json_getqrcode() {
        global $db;
        $rsuser = verifyfast();
        $filename = '/xcxqrcode/' . $rsuser['id'] . '.jpg';
        $ret['qrcode'] = $filename;
        $filename = PATH_WEB . 'ud' . $filename;
        if (file_exists($filename))
            return succjson($ret);
        $wxcfg = gettoken($db, 1);
        $wx = new \ciy\wxfunc($wxcfg);
        if ($wxcfg['exptimes'] < time()) {
            $access = $wx->getaccess();
            if (is_string($access))
                return errjson($access);
            settoken($db, 1, $access);
            $wx->accesstoken = $access['accesstoken'];
        }
        $param = array();
        $param['scene'] = 'upid=' . $rsuser['id'] . '&carduser=' . $rsuser['id'];
        $param['page'] = 'pages/main/index';
        $param['check_path'] = true;
        $param['env_version'] = 'release';
        $param['width'] = 1280;
        $retwx = $wx->getwxacodeunlimit($param);
        if (@$retwx[0] == '{') {
            $json = json_decode($retwx, true);
            if ($json === null)
                return errjson('微信服务器无法连接getwxacodeunlimit');
            if (isset($json['errmsg']))
                return errjson($json['errmsg']);
            return errjson('生成二维码失败');
        }
        dirmake(dirname($filename));
        $fp = fopen($filename, 'w');
        fwrite($fp, $retwx);
        fclose($fp);
        return succjson($ret);
    }
    public static function json_userreal_apply_get() {
        global $db;
        $rsuser = verifyfast();
        $csql = new \ciy\sql('ap_usr_real_apply');
        $csql->where('vuser', $rsuser['id']);
        $csql->order('id desc');
        $ret['data'] = $db->getone($csql);
        return succjson($ret);
    }
    public static function json_userreal_apply_submit() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $name = $post->get('name');
        if (empty($name))
            return errjson('姓名不能为空.');
        $idid = idcard($post->get('idno'));
        if (is_string($idid))
            return errjson($idid);
        $idcard1 = $post->get('idcard1');
        $updata = array();
        $updata['auditstatus'] = 20;
        $updata['vuser'] = $rsuser['id'];
        $updata['name'] = $name;
        $updata['idid'] = $idid['code'];
        $updata['idcard1'] = $idcard1;
        $updata['addtimes'] = tostamp();
        $csql = new \ciy\sql('ap_usr_real_apply');
        if ($db->insert($csql, $updata) === false)
            return errjson('新增失败:' . $db->error);
        return succjson();
    }
    public static function json_wxrecvpay() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $id = $post->getint('id');
        $csql = new \ciy\sql('ap_cash_out');
        $csql->where('id', $id);
        $cashrow = $db->getone($csql);
        if (!is_array($cashrow))
            return errjson('提现不存在');
        if ($cashrow['cashstatus'] == 21) {
            if ($cashrow['cashtimes'] > tostamp() - 86400)
                return errjson('提现打款进程被打断。24小时后重新操作，请务必点击收款');
        } else if ($cashrow['cashstatus'] != 20)
            return errjson('提现不是已通过状态');
        if ($cashrow['vuser'] != $rsuser['id'])
            return errjson('提现用户不匹配');
        $updata = array();
        $updata['cashstatus'] = 21;
        $updata['cashtimes'] = tostamp();
        $csql = new \ciy\sql('ap_cash_out');
        $csql->where('id', $id);
        if ($db->update($csql, $updata) === false)
            return errjson('更新失败');

        $param = array();
        $param['out_bill_no'] = 'TB' . date('ymd') . '8' . $id;
        $param['transfer_scene_id'] = '1005';
        $param['openid'] = $rsuser['wxminaid'];
        $param['notify_url'] = 'https://expo.ciy.cn/ajax/amb/wxpay.transfer';
        $param['transfer_remark'] = '佣金奖励';
        $param['transfer_amount'] = $cashrow['cashmoney'];
        $param['transfer_scene_report_infos'] = array();
        $param['transfer_scene_report_infos'][] = array('info_type' => '岗位类型', 'info_content' => '采集员');
        $param['transfer_scene_report_infos'][] = array('info_type' => '报酬说明', 'info_content' => '用户主动申请提现');

        $wxcfg = gettoken($db, 1);
        $wxpay = new \ciy\wxfunc($wxcfg);
        $rettransfer = $wxpay->transfer($param);
        if (is_string($rettransfer))
            return errjson($rettransfer);
        $rettransfer['mchid'] = $wxpay->mchid;
        $rettransfer['appid'] = $wxpay->appid;
        $ret['transfer'] = $rettransfer;
        return succjson($ret);
    }
    public static function json_userbank_change() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $cashtype = $post->getint('cashtype');
        $bankno = $post->get('bankno');
        $bankname = $post->get('bankname');
        $bankaccount = $post->get('bankaccount');
        $updata = array();
        $updata['cashtype'] = $cashtype;
        $updata['bankno'] = $bankno;
        $updata['bankname'] = $bankname;
        $updata['bankaccount'] = $bankaccount;
        $csql = new \ciy\sql('ap_usr_ext');
        $csql->where('id', $rsuser['id']);
        $extrow = $db->getone($csql);
        if (is_array($extrow))
            if ($db->update($csql, $updata) === false)
                return errjson('更新失败:' . $db->error);
            else {
                $updata['id'] = $rsuser['id'];
                if ($db->insert($csql, $updata) === false)
                    return errjson('新增失败:' . $db->error);
            }
        return succjson();
    }
    public static function json_cashout_get() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $query = $post->get('query');
        $csql = new \ciy\sql('ap_cash_out');
        $csql->where('vuser', $rsuser['id']);
        $liid = objint($query, 'liid');
        if ($liid > 0)
            $csql->where('cashstatus', $liid);
        $csql->order(objstr($query, 'order', 'id desc'));
        $pageno = $post->getint('pageno', 1);
        $pagecount = $post->getint('pagecount', 10);
        $csql->limit($pageno, $pagecount);
        $rows = $db->get($csql);
        $ret = array('pageno' => $pageno, 'pagecount' => $pagecount, 'list' => $rows);
        return succjson($ret);
    }
    public static function json_invoicing_get() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $query = $post->get('query');
        $csql = new \ciy\sql('ap_usr_invoicing');
        $csql->where('vuser', $rsuser['id']);
        $liid = objint($query, 'liid');
        if ($liid == 2)
            $csql->where('invtimes=0');
        if ($liid == 3)
            $csql->where('invtimes>0');
        $csql->order(objstr($query, 'order', 'id desc'));
        $pageno = $post->getint('pageno', 1);
        $pagecount = $post->getint('pagecount', 10);
        $csql->limit($pageno, $pagecount);
        $rows = $db->get($csql);
        $ret = array('pageno' => $pageno, 'pagecount' => $pagecount, 'list' => $rows);
        return succjson($ret);
    }
    public static function json_invoicing() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $invtit = $post->getint('invtit');
        $money = $post->getint('money');
        if ($money < 100)
            return errjson('需填开票金额');
        if ($rsuser['myinvmoney'] < $money)
            return errjson('可开票金额不足');
        $csql = new \ciy\sql('ap_usr_invoicetitle');
        $csql->where('id', $invtit);
        $invoicerow = $db->getone($csql);
        if (!is_array($invoicerow))
            return errjson('开票抬头不存在');
        try {
            $db->begin();
            $updata = array();
            $updata['myinvmoney'] = array('myinvmoney-' . $money);
            $csql = new \ciy\sql('ap_user');
            $csql->where('id', $rsuser['id']);
            if ($db->update($csql, $updata) === false)
                throw new \Exception('更新账户失败:' . $db->error);
            $updata = array();
            $updata['invoicemoney'] = $money;
            $updata['invoicetitleid'] = $invtit;
            $updata['vuser'] = $rsuser['id'];
            $updata['addtimes'] = tostamp();
            $updata['name'] = $invoicerow['name'];
            $updata['taxnumber'] = $invoicerow['taxnumber'];
            $updata['addr'] = $invoicerow['addr'];
            $updata['phone'] = $invoicerow['phone'];
            $updata['bankname'] = $invoicerow['bankname'];
            $updata['bankaccount'] = $invoicerow['bankaccount'];
            $csql = new \ciy\sql('ap_usr_invoicing');
            if ($db->insert($csql, $updata) === false)
                throw new \Exception('创建失败:' . $db->error);
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            return errjson($ex->getMessage());
        }
        return succjson();
    }
    public static function json_cash() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $cash = $post->getint('cash') * 100;
        if ($rsuser['mycashmoney'] < $cash)
            return errjson('余额不足');
        $cashtype = 1;
        $csql = new \ciy\sql('ap_usr_ext');
        $csql->where('id', $rsuser['id']);
        $extrow = $db->getone($csql);
        if (is_array($extrow))
            $cashtype = $extrow['cashtype'];
        if ($cashtype == 1) {
            if ($cash < 1000 * 100)
                return errjson('独立纳税，单次提现金额不能少于1000元');
        }
        try {
            $db->begin();
            $updata = array();
            $updata['mycashmoney'] = array('mycashmoney-' . $cash);
            $csql = new \ciy\sql('ap_user');
            $csql->where('id', $rsuser['id']);
            if ($db->update($csql, $updata) === false)
                throw new \Exception('更新账户失败:' . $db->error);
            $updata = array();
            $updata['cashstatus'] = 10;
            $updata['cashmoney'] = $cash;
            $updata['cashtype'] = $cashtype;
            $updata['vuser'] = $rsuser['id'];
            $updata['addtimes'] = tostamp();
            $csql = new \ciy\sql('ap_cash_out');
            if ($db->insert($csql, $updata) === false)
                throw new \Exception('创建失败:' . $db->error);
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            return errjson($ex->getMessage());
        }
        return succjson();
    }
    public static function json_pntbuy_get() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $query = $post->get('query');
        $csql = new \ciy\sql('ap_pnt_buy');
        $csql->where('vuser', $rsuser['id']);
        $liid = objint($query, 'liid');
        if ($liid == 2)
            $csql->where('paytimes=0');
        if ($liid == 3)
            $csql->where('paytimes>0');
        $csql->order(objstr($query, 'order', 'id desc'));
        $pageno = $post->getint('pageno', 1);
        $pagecount = $post->getint('pagecount', 10);
        $csql->limit($pageno, $pagecount);
        $rows = $db->get($csql);
        $ret = array('pageno' => $pageno, 'pagecount' => $pagecount, 'list' => $rows);
        return succjson($ret);
    }
    public static function json_buy_paychk() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $id = $post->getint('orderid');
        for ($i = 0; $i < 10; $i++) {
            $csql = new \ciy\sql('ap_pnt_buy');
            $csql->where('id', $id);
            $buyrow = $db->getone($csql);
            if (!is_array($buyrow))
                return errjson('订单不存在');
            if ($buyrow['paytimes'] > 0)
                return succjson();
            sleep(1);
        }
        return errjson('支付不成功,请联系客服');
    }

    public static function json_buy_paynow() {
        global $db;
        $rsuser = verifyfast();
        //建立订单，支付
        $post = new \ciy\post();
        $id = $post->getint('id');
        $csql = new \ciy\sql('ap_pnt_bundle');
        $csql->where('id', $id);
        $bundlerow = $db->getone($csql);
        if (!is_array($bundlerow))
            return errjson('购买项目不存在');
        $payprice = $bundlerow['buymoney'];
        $updata = array();
        $updata['pnt'] = $bundlerow['buypnt'];
        $updata['buypntmoney'] = $payprice;
        $updata['vuser'] = $rsuser['id'];
        $updata['addtimes'] = tostamp();

        $csql = new \ciy\sql('ap_pnt_buy');
        if ($db->insert($csql, $updata) === false)
            return errjson('订单创建失败:' . $db->error);
        $orderid = $db->insert_id();
        $param = array();
        $param['description'] = '购买金币';
        $param['out_trade_no'] = 'PT' . date('ymd') . '9' . $orderid;
        $param['notify_url'] = 'https://expo.ciy.cn/ajax/amb/wxpay.pay_pnt';
        $param['amount'] = array();
        $param['amount']['total'] = (int)$payprice;
        $param['payer'] = array();
        $param['payer']['openid'] = $rsuser['wxminaid'];
        $wxcfg = gettoken($db, 1);
        $wxpay = new \ciy\wxfunc($wxcfg);
        $retpay = $wxpay->pay($param);
        if (is_string($retpay))
            return errjson($retpay);
        $retpay['orderid'] = $orderid;
        return succjson($retpay);
    }
    public static function json_buy_init() {
        global $db;
        $csql = new \ciy\sql('ap_pnt_bundle');
        $ret['buys'] = $db->get($csql);
        return succjson($ret);
    }
    public static function json_pass_change() {
        global $db;
        global $tokensalt;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $oldpass = $post->get('oldpass');
        $newpass = $post->get('newpass');
        $csql = new \ciy\sql('ap_user');
        $csql->where('id', $rsuser['id']);
        $rsuser = $db->getone($csql);
        if (!empty($rsuser['password']) && $rsuser['password'] != md5($oldpass . $tokensalt))
            return errjson('旧密码错误.');
        $updata = array();
        $updata['password'] = md5($newpass . $tokensalt);
        $csql = new \ciy\sql('ap_user');
        $csql->where('id', $rsuser['id']);
        if ($db->update($csql, $updata) === false)
            return errjson('密码更新失败:' . $db->error);
        return succjson();
    }
    public static function json_buy_userchk() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $id = $post->getint('orderid');
        for ($i = 0; $i < 10; $i++) {
            $csql = new \ciy\sql('ap_usr_buy_user');
            $csql->where('id', $id);
            $buyrow = $db->getone($csql);
            if (!is_array($buyrow))
                return errjson('订单不存在');
            if ($buyrow['paytimes'] > 0) {
                $csql = new \ciy\sql('ap_user');
                $csql->where('id', $rsuser['id']);
                $userrow = $db->getone($csql);
                $ret['accounttimes'] = $userrow['accounttimes'];
                $ret['userlevel'] = $userrow['userlevel'];
                return succjson($ret);
            }
            sleep(1);
        }
        return errjson('支付信息未同步，请下拉刷新');
    }

    public static function json_buy_usernow() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $id = $post->getint('id');
        $csql = new \ciy\sql('ap_usr_buy_bundle');
        $csql->where('id', $id);
        $bundlerow = $db->getone($csql);
        if (!is_array($bundlerow))
            return errjson('购买项目不存在');
        $money = $bundlerow['usermoney'];
        $updata = array();
        $updata['bundleid'] = $id;
        $updata['userlevel'] = $bundlerow['userlevel'];
        $updata['days'] = $bundlerow['days'];
        $updata['buydaymoney'] = $money;
        $updata['vuser'] = $rsuser['id'];
        $updata['addtimes'] = tostamp();
        $csql = new \ciy\sql('ap_usr_buy_user');
        if ($db->insert($csql, $updata) === false)
            return errjson('订单创建失败:' . $db->error);
        $orderid = $db->insert_id();
        $param = array();
        $param['description'] = '购买会员';
        $param['out_trade_no'] = 'UB' . date('ymd') . '9' . $orderid;
        $param['notify_url'] = 'https://ciyon.ciy.cn/ajax/amb/wxpay.pay_user';
        $param['amount'] = array();
        $param['amount']['total'] = (int)$money;
        $param['payer'] = array();
        $param['payer']['openid'] = $rsuser['wxminaid'];
        $wxcfg = gettoken($db, 1);
        $wxpay = new \ciy\wxfunc($wxcfg);
        $retpay = $wxpay->pay($param);
        if (is_string($retpay))
            return errjson($retpay);
        $retpay['orderid'] = $orderid;
        return succjson($retpay);
    }
    public static function json_usrbuy_get() {
        global $db;
        $csql = new \ciy\sql('ap_usr_buy_bundle');
        $ret['userbuys'] = $db->get($csql);
        $ret['recommend'] = 2;
        return succjson($ret);
    }
    public static function json_userinfo_change() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $name = $post->get('name');
        $icon = $post->get('icon');
        $mobile = $post->get('mobile');
        $wxno = $post->get('wxno');
        $email = $post->get('email');
        $updata = array();
        $updata['name'] = $name;
        $updata['icon'] = $icon;
        $updata['mobile'] = $mobile;
        $csql = new \ciy\sql('ap_user');
        $csql->where('id', $rsuser['id']);
        if ($db->update($csql, $updata) === false)
            return errjson('更新失败:' . $db->error);
        $updata = array();
        $updata['wxno'] = $wxno;
        $updata['email'] = $email;
        $csql = new \ciy\sql('ap_usr_ext');
        $csql->where('id', $rsuser['id']);
        $extrow = $db->getone($csql);
        if (is_array($extrow)) {
            if ($db->update($csql, $updata) === false)
                return errjson('ext更新失败:' . $db->error);
        } else {
            $updata['id'] = $rsuser['id'];
            if ($db->insert($csql, $updata) === false)
                return errjson('ext新增失败:' . $db->error);
        }
        return succjson();
    }
    public static function json_balance_get() {
        global $db;
        $rsuser = verifyfast();
        $csql = new \ciy\sql('ap_user');
        $csql->where('id', $rsuser['id']);
        $csql->column('myinvmoney,mycashmoney,mypnt');
        $ret['user'] = $db->getone($csql);
        return succjson($ret);
    }
    public static function json_cash_get() {
        global $db;
        $rsuser = verifyfast();
        $csql = new \ciy\sql('ap_user');
        $csql->where('id', $rsuser['id']);
        $csql->column('myinvmoney,mycashmoney,mypnt');
        $ret['user'] = $db->getone($csql);
        $csql = new \ciy\sql('ap_cash_out');
        $csql->where('vuser', $rsuser['id']);
        $csql->where('cashstatus', 20);
        $ret['cash'] = $db->get($csql);
        return succjson($ret);
    }
    public static function json_cashin_get() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $query = $post->get('query');
        $csql = new \ciy\sql('ap_cash_in');
        $csql->where('vuser', $rsuser['id']);
        $liid = objint($query, 'liid');
        if ($liid > 0)
            $csql->where('cashtype', $liid);
        $csql->order(objstr($query, 'order', 'id desc'));
        $pageno = $post->getint('pageno', 1);
        $pagecount = $post->getint('pagecount', 10);
        $csql->limit($pageno, $pagecount);
        $rows = $db->get($csql);
        $ret = array('pageno' => $pageno, 'pagecount' => $pagecount, 'list' => $rows);
        return succjson($ret);
    }
    public static function json_pntrecord_get() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $query = $post->get('query');
        $csql = new \ciy\sql('ap_pnt_record');
        $csql->where('vuser', $rsuser['id']);
        $csql->order(objstr($query, 'order', 'id desc'));
        $pageno = $post->getint('pageno', 1);
        $pagecount = $post->getint('pagecount', 10);
        $csql->limit($pageno, $pagecount);
        $rows = $db->get($csql);
        $ret = array('pageno' => $pageno, 'pagecount' => $pagecount, 'list' => $rows);
        return succjson($ret);
    }
    public static function json_pnt_up() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $id = $post->getint('id');
        $csql = new \ciy\sql('ap_pnt_track');
        $csql->where('id', $id);
        $trackrow = $db->getone($csql);
        if (!is_array($trackrow))
            return errjson('埋点不存在' . $id);
        $point = $trackrow['point'];
        $updata = array();
        $updata['vuser'] = $rsuser['id'];
        $updata['fromid'] = $id;
        $updata['name'] = $trackrow['name'];
        $updata['pnt'] = $point;
        $updata['addtimes'] = tostamp();
        $csql = new \ciy\sql('ap_pnt_record');
        $db->insert($csql, $updata);
        $db->execute('update ap_user set mypnt=mypnt+' . $point . ' where id=' . $rsuser['id']);
        $msg = '';
        if ($id == 1)
            $msg = '已签到 +' . $point . ' 积分';
        $ret['msg'] = $msg;
        return succjson($ret);
    }
}
