|
|
<?php
|
|
|
|
|
|
namespace app\distribution\service;
|
|
|
|
|
|
use app\base\model\app\AppWeixinUser;
|
|
|
use app\base\model\mpalipay\MpalipayUser;
|
|
|
use app\base\model\mpweixin\MpweixinUser;
|
|
|
use app\base\model\user\UserAlipay;
|
|
|
use app\base\model\wechat\WechatUser;
|
|
|
use app\distribution\model\DistributionCommissionLog;
|
|
|
use app\distribution\model\DistributionCommissionMy;
|
|
|
use app\distribution\model\DistributionCommissionWithdraw;
|
|
|
use think\facade\Db;
|
|
|
|
|
|
class Commission extends Base
|
|
|
{
|
|
|
|
|
|
/**
|
|
|
* 更新用户佣金并记录明细
|
|
|
* @param int $user_id 用户ID
|
|
|
* @param string $type 变动类型
|
|
|
* @param float $commission 变动数量
|
|
|
* @param string $show_text 显示文案
|
|
|
* @param string $relation_order_number 关联订单编号
|
|
|
* @param array $remark 关联信息
|
|
|
* @date 2022-11-04
|
|
|
*/
|
|
|
public function change($user_id, $type, $commission, $show_text = '', $relation_order_number = '', $remark = [])
|
|
|
{
|
|
|
$my_model = new DistributionCommissionMy();
|
|
|
$log_model = new DistributionCommissionLog();
|
|
|
|
|
|
// 变动数量不能为0
|
|
|
if (!$commission) {
|
|
|
return sendErrorArray(2001, '变动数量不可为0');
|
|
|
}
|
|
|
|
|
|
// 查找我的信息
|
|
|
$my = $my_model->getOneData([
|
|
|
['uid', '=', $this->mid],
|
|
|
['user_id', '=', $user_id]
|
|
|
], 'id,user_id,commission,total_commission');
|
|
|
|
|
|
// 如果用户没有初始化则初始化,再重新获取用户佣金数据
|
|
|
if (empty($my)) {
|
|
|
$my_data = [
|
|
|
'uid' => $this->mid,
|
|
|
'user_id' => $user_id,
|
|
|
'commission' => 0,
|
|
|
'total_commission' => 0
|
|
|
];
|
|
|
$res = $my_model->dataUpdate($my_data);
|
|
|
if (!$res) {
|
|
|
return sendErrorArray(2002, '平台初始化失败');
|
|
|
}
|
|
|
|
|
|
// 查找我的信息
|
|
|
$my = $my_model->getOneData([
|
|
|
['uid', '=', $this->mid],
|
|
|
['user_id', '=', $user_id]
|
|
|
], 'id,user_id,commission,total_commission');
|
|
|
}
|
|
|
|
|
|
// 不可减为负数
|
|
|
if ($commission < 0 && abs($commission) > $my['commission']) {
|
|
|
return sendErrorArray(2003, '变动佣金不可大于当前佣金');
|
|
|
}
|
|
|
|
|
|
// 更新我的佣金表
|
|
|
$my_update = [
|
|
|
'id' => $my['id'],
|
|
|
'user_id' => $user_id,
|
|
|
'commission' => Db::raw('commission + ' . $commission)
|
|
|
];
|
|
|
if (in_array($type, $log_model->getJoinStatisticsTypes())) {
|
|
|
$my_update['total_commission'] = Db::raw('total_commission + ' . $commission);
|
|
|
}
|
|
|
$res = $my_model->dataUpdate($my_update);
|
|
|
if (!$res) {
|
|
|
return sendErrorArray(2004, '用户佣金更新失败');
|
|
|
}
|
|
|
|
|
|
// 更新LOG表
|
|
|
$my_log_data = [
|
|
|
'uid' => $this->mid,
|
|
|
'user_id' => $user_id,
|
|
|
'type' => $type,
|
|
|
'commission' => $commission,
|
|
|
'after_commission' => $my['commission'] + $commission,
|
|
|
'show_text' => $show_text,
|
|
|
'relation_order_number' => $relation_order_number,
|
|
|
'remark' => $remark
|
|
|
];
|
|
|
$res = $log_model->dataUpdate($my_log_data);
|
|
|
if (!$res) {
|
|
|
return sendErrorArray(2005, '用户佣金日志更新失败');
|
|
|
}
|
|
|
|
|
|
return sendSuccessArray();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 检查提现信息完整性
|
|
|
* @param int $user_id 用户ID
|
|
|
* @param int $withdraw_type 提现类型 1--微信 2--支付宝
|
|
|
* @date 2022-11-04
|
|
|
*/
|
|
|
public function checkInfo($user_id, $withdraw_type = 1)
|
|
|
{
|
|
|
$app_weixin_user_model = new AppWeixinUser();
|
|
|
$alipay_model = new UserAlipay();
|
|
|
|
|
|
|
|
|
// 微信提现
|
|
|
if ($withdraw_type == 1) {
|
|
|
switch ($this->userAgent) {
|
|
|
// APP,判断是否授权微信
|
|
|
case 'app';
|
|
|
//检查APP是否有微信授权
|
|
|
$app_weixin_user = $app_weixin_user_model->getOneData([
|
|
|
['user_id', '=', $user_id]
|
|
|
], 'id,openid');
|
|
|
if (empty($app_weixin_user)) {
|
|
|
return sendErrorArray(2101, '您需要先进行微信授权');
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
// 支付宝提现
|
|
|
} else if ($withdraw_type == 2) {
|
|
|
// 是否绑定支付宝
|
|
|
$is_bind_ali_pay = $alipay_model->isBindAliPay();
|
|
|
if (empty($is_bind_ali_pay)) {
|
|
|
return sendErrorArray(2102, '您需要先进行支付宝账户绑定');
|
|
|
}
|
|
|
}
|
|
|
return sendSuccessArray();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 提现
|
|
|
* @param int $withdraw_id 用户提现ID
|
|
|
* @date 2021-04-01
|
|
|
*/
|
|
|
public function withdraw($withdraw_id)
|
|
|
{
|
|
|
$withdraw_model = new DistributionCommissionWithdraw();
|
|
|
$app_weixin_user_model = new AppWeixinUser();
|
|
|
$mp_weixin_user_model = new MpweixinUser();
|
|
|
$wechat_user_model = new WechatUser();
|
|
|
|
|
|
|
|
|
// 查找申请提现记录
|
|
|
$withdraw = $withdraw_model->getOneData([
|
|
|
['id', '=', $withdraw_id]
|
|
|
], 'id,user_id,user_agent,order_number,withdraw_type,money');
|
|
|
|
|
|
// 微信提现
|
|
|
if ($withdraw['withdraw_type'] == 1) {
|
|
|
$openid = '';
|
|
|
switch ($withdraw['user_agent']) {
|
|
|
case 'weixin':
|
|
|
// 查找对应人的OPENID
|
|
|
$openid = $wechat_user_model->getOneData([
|
|
|
['user_id', '=', $withdraw['user_id']]
|
|
|
], 'openid');
|
|
|
break;
|
|
|
case 'mp_weixin':
|
|
|
// 查找对应人的OPENID
|
|
|
$openid = $mp_weixin_user_model->getOneData([
|
|
|
['user_id', '=', $withdraw['user_id']]
|
|
|
], 'openid');
|
|
|
break;
|
|
|
case 'app':
|
|
|
// 查找对应人的微信授权记录
|
|
|
$openid = $app_weixin_user_model->getOneData([
|
|
|
['user_id', '=', $withdraw['user_id']]
|
|
|
], 'openid');
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
if (empty($openid)) {
|
|
|
return sendErrorArray(2001, '提现用户未进行微信授权,无法提现');
|
|
|
}
|
|
|
|
|
|
$wxpay_param = get_wxpay_config_with_weixin($withdraw['user_agent']);
|
|
|
$transfers_class = new \tencent\wechat\pay\Transfers($wxpay_param);
|
|
|
|
|
|
// 提现
|
|
|
$result = $transfers_class->toBalance($openid, $withdraw['money'], $withdraw['order_number'], '佣金提现到账');
|
|
|
|
|
|
// 有错误
|
|
|
if ($result['return_code'] != 'SUCCESS' || $result['result_code'] != 'SUCCESS') {
|
|
|
return sendErrorArray(2002, $result['err_code_des'] . ',您的提现申请已提交到后台,由后台人工审核');
|
|
|
}
|
|
|
|
|
|
// 提现到账成功 更新提现表
|
|
|
$data_update = [
|
|
|
'id' => $withdraw['id'],
|
|
|
'payment_no' => $result['payment_no'],
|
|
|
'status' => 2,
|
|
|
'check_time' => time(),
|
|
|
'is_arrival' => 1,
|
|
|
'arrival_time' => time()
|
|
|
];
|
|
|
$withdraw_model->dataUpdate($data_update);
|
|
|
return sendSuccessArray();
|
|
|
|
|
|
// 支付宝提现
|
|
|
} else if ($withdraw['withdraw_type'] == 2) {
|
|
|
switch ($withdraw['user_agent']) {
|
|
|
case 'app':
|
|
|
$ali_pay_param = get_app_alipay_config();
|
|
|
|
|
|
$alipay_model = new UserAlipay();
|
|
|
$fund_class = new \ali\alipay\pay\Fund($ali_pay_param);
|
|
|
|
|
|
// 获取支付宝账号
|
|
|
$alipay = $alipay_model->getOneData([
|
|
|
['user_id', '=', $withdraw['user_id']]
|
|
|
], 'account,name');
|
|
|
|
|
|
// 单笔转账接口
|
|
|
$result = $fund_class->transfer($withdraw['order_number'], $alipay['account'], $alipay['name'], $withdraw['money'], '佣金提现到账');
|
|
|
|
|
|
break;
|
|
|
case 'mp_alipay':
|
|
|
$ali_pay_param = get_mp_alipay_config();
|
|
|
|
|
|
$mpalipay_user_model = new MpalipayUser();
|
|
|
$fund_class = new \ali\alipay\pay\Fund($ali_pay_param);
|
|
|
|
|
|
// 获取支付宝账号
|
|
|
$alipay_openid = $mpalipay_user_model->getOneData([
|
|
|
['user_id', '=', $withdraw['user_id']]
|
|
|
], 'openid');
|
|
|
|
|
|
//单笔转账接口
|
|
|
$result = $fund_class->transferByOpenid($withdraw['order_number'], $alipay_openid, $withdraw['money']);
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
// 提现未到账
|
|
|
if ($result['code'] != 10000) {
|
|
|
return sendErrorArray($result['code'], $result['sub_msg']);
|
|
|
}
|
|
|
|
|
|
// 提现到账
|
|
|
$data_update = [
|
|
|
'id' => $withdraw['id'],
|
|
|
'payment_no' => $result['pay_fund_order_id'],
|
|
|
'status' => 2,
|
|
|
'check_time' => time(),
|
|
|
'is_arrival' => 1,
|
|
|
'arrival_time' => time()
|
|
|
];
|
|
|
$withdraw_model->dataUpdate($data_update);
|
|
|
return sendSuccessArray([], '提现成功,预计会在2个小时内到账。');
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|