123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- <?php
- namespace app\api\controller\trade;
- use app\models\trade\CashTradeOrder;
- use app\models\user\User;
- use app\models\user\UserMoney;
- use app\models\user\UserMoneyOrder;
- use app\Request;
- use crmeb\basic\BaseModel;
- use crmeb\services\CacheService;
- use crmeb\services\UtilService;
- use Psr\SimpleCache\InvalidArgumentException;
- use think\db\exception\DataNotFoundException;
- use think\db\exception\DbException;
- use think\db\exception\ModelNotFoundException;
- class TradeController
- {
- /**
- * @param Request $request
- * @return string
- * @throws InvalidArgumentException
- */
- public function getNewPayCode(Request $request)
- {
- $uid = $request->uid();
- $code = md5(time() . rand(0, 999999));
- while (CacheService::redisHandler()->get("pay_code_{$code}", '')) {
- $code = md5($code);
- }
- CacheService::redisHandler()->set("pay_code_{$code}", $uid, 60);
- return app('json')->success('ok', ['code' => $code, 'exceed_time' => date('Y-m-d H:i:s', time() + 60)]);
- }
- public function checkTradePsw(Request $request)
- {
- $user = $request->user();
- $psw = $request->post('trade_psw', '');
- if (md5(md5($psw)) != $user['trade_pwd']) {
- return app('json')->fail();
- }
- return app('json')->success();
- }
- /**
- * @param Request $request
- * @return mixed
- * @throws DataNotFoundException
- * @throws DbException
- * @throws InvalidArgumentException
- * @throws ModelNotFoundException
- */
- public function autoPay(Request $request)
- {
- $uid = $request->uid();
- list($type, $num, $code) = UtilService::postMore(
- [
- ['type', 'USDT_ERC20', '', '', ['not_empty_check', function ($item) {
- $moneys = sys_data('money_type');
- $new_money = [];
- foreach ($moneys as $v) {
- if ($v['can_trade'] == 1) {
- $new_money[] = $v['code'];
- }
- }
- return in_array($item, $new_money);
- }], ['请选择交易的币种', '请选择支持交易的币种']],
- ['num', 0],
- ['code', '']
- ], $request, true);
- if (!$num || !$code) {
- return app('json')->fail('参数不足');
- }
- $pay_uid = CacheService::redisHandler()->get("pay_code_{$code}", '');
- if (!$pay_uid) {
- return app('json')->fail('付款码已过期');
- }
- $res = UserMoney::tradeMoney($pay_uid, $uid, $type, $num);
- if ($res) {
- CacheService::redisHandler()->delete("pay_code_{$code}");
- CacheService::redisHandler()->set("pay_result_{$code}", 'SUCCESS');
- return app('json')->success('支付成功');
- } else {
- CacheService::redisHandler()->delete("pay_code_{$code}");
- CacheService::redisHandler()->set("pay_result_{$code}", UserMoney::getErrorInfo('FAIL'));
- return app('json')->fail('支付失败:' . UserMoney::getErrorInfo('支付错误'));
- }
- }
- /**
- * @param Request $request
- * @return mixed
- * @throws InvalidArgumentException
- */
- public function payResult(Request $request)
- {
- $code = $request->get('code', '');
- $result = CacheService::redisHandler()->get("pay_result_{$code}", '');
- if ($result) return app('json')->success($result);
- else return app('json')->success('NOT_USE');
- }
- /**
- * @param Request $request
- * @return mixed
- * @throws DataNotFoundException
- * @throws DbException
- * @throws ModelNotFoundException
- */
- public function goPay(Request $request)
- {
- $user = $request->user();
- $uid = $user['uid'];
- list($type, $num, $to_user_account, $to_uid, , $captcha) = UtilService::postMore(
- [
- ['type', 'USDT_ERC20', '', '', ['not_empty_check', function ($item) {
- $moneys = sys_data('money_type');
- $new_money = [];
- foreach ($moneys as $v) {
- if ($v['can_trade'] == 1) {
- $new_money[] = $v['code'];
- }
- }
- return in_array($item, $new_money);
- }], ['请选择交易的币种', '请选择支持交易的币种']],
- ['num', 0],
- ['to_user_account', ''],
- ['to_uid', 0],
- ['trade_psw', '', '', '', ['not_empty_check', function ($item) use ($user) {
- return md5(md5($item)) == $user['trade_pwd'];
- }], ['请输入交易密码', '交易密码错误']],
- ['captcha', '']
- ], $request, true);
- if (!$num || !$to_uid || !$to_user_account) {
- return app('json')->fail('参数不足');
- }
- $uid_check = User::where('account', $to_user_account)->value('uid');
- if ($to_uid != $uid_check) {
- return app('json')->fail('用户账号与uid不符');
- }
- // $price = 0;
- // $list = sys_data('money_type');
- // foreach ($list as $v) {
- // if ($v['code'] == $type) {
- // $price = $v['price'] ? $v['price'] : CashTradeOrder::averagePrice($v['code']);
- // }
- // }
- // if (bcmul($num, $price, 2) >= 10000) {
- // $verifyCode = CacheService::get('code_' . $user['account']);
- // if (!$verifyCode)
- // return app('json')->fail('请先获取验证码');
- // $verifyCode = substr($verifyCode, 0, 6);
- // if ($verifyCode != $captcha)
- // return app('json')->fail('验证码错误');
- // }
- $res = UserMoney::tradeMoney($uid, $to_uid, $type, $num);
- if ($res) {
- return app('json')->success('支付成功');
- } else {
- return app('json')->fail('支付失败:' . UserMoney::getErrorInfo('支付错误'));
- }
- }
- }
|