| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use app\common\library\CoinRate;
- use app\common\model\ReleaseLog;
- use app\common\model\Trade;
- use think\db;
- use think\Session;
- use Think\Config;
- /**
- * 首页接口
- */
- class Index extends Api
- {
- protected $noNeedLogin = ['*'];
- protected $noNeedRight = ['*'];
- /**
- * 首页
- *
- */
- public function index()
- {
- $yestday = strtotime('today') - 60 * 60 * 24;
- $list = db('trade')->where(['status' => 3, 'issell' => 2, 'create_time' => $yestday])->select();
- foreach ($list as $v) {
- $appointcnt = db('trade')->where(['goodsid' => $v['goodsid'], 'isout' => 0, 'status' => 0])->count();
- $goods = db('goods')->where(['id' => $v['goodsid']])->find();
- if ($goods['on_sale'] == 1 && $appointcnt == 0) {
- db('goods')->where(['id' => $v['goodsid']])->update(['istrade' => 0]);
- db('trade')->where(['id' => $v['id']])->update(['status' => 4]);
- } elseif ($goods['on_sale'] == 1 && $appointcnt > 0) {
- db('trade')->where(['id' => $v['id']])->update(['status' => 4]);
- } elseif ($goods['on_sale'] != 1) {
- db('trade')->where(['id' => $v['id']])->update(['status' => 4]);
- } else {
- continue;
- }
- }
- $list1 = db('goods')->where(['isnew' => 1, 'on_sale' => 0, 'create_user' => 1, 'utime' => [['gt', $yestday], ['lt', strtotime('today')]]])->select();
- foreach ($list1 as $v) {
- db('goods')->where(['id' => $v['id']])->update(['create_user' => 0, 'on_sale' => 1, 'istrade' => 0]);
- }
- $this->success('请求成功');
- }
- /**
- * 转售产品每日释放
- * @throws \think\exception\DbException
- * @throws db\exception\DataNotFoundException
- * @throws db\exception\ModelNotFoundException
- */
- public function release()
- {
- $key = input('key', '');
- if ($key != md5('hua')) {
- $this->error('非法请求');
- }
- if (date('H:i') >= '07:00') {
- $this->error('不在释放处理时间段内');
- }
- // 计算获取奖池和已释放总额
- $awards = Trade::getAwardTotal();
- $releaseUsdtTotal = Trade::where(['status' => 3, 'on_resale' => 1])->sum('release_usdt');
- $awardTotal = isset($awards['total_usdt']) ? $awards['total_usdt'] : 0;
- // 奖池是否还有钱,若已释放完
- if ($awardTotal <= $releaseUsdtTotal) {
- $this->error('抱歉,奖池金额已释放完');
- }
- // 处理转售释放
- $tradeConfig = \app\common\model\Config::getConfigByGroup('trade');
- $config['release_rate'] = isset($tradeConfig['release_rate']) ? $tradeConfig['release_rate']['value'] : 0;
- if ($config['release_rate'] <= 0) {
- $this->error('释放参数错误,请先配置');
- }
- $coinRate = CoinRate::getRate('CNY', 'USD');
- if ($coinRate <= 0) {
- $this->error('汇率参数错误,请稍后重试');
- }
- // 待释放产品
- $tradeList = \app\common\model\Trade::where(['on_resale' => 1])
- ->whereIn('status', [3, 4])
- ->whereRaw('release_usdt < release_total_usdt')
- ->where('release_time', '<', strtotime(date('Y-m-d')))
- ->field('id,orderNo,goodsid,userid,relevant_userid,status,endnums,pre_price,release_total,release_usdt,release_total_usdt,release_time')
- ->order('ctime', 'asc')
- ->limit(1000)
- ->select();
- if (empty($tradeList)) {
- $this->error('暂时没有可释放的转售产品');
- }
- $success = 0;
- $fail = 0;
- foreach ($tradeList as $k => $item) {
- $tradeUserId = isset($item['relevant_userid']) ? $item['relevant_userid'] : 0;
- $releaseTotalUsdt = isset($item['release_total_usdt']) ? $item['release_total_usdt'] : 0;
- $speedData = Trade::getSpeedTotalByUser($tradeUserId); // 加速值
- $speedUsdt = isset($speedData['speed_usdt']) ? $speedData['speed_usdt'] : 0;
- $speedTotal = isset($speedData['speed_total']) ? $speedData['speed_total'] : 0;
- // 计算释放额度
- $amount = isset($item['pre_price']) ? $item['pre_price'] : 0;
- $releaseUsdt = isset($item['release_usdt']) ? $item['release_usdt'] : 0;
- $releaseAmount = $amount ? round($amount * $config['release_rate'] / 100 + $speedTotal, 2) : 0;
- $releaseAmount = ($amount - $releaseAmount) < $releaseAmount ? max(0,$amount - $releaseAmount) : $releaseAmount;
- $totalUsdt = $releaseTotalUsdt ? $releaseTotalUsdt : round(CoinRate::transfer($amount, 'CNY', 'USD'), 2);
- $usdt = $totalUsdt>0? round($totalUsdt * $config['release_rate'] / 100 + $speedUsdt, 2) : 0;
- $usdt = ($totalUsdt - $releaseUsdt) < $usdt ? ($totalUsdt - $releaseUsdt) : $usdt;
- $releaseTotal = isset($item['release_total']) ? $item['release_total'] : 0;
- if ($tradeUserId <= 0 || $amount <= 0 || $usdt <= 0 || $totalUsdt <= $releaseUsdt) {
- $fail++;
- continue;
- }
- // 用户信息
- $user = \app\common\model\User::where(['id' => $tradeUserId, 'status' => 1])
- ->field('id,usdt,nickname,username')
- ->find();
- if (empty($user)) {
- $fail++;
- continue;
- }
- // 释放到余额
- Db::startTrans();
- $balance = $user->usdt;
- $user->usdt = $balance + $usdt;
- $user->updatetime = time();
- if (!$user->save()) {
- $fail++;
- Db::rollback();
- continue;
- }
- // 流水明细
- $changedata = [
- 'userid' => $tradeUserId,
- 'type' => 17,
- 'money' => $usdt,
- 'balance' => $balance,
- 'relevant_userid' => $tradeUserId,
- 'status' => 1,
- 'create_time' => time(),
- 'remark' => '转售产品给平台释放USDT到账',
- 'user_name' => $user->username ? $user->username : '系统',
- 'relevant_name' => $user->username ? $user->username : '系统',
- ];
- if (!Db::name('detailed_usdt')->insertGetId($changedata)) {
- $fail++;
- Db::rollback();
- continue;
- }
- // 释放明细
- $data = [
- 'userid' => $tradeUserId,
- 'type' => 1,
- 'money' => $releaseAmount,
- 'usdt_num' => $usdt,
- 'speed_usdt' => $speedUsdt,
- 'speed_total' => $speedTotal,
- 'balance' => $user->usdt,
- 'orderid' => $item['id'],
- 'status' => 1,
- 'create_time' => time(),
- 'update_time' => time(),
- 'remark' => '转售产品每日释放到账',
- ];
- if (!ReleaseLog::insertGetId($data)) {
- $fail++;
- Db::rollback();
- continue;
- }
- // 更新释放交易商品数据
- $tradeData = ['release_total' => $releaseTotal + $releaseAmount, 'release_usdt' => $releaseUsdt + $usdt, 'release_time' => time()];
- if ($releaseTotalUsdt <= 0) {
- $tradeUserId['release_total_usdt'] = $totalUsdt;
- }
- if ($releaseUsdt + $usdt >= $amount) {
- $tradeData['status'] = 4;
- }
- if (!Trade::where(['id' => $item['id']])->update($tradeData)) {
- $fail++;
- Db::rollback();
- continue;
- }
- $success++;
- Db::commit();
- }
- $this->success('请求处理成功', ['success' => $success, 'fail' => $fail,'total'=>count($tradeList)]);
- }
- /*预约上架*/
- function checkappoint()
- {
- $gids = db('trade')->where(['status' => 0])->group('goodsid')->column('goodsid');
- foreach ($gids as $vl) {
- $tradelist = db('trade')->where(['goodsid' => $vl, 'status' => 0])->select();
- if (count($tradelist) == 1) {
- continue;
- } else {
- $cnt = 0;
- foreach ($tradelist as $vs) {
- $cnt++;
- if ($cnt == 1) {
- continue;
- } else {
- #取消
- db('trade')->where(['id' => $vs['id']])->update(['status' => -2, 'iscancel' => 1, 'confirm_time' => time()]);
- }
- }
- }
- }
- $list = db('goods_cats')->where(['status' => 1])->select();
- foreach ($list as $v) {
- if (time() >= strtotime(date("Y-m-d {$v['start']}")) && time() <= strtotime(date("Y-m-d {$v['end']}"))) {
- db('trade')->where(['catid' => $v['id'], 'appoint_time' => ['lt', strtotime('today')], 'status' => 0])->update(['status' => 1, 'ctime' => time(), 'create_time' => strtotime('today')]);
- }
- }
- $this->success('操作完成');
- }
- function getnowtime()
- {
- $id = input('id', 0);
- $time = date('Y年m月d日H:i:s', time());
- $studio = db('studio')->where(['id' => $id])->find();
- $startTime = isset($studio['start']) ? $studio['start'] : 0;
- $endTime = isset($studio['end']) ? $studio['end'] : 0;
- $startTime = $startTime ? strtotime(date('Y-m-d') . ' ' . $startTime) : 0;
- $endTime = $endTime ? strtotime(date('Y-m-d') . ' ' . $endTime) : 0;
- $expired = $startTime > time() ? $startTime - time() : 0;
- if ($endTime <= time()) {
- $startTime = strtotime(date('Y-m-d', strtotime(date('Y-m-d')) + 86400) . ' ' . $startTime);
- $expired = $startTime > time() ? $startTime - time() : 0;
- }
- if (date('week') == 1) {
- $weeks = '一';
- } elseif (date('week') == 2) {
- $weeks = '二';
- } elseif (date('week') == 3) {
- $weeks = '三';
- } elseif (date('week') == 4) {
- $weeks = '四';
- } elseif (date('week') == 5) {
- $weeks = '五';
- } elseif (date('week') == 6) {
- $weeks = '六';
- } else {
- $weeks = '七';
- }
- $this->success($time . ' 星期' . $weeks, ['time' => $endTime, 'expired' => $expired]);
- }
- function getusername()
- {
- $names = input('username');
- if (empty($names)) {
- $this->error('请输入账户');
- }
- $user = db('user')->where(['username|mobile' => $names, 'status' => 1])->find();
- if ($user) {
- $this->success('', ['name' => $user['nickname']]);
- } else {
- $this->error('用户不存在' . $names);
- }
- }
- /*匹配*/
- function checktrade()
- {
- $bcf = db('bonus_config')->where('id', 4)->find();
- $list = db('trade')->where(['status' => ['in', '1,2']])->select();
- foreach ($list as $v) {
- if ($v['status'] == 2) {
- $JC = time() - ($v['pay_time'] + 60 * (int)$bcf['cap']);
- if ($JC > 0) {//自动确认
- db('trade')->where(['id' => $v['id']])->update(['status' => -1]);
- }
- }
- if ($v['status'] == 1) {
- $JC1 = time() - ($v['ctime'] + 60 * $bcf['value']);
- if ($JC1 > 0) {//超时未支付
- db()->startTrans();
- $res1 = db('trade')->where(['id' => $v['id']])->update(['status' => -2, 'iscancel' => 1, 'confirm_time' => time()]);
- $res2 = db('goods')->where(['id' => $v['goodsid']])->update(['istrade' => 0]);
- if ($res1 && $res2) {
- db()->commit();
- } else {
- db()->rollback();
- }
- }
- }
- }
- echo 'success';
- }
- function getnoticelist()
- {
- $map['status'] = 1;
- $map['catid'] = 1;
- $p = $this->request->request('p');
- if (empty($p)) {
- $this->error('缺少参数页码');
- }
- $pagesize = 10;
- $total = db('article')->where($map)->count();
- $totalPage = ceil($total / $pagesize);
- if ($p > $totalPage && $total > 0) {
- $this->error('页码有误');
- }
- $list = db('article')->where($map)->page($p, $pagesize)->order('id desc')->select();
- foreach ($list as &$v) {
- $v['ctime'] = date('Y-m-d H:i:s', $v['ctime']);
- $v['abs'] = mbsubstr($v['content'], 0, 30);
- $v['prc'] = empty($v['prc']) ? 'http://' . $_SERVER['HTTP_HOST'] . '/assets/shop/img/gg.png' : $v['prc'];
- }
- if (empty($list)) {
- $data['list'] = [];
- } else {
- $data['list'] = $list;
- }
- $data['total'] = $totalPage;
- $this->success('', $data);
- }
- function fdbonus()
- {
- db()->execute('call CashBonus()');
- $this->success('执行完毕');
- }
- /*发送留言 */
- function tosendmsg()
- {
- $uid = input('uid');
- $content = input('content');
- if (empty($content)) {
- $this->error(__("请输入留言"));
- }
- $param['content'] = $content;
- $param['from_uid'] = 0;
- $param['to_uid'] = $uid;
- $param['ctime'] = time();
- $param['userid'] = $uid;
- $ids = db('user_message')->insertGetId($param);
- if ($ids) {
- goeasy_sms($content);
- $this->success(__("留言成功"));
- } else {
- $this->error(__("留言失败"));
- }
- }
- function checkuserappoint()
- {
- $name = $this->request->request('name');
- $sid = $this->request->request('sid');
- $user = db('user')->where(['username|mobile' => $name])->find();
- if ($user) {
- $isappoint = db('studio_user')->where(['userid' => $user['id'], 'sid' => $sid])->find();
- if ($isappoint) {
- $this->success('', ['id' => $user['id'], 'username' => $user['username'], 'isclose' => 0]);
- } else {
- $this->success('', ['id' => $user['id'], 'username' => $user['username'], 'isclose' => 1]);
- }
- } else {
- $this->error('您输入的会员不存在');
- }
- }
- function setuserappoint()
- {
- $userid = $this->request->request('userid');
- $sid = $this->request->request('sid');
- $isappoint = $this->request->request('isappoint');
- $info = db('studio_user')->where(['userid' => $userid, 'sid' => $sid])->find();
- if ($info) {
- if ($isappoint == 1) {
- db('studio_user')->where(['id' => $info['id']])->delete();
- $this->success('设置成功');
- } else {
- $this->error('您并未做任何修改');
- }
- } else {
- if ($isappoint == 1) {
- $this->error('您并未做任何修改');
- } else {
- db('studio_user')->insertGetId(['userid' => $userid, 'sid' => $sid, 'ctime' => time(), 'status' => 1]);
- $this->success('设置成功');
- }
- }
- }
- /* 获取节点 */
- function getClass()
- {
- $rootid = input('rootid');
- $uid = input('uid');
- $where['refereeid'] = $uid;
- if ($uid == 0) {
- $myself = db('user')->where(['id' => $rootid])->find();
- $value['uid'] = $myself['id'];
- $value['status'] = $myself['status'];
- $value['usernumber'] = $myself['username'] . "[姓名:" . $myself['nickname'] . "]";
- $count = db('user')->where('refereeid', $value['uid'])->count();
- $value['count'] = $count;
- if ($count != 0) {
- $value['isParent'] = true;
- } else {
- $value['isParent'] = false;
- }
- $list[] = $value;
- } else {
- $info = db('user')->where($where)->select();
- foreach ($info as $value) {
- $count = db('user')->where('refereeid', $value['id'])->count();
- $value['count'] = $count;
- if ($count != 0) {
- $value['isParent'] = true;
- } else {
- $value['isParent'] = false;
- }
- $value['uid'] = $value['id'];
- $value['usernumber'] = $value['username'] . "[姓名:" . $value['nickname'] . "]";
- $list[] = $value;
- }
- }
- return $list;
- }
- /* 奖金设置 */
- function bonus_set()
- {
- $param = $this->request->get();
- $data_arr = explode('-', $param['id']);
- $column = $data_arr['0'];
- $key = $data_arr['1'];
- //模型
- if (count($data_arr) < 3) {
- $data_model = "bonus_config";
- } else {
- $data_model = $data_arr['2'];
- }
- $data[$column] = $param['val'];
- $info = db($data_model)->where('id', $key)->find();
- $res = db($data_model)->where('id', $key)->update($data);
- if ($res) {
- $this->success("修改完成");
- } else {
- $this->error("修改完成");
- }
- }
- /*切换用户名 */
- function switchname()
- {
- $randStr = str_shuffle('1234567890');
- $randZm = str_shuffle('abcdefghijklmnopqrstuvwxyz');
- $username = substr($randZm, 0, 2) . substr($randStr, 0, 6);//账号为7位随机数
- $this->success($username);
- }
- function getuserid()
- {
- $param = $this->request->get();
- $info = db('user')->where(['username|mobile' => $param['username']])->find();
- if ($info) {
- $this->success($info['id']);
- } else {
- $this->error('用户不存在');
- }
- }
- function getstuid()
- {
- $param = $this->request->get();
- $info = db('studio')->where(['title' => $param['title']])->find();
- if ($info) {
- $this->success($info['id']);
- } else {
- $this->error('商家不存在');
- }
- }
- /* 检测用户名 */
- function checkusername()
- {
- $param = $this->request->get();
- $info = db('user')->where('username', $param['username'])->find();
- if ($info) {
- $this->error("用户名已注册,请更换");
- } else {
- $this->success("用户名可以使用");
- }
- }
- /* 获取昵称 */
- function getnickname()
- {
- $param = $this->request->get();
- $info = db('user')->where(['username|mobile' => $param['username']])->find();
- if ($info) {
- $this->success($info['nickname']);
- } else {
- $this->error('用户不存在');
- }
- }
- /* 获取位置节点pos */
- function getpos()
- {
- $param = $this->request->get();
- $info = db('user')->where(['username' => $param['username'], 'status' => 1])->find();
- if (!$info) {
- $this->error("会员不存在或不可用");
- }
- $str = "<option value=''>请选择</option>";
- $pos = config('pos');
- $list = db('user_parent')->where('pid', $info['id'])->select();
- if ($list) {
- $pos_arr = [];
- foreach ($list as $k => $v) {
- $pos_arr[$v['position']] = $v;
- }
- foreach ($pos as $key => $v) {
- if (empty($pos_arr[$key]['userid'])) {
- $str .= "<option value='{$key}'>" . $v . '</option>';
- } else {
- $str .= "<option value='{$key}' disabled>" . $v . '【' . $pos_arr[$key]['username'] . '】</option>';
- }
- }
- } else {
- foreach ($pos as $key => $v) {
- $str .= "<option value='{$key}'>" . $v . '</option>';
- }
- }
- $this->success($str);
- }
- }
|