Money.php 11 KB

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