Chabao.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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::where('mobile', $this->params['mobile'])->find()) $this->error('账号已存在');
  54. $newUserID = 0;
  55. Db::startTrans();
  56. try {
  57. //注册
  58. $time = time();
  59. $data = [
  60. 'mobile' => $this->params['mobile'],
  61. 'nickname' => "188" . substr((string)$time, 2) . rand(10, 99),//188开头,时间戳去掉开头两位,再后面再加两位随机数
  62. 'avatar' => $this->request->domain().'/assets/img/logo.png',
  63. 'create_time' => $time,
  64. 'team_level_id' => 0,
  65. ];
  66. // 创建用户
  67. $newUserID = $userModel->insertGetId($data);
  68. // 创建钱包
  69. (new LedgerWalletModel())->insertGetId(['user_id' => $newUserID]);
  70. Token::marshal($newUserID, $this->params['mobile']);
  71. // 提交事务
  72. Db::commit();
  73. } catch (Exception $e) {
  74. // 回滚事务
  75. Db::rollback();
  76. return $e->getMessage();
  77. }
  78. if($newUserID == 0) $this->error('注册失败');
  79. $this->success('ok', ['token' => Token::getEncryptedToken($newUserID)]);
  80. }
  81. //发布出售
  82. public function setSell(LedgerWalletModel $ledgerWalletModel, TeacLogin $teacLogin)
  83. {
  84. $params = $this->request->post();
  85. $validate = \think\Loader::validate('Teac');
  86. if(!$validate->scene('sell')->check($params)) $this->error($validate->getError());
  87. if(config('teac_trade.sell_min_num') > $params['stock']) $this->error('数量不能低于'.config('teac_trade.sell_min_num'));
  88. 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').'之间');
  89. if($ledgerWalletModel->getWalletTeac($this->auth->id) < $params['stock']) $this->error('您的钱包Teac余额不足');
  90. Db::startTrans();
  91. try{
  92. //新增求购信息
  93. $teacLogin::setCreateTrade($this->auth->id, $params['price'], $params['stock'], ProductTeac::Sell, $params['stock']);
  94. //冻结Teac
  95. $ledgerWalletModel->changeWalletAccount($this->auth->id, Asset::TEAC, -$params['stock'], LedgerTeacChangeModel::Sell, $this->auth->id);
  96. Db::commit();
  97. }catch(Exception $e){
  98. Db::rollback();
  99. $this->error($e->getMessage());
  100. }
  101. $this->success('ok');
  102. }
  103. /**
  104. * 出售购买
  105. */
  106. public function setSellOrder(ProductTeac $productTeac, LedgerWalletModel $ledgerWalletModel, TeacLogin $teacLogin, UserTeac $userTeac, UserModel $userModel)
  107. {
  108. $params = $this->request->post();
  109. $validate = \think\Loader::validate('Teac');
  110. if(!$validate->scene('sell_order')->check($params)) $this->error($validate->getError());
  111. $row = $productTeac::where(['type_id'=> ProductTeac::Sell, 'id' => $params['teac_id']])->find();
  112. if(empty($row) || $row['status'] != ProductTeac::Normal) $this->error('订单不存在');
  113. if($this->auth->id == $row['user_id']) $this->error('不能购买自己的订单');
  114. if($params['num'] > $row['stock'] - $row['num']) $this->error('库存不足');
  115. $chabao = $ledgerWalletModel->getWalletChaBao($this->auth->id);
  116. $tatal = bcmul($row['price'], $params['num'], 4);
  117. if($chabao < $tatal) $this->error('您的钱包茶宝余额不足');
  118. // 启动事务
  119. Db::startTrans();
  120. try {
  121. $fee = config('teac_trade.sell_serve_fee');
  122. // 出售购买
  123. $fees = $userTeac::setUserCreateOrder($this->auth->id, $row['id'], ProductTeac::Sell, $params['num'], $row['price'], $fee);
  124. //添加扣除相应茶宝Teac
  125. $teacLogin::setCreateSellOrder($this->auth->id, $row['user_id'], $row['price'], $params['num'], $fee);
  126. //等级分润
  127. CommonLogic::setTeamLevelIncome($row['user_id'], $fees, Asset::TEAC, LedgerTeacChangeModel::TeamLevel);
  128. //修改状态
  129. if($row->stock - $row->num == $params['num']) $row->status = ProductTeac::Complete;
  130. $row->frozen -= $params['num'];
  131. $row->num += $params['num'];
  132. $row->save();
  133. // 提交事务
  134. Db::commit();
  135. } catch (Exception $e) {
  136. // 回滚事务
  137. Db::rollback();
  138. return $e->getMessage();
  139. }
  140. $this->success('ok');
  141. }
  142. // 求购出售
  143. public function setBuyOrder(ProductTeac $productTeac, TeacLogin $teacLogin, UserTeac $userTeac)
  144. {
  145. $params = $this->request->post();
  146. $validate = \think\Loader::validate('Teac');
  147. if(!$validate->scene('sell_order')->check($params)) $this->error($validate->getError());
  148. $row = $productTeac::where(['type_id'=> ProductTeac::Buying, 'id' => $params['teac_id']])->find();
  149. if(empty($row) || $row['status'] != ProductTeac::Normal) $this->error('订单不存在');
  150. if($this->auth->id == $row['user_id']) $this->error('不能购买自己的订单');
  151. if($params['num'] > $row['stock'] - $row['num']) $this->error('库存不足');
  152. // 启动事务
  153. Db::startTrans();
  154. try {
  155. $fee = config('teac_trade.buy_serve_fee');
  156. $chabao = bcmul($row['price'], $params['num'], 4);
  157. // 出售购买
  158. $userTeac::setUserCreateOrder($this->auth->id, $row['id'], ProductTeac::Buying, $params['num'], $row['price'], $fee);
  159. //添加扣除相应茶宝Teac
  160. $teacLogin::setCreateBuyingOrder($this->auth->id, $row['user_id'], $row['price'], $params['num'], $fee);
  161. //修改状态
  162. if($row->stock - $row->num == $params['num']) $row->status = ProductTeac::Complete;
  163. $row->frozen -= $chabao;
  164. $row->num += $params['num'];
  165. $row->save();
  166. // 提交事务
  167. Db::commit();
  168. } catch (Exception $e) {
  169. // 回滚事务
  170. Db::rollback();
  171. return $e->getMessage();
  172. }
  173. $this->success('ok');
  174. }
  175. // 我的交易列表
  176. public function getUserTeacList(ProductTeac $productTeac)
  177. {
  178. $list = $productTeac::where('user_id', $this->auth->id)
  179. ->order('create_time desc')
  180. ->paginate($this->pageSize);
  181. $this->success('ok', $list);
  182. }
  183. //取消交易
  184. public function cancelTrade(ProductTeac $productTeac, TeacLogin $teacLogin)
  185. {
  186. $id = $this->request->post('teac_id/d', 0);
  187. $row = $productTeac::where(['id' => $id, 'user_id' => $this->auth->id])->find();
  188. if(empty($row)) $this->error('订单不存在');
  189. if($row['status'] != ProductTeac::Normal) $this->error('订单已完成');
  190. Db::startTrans();
  191. try {
  192. //返回相应茶宝Teac
  193. $teacLogin::setUserReturnOrder($this->auth->id, $row['type_id'], $row['frozen']);
  194. $row->frozen = 0;
  195. $row->status = ProductTeac::Closure;
  196. $row->save();
  197. // 提交事务
  198. Db::commit();
  199. } catch (Exception $e) {
  200. // 回滚事务
  201. Db::rollback();
  202. return $e->getMessage();
  203. }
  204. $this->success('ok');
  205. }
  206. /**
  207. * 获取配置
  208. */
  209. public function getConfig()
  210. {
  211. $this->success('ok', config('teac_trade'));
  212. }
  213. }