Chabao.php 9.6 KB

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