FenxiaoWithdraw.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\fenxiao\model;
  11. use app\model\BaseModel;
  12. use app\model\member\Member;
  13. use app\model\member\MemberAccount;
  14. use app\model\message\Message;
  15. use app\model\message\Sms;
  16. use app\model\shop\ShopAcceptMessage;
  17. use addon\wechat\model\Message as WechatMessage;
  18. use app\model\member\Member as MemberModel;
  19. use addon\weapp\model\Message as WeappMessage;
  20. use app\model\system\Pay;
  21. use think\facade\Cache;
  22. use think\facade\Db;
  23. /**
  24. * 分销商提现
  25. */
  26. class FenxiaoWithdraw extends BaseModel
  27. {
  28. //提现类型
  29. public $withdraw_type = [
  30. 'balance' => '余额',
  31. 'weixin' => '微信',
  32. 'alipay' => '支付宝',
  33. 'bank' => '银行卡',
  34. ];
  35. public $status = array (
  36. 1 => '待审核',
  37. 2 => '待转账',
  38. 3 => '已转账',
  39. -1 => '已拒绝'
  40. );
  41. public function getTransferType($site_id)
  42. {
  43. $pay_model = new Pay();
  44. $transfer_type_list = $pay_model->getTransferType($site_id);
  45. $transfer_type_list[ 'balance' ] = '余额';
  46. return $transfer_type_list;
  47. }
  48. /**
  49. * 分销商申请提现
  50. * @param $data
  51. * @return array
  52. */
  53. public function addFenxiaoWithdraw($data)
  54. {
  55. //获取分销商信息
  56. $fenxiao_model = new Fenxiao();
  57. $fenxiao_info = $fenxiao_model->getFenxiaoInfo([ [ 'member_id', '=', $data[ 'member_id' ] ] ], 'fenxiao_id,fenxiao_name,account');
  58. if (empty($fenxiao_info[ 'data' ])) {
  59. return $this->error('该分销商不存在');
  60. }
  61. if ($fenxiao_info[ 'data' ][ 'account' ] < $data[ 'money' ]) {
  62. return $this->error('', '提现金额大于可提现金额');
  63. }
  64. //获取提现配置信息
  65. $config_model = new Config();
  66. $config = $config_model->getFenxiaoWithdrawConfig($data[ 'site_id' ]);
  67. $config_info = $config[ 'data' ][ 'value' ];
  68. if ($config_info[ 'withdraw' ] > $data[ 'money' ]) {
  69. return $this->error('', '提现金额小于最低提现金额');
  70. }
  71. if ($data[ 'money' ] >= $config_info[ 'min_no_fee' ] && $data[ 'money' ] <= $config_info[ 'max_no_fee' ]) {
  72. $data[ 'withdraw_rate' ] = 0;
  73. $data[ 'withdraw_rate_money' ] = 0;
  74. $data[ 'real_money' ] = $data[ 'money' ];
  75. } else {
  76. $data[ 'withdraw_rate' ] = $config_info[ 'withdraw_rate' ];
  77. if ($config_info[ 'withdraw_rate' ] == 0) {
  78. $data[ 'withdraw_rate' ] = 0;
  79. $data[ 'withdraw_rate_money' ] = 0;
  80. $data[ 'real_money' ] = $data[ 'money' ];
  81. } else {
  82. $data[ 'withdraw_rate' ] = $config_info[ 'withdraw_rate' ];
  83. $data[ 'withdraw_rate_money' ] = round($data[ 'money' ] * $config_info[ 'withdraw_rate' ] / 100, 2);
  84. $data[ 'real_money' ] = $data[ 'money' ] - $data[ 'withdraw_rate_money' ];
  85. }
  86. }
  87. $data[ 'withdraw_no' ] = date('YmdHis') . rand(1000, 9999);
  88. $data[ 'create_time' ] = time();
  89. model('fenxiao_withdraw')->startTrans();
  90. try {
  91. $data[ 'fenxiao_id' ] = $fenxiao_info[ 'data' ][ 'fenxiao_id' ];
  92. $data[ 'fenxiao_name' ] = $fenxiao_info[ 'data' ][ 'fenxiao_name' ];
  93. $res = model('fenxiao_withdraw')->add($data);
  94. //修改分销商提现中金额
  95. model('fenxiao')->setInc([ [ 'member_id', '=', $data[ 'member_id' ] ] ], 'account_withdraw_apply', $data[ 'money' ]);
  96. //修改分销商可提现金额
  97. model('fenxiao')->setDec([ [ 'member_id', '=', $data[ 'member_id' ] ] ], 'account', $data[ 'money' ]);
  98. //判断是否需要审核
  99. if ($config_info[ 'withdraw_status' ] == 2) {//不需要
  100. $result = $this->withdrawPass($res, $data[ 'site_id' ]);
  101. if ($result[ 'code' ] < 0) {
  102. model('fenxiao_withdraw')->rollback();
  103. return $result;
  104. }
  105. }
  106. model('fenxiao_withdraw')->commit();
  107. //申请提现发送消息
  108. $data[ 'keywords' ] = 'FENXIAO_WITHDRAWAL_APPLY';
  109. $message_model = new Message();
  110. $message_model->sendMessage($data);
  111. return $this->success($res);
  112. } catch (\Exception $e) {
  113. model('fenxiao_withdraw')->rollback();
  114. return $this->error('', $e->getMessage());
  115. }
  116. }
  117. /**
  118. * 提现审核通过
  119. * @param $id
  120. * @param $site_id
  121. * @return array
  122. */
  123. public function withdrawPass($ids, $site_id)
  124. {
  125. model('fenxiao_withdraw')->startTrans();
  126. try {
  127. $withdraw_list = $this->getFenxiaoWithdrawList([ [ 'id', 'in', $ids ] ], '*');
  128. foreach ($withdraw_list[ 'data' ] as $k => $v) {
  129. if ($v[ 'status' ] == 1) {
  130. $this->agree([ 'id' => $v[ 'id' ], 'site_id' => $site_id ]);
  131. }
  132. }
  133. model('fenxiao_withdraw')->commit();
  134. return $this->success();
  135. } catch (\Exception $e) {
  136. model('fenxiao_withdraw')->rollback();
  137. return $this->error('', $e->getMessage());
  138. }
  139. }
  140. /**
  141. * 获取提现详情
  142. * @param array $condition
  143. * @return array
  144. */
  145. public function getFenxiaoWithdrawInfo($condition = [], $field = '*')
  146. {
  147. $res = model('fenxiao_withdraw')->getInfo($condition, $field);
  148. return $this->success($res);
  149. }
  150. /**
  151. * 提现详情
  152. * @param $params
  153. * @return array
  154. */
  155. public function getFenxiaoWithdrawDetail($params)
  156. {
  157. $id = $params[ 'id' ];
  158. $site_id = $params[ 'site_id' ] ?? 0;
  159. $member_id = $params[ 'member_id' ] ?? 0;
  160. $condition = array (
  161. [ 'id', '=', $id ]
  162. );
  163. if ($site_id > 0) {
  164. $condition[] = [ 'site_id', '=', $site_id ];
  165. }
  166. if ($member_id > 0) {
  167. $condition[] = [ 'member_id', '=', $member_id ];
  168. }
  169. $info = model('fenxiao_withdraw')->getInfo($condition, '*');
  170. if (!empty($info)) {
  171. $info = $this->tran($info);
  172. }
  173. return $this->success($info);
  174. }
  175. public function tran($data)
  176. {
  177. $status = $data[ 'status' ] ?? 0;
  178. if ($status != 0) {
  179. $data[ 'status_name' ] = $this->status[ $status ] ?? '';
  180. }
  181. return $data;
  182. }
  183. /**
  184. * 获取分销列表
  185. * @param array $condition
  186. * @param string $field
  187. * @param string $order
  188. * @param string $limit
  189. */
  190. public function getFenxiaoWithdrawList($condition = [], $field = '*', $order = '', $limit = null)
  191. {
  192. $list = model('fenxiao_withdraw')->getList($condition, $field, $order, '', '', '', $limit);
  193. return $this->success($list);
  194. }
  195. /**
  196. * 获取分销提现分页列表
  197. * @param array $condition
  198. * @param number $page
  199. * @param string $page_size
  200. * @param string $order
  201. * @param string $field
  202. */
  203. public function getFenxiaoWithdrawPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*', $alias = '', $join = [])
  204. {
  205. $list = model('fenxiao_withdraw')->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
  206. return $this->success($list);
  207. }
  208. /**
  209. * 分销佣金发放通知
  210. * @param $data
  211. */
  212. public function messageOrderCommissionGrant($data)
  213. {
  214. //发送短信
  215. $sms_model = new Sms();
  216. // 分销订单
  217. $fenxiao_order_model = new FenxiaoOrder();
  218. $fenxiao_order = $fenxiao_order_model->getFenxiaoOrderInfo([ [ 'fenxiao_order_id', '=', $data[ 'order_id' ] ] ]);
  219. $fenxiao_order_info = $fenxiao_order[ 'data' ];
  220. $commission = $fenxiao_order_info[ $data[ 'level' ] . '_commission' ];
  221. $fenxiao_id = $fenxiao_order_info[ $data[ 'level' ] . '_fenxiao_id' ];
  222. $fenxiao_model = new Fenxiao();
  223. $fenxiao_info = $fenxiao_model->getFenxiaoInfo([ [ 'fenxiao_id', '=', $fenxiao_id ] ], 'fenxiao_id, member_id')[ 'data' ];
  224. $member_model = new MemberModel();
  225. $member_info_result = $member_model->getMemberInfo([ [ 'member_id', '=', $fenxiao_info[ 'member_id' ] ?? 0 ] ]);
  226. $member_info = $member_info_result[ 'data' ];
  227. //绑定微信公众号才发送
  228. if (!empty($member_info) && !empty($member_info[ 'wx_openid' ])) {
  229. $wechat_model = new WechatMessage();
  230. $data[ 'openid' ] = $member_info[ 'wx_openid' ];
  231. $data[ 'template_data' ] = [
  232. 'keyword1' => $commission,
  233. 'keyword2' => time_to_date($fenxiao_order_info[ 'create_time' ]),
  234. ];
  235. $data[ 'page' ] = '';
  236. $wechat_model->sendMessage($data);
  237. }
  238. //发送订阅消息
  239. if (!empty($member_info) && !empty($member_info[ 'weapp_openid' ])) {
  240. $weapp_model = new WeappMessage();
  241. $data[ 'openid' ] = $member_info[ 'weapp_openid' ];
  242. $data[ 'template_data' ] = [
  243. 'amount1' => [
  244. 'value' => $fenxiao_order_info[ 'real_goods_money' ]
  245. ],
  246. 'amount2' => [
  247. 'value' => $commission
  248. ],
  249. 'thing3' => [
  250. 'value' => $fenxiao_order_info[ 'sku_name' ]
  251. ],
  252. 'time4' => [
  253. 'value' => time_to_date($fenxiao_order_info[ 'create_time' ]),
  254. ],
  255. ];
  256. $data[ 'page' ] = '';
  257. $weapp_model->sendMessage($data);
  258. }
  259. $var_parse = [
  260. 'sitename' => replaceSpecialChar($data[ 'site_info' ][ 'site_name' ]),
  261. 'level' => $data[ 'level' ],
  262. 'username' => empty(replaceSpecialChar($fenxiao_order_info[ 'member_name' ])) ? $fenxiao_order_info[ 'member_mobile' ] : replaceSpecialChar($fenxiao_order_info[ 'member_name' ]),
  263. ];
  264. $data[ 'sms_account' ] = $member_info[ 'mobile' ];
  265. $data[ 'var_parse' ] = $var_parse;
  266. $sms_model->sendMessage($data);
  267. }
  268. /**
  269. * 分销提现成功通知
  270. * @param $data
  271. */
  272. public function messageFenxiaoWithdrawalSuccess($data)
  273. {
  274. //发送短信
  275. $sms_model = new Sms();
  276. $var_parse = array (
  277. 'fenxiaoname' => $data[ 'fenxiao_name' ],//会员名
  278. 'money' => $data[ 'money' ]
  279. );
  280. $data[ 'sms_account' ] = $data[ 'mobile' ];//手机号
  281. $data[ 'var_parse' ] = $var_parse;
  282. $sms_model->sendMessage($data);
  283. $member_model = new MemberModel();
  284. $member_info_result = $member_model->getMemberInfo([ [ 'member_id', '=', $data[ 'member_id' ] ] ]);
  285. $member_info = $member_info_result[ 'data' ];
  286. //绑定微信公众号才发送
  287. if (!empty($member_info) && !empty($member_info[ 'wx_openid' ])) {
  288. $wechat_model = new WechatMessage();
  289. $data[ 'openid' ] = $member_info[ 'wx_openid' ];
  290. $data[ 'template_data' ] = [
  291. 'keyword1' => $data[ 'money' ],
  292. 'keyword2' => time_to_date($data[ 'payment_time' ]),
  293. ];
  294. $data[ 'page' ] = '';
  295. $wechat_model->sendMessage($data);
  296. }
  297. //发送订阅消息
  298. if (!empty($member_info) && !empty($member_info[ 'weapp_openid' ])) {
  299. $weapp_model = new WeappMessage();
  300. $data[ 'openid' ] = $member_info[ 'weapp_openid' ];
  301. $data[ 'template_data' ] = [
  302. 'amount1' => [
  303. 'value' => $data[ 'money' ]
  304. ],
  305. 'time2' => [
  306. 'value' => time_to_date(time())
  307. ],
  308. 'thing3' => [
  309. 'value' => '提现成功'
  310. ]
  311. ];
  312. $data[ 'page' ] = '';
  313. $weapp_model->sendMessage($data);
  314. }
  315. }
  316. /**
  317. * 分销提现失败通知
  318. * @param $data
  319. */
  320. public function messageFenxiaoWithdrawalError($data)
  321. {
  322. //发送短信
  323. $sms_model = new Sms();
  324. $member_model = new MemberModel();
  325. $member_info_result = $member_model->getMemberInfo([ [ 'member_id', '=', $data[ 'member_id' ] ] ]);
  326. $member_info = $member_info_result[ 'data' ];
  327. $var_parse = array (
  328. 'fenxiaoname' => str_replace(' ', '', $data[ 'fenxiao_name' ]),//会员名
  329. 'money' => $data[ 'money' ]
  330. );
  331. $data[ 'sms_account' ] = $member_info[ 'mobile' ];//手机号
  332. $data[ 'var_parse' ] = $var_parse;
  333. $sms_model->sendMessage($data);
  334. //绑定微信公众号才发送
  335. if (!empty($member_info) && !empty($member_info[ 'wx_openid' ])) {
  336. $wechat_model = new WechatMessage();
  337. $data[ 'openid' ] = $member_info[ 'wx_openid' ];
  338. $data[ 'template_data' ] = [
  339. 'keyword1' => time_to_date($data[ 'create_time' ]),
  340. 'keyword2' => '审核失败',
  341. 'keyword3' => '会员申请提现',
  342. 'keyword4' => $data[ 'money' ],
  343. ];
  344. $data[ 'page' ] = '';
  345. $wechat_model->sendMessage($data);
  346. }
  347. //发送订阅消息
  348. if (!empty($member_info) && !empty($member_info[ 'weapp_openid' ])) {
  349. $weapp_model = new WeappMessage();
  350. $data[ 'openid' ] = $member_info[ 'weapp_openid' ];
  351. $data[ 'template_data' ] = [
  352. 'amount2' => [
  353. 'value' => $data[ 'money' ]
  354. ],
  355. 'thing4' => [
  356. 'value' => '提现审核失败'
  357. ]
  358. ];
  359. $data[ 'page' ] = '';
  360. $weapp_model->sendMessage($data);
  361. }
  362. }
  363. /**
  364. * 分销申请提现通知,卖家通知
  365. * @param $data
  366. */
  367. public function messageFenxiaoWithdrawalApply($data)
  368. {
  369. //发送短信
  370. $sms_model = new Sms();
  371. $var_parse = array (
  372. 'fenxiaoname' => replaceSpecialChar($data[ 'fenxiao_name' ]),//会员名
  373. 'money' => $data[ 'money' ],//退款申请金额
  374. );
  375. // $site_id = $data['site_id'];
  376. // $shop_info = model('shop')->getInfo([['site_id', '=', $site_id]], 'mobile,email');
  377. // $message_data['sms_account'] = $shop_info['mobile'];//手机号
  378. $data[ 'var_parse' ] = $var_parse;
  379. $shop_accept_message_model = new ShopAcceptMessage();
  380. $result = $shop_accept_message_model->getShopAcceptMessageList();
  381. $list = $result[ 'data' ];
  382. if (!empty($list)) {
  383. foreach ($list as $v) {
  384. $message_data = $data;
  385. $message_data[ 'sms_account' ] = $v[ 'mobile' ];//手机号
  386. $sms_model->sendMessage($message_data);
  387. if ($v[ 'wx_openid' ] != '') {
  388. $wechat_model = new WechatMessage();
  389. $data[ 'openid' ] = $v[ 'wx_openid' ];
  390. $data[ 'template_data' ] = [
  391. 'keyword1' => replaceSpecialChar($data[ 'fenxiao_name' ]),
  392. 'keyword2' => time_to_date($data[ 'create_time' ]),
  393. 'keyword3' => $data[ 'money' ]
  394. ];
  395. $data[ 'page' ] = '';
  396. $wechat_model->sendMessage($data);
  397. }
  398. }
  399. }
  400. }
  401. /**
  402. * 获取提现数量
  403. * @param array $condition
  404. * @return array
  405. */
  406. public function getFenxiaoWithdrawCount($condition = [], $field = '*')
  407. {
  408. $res = model('fenxiao_withdraw')->getCount($condition, $field);
  409. return $this->success($res);
  410. }
  411. public function apply($data, $site_id = 0, $app_module = 'shop')
  412. {
  413. $config_model = new Config();
  414. $config = $config_model->getFenxiaoWithdrawConfig($site_id)[ 'data' ][ 'value' ] ?? [];
  415. $withdraw_no = $this->createWithdrawNo();
  416. $apply_money = round($data[ 'apply_money' ], 2);
  417. $withdraw_min_money = $config[ 'withdraw' ];
  418. $withdraw_max_money = $config[ 'max' ];
  419. if ($apply_money < $withdraw_min_money) return $this->error([], '申请提现金额不能小于最低提现额度' . $withdraw_min_money);
  420. if ($apply_money > $withdraw_max_money) return $this->error([], '申请提现金额不能大于最高提现额度' . $withdraw_max_money);
  421. $member_id = $data[ 'member_id' ];
  422. $member_model = new Member();
  423. $member_condition = array (
  424. [ 'member_id', '=', $member_id ]
  425. );
  426. $member_info = $member_model->getMemberInfo($member_condition, 'balance_money,headimg,wx_openid,username,mobile,weapp_openid,nickname')[ 'data' ] ?? [];
  427. if (empty($member_info))
  428. return $this->error([], 'MEMBER_NOT_EXIST');
  429. $fenxiao_model = new Fenxiao();
  430. $fenxiao_info = $fenxiao_model->getFenxiaoInfo($member_condition, 'fenxiao_id,fenxiao_name,account')[ 'data' ] ?? [];
  431. if (empty($fenxiao_info)) {
  432. return $this->error('该分销商不存在');
  433. }
  434. $fenxiao_account = $fenxiao_info[ 'account' ];//会员的分销佣金
  435. if ($fenxiao_account < $apply_money) {
  436. return $this->error('', '提现金额大于可提现金额');
  437. }
  438. $transfer_type = $data[ 'transfer_type' ];
  439. $transfer_type_list = $this->getTransferType($site_id);
  440. $transfer_type_name = $transfer_type_list[ $transfer_type ] ?? '';
  441. if (empty($transfer_type_name))
  442. return $this->error([], '不支持的提现方式');
  443. model('fenxiao_withdraw')->startTrans();
  444. try {
  445. $withdraw_rate = $config[ 'withdraw_rate' ];
  446. $bank_name = '';
  447. $account_number = '';
  448. $applet_type = 0;
  449. switch ( $transfer_type ) {
  450. case 'bank':
  451. $bank_name = $data[ 'bank_name' ];
  452. $account_number = $data[ 'account_number' ];
  453. break;
  454. case 'alipay':
  455. $bank_name = '';
  456. $account_number = $data[ 'account_number' ];
  457. break;
  458. case 'wechatpay':
  459. $bank_name = '';
  460. if (empty($member_info[ 'wx_openid' ]) && empty($member_info[ 'weapp_openid' ])) {
  461. return $this->error('', '请绑定微信或更换提现账户');
  462. }
  463. if (!empty($member_info[ 'wx_openid' ])) {
  464. $account_number = $member_info[ 'wx_openid' ];
  465. $applet_type = 0; // 公众号
  466. } else {
  467. $account_number = $member_info[ 'weapp_openid' ];
  468. $applet_type = 1; // 小程序
  469. }
  470. break;
  471. }
  472. if ($transfer_type == 'balance') {
  473. $withdraw_rate = 0;
  474. }
  475. $service_money = round($apply_money * $withdraw_rate / 100, 2);//手续费
  476. $real_money = $apply_money - $service_money;
  477. $data = array (
  478. 'site_id' => $site_id,
  479. 'withdraw_no' => $withdraw_no,
  480. 'member_id' => $member_id,
  481. 'fenxiao_id' => $fenxiao_info[ 'fenxiao_id' ],
  482. 'fenxiao_name' => $fenxiao_info[ 'fenxiao_name' ],
  483. 'transfer_type' => $transfer_type,
  484. 'transfer_name' => $transfer_type_name,
  485. 'money' => $apply_money,
  486. 'withdraw_rate_money' => $service_money,
  487. 'withdraw_rate' => $withdraw_rate,
  488. 'real_money' => $real_money,
  489. 'create_time' => time(),
  490. 'status' => 1,
  491. 'member_headimg' => $member_info[ 'headimg' ],
  492. 'realname' => $data[ 'realname' ],
  493. 'bank_name' => $bank_name,
  494. 'account_number' => $account_number,
  495. 'mobile' => $data[ 'mobile' ],
  496. 'applet_type' => $applet_type
  497. );
  498. $result = model('fenxiao_withdraw')->add($data);
  499. //修改分销商提现中金额
  500. model('fenxiao')->setInc($member_condition, 'account_withdraw_apply', $apply_money);
  501. //修改分销商可提现金额
  502. model('fenxiao')->setDec($member_condition, 'account', $apply_money);
  503. //申请提现发送消息
  504. //申请提现发送消息
  505. $data[ 'keywords' ] = 'FENXIAO_WITHDRAWAL_APPLY';
  506. $message_model = new Message();
  507. $message_model->sendMessage($data);
  508. //判断是否需要审核
  509. if ($config[ 'withdraw_status' ] == 2) {//不需要
  510. $result = $this->agree([ 'id' => $result, 'site_id' => $site_id ]);
  511. if ($result[ 'code' ] < 0) {
  512. model('fenxiao_withdraw')->rollback();
  513. return $result;
  514. }
  515. }
  516. model('fenxiao_withdraw')->commit();
  517. return $this->success();
  518. } catch (\Exception $e) {
  519. model('fenxiao_withdraw')->rollback();
  520. return $this->error('', $e->getMessage());
  521. }
  522. }
  523. /**
  524. * 提现流水号
  525. */
  526. private function createWithdrawNo()
  527. {
  528. $cache = Cache::get('member_withdraw_no' . time());
  529. if (empty($cache)) {
  530. Cache::set('niutk' . time(), 1000);
  531. $cache = Cache::get('member_withdraw_no' . time());
  532. } else {
  533. $cache = $cache + 1;
  534. Cache::set('member_withdraw_no' . time(), $cache);
  535. }
  536. $no = date('Ymdhis', time()) . rand(1000, 9999) . $cache;
  537. return $no;
  538. }
  539. public function agree($params)
  540. {
  541. $id = $params[ 'id' ];
  542. $site_id = $params[ 'site_id' ];
  543. if (empty($site_id)) {
  544. return $this->error(-1, '参数错误');
  545. }
  546. $condition = array (
  547. [ 'id', '=', $id ],
  548. [ 'site_id', '=', $site_id ],
  549. );
  550. $info = model('fenxiao_withdraw')->getInfo($condition);
  551. if (empty($info))
  552. return $this->error();
  553. $config_model = new Config();
  554. $config = $config_model->getFenxiaoWithdrawConfig($site_id)[ 'data' ][ 'value' ] ?? [];
  555. model('fenxiao_withdraw')->startTrans();
  556. try {
  557. $data = array (
  558. 'status' => 2,
  559. // 'status_name' => '待转账',//已审核待转账
  560. 'audit_time' => time(),
  561. );
  562. $result = model('fenxiao_withdraw')->update($data, $condition);
  563. //是否启用自动转账(必须是微信或支付宝)
  564. if ($config[ 'is_auto_transfer' ] == 1) {
  565. $this->transfer([ 'id' => $id ]);
  566. }
  567. model('fenxiao_withdraw')->commit();
  568. return $this->success();
  569. } catch (\Exception $e) {
  570. model('fenxiao_withdraw')->rollback();
  571. return $this->error('', $e->getMessage() . $e->getLine());
  572. }
  573. }
  574. /**
  575. * 转账
  576. * @param $condition
  577. */
  578. public function transfer($params)
  579. {
  580. $id = $params[ 'id' ];
  581. $site_id = $params[ 'site_id' ] ?? 0;
  582. $condition = array (
  583. [ 'id', '=', $id ],
  584. );
  585. if ($site_id > 0) {
  586. $condition[] = [ 'site_id', '=', $site_id ];
  587. }
  588. $info = model('fenxiao_withdraw')->getInfo($condition);
  589. if (empty($info))
  590. return $this->error();
  591. $site_id = $info[ 'site_id' ];
  592. $transfer_type = $info[ 'transfer_type' ];
  593. $member_id = $info[ 'member_id' ];
  594. $money = $info[ 'money' ];
  595. $fenxiao_id = $info[ 'fenxiao_id' ];
  596. $fenxiao_name = $info[ 'fenxiao_name' ];
  597. $real_money = $info[ 'real_money' ];
  598. if ($transfer_type == 'balance') {
  599. //添加会员账户流水
  600. $member_account = new MemberAccount();
  601. $member_result = $member_account->addMemberAccount($site_id, $member_id, 'balance_money', $real_money, 'fenxiao', '佣金提现', '分销佣金提现');
  602. if ($member_result[ 'code' ] < 0) {
  603. return $member_result;
  604. }
  605. } else {
  606. if (!in_array($transfer_type, [ 'wechatpay', 'alipay' ]))
  607. return $this->error('', '当前提现方式不支持在线转账');
  608. $pay_data = array (
  609. 'id' => $id,
  610. 'out_trade_no' => $info[ 'withdraw_no' ],
  611. 'real_name' => $info[ 'realname' ],
  612. 'amount' => $info[ 'real_money' ],
  613. 'desc' => '会员提现',
  614. 'transfer_type' => $transfer_type,
  615. 'account_number' => $info[ 'account_number' ],
  616. 'site_id' => $info[ 'site_id' ],
  617. 'is_weapp' => $info[ 'applet_type' ],
  618. 'member_id' => $info[ 'member_id' ]
  619. );
  620. //调用在线转账借口
  621. $pay_result = event('PayTransfer', $pay_data, true);
  622. if (empty($pay_result)) {
  623. $pay_result = $this->error();
  624. }
  625. if ($pay_result[ 'code' ] < 0) {
  626. return $pay_result;
  627. }
  628. }
  629. $account_model = new FenxiaoAccount();
  630. $account_result = $account_model->addAccountLog($fenxiao_id, $fenxiao_name, 'withdraw', '-' . $money, $id);
  631. if ($account_result[ 'code' ] != 0) {
  632. return $account_result;
  633. }
  634. //调用完成转账
  635. $result = $this->transferFinish([ 'id' => $id, 'site_id' => $info[ 'site_id' ] ]);
  636. return $result;
  637. }
  638. /**
  639. * 提现转账完成
  640. * @param $id
  641. */
  642. public function transferFinish($param = [])
  643. {
  644. $condition = [
  645. [ 'id', '=', $param[ 'id' ] ],
  646. [ 'site_id', '=', $param[ 'site_id' ] ],
  647. [ 'status', '=', 2 ]
  648. ];
  649. $info = model('fenxiao_withdraw')->getInfo($condition);
  650. if (empty($info)) return $this->error();
  651. $site_id = $info[ 'site_id' ];
  652. $member_id = $info[ 'member_id' ];
  653. $fenxiao_id = $info[ 'fenxiao_id' ];
  654. $money = $info[ 'money' ];
  655. $payment_time = time();
  656. model('fenxiao_withdraw')->startTrans();
  657. try {
  658. $data = [
  659. 'status' => 3,
  660. 'status_name' => '已转账',
  661. 'payment_time' => $payment_time,
  662. 'document' => $param[ 'certificate' ] ?? '',
  663. 'transfer_remark' => $param[ 'certificate_remark' ] ?? ''
  664. ];
  665. $result = model('fenxiao_withdraw')->update($data, $condition);
  666. $fenxiao_condition = array (
  667. [ 'fenxiao_id', '=', $fenxiao_id ]
  668. );
  669. //修改分销商提现中金额
  670. model('fenxiao')->setDec($fenxiao_condition, 'account_withdraw_apply', $money);
  671. //修改分销商已提现金额
  672. model('fenxiao')->setInc($fenxiao_condition, 'account_withdraw', $money);
  673. model('fenxiao_withdraw')->commit();
  674. // $member_info = model('member')->getInfo([ [ 'member_id', '=', $member_id ] ], 'nickname');
  675. // $stat_model = new Stat();
  676. // $stat_model->switchStat(['type' => 'member_withdraw', 'data' => ['site_id' => $info['site_id'], 'id' => $info['id']]]);
  677. $message_model = new Message();
  678. $info[ 'keywords' ] = 'FENXIAO_WITHDRAWAL_SUCCESS';
  679. $message_model->sendMessage($info);
  680. return $this->success();
  681. } catch (\Exception $e) {
  682. model('fenxiao_withdraw')->rollback();
  683. return $this->error('', $e->getMessage());
  684. }
  685. }
  686. /**
  687. * 拒绝提现申请
  688. * @param $condition
  689. */
  690. public function refuse($params)
  691. {
  692. $id = $params[ 'id' ];
  693. $site_id = $params[ 'site_id' ];
  694. $data = [
  695. 'status' => -1,
  696. "refuse_reason" => $params[ "refuse_reason" ],
  697. "audit_time" => time(),
  698. ];
  699. $condition = array (
  700. [ 'id', '=', $id ],
  701. [ 'site_id', '=', $site_id ]
  702. );
  703. $info = model('fenxiao_withdraw')->getInfo($condition, '*');
  704. if (empty($info)) return $this->error();
  705. model('fenxiao_withdraw')->startTrans();
  706. try {
  707. $money = $info[ 'money' ];
  708. $fenxiao_id = $info[ 'fenxiao_id' ];
  709. if ($info[ 'status' ] == 1) {
  710. model('fenxiao_withdraw')->update($data, $condition);
  711. $fenxiao_condition = array (
  712. [ 'fenxiao_id', '=', $fenxiao_id ]
  713. );
  714. //修改分销商提现中金额
  715. model('fenxiao')->setDec($fenxiao_condition, 'account_withdraw_apply', $money);
  716. //修改分销商可提现金额
  717. model('fenxiao')->setInc($fenxiao_condition, 'account', $money);
  718. //提现失败发送消息
  719. $message_model = new Message();
  720. $info[ 'keywords' ] = 'FENXIAO_WITHDRAWAL_ERROR';
  721. $message_model->sendMessage($info);
  722. }
  723. model('fenxiao_withdraw')->commit();
  724. return $this->success();
  725. } catch (\Exception $e) {
  726. model('fenxiao_withdraw')->rollback();
  727. return $this->error('', $e->getMessage());
  728. }
  729. }
  730. public function exportFenxiaoWithdraw($condition, $order, $site_id)
  731. {
  732. try {
  733. $file_name = date('Y年m月d日-分销提现', time()) . '.csv';
  734. // $file_name = date('YmdHis').'.csv';//csv文件名
  735. //通过分批次执行数据导出(防止内存超出配置设置的)
  736. set_time_limit(0);
  737. ini_set('memory_limit', '256M');
  738. //设置header头
  739. header('Content-Description: File Transfer');
  740. header('Content-Type: application/vnd.ms-excel');
  741. header('Content-Disposition: attachment; filename="' . $file_name . '"');
  742. header('Expires: 0');
  743. header('Cache-Control: must-revalidate');
  744. header('Pragma: public');
  745. //打开php数据输入缓冲区
  746. $fp = fopen('php://output', 'a');
  747. // fwrite($fp, chr(0xEF).chr(0xBB).chr(0xBF)); // 添加 BOM
  748. $heade = [ '分销商', '提现方式', '申请提现金额', '提现手续费', '实际转账金额', '提现状态', '申请时间', '收款账号', '真实姓名', '手机号', '银行名称', '银行账号' ];
  749. //将数据编码转换成GBK格式
  750. mb_convert_variables('GBK', 'UTF-8', $heade);
  751. //将数据格式化为CSV格式并写入到output流中
  752. fputcsv($fp, $heade);
  753. $transfer_type_list = $this->getTransferType($site_id);
  754. $status_name = [ 1 => "待审核", 2 => "待转账", 3 => "已转账", -1 => "已拒绝", -2 => "转账失败" ];
  755. //写入第一行表头
  756. Db::name('fenxiao_withdraw')->where($condition)->order($order)->chunk(500, function($item_list) use ($fp, $transfer_type_list, $status_name) {
  757. //写入导出信息
  758. foreach ($item_list as $k => $item_v) {
  759. $temp_data = [
  760. $item_v[ 'fenxiao_name' ] . "\t",
  761. $transfer_type_list[ $item_v[ 'transfer_type' ] ] . "\t",
  762. (float) $item_v[ 'money' ] . "\t",
  763. (float) $item_v[ 'withdraw_rate_money' ] . "\t",
  764. (float) $item_v[ 'real_money' ] . "\t",
  765. $status_name[ $item_v[ 'status' ] ] . "\t",
  766. time_to_date($item_v[ 'create_time' ]) . "\t",
  767. $item_v[ 'account_number' ] . "\t",
  768. $item_v[ 'realname' ] . "\t",
  769. $item_v[ 'mobile' ] . "\t",
  770. $item_v[ 'bank_name' ] . "\t",
  771. $item_v[ 'transfer_account_no' ] . "\t",
  772. ];
  773. mb_convert_variables('GBK', 'UTF-8', $temp_data);
  774. fputcsv($fp, $temp_data);
  775. //将已经存储到csv中的变量数据销毁,释放内存
  776. unset($item_v);
  777. }
  778. unset($item_list);
  779. });
  780. //关闭句柄
  781. fclose($fp);
  782. die;
  783. } catch (\Exception $e) {
  784. return $this->error([], $e->getMessage() . $e->getFile() . $e->getLine());
  785. }
  786. }
  787. }