Chabao.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\logic\CommonLogic;
  4. use app\common\controller\Api;
  5. use app\api\logic\TeacLogin;
  6. use app\common\model\LedgerTeacChangeModel;
  7. use app\common\model\LedgerTokenChangeModel;
  8. use app\common\model\UserTeac;
  9. use app\common\model\LedgerWalletModel;
  10. use think\Log;
  11. use fast\Asset;
  12. use Exception;
  13. use app\common\model\UserModel;
  14. use think\Db;
  15. use app\common\model\ProductTeac;
  16. use app\common\library\Token;
  17. use think\Env;
  18. //茶宝Api
  19. class Chabao extends Api
  20. {
  21. protected array $noNeedLogin = ['*'];
  22. protected $params = [];
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $this->params = $this->request->post();
  27. if(empty($this->params) || !array_filter($this->params)) $this->error('参数错误');
  28. $sign = '';
  29. foreach ($this->params as $key => $value) {
  30. if($key != 'sign') $sign .= $value;
  31. }
  32. $sign .= Env::get('rwa.AppID');
  33. $sign = md5($sign);
  34. if($this->params['sign'] != $sign) $this->error('签名验签错误');
  35. }
  36. /*
  37. * 判断登录
  38. */
  39. public function login(UserModel $userModel)
  40. {
  41. $user = $userModel::where('mobile', $this->params['mobile'])->find();
  42. if(!$user) $this->error('账号不存在', [], 4000);
  43. Token::marshal($user['id'], $user['address']);
  44. $user['token'] = Token::getEncryptedToken($user['id']);
  45. $this->success('ok', $user);
  46. }
  47. /*
  48. * 注册
  49. */
  50. public function register(UserModel $userModel)
  51. {
  52. //判断地址存在
  53. if($userModel->getByAddress($this->params['address'])) $this->error('地址已存在');
  54. //手机号存在
  55. if($userModel::where('mobile', $this->params['mobile'])->find()) $this->error('账号已存在');
  56. $newUserID = 0;
  57. Db::startTrans();
  58. try {
  59. //注册
  60. $time = time();
  61. $data = [
  62. 'address' => $this->params['address'],
  63. 'mobile' => $this->params['mobile'],
  64. 'nickname' => "188" . substr((string)$time, 2) . rand(10, 99),//188开头,时间戳去掉开头两位,再后面再加两位随机数
  65. 'avatar' => $this->request->domain().'/assets/img/logo.png',
  66. 'create_time' => $time,
  67. 'team_level_id' => 0,
  68. ];
  69. // 创建用户
  70. $newUserID = $userModel->insertGetId($data);
  71. // 创建钱包
  72. (new LedgerWalletModel())->insertGetId(['user_id' => $newUserID]);
  73. Token::marshal($newUserID, $this->params['address']);
  74. // 提交事务
  75. Db::commit();
  76. } catch (Exception $e) {
  77. // 回滚事务
  78. Db::rollback();
  79. return $e->getMessage();
  80. }
  81. if($newUserID == 0) $this->error('注册失败');
  82. $this->success('ok', ['token' => Token::getEncryptedToken($newUserID)]);
  83. }
  84. //发布出售
  85. public function setSell(LedgerWalletModel $ledgerWalletModel, TeacLogin $teacLogin)
  86. {
  87. $params = $this->request->post();
  88. $validate = \think\Loader::validate('Teac');
  89. if(!$validate->scene('sell')->check($params)) $this->error($validate->getError());
  90. if(config('teac_trade.sell_min_num') > $params['stock']) $this->error('数量不能低于'.config('teac_trade.sell_min_num'));
  91. 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').'之间');
  92. if($ledgerWalletModel->getWalletTeac($this->auth->id) < $params['stock']) $this->error('您的钱包Teac余额不足');
  93. Db::startTrans();
  94. try{
  95. //新增求购信息
  96. $teacLogin::setCreateTrade($this->auth->id, $params['price'], $params['stock'], ProductTeac::Sell, $params['stock']);
  97. //冻结Teac
  98. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TEAC, -$params['stock'], LedgerTeacChangeModel::Sell, $this->auth->id);
  99. Db::commit();
  100. }catch(Exception $e){
  101. Db::rollback();
  102. $this->error($e->getMessage());
  103. }
  104. $this->success('ok');
  105. }
  106. /**
  107. * 出售购买
  108. */
  109. public function setSellOrder(ProductTeac $productTeac, LedgerWalletModel $ledgerWalletModel, TeacLogin $teacLogin, UserTeac $userTeac, UserModel $userModel)
  110. {
  111. $params = $this->request->post();
  112. $validate = \think\Loader::validate('Teac');
  113. if(!$validate->scene('sell_order')->check($params)) $this->error($validate->getError());
  114. $row = $productTeac::where(['type_id'=> ProductTeac::Sell, 'id' => $params['teac_id']])->find();
  115. if(empty($row) || $row['status'] != ProductTeac::Normal) $this->error('订单不存在');
  116. if($this->auth->id == $row['user_id']) $this->error('不能购买自己的订单');
  117. if($params['num'] > $row['stock'] - $row['num']) $this->error('库存不足');
  118. $chabao = $ledgerWalletModel->getWalletChaBao($this->auth->id);
  119. $tatal = bcmul($row['price'], $params['num'], 4);
  120. if($chabao < $tatal) $this->error('您的钱包茶宝余额不足');
  121. // 启动事务
  122. Db::startTrans();
  123. try {
  124. $fee = config('teac_trade.sell_serve_fee');
  125. // 出售购买
  126. $fees = $userTeac::setUserCreateOrder($this->auth->id, $row['id'], ProductTeac::Sell, $params['num'], $row['price'], $fee);
  127. //添加扣除相应茶宝Teac
  128. $teacLogin::setCreateSellOrder($this->auth->id, $row['user_id'], $row['price'], $params['num'], $fee);
  129. //等级分润
  130. CommonLogic::setTeamLevelIncome($row['user_id'], $fees, Asset::TEAC, LedgerTeacChangeModel::TeamLevel);
  131. //修改状态
  132. if($row->stock - $row->num == $params['num']) $row->status = ProductTeac::Complete;
  133. $row->frozen -= $params['num'];
  134. $row->num += $params['num'];
  135. $row->save();
  136. // 提交事务
  137. Db::commit();
  138. } catch (Exception $e) {
  139. // 回滚事务
  140. Db::rollback();
  141. return $e->getMessage();
  142. }
  143. $this->success('ok');
  144. }
  145. // 求购出售
  146. public function setBuyOrder(ProductTeac $productTeac, TeacLogin $teacLogin, UserTeac $userTeac)
  147. {
  148. $params = $this->request->post();
  149. $validate = \think\Loader::validate('Teac');
  150. if(!$validate->scene('sell_order')->check($params)) $this->error($validate->getError());
  151. $row = $productTeac::where(['type_id'=> ProductTeac::Buying, 'id' => $params['teac_id']])->find();
  152. if(empty($row) || $row['status'] != ProductTeac::Normal) $this->error('订单不存在');
  153. if($this->auth->id == $row['user_id']) $this->error('不能购买自己的订单');
  154. if($params['num'] > $row['stock'] - $row['num']) $this->error('库存不足');
  155. // 启动事务
  156. Db::startTrans();
  157. try {
  158. $fee = config('teac_trade.buy_serve_fee');
  159. $chabao = bcmul($row['price'], $params['num'], 4);
  160. // 出售购买
  161. $userTeac::setUserCreateOrder($this->auth->id, $row['id'], ProductTeac::Buying, $params['num'], $row['price'], $fee);
  162. //添加扣除相应茶宝Teac
  163. $teacLogin::setCreateBuyingOrder($this->auth->id, $row['user_id'], $row['price'], $params['num'], $fee);
  164. //修改状态
  165. if($row->stock - $row->num == $params['num']) $row->status = ProductTeac::Complete;
  166. $row->frozen -= $chabao;
  167. $row->num += $params['num'];
  168. $row->save();
  169. // 提交事务
  170. Db::commit();
  171. } catch (Exception $e) {
  172. // 回滚事务
  173. Db::rollback();
  174. return $e->getMessage();
  175. }
  176. $this->success('ok');
  177. }
  178. // 我的交易列表
  179. public function getUserTeacList(ProductTeac $productTeac)
  180. {
  181. $list = $productTeac::where('user_id', $this->auth->id)
  182. ->order('create_time desc')
  183. ->paginate($this->pageSize);
  184. $this->success('ok', $list);
  185. }
  186. //取消交易
  187. public function cancelTrade(ProductTeac $productTeac, TeacLogin $teacLogin)
  188. {
  189. $id = $this->request->post('teac_id/d', 0);
  190. $row = $productTeac::where(['id' => $id, 'user_id' => $this->auth->id])->find();
  191. if(empty($row)) $this->error('订单不存在');
  192. if($row['status'] != ProductTeac::Normal) $this->error('订单已完成');
  193. Db::startTrans();
  194. try {
  195. //返回相应茶宝Teac
  196. $teacLogin::setUserReturnOrder($this->auth->id, $row['type_id'], $row['frozen']);
  197. $row->frozen = 0;
  198. $row->status = ProductTeac::Closure;
  199. $row->save();
  200. // 提交事务
  201. Db::commit();
  202. } catch (Exception $e) {
  203. // 回滚事务
  204. Db::rollback();
  205. return $e->getMessage();
  206. }
  207. $this->success('ok');
  208. }
  209. /**
  210. * 获取配置
  211. */
  212. public function getConfig()
  213. {
  214. $this->success('ok', config('teac_trade'));
  215. }
  216. }