Money.php 12 KB

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