Index.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\CoinRate;
  5. use app\common\model\ReleaseLog;
  6. use app\common\model\Trade;
  7. use think\db;
  8. use think\Session;
  9. use Think\Config;
  10. /**
  11. * 首页接口
  12. */
  13. class Index extends Api
  14. {
  15. protected $noNeedLogin = ['*'];
  16. protected $noNeedRight = ['*'];
  17. /**
  18. * 首页
  19. *
  20. */
  21. public function index()
  22. {
  23. $yestday = strtotime('today') - 60 * 60 * 24;
  24. $list = db('trade')->where(['status' => 3, 'issell' => 2, 'create_time' => $yestday])->select();
  25. foreach ($list as $v) {
  26. $appointcnt = db('trade')->where(['goodsid' => $v['goodsid'], 'isout' => 0, 'status' => 0])->count();
  27. $goods = db('goods')->where(['id' => $v['goodsid']])->find();
  28. if ($goods['on_sale'] == 1 && $appointcnt == 0) {
  29. db('goods')->where(['id' => $v['goodsid']])->update(['istrade' => 0]);
  30. db('trade')->where(['id' => $v['id']])->update(['status' => 4]);
  31. } elseif ($goods['on_sale'] == 1 && $appointcnt > 0) {
  32. db('trade')->where(['id' => $v['id']])->update(['status' => 4]);
  33. } elseif ($goods['on_sale'] != 1) {
  34. db('trade')->where(['id' => $v['id']])->update(['status' => 4]);
  35. } else {
  36. continue;
  37. }
  38. }
  39. $list1 = db('goods')->where(['isnew' => 1, 'on_sale' => 0, 'create_user' => 1, 'utime' => [['gt', $yestday], ['lt', strtotime('today')]]])->select();
  40. foreach ($list1 as $v) {
  41. db('goods')->where(['id' => $v['id']])->update(['create_user' => 0, 'on_sale' => 1, 'istrade' => 0]);
  42. }
  43. $this->success('请求成功');
  44. }
  45. /**
  46. * 转售产品每日释放
  47. * @throws \think\exception\DbException
  48. * @throws db\exception\DataNotFoundException
  49. * @throws db\exception\ModelNotFoundException
  50. */
  51. public function release()
  52. {
  53. $key = input('key', '');
  54. if ($key != md5('hua')) {
  55. $this->error('非法请求');
  56. }
  57. if (date('H:i') >= '07:00') {
  58. $this->error('不在释放处理时间段内');
  59. }
  60. // 计算获取奖池和已释放总额
  61. $awards = Trade::getAwardTotal();
  62. $releaseUsdtTotal = Trade::where(['status' => 3, 'on_resale' => 1])->sum('release_usdt');
  63. $awardTotal = isset($awards['total_usdt']) ? $awards['total_usdt'] : 0;
  64. // 奖池是否还有钱,若已释放完
  65. if ($awardTotal <= $releaseUsdtTotal) {
  66. $this->error('抱歉,奖池金额已释放完');
  67. }
  68. // 处理转售释放
  69. $tradeConfig = \app\common\model\Config::getConfigByGroup('trade');
  70. $config['release_rate'] = isset($tradeConfig['release_rate']) ? $tradeConfig['release_rate']['value'] : 0;
  71. if ($config['release_rate'] <= 0) {
  72. $this->error('释放参数错误,请先配置');
  73. }
  74. $coinRate = CoinRate::getRate('CNY', 'USD');
  75. if ($coinRate <= 0) {
  76. $this->error('汇率参数错误,请稍后重试');
  77. }
  78. // 待释放产品
  79. $tradeList = \app\common\model\Trade::where(['on_resale' => 1])
  80. ->whereIn('status', [3, 4])
  81. ->whereRaw('release_usdt < release_total_usdt')
  82. ->where('release_time', '<', strtotime(date('Y-m-d')))
  83. ->field('id,orderNo,goodsid,userid,relevant_userid,status,endnums,pre_price,release_total,release_usdt,release_total_usdt,release_time')
  84. ->order('ctime', 'asc')
  85. ->limit(1000)
  86. ->select();
  87. if (empty($tradeList)) {
  88. $this->error('暂时没有可释放的转售产品');
  89. }
  90. $success = 0;
  91. $fail = 0;
  92. foreach ($tradeList as $k => $item) {
  93. $tradeUserId = isset($item['relevant_userid']) ? $item['relevant_userid'] : 0;
  94. $releaseTotalUsdt = isset($item['release_total_usdt']) ? $item['release_total_usdt'] : 0;
  95. $speedData = Trade::getSpeedTotalByUser($tradeUserId); // 加速值
  96. $speedUsdt = isset($speedData['speed_usdt']) ? $speedData['speed_usdt'] : 0;
  97. $speedTotal = isset($speedData['speed_total']) ? $speedData['speed_total'] : 0;
  98. // 计算释放额度
  99. $amount = isset($item['pre_price']) ? $item['pre_price'] : 0;
  100. $releaseUsdt = isset($item['release_usdt']) ? $item['release_usdt'] : 0;
  101. $releaseAmount = $amount ? round($amount * $config['release_rate'] / 100 + $speedTotal, 2) : 0;
  102. $releaseAmount = ($amount - $releaseAmount) < $releaseAmount ? max(0,$amount - $releaseAmount) : $releaseAmount;
  103. $totalUsdt = $releaseTotalUsdt ? $releaseTotalUsdt : round(CoinRate::transfer($amount, 'CNY', 'USD'), 2);
  104. $usdt = $totalUsdt>0? round($totalUsdt * $config['release_rate'] / 100 + $speedUsdt, 2) : 0;
  105. $usdt = ($totalUsdt - $releaseUsdt) < $usdt ? ($totalUsdt - $releaseUsdt) : $usdt;
  106. $releaseTotal = isset($item['release_total']) ? $item['release_total'] : 0;
  107. if ($tradeUserId <= 0 || $amount <= 0 || $usdt <= 0 || $totalUsdt <= $releaseUsdt) {
  108. $fail++;
  109. continue;
  110. }
  111. // 用户信息
  112. $user = \app\common\model\User::where(['id' => $tradeUserId, 'status' => 1])
  113. ->field('id,usdt,nickname,username')
  114. ->find();
  115. if (empty($user)) {
  116. $fail++;
  117. continue;
  118. }
  119. // 释放到余额
  120. Db::startTrans();
  121. $balance = $user->usdt;
  122. $user->usdt = $balance + $usdt;
  123. $user->updatetime = time();
  124. if (!$user->save()) {
  125. $fail++;
  126. Db::rollback();
  127. continue;
  128. }
  129. // 流水明细
  130. $changedata = [
  131. 'userid' => $tradeUserId,
  132. 'type' => 17,
  133. 'money' => $usdt,
  134. 'balance' => $balance,
  135. 'relevant_userid' => $tradeUserId,
  136. 'status' => 1,
  137. 'create_time' => time(),
  138. 'remark' => '转售产品给平台释放USDT到账',
  139. 'user_name' => $user->username ? $user->username : '系统',
  140. 'relevant_name' => $user->username ? $user->username : '系统',
  141. ];
  142. if (!Db::name('detailed_usdt')->insertGetId($changedata)) {
  143. $fail++;
  144. Db::rollback();
  145. continue;
  146. }
  147. // 释放明细
  148. $data = [
  149. 'userid' => $tradeUserId,
  150. 'type' => 1,
  151. 'money' => $releaseAmount,
  152. 'usdt_num' => $usdt,
  153. 'speed_usdt' => $speedUsdt,
  154. 'speed_total' => $speedTotal,
  155. 'balance' => $user->usdt,
  156. 'orderid' => $item['id'],
  157. 'status' => 1,
  158. 'create_time' => time(),
  159. 'update_time' => time(),
  160. 'remark' => '转售产品每日释放到账',
  161. ];
  162. if (!ReleaseLog::insertGetId($data)) {
  163. $fail++;
  164. Db::rollback();
  165. continue;
  166. }
  167. // 更新释放交易商品数据
  168. $tradeData = ['release_total' => $releaseTotal + $releaseAmount, 'release_usdt' => $releaseUsdt + $usdt, 'release_time' => time()];
  169. if ($releaseTotalUsdt <= 0) {
  170. $tradeUserId['release_total_usdt'] = $totalUsdt;
  171. }
  172. if ($releaseUsdt + $usdt >= $amount) {
  173. $tradeData['status'] = 4;
  174. }
  175. if (!Trade::where(['id' => $item['id']])->update($tradeData)) {
  176. $fail++;
  177. Db::rollback();
  178. continue;
  179. }
  180. $success++;
  181. Db::commit();
  182. }
  183. $this->success('请求处理成功', ['success' => $success, 'fail' => $fail,'total'=>count($tradeList)]);
  184. }
  185. /*预约上架*/
  186. function checkappoint()
  187. {
  188. $gids = db('trade')->where(['status' => 0])->group('goodsid')->column('goodsid');
  189. foreach ($gids as $vl) {
  190. $tradelist = db('trade')->where(['goodsid' => $vl, 'status' => 0])->select();
  191. if (count($tradelist) == 1) {
  192. continue;
  193. } else {
  194. $cnt = 0;
  195. foreach ($tradelist as $vs) {
  196. $cnt++;
  197. if ($cnt == 1) {
  198. continue;
  199. } else {
  200. #取消
  201. db('trade')->where(['id' => $vs['id']])->update(['status' => -2, 'iscancel' => 1, 'confirm_time' => time()]);
  202. }
  203. }
  204. }
  205. }
  206. $list = db('goods_cats')->where(['status' => 1])->select();
  207. foreach ($list as $v) {
  208. if (time() >= strtotime(date("Y-m-d {$v['start']}")) && time() <= strtotime(date("Y-m-d {$v['end']}"))) {
  209. db('trade')->where(['catid' => $v['id'], 'appoint_time' => ['lt', strtotime('today')], 'status' => 0])->update(['status' => 1, 'ctime' => time(), 'create_time' => strtotime('today')]);
  210. }
  211. }
  212. $this->success('操作完成');
  213. }
  214. function getnowtime()
  215. {
  216. $id = input('id', 0);
  217. $time = date('Y年m月d日H:i:s', time());
  218. $studio = db('studio')->where(['id' => $id])->find();
  219. $startTime = isset($studio['start']) ? $studio['start'] : 0;
  220. $endTime = isset($studio['end']) ? $studio['end'] : 0;
  221. $startTime = $startTime ? strtotime(date('Y-m-d') . ' ' . $startTime) : 0;
  222. $endTime = $endTime ? strtotime(date('Y-m-d') . ' ' . $endTime) : 0;
  223. $expired = $startTime > time() ? $startTime - time() : 0;
  224. if ($endTime <= time()) {
  225. $startTime = strtotime(date('Y-m-d', strtotime(date('Y-m-d')) + 86400) . ' ' . $startTime);
  226. $expired = $startTime > time() ? $startTime - time() : 0;
  227. }
  228. if (date('week') == 1) {
  229. $weeks = '一';
  230. } elseif (date('week') == 2) {
  231. $weeks = '二';
  232. } elseif (date('week') == 3) {
  233. $weeks = '三';
  234. } elseif (date('week') == 4) {
  235. $weeks = '四';
  236. } elseif (date('week') == 5) {
  237. $weeks = '五';
  238. } elseif (date('week') == 6) {
  239. $weeks = '六';
  240. } else {
  241. $weeks = '七';
  242. }
  243. $this->success($time . ' 星期' . $weeks, ['time' => $endTime, 'expired' => $expired]);
  244. }
  245. function getusername()
  246. {
  247. $names = input('username');
  248. if (empty($names)) {
  249. $this->error('请输入账户');
  250. }
  251. $user = db('user')->where(['username|mobile' => $names, 'status' => 1])->find();
  252. if ($user) {
  253. $this->success('', ['name' => $user['nickname']]);
  254. } else {
  255. $this->error('用户不存在' . $names);
  256. }
  257. }
  258. /*匹配*/
  259. function checktrade()
  260. {
  261. $bcf = db('bonus_config')->where('id', 4)->find();
  262. $list = db('trade')->where(['status' => ['in', '1,2']])->select();
  263. foreach ($list as $v) {
  264. if ($v['status'] == 2) {
  265. $JC = time() - ($v['pay_time'] + 60 * (int)$bcf['cap']);
  266. if ($JC > 0) {//自动确认
  267. db('trade')->where(['id' => $v['id']])->update(['status' => -1]);
  268. }
  269. }
  270. if ($v['status'] == 1) {
  271. $JC1 = time() - ($v['ctime'] + 60 * $bcf['value']);
  272. if ($JC1 > 0) {//超时未支付
  273. db()->startTrans();
  274. $res1 = db('trade')->where(['id' => $v['id']])->update(['status' => -2, 'iscancel' => 1, 'confirm_time' => time()]);
  275. $res2 = db('goods')->where(['id' => $v['goodsid']])->update(['istrade' => 0]);
  276. if ($res1 && $res2) {
  277. db()->commit();
  278. } else {
  279. db()->rollback();
  280. }
  281. }
  282. }
  283. }
  284. echo 'success';
  285. }
  286. function getnoticelist()
  287. {
  288. $map['status'] = 1;
  289. $map['catid'] = 1;
  290. $p = $this->request->request('p');
  291. if (empty($p)) {
  292. $this->error('缺少参数页码');
  293. }
  294. $pagesize = 10;
  295. $total = db('article')->where($map)->count();
  296. $totalPage = ceil($total / $pagesize);
  297. if ($p > $totalPage && $total > 0) {
  298. $this->error('页码有误');
  299. }
  300. $list = db('article')->where($map)->page($p, $pagesize)->order('id desc')->select();
  301. foreach ($list as &$v) {
  302. $v['ctime'] = date('Y-m-d H:i:s', $v['ctime']);
  303. $v['abs'] = mbsubstr($v['content'], 0, 30);
  304. $v['prc'] = empty($v['prc']) ? 'http://' . $_SERVER['HTTP_HOST'] . '/assets/shop/img/gg.png' : $v['prc'];
  305. }
  306. if (empty($list)) {
  307. $data['list'] = [];
  308. } else {
  309. $data['list'] = $list;
  310. }
  311. $data['total'] = $totalPage;
  312. $this->success('', $data);
  313. }
  314. function fdbonus()
  315. {
  316. db()->execute('call CashBonus()');
  317. $this->success('执行完毕');
  318. }
  319. /*发送留言 */
  320. function tosendmsg()
  321. {
  322. $uid = input('uid');
  323. $content = input('content');
  324. if (empty($content)) {
  325. $this->error(__("请输入留言"));
  326. }
  327. $param['content'] = $content;
  328. $param['from_uid'] = 0;
  329. $param['to_uid'] = $uid;
  330. $param['ctime'] = time();
  331. $param['userid'] = $uid;
  332. $ids = db('user_message')->insertGetId($param);
  333. if ($ids) {
  334. goeasy_sms($content);
  335. $this->success(__("留言成功"));
  336. } else {
  337. $this->error(__("留言失败"));
  338. }
  339. }
  340. function checkuserappoint()
  341. {
  342. $name = $this->request->request('name');
  343. $sid = $this->request->request('sid');
  344. $user = db('user')->where(['username|mobile' => $name])->find();
  345. if ($user) {
  346. $isappoint = db('studio_user')->where(['userid' => $user['id'], 'sid' => $sid])->find();
  347. if ($isappoint) {
  348. $this->success('', ['id' => $user['id'], 'username' => $user['username'], 'isclose' => 0]);
  349. } else {
  350. $this->success('', ['id' => $user['id'], 'username' => $user['username'], 'isclose' => 1]);
  351. }
  352. } else {
  353. $this->error('您输入的会员不存在');
  354. }
  355. }
  356. function setuserappoint()
  357. {
  358. $userid = $this->request->request('userid');
  359. $sid = $this->request->request('sid');
  360. $isappoint = $this->request->request('isappoint');
  361. $info = db('studio_user')->where(['userid' => $userid, 'sid' => $sid])->find();
  362. if ($info) {
  363. if ($isappoint == 1) {
  364. db('studio_user')->where(['id' => $info['id']])->delete();
  365. $this->success('设置成功');
  366. } else {
  367. $this->error('您并未做任何修改');
  368. }
  369. } else {
  370. if ($isappoint == 1) {
  371. $this->error('您并未做任何修改');
  372. } else {
  373. db('studio_user')->insertGetId(['userid' => $userid, 'sid' => $sid, 'ctime' => time(), 'status' => 1]);
  374. $this->success('设置成功');
  375. }
  376. }
  377. }
  378. /* 获取节点 */
  379. function getClass()
  380. {
  381. $rootid = input('rootid');
  382. $uid = input('uid');
  383. $where['refereeid'] = $uid;
  384. if ($uid == 0) {
  385. $myself = db('user')->where(['id' => $rootid])->find();
  386. $value['uid'] = $myself['id'];
  387. $value['status'] = $myself['status'];
  388. $value['usernumber'] = $myself['username'] . "[姓名:" . $myself['nickname'] . "]";
  389. $count = db('user')->where('refereeid', $value['uid'])->count();
  390. $value['count'] = $count;
  391. if ($count != 0) {
  392. $value['isParent'] = true;
  393. } else {
  394. $value['isParent'] = false;
  395. }
  396. $list[] = $value;
  397. } else {
  398. $info = db('user')->where($where)->select();
  399. foreach ($info as $value) {
  400. $count = db('user')->where('refereeid', $value['id'])->count();
  401. $value['count'] = $count;
  402. if ($count != 0) {
  403. $value['isParent'] = true;
  404. } else {
  405. $value['isParent'] = false;
  406. }
  407. $value['uid'] = $value['id'];
  408. $value['usernumber'] = $value['username'] . "[姓名:" . $value['nickname'] . "]";
  409. $list[] = $value;
  410. }
  411. }
  412. return $list;
  413. }
  414. /* 奖金设置 */
  415. function bonus_set()
  416. {
  417. $param = $this->request->get();
  418. $data_arr = explode('-', $param['id']);
  419. $column = $data_arr['0'];
  420. $key = $data_arr['1'];
  421. //模型
  422. if (count($data_arr) < 3) {
  423. $data_model = "bonus_config";
  424. } else {
  425. $data_model = $data_arr['2'];
  426. }
  427. $data[$column] = $param['val'];
  428. $info = db($data_model)->where('id', $key)->find();
  429. $res = db($data_model)->where('id', $key)->update($data);
  430. if ($res) {
  431. $this->success("修改完成");
  432. } else {
  433. $this->error("修改完成");
  434. }
  435. }
  436. /*切换用户名 */
  437. function switchname()
  438. {
  439. $randStr = str_shuffle('1234567890');
  440. $randZm = str_shuffle('abcdefghijklmnopqrstuvwxyz');
  441. $username = substr($randZm, 0, 2) . substr($randStr, 0, 6);//账号为7位随机数
  442. $this->success($username);
  443. }
  444. function getuserid()
  445. {
  446. $param = $this->request->get();
  447. $info = db('user')->where(['username|mobile' => $param['username']])->find();
  448. if ($info) {
  449. $this->success($info['id']);
  450. } else {
  451. $this->error('用户不存在');
  452. }
  453. }
  454. function getstuid()
  455. {
  456. $param = $this->request->get();
  457. $info = db('studio')->where(['title' => $param['title']])->find();
  458. if ($info) {
  459. $this->success($info['id']);
  460. } else {
  461. $this->error('商家不存在');
  462. }
  463. }
  464. /* 检测用户名 */
  465. function checkusername()
  466. {
  467. $param = $this->request->get();
  468. $info = db('user')->where('username', $param['username'])->find();
  469. if ($info) {
  470. $this->error("用户名已注册,请更换");
  471. } else {
  472. $this->success("用户名可以使用");
  473. }
  474. }
  475. /* 获取昵称 */
  476. function getnickname()
  477. {
  478. $param = $this->request->get();
  479. $info = db('user')->where(['username|mobile' => $param['username']])->find();
  480. if ($info) {
  481. $this->success($info['nickname']);
  482. } else {
  483. $this->error('用户不存在');
  484. }
  485. }
  486. /* 获取位置节点pos */
  487. function getpos()
  488. {
  489. $param = $this->request->get();
  490. $info = db('user')->where(['username' => $param['username'], 'status' => 1])->find();
  491. if (!$info) {
  492. $this->error("会员不存在或不可用");
  493. }
  494. $str = "<option value=''>请选择</option>";
  495. $pos = config('pos');
  496. $list = db('user_parent')->where('pid', $info['id'])->select();
  497. if ($list) {
  498. $pos_arr = [];
  499. foreach ($list as $k => $v) {
  500. $pos_arr[$v['position']] = $v;
  501. }
  502. foreach ($pos as $key => $v) {
  503. if (empty($pos_arr[$key]['userid'])) {
  504. $str .= "<option value='{$key}'>" . $v . '</option>';
  505. } else {
  506. $str .= "<option value='{$key}' disabled>" . $v . '【' . $pos_arr[$key]['username'] . '】</option>';
  507. }
  508. }
  509. } else {
  510. foreach ($pos as $key => $v) {
  511. $str .= "<option value='{$key}'>" . $v . '</option>';
  512. }
  513. }
  514. $this->success($str);
  515. }
  516. }