Money.php 10 KB

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