343 lines
13 KiB
PHP
343 lines
13 KiB
PHP
<?php
|
|
/**
|
|
* SAAS应用系统 --- 十年开发经验汇集巨献!
|
|
* ==========================================================
|
|
* Copy right 2020-2050 成都众联思索科技有限公司,保留所有权利。
|
|
* ----------------------------------------------------------
|
|
* 官方网址: https://www.zoomtk.com
|
|
* 这不是自由软件!未经允许不得用于商业目或程序代码摘取及修改。
|
|
* 任何企业和个人未经允许对程序代码以任何形式任何目的再发布传播。
|
|
* 唯一发布渠道www.zoomtk.com;非官方渠道统一视为侵权行为。
|
|
* ==========================================================
|
|
*/
|
|
|
|
namespace addon\saas\model;
|
|
|
|
use app\model\BaseModel;
|
|
use app\model\member\Member as MemberModel;
|
|
use app\model\member\MemberAccount;
|
|
use app\model\message\Message;
|
|
use app\model\system\Pay;
|
|
use think\facade\Cache;
|
|
use think\facade\Db;
|
|
|
|
//结算芝麻流水佣金
|
|
class RegionWithdraw extends BaseModel
|
|
{
|
|
|
|
|
|
/**
|
|
* 获取区域经理提现列表
|
|
* @param unknown $condition
|
|
* @param number $page
|
|
* @param string $page_size
|
|
* @param string $order
|
|
*/
|
|
public function getWithdrawPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '')
|
|
{
|
|
$field = '*';
|
|
$list = model('region_withdraw')->pageList($condition, $field, $order, $page, $page_size);
|
|
return $this->success($list);
|
|
}
|
|
|
|
public function getWithdrawInfo($condition = [], $field = '*')
|
|
{
|
|
$info = model('region_withdraw')->getInfo($condition, $field);
|
|
return $this->success($info);
|
|
}
|
|
|
|
public function apply($data, $site_id = 0, $app_module = 'shop')
|
|
{
|
|
$config_model = new Config();
|
|
$config = $config_model->getRegionSettlementConfig($site_id)['data']['value'] ?? [];
|
|
$withdraw_no = $this->createWithdrawNo();
|
|
$apply_money = round(abs($data['apply_money']), 2);
|
|
$withdraw_min_money = $config['min'];
|
|
$withdraw_max_money = $config['max'];
|
|
$time_week = $config['time_week']??[];
|
|
$member_id = $data['member_id'];
|
|
if ($apply_money < $withdraw_min_money) return $this->error([], '申请提现金额不能小于最低提现额度' . $withdraw_min_money);
|
|
if ($withdraw_max_money && $apply_money > $withdraw_max_money) return $this->error([], '申请提现金额不能大于最高提现额度' . $withdraw_max_money);
|
|
if(in_array(date('w'),$time_week)) return $this->error([], '每周提现时间为' . implode(',',$time_week));
|
|
if(isset($data['is_flexible_transfer'])&&$data['is_flexible_transfer']==1){
|
|
$status=model('member_bank_account')->getValue([['member_id','=',$member_id],['site_id','=',$site_id]],'status');
|
|
if($status!=4) return $this->error([], '银行卡未签约');
|
|
}
|
|
$member_model = new MemberModel();
|
|
$member_condition = array(
|
|
['member_id', '=', $member_id]
|
|
);
|
|
$member_info = $member_model->getMemberInfo($member_condition, 'balance_money,headimg,wx_openid,username,mobile,weapp_openid,nickname')['data'] ?? [];
|
|
if (empty($member_info))
|
|
return $this->error([], 'MEMBER_NOT_EXIST');
|
|
$member_condition[] = ['is_delete' ,'=' , 1];
|
|
$manage_info = model('region_manage')->getInfo($member_condition, 'id,account,status');
|
|
if (empty($manage_info)) {
|
|
return $this->error('不存在的账号');
|
|
}
|
|
if($manage_info['status'] !== 1){
|
|
return $this->error('账号状态异常,请联系管理员');
|
|
}
|
|
$account = $manage_info['account'];//会员的分销佣金
|
|
if ($account < $apply_money) {
|
|
return $this->error('', '提现金额大于可提现金额');
|
|
}
|
|
$transfer_type = $data['transfer_type'];
|
|
$transfer_type_list = $this->getTransferType($site_id);
|
|
$transfer_type_name = $transfer_type_list[$transfer_type] ?? '';
|
|
if (empty($transfer_type_name))
|
|
return $this->error([], '不支持的提现方式');
|
|
model('region_withdraw')->startTrans();
|
|
try {
|
|
$withdraw_rate = $config['withdraw_rate'];
|
|
$bank_name = '';
|
|
$account_number = '';
|
|
$applet_type = 0;
|
|
switch ($transfer_type) {
|
|
case 'bank':
|
|
$bank_name = $data['bank_name'];
|
|
$account_number = $data['account_number'];
|
|
break;
|
|
case 'alipay':
|
|
$bank_name = '';
|
|
$account_number = $data['account_number'];
|
|
break;
|
|
case 'wechatpay':
|
|
$bank_name = '';
|
|
if (empty($member_info['wx_openid']) && empty($member_info['weapp_openid'])) {
|
|
return $this->error('', '请绑定微信或更换提现账户');
|
|
}
|
|
if (!empty($member_info['wx_openid'])) {
|
|
$account_number = $member_info['wx_openid'];
|
|
$applet_type = 0; // 公众号
|
|
} else {
|
|
$account_number = $member_info['weapp_openid'];
|
|
$applet_type = 1; // 小程序
|
|
}
|
|
break;
|
|
}
|
|
if ($transfer_type == 'balance') {
|
|
$withdraw_rate = 0;
|
|
}
|
|
$service_money = round($apply_money * $withdraw_rate / 100, 2);//手续费
|
|
$real_money = $apply_money - $service_money;
|
|
$data = array(
|
|
'site_id' => $site_id,
|
|
'withdraw_no' => $withdraw_no,
|
|
'member_id' => $member_id,
|
|
'manage_id' => $manage_info['id'],
|
|
'real_name' => $manage_info['real_name'],
|
|
'transfer_type' => $transfer_type,
|
|
'transfer_name' => $transfer_type_name,
|
|
'money' => $apply_money,
|
|
'withdraw_rate_money' => $service_money,
|
|
'withdraw_rate' => $withdraw_rate,
|
|
'real_money' => $real_money,
|
|
'create_time' => time(),
|
|
'status' => 1,
|
|
'member_headimg' => $member_info['headimg'],
|
|
'realname' => $data['realname'],
|
|
'bank_name' => $bank_name,
|
|
'account_number' => $account_number,
|
|
'mobile' => $data['mobile'],
|
|
'applet_type' => $applet_type
|
|
);
|
|
$result = model('region_withdraw')->add($data);
|
|
//修改分销商可提现金额
|
|
model('region_manage')->setDec($member_condition, 'account', $apply_money);
|
|
//申请提现发送消息
|
|
$data['keywords'] = 'manage_withdraw_apply';
|
|
$message_model = new Message();
|
|
$message_model->sendMessage($data);
|
|
//判断是否需要审核
|
|
if ($config['withdraw_status'] == 2) {//不需要
|
|
$result = $this->agree(['id' => $result, 'site_id' => $site_id]);
|
|
if ($result['code'] < 0) {
|
|
model('region_withdraw')->rollback();
|
|
return $result;
|
|
}
|
|
}
|
|
model('region_withdraw')->commit();
|
|
return $this->success();
|
|
} catch (\Exception $e) {
|
|
model('region_withdraw')->rollback();
|
|
return $this->error('', $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/***
|
|
* 同意提现
|
|
* @param $params
|
|
* @return array
|
|
*/
|
|
public function agree($params)
|
|
{
|
|
$id = $params['id'];
|
|
$site_id = $params['site_id'];
|
|
if (empty($site_id)) {
|
|
return $this->error(-1, '参数错误');
|
|
}
|
|
$condition = array(
|
|
['id', '=', $id],
|
|
['site_id', '=', $site_id],
|
|
);
|
|
$info = model('region_withdraw')->getInfo($condition);
|
|
if (empty($info))
|
|
return $this->error();
|
|
$config_model = new Config();
|
|
$config = $config_model->getRegionSettlementConfig($site_id)['data']['value'] ?? [];
|
|
model('region_withdraw')->startTrans();
|
|
try {
|
|
$data = array(
|
|
'status' => 2,
|
|
'audit_time' => time(),
|
|
);
|
|
model('region_withdraw')->update($data, $condition);
|
|
//是否启用自动转账(必须是微信或支付宝)
|
|
if ($config['is_auto_transfer'] == 1) {
|
|
$this->transfer(['id' => $id]);
|
|
}
|
|
model('region_withdraw')->commit();
|
|
return $this->success();
|
|
} catch (\Exception $e) {
|
|
model('region_withdraw')->rollback();
|
|
return $this->error('', $e->getMessage() . $e->getLine());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 转账
|
|
* @param $condition
|
|
*/
|
|
public function transfer($params)
|
|
{
|
|
$id = $params['id'];
|
|
$site_id = $params['site_id'] ?? 0;
|
|
$condition = array(
|
|
['id', '=', $id],
|
|
);
|
|
if ($site_id > 0) {
|
|
$condition[] = ['site_id', '=', $site_id];
|
|
}
|
|
$info = model('region_withdraw')->getInfo($condition);
|
|
|
|
if (empty($info)) return $this->error();
|
|
$site_id = $info['site_id'];
|
|
$transfer_type = $info['transfer_type'];
|
|
$member_id = $info['member_id'];
|
|
$money = $info['money'];
|
|
$manage_id = $info['manage_id'];
|
|
$real_name = $info['real_name'];
|
|
$real_money = $info['real_money'];
|
|
if ($transfer_type == 'balance') {
|
|
//添加会员账户流水
|
|
$member_account = new MemberAccount();
|
|
$member_result = $member_account->addMemberAccount($site_id, $member_id, 'balance_money', $real_money, 'manage', '佣金提现', '业务佣金提现');
|
|
if ($member_result['code'] < 0) {
|
|
return $member_result;
|
|
}
|
|
} else {
|
|
if (!in_array($transfer_type, ['wechatpay', 'alipay']))
|
|
return $this->error('', '当前提现方式不支持在线转账');
|
|
$pay_data = array(
|
|
'id' => $id,
|
|
'out_trade_no' => $info['withdraw_no'],
|
|
'real_name' => $info['realname'],
|
|
'amount' => $info['real_money'],
|
|
'desc' => '会员提现',
|
|
'transfer_type' => $transfer_type,
|
|
'account_number' => $info['account_number'],
|
|
'site_id' => $info['site_id'],
|
|
'is_weapp' => $info['applet_type'],
|
|
'member_id' => $info['member_id']
|
|
);
|
|
//调用在线转账借口
|
|
$pay_result = event('PayTransfer', $pay_data, true);
|
|
if (empty($pay_result)) {
|
|
$pay_result = $this->error();
|
|
}
|
|
if ($pay_result['code'] < 0) {
|
|
return $pay_result;
|
|
}
|
|
}
|
|
$account_model = new ManageAccount();
|
|
$account_result = $account_model->addAccountLog($manage_id, $real_name, 'region_withdraw', '-' . $money, $id);
|
|
if ($account_result['code'] != 0) {
|
|
return $account_result;
|
|
}
|
|
//调用完成转账
|
|
$result = $this->transferFinish(['id' => $id, 'site_id' => $info['site_id']]);
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* 提现转账完成
|
|
* @param $id
|
|
*/
|
|
public function transferFinish($param = [])
|
|
{
|
|
$condition = [
|
|
['id', '=', $param['id']],
|
|
['site_id', '=', $param['site_id']],
|
|
['status', '=', 2]
|
|
];
|
|
$info = model('region_withdraw')->getInfo($condition);
|
|
if (empty($info)) return $this->error();
|
|
$site_id = $info['site_id'];
|
|
$manage_id = $info['manage_id'];
|
|
$money = $info['money'];
|
|
$payment_time = time();
|
|
model('region_withdraw')->startTrans();
|
|
try {
|
|
$data = [
|
|
'status' => 3,
|
|
'status_name' => '已转账',
|
|
'payment_time' => $payment_time,
|
|
'document' => $param['certificate'] ?? '',
|
|
'transfer_remark' => $param['certificate_remark'] ?? ''
|
|
];
|
|
$result = model('region_withdraw')->update($data, $condition);
|
|
$_condition = array(
|
|
['id', '=', $manage_id],
|
|
['site_id', '=', $site_id]
|
|
);
|
|
//修改分销商已提现金额
|
|
model('manage_user')->setInc($_condition, 'account_withdraw', $money);
|
|
model('region_withdraw')->commit();
|
|
$message_model = new Message();
|
|
$info['keywords'] = 'manage_withdraw_success';
|
|
$message_model->sendMessage($info);
|
|
return $this->success();
|
|
} catch (\Exception $e) {
|
|
model('region_withdraw')->rollback();
|
|
return $this->error('', $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 提现流水号
|
|
*/
|
|
private function createWithdrawNo()
|
|
{
|
|
$cache = Cache::get('region_withdraw_no' . time());
|
|
if (empty($cache)) {
|
|
Cache::set('saasrka' . time(), 1000);
|
|
$cache = Cache::get('region_withdraw_no' . time());
|
|
} else {
|
|
$cache = $cache + 1;
|
|
Cache::set('region_withdraw_no' . time(), $cache);
|
|
}
|
|
$no = date('Ymdhis', time()) . rand(1000, 9999) . $cache;
|
|
return $no;
|
|
}
|
|
|
|
public function getTransferType($site_id)
|
|
{
|
|
$pay_model = new Pay();
|
|
$transfer_type_list = $pay_model->getTransferType($site_id);
|
|
$transfer_type_list['balance'] = '余额';
|
|
return $transfer_type_list;
|
|
}
|
|
|
|
}
|