Money.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\Config;
  5. use app\common\model\MoneyIn;
  6. use app\common\model\MoneyLog;
  7. use app\common\model\MoneyOut;
  8. use app\common\model\Users AS UserModel;
  9. use think\Db;
  10. use think\Exception;
  11. /**
  12. * 首页接口
  13. */
  14. class Money extends Api
  15. {
  16. protected $noNeedLogin = [];
  17. protected $noNeedRight = ['*'];
  18. /**
  19. * 充值信息
  20. * @return void
  21. * @throws \think\exception\DbException
  22. */
  23. public function recharge()
  24. {
  25. $user = $this->auth->getUser();
  26. $data['amount_list'] = [100,200,500,1000,5000,10000];//快捷输入额度
  27. $data['usdt'] = $data['bank'] = 1;
  28. $recharge_info = UserModel::getAgentRechargeInfoByAgentId($user['agent_id']);
  29. if(empty($recharge_info)){
  30. $this->error(__('无充值信息'));
  31. }
  32. if(empty($recharge_info['usdt'])){
  33. $data['usdt'] = 0;
  34. }
  35. if(empty($recharge_info['bank'])){
  36. $data['bank'] = 0;
  37. }
  38. $this->success('', $data);
  39. }
  40. /**
  41. * 创建充值订单
  42. * @return void
  43. */
  44. public function recharge_create()
  45. {
  46. $recharge_type = $this->request->post('type');
  47. $amount = $this->request->post('amount');
  48. if(!in_array($recharge_type, [1,2])){
  49. $this->error(__('参数有误'));
  50. }
  51. if(!($amount > 0)){
  52. $this->error(__('参数有误'));
  53. }
  54. $user = $this->auth->getUser();
  55. $recharge_info = UserModel::getAgentRechargeInfoByAgentId($user['agent_id']);
  56. if(empty($recharge_info)){
  57. $this->error(__('无充值信息'));
  58. }
  59. $insert_data = [
  60. 'order_type' => $recharge_type,
  61. 'user_id' => $user['id'],
  62. 'amount' => $amount,
  63. 'status' => MoneyIn::Default,
  64. 'user_type' => $user['user_type'],
  65. 'agent_id' => $recharge_info['agent_id']
  66. ];
  67. //USDT充值
  68. if($recharge_type == 1){
  69. if(empty($recharge_info['usdt'])){
  70. $this->error(__('参数有误'));
  71. }
  72. $insert_data['order_no'] = 'U' . time() . $user['id'];
  73. $insert_data['usdt_address'] = $recharge_info['usdt'];
  74. }else{
  75. if(empty($recharge_info['bank'])){
  76. $this->error(__('参数有误'));
  77. }
  78. $insert_data['order_no'] = 'B' . time() . $user['id'];
  79. $insert_data['bank_name'] = $recharge_info['bank']['bank_name'];
  80. $insert_data['bank_card'] = $recharge_info['bank']['bank_card'];
  81. $insert_data['account_name']= $recharge_info['bank']['account_name'];
  82. }
  83. //写入
  84. Db::startTrans();
  85. try {
  86. (new MoneyIn())->save($insert_data);
  87. Db::commit();
  88. } catch (Exception $e) {
  89. $this->error($e->getMessage());
  90. }
  91. $this->success('', $insert_data);
  92. }
  93. /**
  94. * 上传图片
  95. * @return void
  96. * @throws \think\exception\DbException
  97. */
  98. public function recharge_upload()
  99. {
  100. $user = $this->auth->getUser();
  101. $order_no = $this->request->post('order_no');
  102. if (empty($order_no)) {
  103. $this->error(__('参数有误'));
  104. }
  105. $order_info = (new MoneyIn())
  106. ->where('user_id', $user['id'])
  107. ->where('order_no', $order_no)
  108. ->find();
  109. if (empty($order_no)) {
  110. $this->error(__('参数有误'));
  111. }
  112. $file_info = ali_oss_upload($this->request, 'recharge', $order_no);
  113. if($file_info['code'] == 0){
  114. $this->error($file_info['msg']);
  115. }
  116. $this->success('', $file_info['data']);
  117. }
  118. /**
  119. * 提交充值信息
  120. * @return void
  121. * @throws \think\exception\DbException
  122. */
  123. public function recharge_submit()
  124. {
  125. $user = $this->auth->getUser();
  126. $order_no = $this->request->post('order_no');
  127. if (empty($order_no)) {
  128. $this->error(__('参数有误'));
  129. }
  130. $order_info = (new MoneyIn())
  131. ->where('user_id', $user['id'])
  132. ->where('order_no', $order_no)
  133. ->find();
  134. if (empty($order_info)) {
  135. $this->error(__('参数有误'));
  136. }
  137. //上传图片
  138. $file_info = ali_oss_upload($this->request, 'recharge', $order_no);
  139. if($file_info['code'] == 0){
  140. $this->error($file_info['msg']);
  141. }
  142. $img_url = $file_info['data']['full_url'];
  143. if ($order_info['status'] != MoneyIn::Default) {
  144. if (empty($order_info['img_url'])) {
  145. (new MoneyIn())
  146. ->where('order_no', $order_no)
  147. ->update([
  148. 'img_url' => $img_url
  149. ]);
  150. }
  151. $this->success(__('提交成功'));
  152. }
  153. (new MoneyIn())
  154. ->where('order_no', $order_no)
  155. ->update([
  156. 'img_url' => $img_url,
  157. 'status' => MoneyIn::Pending,
  158. ]);
  159. $this->success(__('提交成功'));
  160. }
  161. /**
  162. * 提现
  163. * @return void
  164. */
  165. public function withdraw()
  166. {
  167. $user = $this->auth->getUser();
  168. $data['balance'] = $user['balance'];
  169. $data['money_out_sum'] = MoneyOut::where('user_id', $user['id'])->where('status', MoneyOut::Success)->sum('amount');
  170. $withdraw_info = UserModel::getAgentWithdrawInfoByAgentId($user['agent_id']);
  171. if(empty($withdraw_info)){
  172. $this->error(__('无提现信息'));
  173. }
  174. $data = array_merge($data, $withdraw_info);
  175. $this->success('', $data);
  176. }
  177. /**
  178. * 提交提现信息
  179. * @return void
  180. * @throws \think\exception\DbException
  181. */
  182. public function withdraw_submit()
  183. {
  184. $type = $this->request->post('type');
  185. $amount = $this->request->post('amount');
  186. if(!in_array($type, [1,2])){
  187. $this->error(__('参数有误'));
  188. }
  189. if(!($amount > 0)){
  190. $this->error(__('参数有误'));
  191. }
  192. $user = $this->auth->getUser();
  193. if($amount > $user['balance']){
  194. $this->error(__('余额不足'));
  195. }
  196. $withdraw_info = UserModel::getAgentWithdrawInfoByAgentId($user['agent_id']);
  197. if(empty($withdraw_info)){
  198. $this->error(__('无提现信息'));
  199. }
  200. $withdraw_fee = (new Config())->getValue('withdrawal_fee');
  201. if(empty($withdraw_fee)){
  202. $withdraw_fee = 0;
  203. }elseif($withdraw_fee > 1){
  204. $this->error(__('提现手续费有误'));
  205. }
  206. $insert_data = [
  207. 'order_type' => $type,
  208. 'user_id' => $user['id'],
  209. 'amount' => $amount,
  210. 'fee' => $amount * $withdraw_fee,
  211. 'real_amount' => $amount * (1 - $withdraw_fee),
  212. 'status' => MoneyOut::Pending,
  213. 'agent_id' => $withdraw_info['agent_id'],
  214. 'user_type' => $user['user_type'],
  215. ];
  216. //USDT充值
  217. if($type == 1){
  218. if(empty($withdraw_info['usdt'])){
  219. $this->error(__('参数有误'));
  220. }
  221. $usdt_address = $this->request->post('usdt');
  222. if(empty($usdt_address)){
  223. $this->error(__('参数有误'));
  224. }
  225. $insert_data['order_no'] = 'U' . time() . $user['id'];
  226. $insert_data['usdt_address'] = $usdt_address;
  227. }else{
  228. if(empty($withdraw_info['bank'])){
  229. $this->error(__('参数有误'));
  230. }
  231. $insert_data['bank_name'] = $this->request->post('bank_name');
  232. $insert_data['bank_card'] = $this->request->post('bank_card');
  233. $insert_data['account_name']= $this->request->post('account_name');
  234. if(empty($insert_data['bank_name']) || empty($insert_data['bank_card']) || empty($insert_data['account_name'])){
  235. $this->error(__('参数有误'));
  236. }
  237. $insert_data['order_no'] = 'B' . time() . $user['id'];
  238. }
  239. $fund_pwd = $this->request->post('fund_pwd');
  240. if(empty($fund_pwd)){
  241. $this->error(__('参数有误'));
  242. }
  243. if($user['fund_pwd'] != md5($fund_pwd)){
  244. $this->error(__('资金密码有误'));
  245. }
  246. //写入
  247. Db::startTrans();
  248. try {
  249. (new MoneyOut())->save($insert_data);
  250. //扣款
  251. (new MoneyLog())->change($user['id'], -$amount, MoneyLog::Withdraw, '', '');
  252. Db::commit();
  253. } catch (Exception $e) {
  254. $this->error($e->getMessage());
  255. }
  256. $this->success('');
  257. }
  258. /**
  259. * 充值提现列表
  260. * @return void
  261. * @throws \think\exception\DbException
  262. */
  263. public function money_list()
  264. {
  265. $type = $this->request->post('type');
  266. if(empty($type)){
  267. $type = 1;
  268. }
  269. if(!in_array($type, [1,2])){
  270. $this->error(__('参数有误'));
  271. }
  272. $user = $this->auth->getUser();
  273. $res_data = [];
  274. if($type == 1){
  275. //充值列表
  276. $info_list = MoneyIn::where('user_id', $user['id'])
  277. ->field('order_no,order_type,usdt_address,bank_name,bank_card,account_name,amount,status,create_time')
  278. ->order('id DESC')
  279. ->paginate($this->pageSize);
  280. foreach ($info_list as $k => $v) {
  281. $info_list[$k]['status_name'] = (new MoneyIn())->getStatusNames($v['status']);
  282. }
  283. $res_data = $this->buildResp($info_list->total(), $info_list->currentPage(), $info_list->items());
  284. }else{
  285. //提现列表
  286. $info_list = MoneyOut::where('user_id', $user['id'])
  287. ->field('order_no,order_type,amount,status,create_time')
  288. ->order('id DESC')
  289. ->paginate($this->pageSize);
  290. foreach ($info_list as $k => $v) {
  291. $info_list[$k]['status_name'] = (new MoneyOut())->getStatusNames($v['status']);
  292. }
  293. $res_data = $this->buildResp($info_list->total(), $info_list->currentPage(), $info_list->items());
  294. }
  295. $res_data['money_in_sum'] = MoneyIn::where('user_id', $user['id'])->where('status', MoneyIn::Success)->sum('amount');
  296. $res_data['money_out_sum'] = MoneyOut::where('user_id', $user['id'])->where('status', MoneyOut::Success)->sum('amount');
  297. $this->success('', $res_data);
  298. }
  299. }