Withdraw.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. <?php
  2. /**
  3. * 提现
  4. */
  5. namespace app\admin\controller\withdraw;
  6. use alipay\AopCertClient;
  7. use alipay\request\AlipayFundTransUniTransferRequest;
  8. use app\admin\logic\WithdrawLogLogic;
  9. use app\admin\model\dao\WithdrawLog;
  10. use app\admin\traits\Curd;
  11. use app\api\services\AliPayServices;
  12. use app\api\services\ThirdPayServices;
  13. use app\validate\admin\withdraw\withdraw\ReturnServiceMoney;
  14. use EasyAdmin\annotation\ControllerAnnotation;
  15. use EasyAdmin\annotation\NodeAnotation;
  16. use app\common\controller\AdminController;
  17. use app\common\model\WithDrawLogModel;
  18. use think\App;
  19. use think\Exception;
  20. use think\exception\ValidateException;
  21. use think\facade\Db;
  22. /**
  23. * Class Admin
  24. * @package app\admin\controller\withdraw
  25. * @ControllerAnnotation(title="提现管理处理")
  26. */
  27. class Withdraw extends AdminController
  28. {
  29. public function __construct(App $app, WithDrawLogModel $model)
  30. {
  31. parent::__construct($app);
  32. $this->model = $model;
  33. }
  34. use Curd;
  35. /**
  36. * @NodeAnotation(title="列表")
  37. */
  38. public function index()
  39. {
  40. if ($this->request->isAjax()) {
  41. if (input('selectFields')) {
  42. return $this->selectList();
  43. }
  44. list($page, $limit, $where) = $this->buildTableParames();
  45. foreach ($where as $key => &$val) {
  46. if ($val[0] == 'status') {
  47. $where[$key][0] = 'w.status';
  48. }
  49. }
  50. $count = $this->model
  51. // ->withJoin('user', 'INNER')
  52. ->where($this->user_map)
  53. ->where($where)
  54. ->count();
  55. $list = $this->model
  56. ->alias('w')
  57. ->leftJoin('user u', 'u.id = w.uid')
  58. // ->withJoin('user', 'INNER')
  59. ->where($this->user_map)
  60. ->where($where)
  61. ->field('w.*,u.path,u.user_type,u.luzi_money')
  62. ->page($page, $limit)
  63. ->order($this->sort)
  64. ->select();
  65. $withdrawLog = new WithdrawLogLogic();
  66. foreach ($list as $key => &$val) {
  67. // 老刘下级 13824334135 uid 5344367
  68. // 郑跃兵 17888779318 uid 5344369,13866400965 uid 5344368
  69. // 大飞 19805553599 uid 5344633 ,18682271568 uid 5344640
  70. //
  71. // if ($val['user_type'] == 10){
  72. // $val['tips_text'] = '潘总号';
  73. // }else{
  74. // $val['tips_text'] = '其他线';
  75. // }
  76. // $val['tips_text'] = '其他线';
  77. //
  78. // if (strpos($val['path'], '5344367') !== false || $val['uid'] == 5344367){
  79. // $val['tips_text'] = '老刘下级';
  80. // }
  81. //
  82. // if ((strpos($val['path'], '5344633') !== false || $val['uid'] == 5344633) || (strpos($val['path'], '5344640') !== false || $val['uid'] == 5344640)){
  83. // $val['tips_text'] = '大飞下级';
  84. // }elseif ((strpos($val['path'], '5344369') !== false || $val['uid'] == 5344369) || (strpos($val['path'], '5344368') !== false || $val['uid'] == 5344368)){
  85. // $val['tips_text'] = '郑跃兵下级';
  86. // }
  87. // if (strpos($val['path'], '7493433') !== false){
  88. // $val['tips_text'] = '心态下级';
  89. // }
  90. //
  91. if (strpos($val['path'], '7493466') !== false) {
  92. $val['tips_text'] = '郑下级';
  93. }
  94. if ($val['luzi_money'] > 0) {
  95. $val['tips_text'] = '撸子标记,不要提现';
  96. } else {
  97. $val['tips_text'] = '';
  98. }
  99. $val['is_show'] = $withdrawLog->isShowReturnServiceMoney($val['id']);
  100. }
  101. // tips_text
  102. $data = [
  103. 'code' => 0,
  104. 'msg' => '',
  105. 'count' => $count,
  106. 'data' => $list,
  107. ];
  108. return json($data);
  109. }
  110. return $this->fetch();
  111. }
  112. /**
  113. * @NodeAnotation(title="取消提现")
  114. */
  115. // public function cancel ($id)
  116. // {
  117. // $row = $this->model->where('id', $id)->where('status', 'in', '0')->find();
  118. // empty($row) && $this->error('取消失败');
  119. // $row->status = 3;
  120. // Db::startTrans();
  121. // try {
  122. // edit_user_money(9, $row['uid'], $row['apply_money']);
  123. // $row->save();
  124. // Db::commit();
  125. // } catch (\Exception $e) {
  126. // Db::rollback();
  127. // $this->error('取消失败');
  128. // }
  129. // $this->success('取消成功');
  130. // }
  131. /**
  132. * @NodeAnotation(title="提现失败")
  133. */
  134. public function withdrawerror($id)
  135. {
  136. if ($this->request->isPost()) {
  137. $post = $this->request->post();
  138. $row = $this->model->where('id', $id)->where('status', 'in', '0')->find();
  139. empty($row) && $this->error('取消失败');
  140. $row->status = 3;
  141. $row->fail_log = $post['error_text'];
  142. $is_backsx = $post['is_backsx'];
  143. $row->is_backsx = $is_backsx;
  144. Db::startTrans();
  145. try {
  146. if ($is_backsx == 1) {
  147. edit_user_money(10, $row['uid'], $row['apply_money']);
  148. } else {
  149. edit_user_money(10, $row['uid'], $row['practical_money']);
  150. }
  151. Db::name('user')->where('id', $row['uid'])->dec('total_withdraw', $row['apply_money'])->update();
  152. // Db::name('user')->whereIn('id', $row['path'])->dec('total_team_withdraw', $row['apply_money'])->update();
  153. // Db::name('user')->whereIn('id', $row['uid'])->dec('total_team_withdraw', $row['apply_money'])->update();
  154. $row->save();
  155. Db::commit();
  156. } catch (\Exception $e) {
  157. Db::rollback();
  158. $this->error('取消失败' . $e->getMessage());
  159. }
  160. $this->success('取消成功');
  161. }
  162. //
  163. // if ($this->request->isPost()) {
  164. // $row = $this->model->where('id', $id)->where('status', 'in', '0')->find();
  165. // empty($row) && $this->error('取消失败');
  166. // if ($row['status'] != 1){
  167. // $this->error('状态错误,请刷新数据');
  168. // }
  169. //
  170. // $this->model->startTrans();
  171. // try {
  172. // $this->model->where('id', $post['id'])->save(['status'=>3,'error_text'=>$error_text, 'is_backsx'=>$is_backsx]);
  173. // if ($is_backsx == 1){
  174. // edit_user_xz(15, $order_info['uid'], $order_info['xz_num']+$order_info['shouxu'], $order_info['hf_ordersn'], $order_info['shouxu']);
  175. // edit_user_couponnum(5, $order_info['uid'], $order_info['price']/10);
  176. // }else{
  177. // edit_user_xz(15, $order_info['uid'], $order_info['xz_num'], $order_info['hf_ordersn'], 0);
  178. // edit_user_couponnum(5, $order_info['uid'], $order_info['price']/10);
  179. // }
  180. //
  181. // $this->model->commit();
  182. // } catch (\Exception $e) {
  183. // $this->model->rollback();
  184. // $this->error('失败'.$e->getMessage());
  185. // }
  186. // $this->success('成功');
  187. // }
  188. return $this->fetch();
  189. }
  190. /**
  191. * @NodeAnotation(title="执行提现")
  192. */
  193. public function tx($id)
  194. {
  195. // if (in_array(env('APP.CUR_SYS_PARAMS'), [1, 2])){
  196. // $this->error('错误');
  197. // }
  198. $info = $this->model->where('id', $id)->where('status', 0)->find();
  199. if (empty($info))
  200. $this->error('提现记录不存在');
  201. $user = Db::name('user')->findOrEmpty(['id' => $info['uid']]);
  202. if (empty($user) || $user['status'] != 1)
  203. $this->error('用户不存在或已被禁用');
  204. if ($user['luzi_money'] > 0) {
  205. $this->error('撸子账号 联系技术处理');
  206. }
  207. // $this->error('通道错误,失败');
  208. // if ($user['is_auth'] != 1)
  209. // $this->error('该用户还未实名认证');
  210. Db::name('user')->whereIn('id', $user['path'])->inc('total_team_withdraw', $info['apply_money'])->update();
  211. Db::name('user')->whereIn('id', $user['id'])->inc('total_team_withdraw', $info['apply_money'])->update();
  212. // 支付宝提现
  213. if ($info['channel'] == 1) {
  214. // 电科支付宝提现代付
  215. if (false) {
  216. Db::startTrans();
  217. try {
  218. $interface_url = 'http://1.14.197.47/proxy/pay/fetch'; //正式
  219. $out_trade_no = createdOrderWithdrawSn($info['uid']);
  220. $params = [
  221. 'appId' => 'diankef',
  222. 'version' => '1.0',
  223. 'nonceStr' => nonce_str(15),
  224. 'amount' => intval($info['practical_money'] * 100),
  225. 'orderId' => $out_trade_no,
  226. 'accountName' => $info['zfb_name'],
  227. 'accountNo' => $info['zfb_number'],
  228. 'bankName' => '网商银行',
  229. 'asyncNotifyUrl' => env('PAYSQZ.PAYBACK_DAIFU_ZFB_NEWURL'),
  230. 'quick' => 1,
  231. 'subject' => '余额提现'
  232. ];
  233. ksort($params);
  234. $services = new ThirdPayServices();
  235. $params['sign'] = $services->diankesigndf($params);
  236. // 提交确定支付
  237. sr_log('确认提交提现' . json_encode($params));
  238. $result = curlPost($interface_url, $params);
  239. sr_log('result:' . $result);
  240. $res = json_decode($result);
  241. if (isset($res->status) && $res->status == 9) {
  242. Db::name('withdraw_log')->where('id', $id)->update(['status' => 4, 'out_biz_no' => $out_trade_no, 'update_at' => date('Y-m-d H:i:s', time()), 'final_transfer_type' => 1]);
  243. } else {
  244. sr_throw('支付错误,请联系客服');
  245. }
  246. Db::commit();
  247. } catch (\Exception $e) {
  248. Db::rollback();
  249. $this->error($e->getMessage());
  250. }
  251. }
  252. Db::name('withdraw_log')->where('id', $id)->update(['status' => 1, 'out_biz_no' => '1111', 'update_at' => date('Y-m-d H:i:s', time()), 'final_transfer_type' => 1]);
  253. $this->success('提现成功');
  254. // try {
  255. ////
  256. // /** 初始化 **/
  257. // $aop = new AopCertClient();
  258. // $pay_info = Db::name('pay_config')->where('channel', 1)->where('status', 1)->find();
  259. // /** 支付宝网关 **/
  260. // $aop -> gatewayUrl = "https://openapi.alipay.com/gateway.do";
  261. //
  262. // /** 应用id,如何获取请参考:https://opensupport.alipay.com/support/helpcenter/190/201602493024 **/
  263. // $aop -> appId = $pay_info['app_id'];
  264. //
  265. // /** 密钥格式为pkcs1,如何获取私钥请参考:https://opensupport.alipay.com/support/helpcenter/207/201602471154?ant_source=antsupport **/
  266. // $aop -> rsaPrivateKey = $pay_info['private_key'];
  267. //
  268. // /** 应用公钥证书路径,下载后保存位置的绝对路径 **/
  269. // $appCertPath = $pay_info['app_cert_path'];
  270. //
  271. // /** 支付宝公钥证书路径,下载后保存位置的绝对路径 **/
  272. // $alipayCertPath = $pay_info['pay_cert_path'];
  273. //
  274. // /** 支付宝根证书路径,下载后保存位置的绝对路径 **/
  275. // $rootCertPath = $pay_info['pay_root_cert'];
  276. //
  277. // /** 设置签名类型 **/
  278. // $aop -> signType= "RSA2";
  279. //
  280. // /** 设置请求格式,固定值json **/
  281. // $aop -> format = "json";
  282. //
  283. // /** 设置编码格式 **/
  284. // $aop -> charset= "utf-8";
  285. //
  286. // /** 调用getPublicKey从支付宝公钥证书中提取公钥 **/
  287. // $aop -> alipayrsaPublicKey = $aop -> getPublicKey($alipayCertPath);
  288. //
  289. // /** 是否校验自动下载的支付宝公钥证书,如果开启校验要保证支付宝根证书在有效期内 **/
  290. // $aop -> isCheckAlipayPublicCert = true;
  291. //
  292. // /** 调用getCertSN获取证书序列号 **/
  293. // $aop -> appCertSN = $aop -> getCertSN($appCertPath);
  294. //
  295. // /** 调用getRootCertSN获取支付宝根证书序列号 **/
  296. // $aop -> alipayRootCertSN = $aop -> getRootCertSN($rootCertPath);
  297. //
  298. // /** 实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.fund.trans.uni.transfer(单笔转账接口) **/
  299. // $request = new AlipayFundTransUniTransferRequest();
  300. // $out_biz_no = time() . rand(10000, 99999) . $info['uid'];
  301. // /** 设置业务参数,具体接口参数传值以文档说明为准:https://opendocs.alipay.com/apis/api_28/alipay.fund.trans.uni.transfer/ **/
  302. // $request -> setBizContent("{".
  303. //
  304. // /** 商户端的唯一订单号,对于同一笔转账请求,商户需保证该订单号唯一 **/
  305. // "\"out_biz_no\":\"".$out_biz_no."\",".
  306. //
  307. // /** 转账金额,TRANS_ACCOUNT_NO_PWD产品取值最低0.1 **/
  308. // "\"trans_amount\":\"".$info['practical_money']."\",".
  309. //
  310. // /** 产品码,单笔无密转账到支付宝账户固定为:TRANS_ACCOUNT_NO_PWD **/
  311. // "\"product_code\":\"TRANS_ACCOUNT_NO_PWD\",".
  312. //
  313. // /** 场景码,单笔无密转账到支付宝账户固定为:DIRECT_TRANSFER **/
  314. // "\"biz_scene\":\"DIRECT_TRANSFER\",".
  315. //
  316. // /** 转账业务的标题,用于在支付宝用户的账单里显示 **/
  317. // "\"order_title\":\"提现\",".
  318. //
  319. // /** 收款方信息 **/
  320. // "\"payee_info\":{".
  321. //
  322. // /** 参与方的唯一标识,收款支付宝账号或者支付宝吧账号唯一会员ID **/
  323. // "\"identity\":\"".$info['zfb_number']."\",".
  324. //
  325. // /** 参与方的标识类型:ALIPAY_USER_ID 支付宝的会员ID **/
  326. // "\"identity_type\":\"ALIPAY_LOGON_ID\",".
  327. //
  328. // /** 参与方真实姓名,如果非空,将校验收款支付宝账号姓名一致性。当identity_type=ALIPAY_LOGON_ID时,本字段必填 **/
  329. // "\"name\":\"".$info['zfb_name']."\"".
  330. // "},".
  331. //
  332. // /** 业务备注 **/
  333. // "\"remark\":\"单笔转账\"".
  334. //
  335. // "}");
  336. //
  337. // $result = $aop -> execute($request);
  338. //
  339. //// sr_log('支付宝体现返回'.json_encode($result));
  340. //
  341. //
  342. // /** 获取接口调用结果,如果调用失败,可根据返回错误信息到该文档寻找排查方案:https://opensupport.alipay.com/support/helpcenter/114 **/
  343. //// $res = json_encode($result, JSON_UNESCAPED_UNICODE);
  344. //// $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
  345. //// $resultCode = $result->{$responseNode}->code;
  346. //
  347. // if (isset($result->alipay_fund_trans_uni_transfer_response->code) && $result->alipay_fund_trans_uni_transfer_response->code == 10000) {
  348. // } else {
  349. // Db::name('withdraw_log')->where('id', $id)->update(['fail_log' => $result->alipay_fund_trans_uni_transfer_response->sub_msg, 'status' => 3]);
  350. // edit_user_money(9, $info['uid'], $info['apply_money']);
  351. // $this->success('提现失败');
  352. // }
  353. //// sr_log(json_encode($res));
  354. //// $rpm = $this->apiRequest('adminWithdrawal', compact('id'));
  355. //// if ($rpm['code'] != 200)
  356. //// $this->error($rpm['msg']);
  357. // } catch (Exception $e) {
  358. // sr_log($e->getMessage());
  359. // $this->error('提现失败');
  360. // }
  361. } else {
  362. $info->status = 1;
  363. $info->update_at = date('Y-m-d H:i:s', time());
  364. $info->final_transfer_type = 3;
  365. $info->save();
  366. }
  367. // $transferstatus = get_config(2, 'TRANSFERSTATUS'); // 获取支付设置
  368. // if ($transferstatus == 1 || $transferstatus == 2) { // 自动转账
  369. // try {
  370. // $rpm = $this->apiRequest('adminWithdrawal', compact('id'));
  371. // if ($rpm['code'] != 200)
  372. // $this->error($rpm['msg']);
  373. // } catch (\Exception $e) {
  374. // $this->error('提现失败');
  375. // }
  376. // } else { // 手动执行
  377. // $info->status = 1;
  378. // $info->update_at = date('Y-m-d H:i:s', time());
  379. // $info->final_transfer_type = 3;
  380. // $info->save();
  381. // }
  382. $this->success('提现成功');
  383. }
  384. /**
  385. * @NodeAnotation(title="导出")
  386. */
  387. public function export()
  388. {
  389. list($page, $limit, $where) = $this->buildTableParames();
  390. $tableName = $this->model->getName();
  391. $tableName = CommonTool::humpToLine(lcfirst($tableName));
  392. $prefix = config('database.connections.mysql.prefix');
  393. $dbList = Db::query("show full columns from {$prefix}{$tableName}");
  394. $header = [];
  395. foreach ($dbList as $vo) {
  396. $comment = !empty($vo['Comment']) ? $vo['Comment'] : $vo['Field'];
  397. if (!in_array($vo['Field'], $this->noExportFields)) {
  398. $header[] = [$comment, $vo['Field']];
  399. }
  400. }
  401. $list = $this->model
  402. ->where($where)
  403. ->withJoin('user', 'INNER')
  404. ->where($where)
  405. ->limit(100000)
  406. ->order('id', 'desc')
  407. ->select()
  408. ->toArray();
  409. $fileName = time();
  410. return Excel::exportData($list, $header, $fileName, 'xlsx');
  411. }
  412. /**
  413. * @NodeAnotation(title="提现数据")
  414. */
  415. public function withdrawdata()
  416. {
  417. $tx_success = Db::name('withdraw_log')->where('status', 1)->sum('practical_money');
  418. $this->assign('tx_success', $tx_success);
  419. $tx = Db::name('withdraw_log')->where('status', 0)->sum('practical_money');
  420. $this->assign('tx', $tx);
  421. return $this->fetch();
  422. }
  423. /**
  424. * 退还手续费
  425. * @return mixed
  426. */
  427. public function returnservicemoney()
  428. {
  429. $id = $this->request['id'];
  430. $withdrawLog = new WithdrawLogLogic();
  431. if ($this->request->isPost()) {
  432. $post = $this->request->post();
  433. try {
  434. validate(ReturnServiceMoney::class)->check($post);
  435. } catch (ValidateException $e) {
  436. $this->error($e->getMessage());
  437. }
  438. $result = $withdrawLog->returnServiceMoney($post);
  439. if ($result !== true) {
  440. $this->error($result);
  441. }
  442. $this->success('成功');
  443. }
  444. $withdrawLog->getWithdrawLog($id);
  445. $this->assign('info', $withdrawLog);
  446. return $this->fetch();
  447. }
  448. }