Money.php 12 KB

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