Money.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 = ['getMongyOut'];
  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);
  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);
  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. if(empty($user['fund_pwd'])){
  169. $this->error(__('资金密码未设置'), '', 4001);
  170. }
  171. $data['balance'] = $user['freeze'] < 0 ? $user['freeze']: $user['balance'];
  172. $data['money_out_sum'] = MoneyOut::where('user_id', $user['id'])->where('status', MoneyOut::Success)->sum('amount');
  173. $withdraw_info = UserModel::getAgentWithdrawInfoByAgentId($user['agent_id']);
  174. if(empty($withdraw_info)){
  175. $this->error(__('无提现信息'));
  176. }
  177. $data['usdt']['is_open'] = $withdraw_info['usdt'];
  178. $data['bank']['is_open'] = $withdraw_info['bank'];
  179. if($withdraw_info['usdt']){
  180. if(empty($user['usdt_address'])){
  181. $this->error(__('出款信息未配置'), '', 4002);
  182. }
  183. $data['usdt']['address'] = $user['usdt_address'];
  184. }
  185. if($withdraw_info['bank']){
  186. if(empty($user['bank_info'])){
  187. $this->error(__('出款信息未配置'), '', 4002);
  188. }
  189. $data['bank']['account_name'] = $data['bank']['bank_name'] = $data['bank']['bank_card'] = '';
  190. $bank_info = json_decode($user['bank_info'], true);
  191. if(!empty($bank_info)){
  192. $data['bank']['account_name'] = $bank_info['account_name'];
  193. $data['bank']['bank_name'] = $bank_info['bank_name'];
  194. $data['bank']['bank_card'] = $bank_info['bank_card'];
  195. }
  196. }
  197. $this->success('', $data);
  198. }
  199. /**
  200. * 提交提现信息
  201. * @return void
  202. * @throws \think\exception\DbException
  203. */
  204. public function withdraw_submit()
  205. {
  206. $type = $this->request->post('type');
  207. $amount = $this->request->post('amount');
  208. if(!in_array($type, [1,2])){
  209. $this->error(__('参数有误'));
  210. }
  211. if(!($amount > 0)){
  212. $this->error(__('参数有误'));
  213. }
  214. $user = $this->auth->getUser();
  215. if($amount > $user['balance']){
  216. $this->error(__('余额不足'));
  217. }
  218. if($user['task_num'] != (new Config())->getValue('day_tasks_num')){
  219. $this->error(__('任务不足'));
  220. }
  221. $withdraw_info = UserModel::getAgentWithdrawInfoByAgentId($user['agent_id']);
  222. if(empty($withdraw_info)){
  223. $this->error(__('无提现信息'));
  224. }
  225. $withdraw_fee = (new Config())->getValue('withdrawal_fee');
  226. if(empty($withdraw_fee)){
  227. $withdraw_fee = 0;
  228. }elseif($withdraw_fee > 1){
  229. $this->error(__('提现手续费有误'));
  230. }
  231. $insert_data = [
  232. 'order_type' => $type,
  233. 'user_id' => $user['id'],
  234. 'amount' => $amount,
  235. 'fee' => $amount * $withdraw_fee,
  236. 'real_amount' => $amount * (1 - $withdraw_fee),
  237. 'status' => MoneyOut::Pending,
  238. 'agent_id' => $withdraw_info['agent_id'],
  239. 'user_type' => $user['user_type'],
  240. ];
  241. //USDT充值
  242. if($type == 1){
  243. if(empty($withdraw_info['usdt'])){
  244. $this->error(__('参数有误'));
  245. }
  246. // $usdt_address = $this->request->post('usdt');
  247. if(empty($user['usdt_address'])){
  248. $this->error(__('参数有误'));
  249. }
  250. $insert_data['order_no'] = 'U' . time() . $user['id'];
  251. $insert_data['usdt_address'] = $user['usdt_address'];
  252. }else{
  253. if(empty($withdraw_info['bank'])){
  254. $this->error(__('参数有误'));
  255. }
  256. if(empty($user['bank_info'])){
  257. $this->error(__('参数有误'));
  258. }
  259. $bank_info = json_decode($user['bank_info'], true);
  260. $insert_data['bank_name'] = $bank_info['bank_name'];
  261. $insert_data['bank_card'] = $bank_info['bank_card'];
  262. $insert_data['account_name']= $bank_info['account_name'];
  263. $insert_data['order_no'] = 'B' . time() . $user['id'];
  264. }
  265. $fund_pwd = $this->request->post('fund_pwd');
  266. if(empty($fund_pwd)){
  267. $this->error(__('参数有误'));
  268. }
  269. if($user['fund_pwd'] != md5($fund_pwd)){
  270. $this->error(__('资金密码有误'));
  271. }
  272. //写入
  273. Db::startTrans();
  274. try {
  275. (new MoneyOut())->save($insert_data);
  276. //扣款
  277. (new MoneyLog())->change($user['id'], -$amount, MoneyLog::Withdraw, '', '');
  278. Db::commit();
  279. } catch (Exception $e) {
  280. $this->error($e->getMessage());
  281. }
  282. $this->success('');
  283. }
  284. /**
  285. * 充值提现列表
  286. * @return void
  287. * @throws \think\exception\DbException
  288. */
  289. public function money_list()
  290. {
  291. $type = $this->request->post('type');
  292. if(empty($type)){
  293. $type = 1;
  294. }
  295. if(!in_array($type, [1,2])){
  296. $this->error(__('参数有误'));
  297. }
  298. $user = $this->auth->getUser();
  299. $res_data = [];
  300. if($type == 1){
  301. //充值列表
  302. $info_list = MoneyIn::where('user_id', $user['id'])
  303. ->field('order_no,order_type,usdt_address,bank_name,bank_card,account_name,amount,status,create_time')
  304. ->order('id DESC')
  305. ->paginate($this->pageSize);
  306. foreach ($info_list as $k => $v) {
  307. $info_list[$k]['status_name'] = (new MoneyIn())->getStatusNames($v['status']);
  308. }
  309. $res_data = $this->buildResp($info_list->total(), $info_list->currentPage(), $info_list->items());
  310. }else{
  311. //提现列表
  312. $info_list = MoneyOut::where('user_id', $user['id'])
  313. ->field('order_no,order_type,amount,status,create_time')
  314. ->order('id DESC')
  315. ->paginate($this->pageSize);
  316. foreach ($info_list as $k => $v) {
  317. $info_list[$k]['status_name'] = (new MoneyOut())->getStatusNames($v['status']);
  318. }
  319. $res_data = $this->buildResp($info_list->total(), $info_list->currentPage(), $info_list->items());
  320. }
  321. $res_data['money_in_sum'] = MoneyIn::where('user_id', $user['id'])->where('status', MoneyIn::Success)->sum('amount');
  322. $res_data['money_out_sum'] = MoneyOut::where('user_id', $user['id'])->where('status', MoneyOut::Success)->sum('amount');
  323. $this->success('', $res_data);
  324. }
  325. /**
  326. * 获取待处理提现订单
  327. * @return void
  328. * @throws \think\exception\DbException
  329. */
  330. public function getMongyOut()
  331. {
  332. $count = MoneyOut::where('status', MoneyOut::Pending)->count();
  333. $this->success('', $count);
  334. }
  335. }