| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use app\api\logic\TeacLogin;
- use app\common\model\LedgerTeacChangeModel;
- use app\common\model\LedgerTokenChangeModel;
- use app\common\model\UserTeac;
- use app\common\model\LedgerWalletModel;
- use fast\Action;
- use fast\Asset;
- use Exception;
- use fast\RechargeStatus;
- use think\Db;
- use app\common\model\ProductTeac;
- //Teac交易
- class Teac extends Api
- {
-
- /*
- * 交易列表
- */
- public function getTeacList(ProductTeac $productTeac)
- {
- $type_id = $this->request->post('type_id/d', 0);
- if(!in_array($type_id, [ProductTeac::Buying, ProductTeac::Sell])) $this->error('类型错误');
-
- $list = $productTeac->with('users')
- ->where('product_teac.status', ProductTeac::Normal)->where('type_id', $type_id)
- ->order('create_time desc')
- ->paginate($this->pageSize);
- foreach ($list as &$item) {
- $item->users->avatar = !empty($item->users->avatar)? $item->users->avatar:$this->request->domain().'/assets/img/logo.png'; // 头像
- }
- $this->success('ok', $list);
- }
- /*
- * 发布求购
- */
- public function setBuying(LedgerWalletModel $ledgerWalletModel, TeacLogin $teacLogin)
- {
- $params = $this->request->post();
- $validate = \think\Loader::validate('Teac');
- if(!$validate->scene('buying')->check($params)) $this->error($validate->getError());
- if(config('teac_trade.buy_min_num') > $params['stock']) $this->error('数量不能低于'.config('teac_trade.buy_min_num'));
- if(config('teac_trade.buy_min_price') > $params['price']) $this->error('价格不能低于'.config('teac_trade.buy_min_price'));
- $chabao = bcmul($params['price'], $params['stock'], 2) ;
- if($ledgerWalletModel->getWalletChaBao($this->auth->id) < $chabao) $this->error('您的钱包茶宝余额不足');
-
- Db::startTrans();
- try{
-
- //新增求购信息
- $teacLogin::setCreateTrade($this->auth->id, $params['price'], $params['stock'], ProductTeac::Buying, $chabao);
- //冻结茶宝
- $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TOKEN, -$chabao, LedgerTokenChangeModel::Buying, $this->auth->id);
- Db::commit();
-
- }catch(Exception $e){
- Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success('ok');
- }
- //发布出售
- public function setSell(LedgerWalletModel $ledgerWalletModel, TeacLogin $teacLogin)
- {
- $params = $this->request->post();
- $validate = \think\Loader::validate('Teac');
- if(!$validate->scene('sell')->check($params)) $this->error($validate->getError());
-
- if(config('teac_trade.sell_min_num') > $params['stock']) $this->error('数量不能低于'.config('teac_trade.sell_min_num'));
- if(config('teac_trade.sell_min_price') > $params['price']) $this->error('价格不能低于'.config('teac_trade.sell_min_price'));
- if($ledgerWalletModel->getWalletTeac($this->auth->id) < $params['stock']) $this->error('您的钱包Teac余额不足');
- Db::startTrans();
- try{
-
- //新增求购信息
- $teacLogin::setCreateTrade($this->auth->id, $params['price'], $params['stock'], ProductTeac::Sell, $params['stock']);
- //冻结Teac
- $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TEAC, -$params['stock'], LedgerTokenChangeModel::BuySellg, $this->auth->id);
- Db::commit();
-
- }catch(Exception $e){
- Db::rollback();
- $this->error($e->getMessage());
- }
- $this->success('ok');
- }
- /**
- * 出售购买
- */
- public function setSellOrder(ProductTeac $productTeac, LedgerWalletModel $ledgerWalletModel, TeacLogin $teacLogin, UserTeac $userTeac)
- {
- $params = $this->request->post();
- $validate = \think\Loader::validate('Teac');
- if(!$validate->scene('sell_order')->check($params)) $this->error($validate->getError());
- $row = $productTeac::where(['type_id'=> ProductTeac::Sell, 'id' => $params['teac_id']])->find();
- if(empty($row) || $row['status'] != ProductTeac::Normal) $this->error('订单不存在');
- if($params['num'] < $row['stock'] - $row['num']) $this->error('库存不足');
- $chabao = $ledgerWalletModel->getWalletChaBao($this->auth->id);
- $tatal = bcmul($row['price'], $params['num'], 2);
- if($chabao < bcmul($row['price'], $tatal, 2)) $this->error('您的钱包茶宝余额不足');
- // 启动事务
- Db::startTrans();
- try {
- $fee = config('teac_trade.sell_fee');
-
- // 出售购买
- $userTeac::setUserCreateOrder($this->auth->id, $row['id'], ProductTeac::Sell, $params['num'], $row['price'], $fee);
- //添加扣除相应茶宝Teac
- $teacLogin::setCreateSellOrder($this->auth->id, $row['user_id'], $row['price'], $params['num'], $fee);
- //修改状态
- if($row->stock - $row->num == $params['num']) $row->status = ProductTeac::Complete;
- $row->frozen -= $params['num'];
- $row->num += $params['num'];
- $row->save();
- // 提交事务
- Db::commit();
- } catch (Exception $e) {
- // 回滚事务
- Db::rollback();
- return $e->getMessage();
- }
- $this->success('ok');
- }
- /**
- * 求购出售
- */
- public function getBuyOrder(ProductTeac $productTeac, LedgerWalletModel $ledgerWalletModel, TeacLogin $teacLogin, UserTeac $userTeac)
- {
- $params = $this->request->post();
- $validate = \think\Loader::validate('Teac');
- if(!$validate->scene('sell_order')->check($params)) $this->error($validate->getError());
- $row = $productTeac::where(['type_id'=> ProductTeac::Buying, 'id' => $params['teac_id']])->find();
- if(empty($row) || $row['status'] != ProductTeac::Normal) $this->error('订单不存在');
- if($params['num'] < $row['stock'] - $row['num']) $this->error('库存不足');
- $chabao = $ledgerWalletModel->getWalletChaBao($this->auth->id);
- $tatal = bcmul($row['price'], $params['num'], 2);
- if($chabao < bcmul($row['price'], $tatal, 2)) $this->error('您的钱包茶宝余额不足');
- // 启动事务
- Db::startTrans();
- try {
- $fee = config('teac_trade.sell_fee');
- $chabao = bcmul($row['price'], $params['num'], 2);
- // 出售购买
- $userTeac::setUserCreateOrder($this->auth->id, $row['id'], ProductTeac::Buying, $params['num'], $row['price'], $fee);
- //添加扣除相应茶宝Teac
- $teacLogin::setCreateBuyingOrder($this->auth->id, $row['user_id'], $row['price'], $params['num'], $fee);
- //修改状态
- if($row->stock - $row->num == $params['num']) $row->status = ProductTeac::Complete;
- $row->frozen -= $chabao;
- $row->num += $params['num'];
- $row->save();
- // 提交事务
- Db::commit();
- } catch (Exception $e) {
- // 回滚事务
- Db::rollback();
- return $e->getMessage();
- }
- $this->success('ok');
- }
- /**
- * 获取配置
- */
- public function getConfig()
- {
- $this->success('ok', config('teac_trade'));
- }
-
- }
|