Withdraw.php 18 KB

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