UserExtractServices.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\user;
  13. use app\dao\user\UserExtractDao;
  14. use app\services\BaseServices;
  15. use app\services\order\StoreOrderCreateServices;
  16. use app\services\statistic\CapitalFlowServices;
  17. use app\services\system\admin\SystemAdminServices;
  18. use app\services\wechat\WechatUserServices;
  19. use crmeb\exceptions\AdminException;
  20. use crmeb\exceptions\ApiException;
  21. use crmeb\services\AliPayService;
  22. use crmeb\services\FormBuilder as Form;
  23. use crmeb\services\app\WechatService;
  24. use crmeb\services\pay\Pay;
  25. use crmeb\services\wechat\Payment;
  26. use crmeb\services\workerman\ChannelService;
  27. use EasyWeChat\Payment\Order;
  28. use think\exception\ValidateException;
  29. use think\facade\Route as Url;
  30. /**
  31. *
  32. * Class UserExtractServices
  33. * @package app\services\user
  34. */
  35. class UserExtractServices extends BaseServices
  36. {
  37. /**
  38. * UserExtractServices constructor.
  39. * @param UserExtractDao $dao
  40. */
  41. public function __construct(UserExtractDao $dao)
  42. {
  43. $this->dao = $dao;
  44. }
  45. /**
  46. * 获取一条提现记录
  47. * @param int $id
  48. * @param array $field
  49. * @return array|\think\Model|null
  50. */
  51. public function getExtract(int $id, array $field = [])
  52. {
  53. return $this->dao->get($id, $field);
  54. }
  55. /**
  56. * 获取某个用户提现总数
  57. * @param int $uid
  58. * @return float
  59. */
  60. public function getUserExtract(int $uid)
  61. {
  62. return $this->dao->getWhereSum(['uid' => $uid, 'status' => 1]);
  63. }
  64. /**
  65. * 获取某些用户的提现总数列表
  66. * @param array $uids
  67. */
  68. public function getUsersSumList(array $uids)
  69. {
  70. return $this->dao->getWhereSumList(['uid' => $uids, 'status' => 1]);
  71. }
  72. public function getCount(array $where = [])
  73. {
  74. return $this->dao->getCount($where);
  75. }
  76. /**
  77. * 获取提现列表
  78. * @param array $where
  79. * @param string $field
  80. * @return array
  81. * @throws \think\db\exception\DataNotFoundException
  82. * @throws \think\db\exception\DbException
  83. * @throws \think\db\exception\ModelNotFoundException
  84. */
  85. public function getUserExtractList(array $where, string $field = '*')
  86. {
  87. [$page, $limit] = $this->getPageValue();
  88. $list = $this->dao->getExtractList($where, $field, $page, $limit);
  89. foreach ($list as &$item) {
  90. $item['nickname'] = $item['user']['nickname'] ?? '';
  91. $item['receive_price'] = bcsub((string)$item['extract_price'], (string)$item['extract_fee'], 2);
  92. }
  93. $count = $this->dao->count($where);
  94. return compact('list', 'count');
  95. }
  96. /**
  97. * 获取提现总数
  98. * @param array $where
  99. */
  100. public function getExtractSum(array $where)
  101. {
  102. return $this->dao->getExtractMoneyByWhere($where, 'extract_price');
  103. }
  104. /**
  105. * 拒绝提现申请
  106. * @param $id
  107. * @param $fail_msg
  108. * @return bool
  109. * @throws \think\db\exception\DataNotFoundException
  110. * @throws \think\db\exception\ModelNotFoundException
  111. * @throws \think\exception\DbException
  112. */
  113. public function changeFail(int $id, $userExtract, $message)
  114. {
  115. $fail_time = time();
  116. $extract_number = $userExtract['extract_price'];
  117. $mark = '提现失败,退回佣金' . $extract_number . '元';
  118. $uid = $userExtract['uid'];
  119. $status = -1;
  120. /** @var UserServices $userServices */
  121. $userServices = app()->make(UserServices::class);
  122. $user = $userServices->getUserInfo($uid);
  123. $this->transaction(function () use ($user, $uid, $id, $extract_number, $message, $userServices, $status, $fail_time) {
  124. //增加佣金记录
  125. /** @var UserBrokerageServices $userBrokerageServices */
  126. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  127. $now_brokerage = bcadd((string)$user['brokerage_price'], (string)$extract_number, 2);
  128. $userBrokerageServices->income('extract_fail', $uid, $extract_number, $now_brokerage, $id);
  129. if (!$userServices->update($uid, ['brokerage_price' => bcadd((string)$user['brokerage_price'], (string)$extract_number, 2)], 'uid'))
  130. throw new AdminException(400657);
  131. if (!$this->dao->update($id, ['fail_time' => $fail_time, 'fail_msg' => $message, 'status' => $status])) {
  132. throw new AdminException(100007);
  133. }
  134. });
  135. event('NoticeListener', [['uid' => $uid, 'userType' => strtolower($user['user_type']), 'extract_number' => $extract_number, 'nickname' => $user['nickname'], 'message' => $message], 'user_balance_change']);
  136. //自定义通知-用户提现失败
  137. $userExtract['nickname'] = $user['nickname'];
  138. $userExtract['message'] = $message;
  139. $userExtract['time'] = date('Y-m-d H:i:s');
  140. $userExtract['price'] = $extract_number;
  141. $userExtract['phone'] = app()->make(UserServices::class)->value($userExtract['uid'], 'phone');
  142. event('CustomNoticeListener', [$userExtract['uid'], $userExtract, 'extract_fail']);
  143. //自定义事件-用户提现失败
  144. event('CustomEventListener', ['admin_extract_fail', [
  145. 'uid' => $userExtract['uid'],
  146. 'price' => $userExtract['price'],
  147. 'pay_type' => $userExtract['extract_type'],
  148. 'nickname' => $userExtract['price'],
  149. 'phone' => $userExtract['phone'],
  150. 'fail_time' => date('Y-m-d H:i:s')
  151. ]]);
  152. return true;
  153. }
  154. /**
  155. * 通过提现申请
  156. * @param int $id
  157. * @param $userExtract
  158. * @return bool
  159. * @throws \think\db\exception\DataNotFoundException
  160. * @throws \think\db\exception\DbException
  161. * @throws \think\db\exception\ModelNotFoundException
  162. */
  163. public function changeSuccess(int $id, $userExtract)
  164. {
  165. $extractNumber = bcsub($userExtract['extract_price'], $userExtract['extract_fee'], 2);
  166. /** @var WechatUserServices $wechatServices */
  167. $wechatServices = app()->make(WechatUserServices::class);
  168. /** @var UserServices $userServices */
  169. $userServices = app()->make(UserServices::class);
  170. $userType = $userServices->value(['uid' => $userExtract['uid']], 'user_type');
  171. $nickname = $userServices->value(['uid' => $userExtract['uid']], 'nickname');
  172. $phone = $userServices->value(['uid' => $userExtract['uid']], 'phone');
  173. $order_id = $userExtract['wechat_order_id'] != '' ? $userExtract['wechat_order_id'] : app()->make(StoreOrderCreateServices::class)->getNewOrderId('tx');
  174. $insertData = ['wechat_order_id' => $order_id, 'nickname' => $nickname, 'phone' => $phone];
  175. //微信自动提现到零钱
  176. if (sys_config('weixin_extract_type', 0) && $userExtract['extract_type'] == 'weixin') {
  177. $type = '';
  178. $openid = $wechatServices->uidToOpenid($userExtract['uid'], $userExtract['channel_type']);
  179. if ($userExtract['channel_type'] == 'wechat') {
  180. $type = Order::JSAPI;
  181. } elseif ($userExtract['channel_type'] == 'routine') {
  182. $type = 'mini';
  183. } elseif ($userExtract['channel_type'] == 'app') {
  184. $type = Order::APP;
  185. }
  186. if (!$openid) {
  187. $openid = $wechatServices->uidToOpenid($userExtract['uid'], 'wechat');
  188. $type = Order::JSAPI;
  189. }
  190. if (!$openid) {
  191. $openid = $wechatServices->uidToOpenid($userExtract['uid'], 'routine');
  192. $type = 'mini';
  193. }
  194. if (!$openid) {
  195. $openid = $wechatServices->uidToOpenid((int)$userExtract['uid'], 'app');
  196. $type = Order::APP;
  197. }
  198. if (!$openid) {
  199. throw new ValidateException('该用户暂不支持自动转账到零钱,请手动转账');
  200. }
  201. //v3商家转账
  202. if (sys_config('pay_wechat_type')) {
  203. $pay = new Pay('v3_wechat_pay');
  204. if (sys_config('v3_pay_public_key') != '') {
  205. $res = $pay->merchantPayNew(
  206. $type,
  207. $order_id,
  208. sys_config('v3_transfer_scene_id', '1000'),
  209. $openid,
  210. $userExtract['real_name'],
  211. bcmul($extractNumber, '100', 0),
  212. '佣金提现到零钱',
  213. sys_config('site_url') . '/api/transfer/notify/' . $type,
  214. '劳务报酬',
  215. [
  216. [
  217. 'info_type' => '岗位类型',
  218. 'info_content' => '推广员奖励'
  219. ],
  220. [
  221. 'info_type' => '报酬说明',
  222. 'info_content' => '推广订单奖励提现'
  223. ],
  224. ]
  225. );
  226. $this->dao->update($id, [
  227. 'out_bill_no' => $res['out_bill_no'] ?? '',
  228. 'package_info' => $res['package_info'] ?? '',
  229. 'state' => $res['state'] ?? '',
  230. 'transfer_bill_no' => $res['transfer_bill_no'] ?? '',
  231. 'fail_reason' => $res['fail_reason'] ?? '',
  232. 'status' => 1
  233. ]);
  234. event('NoticeListener', [['uid' => $userExtract['uid'], 'order_id' => $order_id, 'extractNumber' => $extractNumber, 'type' => 1], 'revenue_received']);
  235. return 'v3_extract';
  236. } else {
  237. $res = $pay->merchantPay($openid, $order_id, $extractNumber, [
  238. 'type' => $type,
  239. 'batch_name' => '提现佣金到零钱',
  240. 'batch_remark' => '您于' . date('Y-m-d H:i:s') . '提现.' . $extractNumber . '元'
  241. ]);
  242. $this->dao->update($id, ['wechat_order_id' => $order_id]);
  243. }
  244. } else {
  245. // 微信提现
  246. $res = WechatService::merchantPay($openid, $order_id, (string)$extractNumber, '提现佣金到零钱');
  247. }
  248. if (!$res) {
  249. throw new ApiException(400658);
  250. }
  251. }
  252. if (sys_config('alipay_extract_type', 0) && $userExtract['extract_type'] == 'alipay') {
  253. // 构造支付宝提现参数
  254. $alipaySignType = sys_config('alipay_sign_type');
  255. if ($alipaySignType == 0) {
  256. $bizParams = [
  257. 'payee_type' => 'ALIPAY_LOGONID', // 收款方账户类型,ALIPAY_LOGONID-支付宝登录账号
  258. 'payee_account' => $userExtract['real_name'], // 收款方账户,实名认证的支付宝账号
  259. 'amount' => $extractNumber, // 提现金额
  260. 'payer_show_name' => sys_config('site_name'), // 付款方姓名/个人名称
  261. 'payee_real_name' => $userExtract['user_name'], // 收款方真实姓名/个人名称
  262. 'remark' => '提现 ¥' . $extractNumber . ' 到支付宝', // 业务备注
  263. ];
  264. } else {
  265. $bizParams = [
  266. 'out_biz_no' => $order_id, // 商户订单号
  267. 'trans_amount' => $extractNumber,
  268. 'biz_scene' => 'DIRECT_TRANSFER',
  269. 'product_code' => 'TRANS_ACCOUNT_NO_PWD',
  270. 'order_title' => sys_config('site_name') . '提现',
  271. 'payee_info' => [
  272. 'identity' => $userExtract['alipay_code'],
  273. 'identity_type' => 'ALIPAY_LOGON_ID',
  274. 'name' => $userExtract['user_name'],
  275. ],
  276. 'remark' => '提现 ¥' . $extractNumber . ' 到支付宝', // 业务备注
  277. ];
  278. }
  279. // 调用支付宝服务发起支付宝提现请求
  280. $res = AliPayService::instance()->merchantPay($bizParams, $alipaySignType);
  281. // 如果支付宝提现请求失败,则抛出异常
  282. if (!$res) {
  283. throw new ApiException('提现失败,请稍查看日志!');
  284. }
  285. }
  286. /** @var UserServices $userService */
  287. $userService = app()->make(UserServices::class);
  288. $user = $userService->getUserInfo($userExtract['uid']);
  289. $insertData['nickname'] = $user['nickname'];
  290. $insertData['phone'] = $user['phone'];
  291. /** @var CapitalFlowServices $capitalFlowServices */
  292. $capitalFlowServices = app()->make(CapitalFlowServices::class);
  293. $capitalFlowServices->setFlow([
  294. 'order_id' => $order_id,
  295. 'uid' => $userExtract['uid'],
  296. 'price' => bcmul('-1', $extractNumber, 2),
  297. 'pay_type' => $userExtract['extract_type'],
  298. 'nickname' => $insertData['nickname'],
  299. 'phone' => $insertData['phone']
  300. ], 'extract');
  301. if (!$this->dao->update($id, ['status' => 1])) {
  302. throw new AdminException(100007);
  303. }
  304. event('NoticeListener', [['uid' => $userExtract['uid'], 'userType' => strtolower($userType), 'extractNumber' => $extractNumber, 'nickname' => $nickname], 'user_extract']);
  305. //自定义通知-用户提现成功
  306. $userExtract['nickname'] = $nickname;
  307. $userExtract['phone'] = $phone;
  308. $userExtract['time'] = date('Y-m-d H:i:s');
  309. $userExtract['price'] = $extractNumber;
  310. event('CustomNoticeListener', [$userExtract['uid'], $userExtract, 'extract_success']);
  311. //自定义事件-用户提现成功
  312. event('CustomEventListener', ['admin_extract_success', [
  313. 'uid' => $userExtract['uid'],
  314. 'price' => $extractNumber,
  315. 'pay_type' => $userExtract['extract_type'],
  316. 'nickname' => $insertData['nickname'],
  317. 'phone' => $phone,
  318. 'success_time' => date('Y-m-d H:i:s')
  319. ]]);
  320. return true;
  321. }
  322. /**
  323. * 显示资源列表
  324. * @param array $where
  325. * @return array
  326. * @throws \think\db\exception\DataNotFoundException
  327. * @throws \think\db\exception\DbException
  328. * @throws \think\db\exception\ModelNotFoundException
  329. */
  330. public function index(array $where)
  331. {
  332. $list = $this->getUserExtractList($where);
  333. /** @var UserServices $userServices */
  334. $userServices = app()->make(UserServices::class);
  335. //待提现金额
  336. $where['status'] = 0;
  337. $extract_statistics['price'] = $this->getExtractSum($where);
  338. //已提现金额
  339. $where['status'] = 1;
  340. $extract_statistics['priced'] = $this->getExtractSum($where);
  341. /** @var UserBrokerageServices $userBrokerageServices */
  342. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  343. $where['pm'] = 1;
  344. $brokerage_count = $userBrokerageServices->getUsersBokerageSum($where);
  345. $refund_brokerage = $userBrokerageServices->sum(['type' => 'refund'], 'number');
  346. $extract_statistics['brokerage_count'] = bcsub((string)$brokerage_count, (string)$refund_brokerage, 2);
  347. //未提现金额
  348. $extract_statistics['brokerage_not'] = $extract_statistics['brokerage_count'] > $extract_statistics['priced'] ? bcsub((string)$extract_statistics['brokerage_count'], (string)$extract_statistics['priced'], 2) : 0.00;
  349. return compact('extract_statistics', 'list');
  350. }
  351. /**
  352. * 显示编辑资源表单页.
  353. *
  354. * @param int $id
  355. * @return \think\Response
  356. */
  357. public function edit(int $id)
  358. {
  359. $UserExtract = $this->getExtract($id);
  360. if (!$UserExtract) {
  361. throw new AdminException(100026);
  362. }
  363. $f = array();
  364. $f[] = Form::input('real_name', '姓名', $UserExtract['real_name']);
  365. $f[] = Form::number('extract_price', '提现金额', (float)$UserExtract['extract_price'])->precision(2)->disabled(true);
  366. if ($UserExtract['extract_type'] == 'alipay') {
  367. $f[] = Form::input('alipay_code', '支付宝账号', $UserExtract['alipay_code']);
  368. } else if ($UserExtract['extract_type'] == 'weixin') {
  369. $f[] = Form::input('wechat', '微信号', $UserExtract['wechat']);
  370. } else if ($UserExtract['extract_type'] == 'balance') {
  371. } else {
  372. $f[] = Form::input('bank_code', '银行卡号', $UserExtract['bank_code']);
  373. $f[] = Form::input('bank_address', '开户行', $UserExtract['bank_address']);
  374. }
  375. $f[] = Form::input('mark', '备注', $UserExtract['mark'])->type('textarea');
  376. return create_form('编辑', $f, Url::buildUrl('/finance/extract/' . $id), 'PUT');
  377. }
  378. public function update(int $id, array $data)
  379. {
  380. if (!$this->dao->update($id, $data))
  381. throw new AdminException(100007);
  382. else
  383. return true;
  384. }
  385. /**
  386. * 拒绝
  387. * @param $id
  388. * @return mixed
  389. */
  390. public function refuse(int $id, string $message)
  391. {
  392. $extract = $this->getExtract($id);
  393. if (!$extract) {
  394. throw new AdminException(100026);
  395. }
  396. if ($extract->status == 1) {
  397. throw new AdminException(400659);
  398. }
  399. if ($extract->status == -1) {
  400. throw new AdminException(400660);
  401. }
  402. $res = $this->changeFail($id, $extract, $message);
  403. if ($res) {
  404. return true;
  405. } else {
  406. throw new AdminException(100005);
  407. }
  408. }
  409. /**
  410. * 通过
  411. * @param int $id
  412. * @return mixed
  413. * @throws \think\db\exception\DataNotFoundException
  414. * @throws \think\db\exception\ModelNotFoundException
  415. */
  416. public function adopt(int $id)
  417. {
  418. $extract = $this->getExtract($id);
  419. if (!$extract) {
  420. throw new AdminException(100026);
  421. }
  422. if ($extract->status == 1) {
  423. throw new AdminException(400659);
  424. }
  425. if ($extract->status == -1) {
  426. throw new AdminException(400660);
  427. }
  428. $res = $this->changeSuccess($id, $extract);
  429. if ($res) {
  430. return $res;
  431. } else {
  432. throw new AdminException(100005);
  433. }
  434. }
  435. /**待提现的数量
  436. * @return int
  437. */
  438. public function userExtractCount()
  439. {
  440. return $this->dao->count(['status' => 0]);
  441. }
  442. /**
  443. * 银行卡提现
  444. * @param int $uid
  445. * @return mixed
  446. */
  447. public function bank(int $uid)
  448. {
  449. /** @var UserServices $userService */
  450. $userService = app()->make(UserServices::class);
  451. $user = $userService->getUserInfo($uid, 'brokerage_price,uid');
  452. if (!$user) {
  453. throw new ApiException(100026);
  454. }
  455. /** @var UserBrokerageServices $services */
  456. $services = app()->make(UserBrokerageServices::class);
  457. $data['broken_commission'] = $services->getUserFrozenPrice($uid);
  458. if ($data['broken_commission'] < 0)
  459. $data['broken_commission'] = '0';
  460. $data['brokerage_price'] = $user['brokerage_price'];
  461. //可提现佣金
  462. $data['commissionCount'] = bcsub((string)$data['brokerage_price'], (string)$data['broken_commission'], 2);
  463. $extractBank = sys_config('user_extract_bank') ?? []; //提现银行
  464. $extractBank = str_replace("\r\n", "\n", $extractBank);//防止不兼容
  465. $data['extractBank'] = explode("\n", is_array($extractBank) ? ($extractBank[0] ?? $extractBank) : $extractBank);
  466. $data['minPrice'] = sys_config('user_extract_min_price');//提现最低金额
  467. $data['weixinExtractType'] = (int)sys_config('weixin_extract_type', 0);//微信到账方式
  468. $data['alipayExtractType'] = (int)sys_config('alipay_extract_type', 0);//支付宝到账方式
  469. $data['withdrawal_fee'] = sys_config('withdrawal_fee', 0);//提现手续费
  470. return $data;
  471. }
  472. /**
  473. * 提现申请
  474. * @param int $uid
  475. * @param array $data
  476. */
  477. public function cash(int $uid, array $data)
  478. {
  479. /** @var UserServices $userService */
  480. $userService = app()->make(UserServices::class);
  481. $user = $userService->getUserInfo($uid);
  482. if (!$user) {
  483. throw new ApiException(100026);
  484. }
  485. if ($data['extract_type'] == 'weixin' && !sys_config('weixin_extract_type', 0) && !$data['weixin']) {
  486. throw new ApiException(400110);
  487. }
  488. if ($data['extract_type'] == 'weixin' && bccomp($data['money'], '0.1', 2) < 0) {
  489. throw new ApiException('微信提现最低不能小于0.1元');
  490. }
  491. /** @var WechatUserServices $wechatServices */
  492. $wechatServices = app()->make(WechatUserServices::class);
  493. $openid = $wechatServices->uidToOpenid($uid, 'wechat');
  494. if (!$openid) $openid = $wechatServices->uidToOpenid($uid, 'routine');
  495. if ($data['extract_type'] == 'weixin' && sys_config('weixin_extract_type', 0) && !$openid) {
  496. throw new ApiException(410024);
  497. }
  498. /** @var UserBrokerageServices $services */
  499. $services = app()->make(UserBrokerageServices::class);
  500. $data['broken_commission'] = $services->getUserFrozenPrice($uid);
  501. if ($data['broken_commission'] < 0)
  502. $data['broken_commission'] = 0;
  503. $data['brokerage_price'] = $user['brokerage_price'];
  504. //可提现佣金
  505. $commissionCount = bcsub((string)$data['brokerage_price'], (string)$data['broken_commission'], 2);
  506. if ($data['money'] > $commissionCount) {
  507. throw new ApiException(400661);
  508. }
  509. $extractPrice = $user['brokerage_price'];
  510. $userExtractMinPrice = sys_config('user_extract_min_price');
  511. if ($data['money'] < $userExtractMinPrice) {
  512. throw new ApiException(400662, ['money' => $userExtractMinPrice]);
  513. }
  514. if ($extractPrice < 0) {
  515. throw new ApiException(400663, ['money' => $data['money']]);
  516. }
  517. if ($data['money'] > $extractPrice) {
  518. throw new ApiException(400663, ['money' => $data['money']]);
  519. }
  520. if ($data['money'] <= 0) {
  521. throw new ApiException(400664);
  522. }
  523. $data['extract_price'] = bcmul($data['money'], '1', 2);
  524. $insertData = [
  525. 'wechat_order_id' => app()->make(StoreOrderCreateServices::class)->getNewOrderId('tx'),
  526. 'uid' => $user['uid'],
  527. 'extract_type' => $data['extract_type'],
  528. 'extract_price' => $data['extract_price'],
  529. 'extract_fee' => bcmul((string)$data['extract_price'], bcdiv((string)sys_config('withdrawal_fee', '0'), '100', 4), 2),
  530. 'add_time' => time(),
  531. 'balance' => $user['brokerage_price'],
  532. 'status' => 0,
  533. 'channel_type' => $data['channel_type']
  534. ];
  535. if (isset($data['name']) && strlen(trim($data['name']))) $insertData['real_name'] = $data['name'];
  536. else $insertData['real_name'] = $user['nickname'];
  537. if (isset($data['cardnum'])) $insertData['bank_code'] = $data['cardnum'];
  538. else $insertData['bank_code'] = '';
  539. if (isset($data['bankname'])) $insertData['bank_address'] = $data['bankname'];
  540. else $insertData['bank_address'] = '';
  541. if (isset($data['weixin'])) $insertData['wechat'] = $data['weixin'];
  542. else $insertData['wechat'] = $user['nickname'];
  543. $mark = '';
  544. $feeMark = sys_config('withdrawal_fee', 0) == 0 ? '' : ',手续费' . $insertData['extract_fee'] . '元';
  545. if ($data['extract_type'] == 'alipay') {
  546. $insertData['alipay_code'] = $data['alipay_code'];
  547. $insertData['qrcode_url'] = $data['qrcode_url'];
  548. $insertData['user_name'] = $data['user_name'];
  549. $insertData['real_name'] = $data['user_name'];
  550. $mark = '使用支付宝提现' . $insertData['extract_price'] . '元' . $feeMark;
  551. } else if ($data['extract_type'] == 'bank') {
  552. $mark = '使用银联卡' . $insertData['bank_code'] . '提现' . $insertData['extract_price'] . '元' . $feeMark;
  553. } else if ($data['extract_type'] == 'weixin') {
  554. $insertData['user_name'] = $data['user_name'];
  555. $insertData['real_name'] = $data['user_name'];
  556. $insertData['qrcode_url'] = $data['qrcode_url'];
  557. $mark = '使用微信提现' . $insertData['extract_price'] . '元' . $feeMark;
  558. if (sys_config('weixin_extract_type', 0) && $openid) {
  559. if ($data['extract_price'] < 0.1) {
  560. throw new ApiException(400665);
  561. }
  562. }
  563. }
  564. $res1 = $this->transaction(function () use ($insertData, $data, $uid, $userService, $user, $mark) {
  565. if (!$res1 = $this->dao->save($insertData)) {
  566. throw new ApiException(410121);
  567. }
  568. $balance = bcsub((string)$user['brokerage_price'], $data['extract_price'], 2) ?? 0;
  569. if (!$userService->update($uid, ['brokerage_price' => $balance], 'uid')) {
  570. throw new ApiException(410121);
  571. }
  572. //保存佣金记录
  573. /** @var UserBrokerageServices $userBrokerageServices */
  574. $userBrokerageServices = app()->make(UserBrokerageServices::class);
  575. $userBrokerageServices->income('extract', $uid, ['mark' => $mark, 'number' => $data['extract_price']], $balance, $res1['id']);
  576. return $res1;
  577. });
  578. try {
  579. ChannelService::instance()->send('WITHDRAW', ['id' => $res1->id]);
  580. } catch (\Exception $e) {
  581. }
  582. /** @var SystemAdminServices $systemAdmin */
  583. $systemAdmin = app()->make(SystemAdminServices::class);
  584. $systemAdmin->adminNewPush();
  585. //消息
  586. event('NoticeListener', [['nickname' => $user['nickname'], 'money' => $data['extract_price']], 'kefu_send_extract_application']);
  587. //自定义事件-用户提现
  588. event('CustomEventListener', ['user_extract', [
  589. 'uid' => $insertData['uid'],
  590. 'phone' => $user['phone'],
  591. 'extract_type' => $insertData['extract_type'],
  592. 'extract_price' => $insertData['extract_price'],
  593. 'extract_fee' => $insertData['extract_fee'],
  594. 'extract_time' => date('Y-m-d H:i:s'),
  595. ]]);
  596. return true;
  597. }
  598. /**
  599. * @param array $where
  600. * @param string $SumField
  601. * @param string $selectType
  602. * @param string $group
  603. * @return float|mixed
  604. */
  605. public function getOutMoneyByWhere(array $where, string $SumField, string $selectType, string $group = "")
  606. {
  607. switch ($selectType) {
  608. case "sum" :
  609. return $this->dao->getWhereSumField($where, $SumField);
  610. case "group" :
  611. return $this->dao->getGroupField($where, $SumField, $group);
  612. }
  613. }
  614. }