|
|
@@ -15,7 +15,7 @@ use fast\Asset;
|
|
|
use Exception;
|
|
|
use app\common\model\UserModel;
|
|
|
use think\Db;
|
|
|
-use app\common\model\ProductTeac;
|
|
|
+use app\api\validate\Chabao as ChabaoValidate;
|
|
|
use app\common\library\Token;
|
|
|
use think\Env;
|
|
|
|
|
|
@@ -42,7 +42,6 @@ class Chabao extends Api
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
/*
|
|
|
* 判断登录
|
|
|
*/
|
|
|
@@ -61,14 +60,11 @@ class Chabao extends Api
|
|
|
*/
|
|
|
public function register(UserModel $userModel)
|
|
|
{
|
|
|
-
|
|
|
-
|
|
|
//手机号存在
|
|
|
if($userModel::where('mobile', $this->params['mobile'])->find()) $this->error('账号已存在');
|
|
|
$newUserID = 0;
|
|
|
Db::startTrans();
|
|
|
try {
|
|
|
-
|
|
|
//注册
|
|
|
$time = time();
|
|
|
$data = [
|
|
|
@@ -98,130 +94,23 @@ class Chabao extends Api
|
|
|
}
|
|
|
|
|
|
|
|
|
- //发布出售
|
|
|
- public function setSell(LedgerWalletModel $ledgerWalletModel, TeacLogin $teacLogin)
|
|
|
+ //绑定地址
|
|
|
+ public function bindAddress(UserModel $userModel)
|
|
|
{
|
|
|
- $params = $this->request->post();
|
|
|
- $validate = \think\Loader::validate('Teac');
|
|
|
- if(!$validate->scene('sell')->check($params)) $this->error($validate->getError());
|
|
|
+ $user = $userModel->getByAddress($this->params['address']);
|
|
|
|
|
|
- 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'] || config('teac_trade.sell_max_price') < $params['price']) $this->error('价格不在'.config('teac_trade.sell_min_price').'~'.config('teac_trade.sell_max_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']);
|
|
|
+ if(!$user) $this->error('钱包地址不存在');
|
|
|
+ if($user->mobile) $this->error('钱包地址已被绑定');
|
|
|
|
|
|
- //冻结Teac
|
|
|
- $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TEAC, -$params['stock'], LedgerTeacChangeModel::Sell, $this->auth->id);
|
|
|
- Db::commit();
|
|
|
-
|
|
|
- }catch(Exception $e){
|
|
|
- Db::rollback();
|
|
|
- $this->error($e->getMessage());
|
|
|
- }
|
|
|
+ //修改手机号
|
|
|
+ $user->mobile = $this->params['mobile'];
|
|
|
+ $user->save();
|
|
|
$this->success('ok');
|
|
|
}
|
|
|
|
|
|
|
|
|
- /**
|
|
|
- * 出售购买
|
|
|
- */
|
|
|
- public function setSellOrder(ProductTeac $productTeac, LedgerWalletModel $ledgerWalletModel, TeacLogin $teacLogin, UserTeac $userTeac, UserModel $userModel)
|
|
|
- {
|
|
|
- $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($this->auth->id == $row['user_id']) $this->error('不能购买自己的订单');
|
|
|
- if($params['num'] > $row['stock'] - $row['num']) $this->error('库存不足');
|
|
|
- $chabao = $ledgerWalletModel->getWalletChaBao($this->auth->id);
|
|
|
- $tatal = bcmul($row['price'], $params['num'], 4);
|
|
|
- if($chabao < $tatal) $this->error('您的钱包茶宝余额不足');
|
|
|
|
|
|
- // 启动事务
|
|
|
- Db::startTrans();
|
|
|
- try {
|
|
|
-
|
|
|
- $fee = config('teac_trade.sell_serve_fee');
|
|
|
-
|
|
|
- // 出售购买
|
|
|
- $fees = $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);
|
|
|
|
|
|
- //等级分润
|
|
|
- CommonLogic::setTeamLevelIncome($row['user_id'], $fees, Asset::TEAC, LedgerTeacChangeModel::TeamLevel);
|
|
|
-
|
|
|
- //修改状态
|
|
|
- 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 setBuyOrder(ProductTeac $productTeac, 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($this->auth->id == $row['user_id']) $this->error('不能购买自己的订单');
|
|
|
- if($params['num'] > $row['stock'] - $row['num']) $this->error('库存不足');
|
|
|
-
|
|
|
- // 启动事务
|
|
|
- Db::startTrans();
|
|
|
- try {
|
|
|
-
|
|
|
- $fee = config('teac_trade.buy_serve_fee');
|
|
|
- $chabao = bcmul($row['price'], $params['num'], 4);
|
|
|
- // 出售购买
|
|
|
- $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 getUserTeacList(ProductTeac $productTeac)
|
|
|
- {
|
|
|
- $list = $productTeac::where('user_id', $this->auth->id)
|
|
|
- ->order('create_time desc')
|
|
|
- ->paginate($this->pageSize);
|
|
|
- $this->success('ok', $list);
|
|
|
- }
|
|
|
|
|
|
//取消交易
|
|
|
public function cancelTrade(ProductTeac $productTeac, TeacLogin $teacLogin)
|
|
|
@@ -248,13 +137,7 @@ class Chabao extends Api
|
|
|
$this->success('ok');
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 获取配置
|
|
|
- */
|
|
|
- public function getConfig()
|
|
|
- {
|
|
|
- $this->success('ok', config('teac_trade'));
|
|
|
- }
|
|
|
+
|
|
|
|
|
|
|
|
|
|