585 行
21 KiB
PHP
可执行文件
585 行
21 KiB
PHP
可执行文件
<?php
|
|
|
|
/**
|
|
* 小程序--分销员接口
|
|
* @author nianyi
|
|
* @create_time 2020年07月10日
|
|
*/
|
|
|
|
namespace Distribution\Controller;
|
|
|
|
use Common\Model\RetailSmallProgramUserModel;
|
|
use Common\Model\RetailStoreModel;
|
|
use Distribution\Model\DistributorModel;
|
|
use Distribution\Model\WithdrawalModel;
|
|
use Distribution\Model\StoreModel;
|
|
use Distribution\Model\ConfigModel;
|
|
use Distribution\Model\OrderModel;
|
|
use Distribution\Model\GradeModel;
|
|
use Think\Hook;
|
|
|
|
class MiniDistributorController extends \SmallProgramApi\Controller\BaseController
|
|
{
|
|
public function _initialize()
|
|
{
|
|
parent::_initialize();
|
|
if (empty(self::$uid)) {
|
|
$res['status'] = -1;
|
|
$res['msg'] = '用户未登录';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
}
|
|
|
|
|
|
/* ******************************************* 我的信息 Start ******************************************** */
|
|
// 用户是否是分销员--小程序登录判断用
|
|
public function isUserDistributor()
|
|
{
|
|
$uid = self::$uid; //用户id
|
|
$merchant_id = self::$merchant_id;
|
|
|
|
$DistributorModel = new DistributorModel();
|
|
$isDistributor = $DistributorModel->getFind(['merchant_id' => $merchant_id, 'uid' => $uid, 'status' => 0], 'id');
|
|
if ($isDistributor['id']) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// 获取分销员基本信息
|
|
public function getDistributorBasicInfo()
|
|
{
|
|
$uid = self::$uid; //用户id
|
|
$merchant_id = self::$merchant_id;
|
|
|
|
$DistributorModel = new DistributorModel();
|
|
$OrderModel = new OrderModel();
|
|
$SmallProgramUserModel = new RetailSmallProgramUserModel();
|
|
|
|
$distributor_id = $this->verifyDistributor($uid, $merchant_id);
|
|
|
|
$where['dor.id'] = $distributor_id;
|
|
|
|
$field = [
|
|
'dor.id as distributor_id',
|
|
'dor.phone_number',
|
|
'dor.uid',
|
|
'dor.pid',
|
|
'dor.add_time',
|
|
'dor.settable_amount',
|
|
'rx.nickname',
|
|
'rx.avatar',
|
|
'dg.id as grade_id',
|
|
'dg.grade_name',
|
|
'dg.proportion'
|
|
];
|
|
|
|
$personal = $DistributorModel->getUserBasicInfo($where, $field);
|
|
if ($personal) {
|
|
// 获取上级昵称
|
|
if ($personal['pid']) {
|
|
$superior_uid = $DistributorModel->getFind(['id' => $personal['pid']], 'id,status,uid'); // 获取上级用户的UID
|
|
if (!$superior_uid['id'] || $superior_uid['status'] == 1) {
|
|
$personal['p_name'] = NULL;
|
|
$personal['pid'] = 0;
|
|
} else {
|
|
$superior = $SmallProgramUserModel->getFind(['id' => $superior_uid['uid']], 'nickname'); // 获取上级用户的昵称
|
|
$personal['p_name'] = $superior['nickname'];
|
|
}
|
|
}
|
|
|
|
$personal['commission'] = $OrderModel->userCommissionExpected($distributor_id, $merchant_id);
|
|
|
|
$balanceWhere['rbo.merchant_id'] = $merchant_id;
|
|
$balanceWhere['diso.withdraw_state'] = 0;
|
|
$balance = $OrderModel->userCommissionExpectedByTime($balanceWhere, $distributor_id, 3, 5);
|
|
$personal['balance'] = bcadd($balance, $personal['settable_amount'], 2);
|
|
|
|
$personal['add_time'] = date('Y-m-d H:i:s', $personal['add_time']);
|
|
$res['status'] = 1;
|
|
$res['msg'] = '信息获取成功!';
|
|
$res['data'] = $personal;
|
|
} else {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '信息获取失败,请稍后重试!';
|
|
$res['data'] = (object)[];
|
|
}
|
|
$this->ajaxReturn($res);
|
|
}
|
|
|
|
// 获取升级要求V2--2020年8月4日
|
|
public function getUpgradeConditions()
|
|
{
|
|
$merchant_id = self::$merchant_id;
|
|
$uid = self::$uid;
|
|
|
|
$GradeModel = new GradeModel();
|
|
$DistributorModel = new DistributorModel();
|
|
|
|
$distributor_id = $this->verifyDistributor($uid, $merchant_id);
|
|
|
|
// 获取用户当前等级信息
|
|
$result['grade_info'] = $DistributorModel->getUserGradeInfo($distributor_id);
|
|
|
|
$field = [
|
|
'id',
|
|
'grade_name',
|
|
'invite',
|
|
'commission',
|
|
'myself_money',
|
|
'client',
|
|
'is_invite',
|
|
'is_commission',
|
|
'is_myself_money',
|
|
'is_client',
|
|
'proportion',
|
|
'upgrade_type',
|
|
'grade_type'
|
|
];
|
|
|
|
$result['upgrade_conditions'] = $GradeModel->getAllUpgradeConditions($merchant_id, $field);
|
|
|
|
if ($result['upgrade_conditions']) {
|
|
// 获取用户的当前数据
|
|
$userInfo = $DistributorModel->getFind(['id' => $distributor_id], 'total_invite, total_commission, total_myself_money, total_client');
|
|
|
|
foreach ($result['upgrade_conditions'] as &$value) {
|
|
$value['upgrade'] = [];
|
|
if ($value['grade_type'] == 1) {
|
|
if ($value['is_invite'] == 1) {
|
|
$value['upgrade'][] = '邀请分销员数量达到' . $userInfo['total_invite'] . '/' . $value['invite'] . '人';
|
|
}
|
|
if ($value['is_commission'] == 1) {
|
|
$value['upgrade'][] = '推广佣金达到' . $userInfo['total_commission'] . '/' . $value['commission'] . '元';
|
|
}
|
|
if ($value['is_myself_money'] == 1) {
|
|
$value['upgrade'][] = '自购商品金额达到' . $userInfo['total_myself_money'] . '/' . $value['myself_money'] . '元';
|
|
}
|
|
if ($value['is_client'] == 1) {
|
|
$value['upgrade'][] = '邀请客户数达到' . $userInfo['total_client'] . '/' . $value['client'] . '人';
|
|
}
|
|
|
|
if ($value['upgrade_type'] == 0) {
|
|
$value['upgrade_condition'] = '满足其中一个条件即可升级';
|
|
} else {
|
|
$value['upgrade_condition'] = '满足其中所有条件即可升级';
|
|
}
|
|
} else {
|
|
$value['upgrade'][] = '该等级为初始等级,没有升级条件!';
|
|
$value['upgrade_condition'] = '';
|
|
}
|
|
|
|
unset($value['upgrade_type']);
|
|
unset($value['grade_type']);
|
|
unset($value['invite']);
|
|
unset($value['commission']);
|
|
unset($value['myself_money']);
|
|
unset($value['client']);
|
|
unset($value['is_invite']);
|
|
unset($value['is_client']);
|
|
unset($value['is_commission']);
|
|
unset($value['is_myself_money']);
|
|
}
|
|
$this->successAjaxReturn('信息获取成功!', $result);
|
|
} else {
|
|
$this->successAjaxReturn('商户暂未添加升级条件!', $result);
|
|
}
|
|
}
|
|
|
|
// 获取我的分销员
|
|
public function getMyDistributor()
|
|
{
|
|
$page = I('post.page') ? I('post.page') : 1;
|
|
$limit = I('post.limit') ? I('post.limit') : 10;
|
|
$search = I('post.search');
|
|
$uid = self::$uid; //用户id
|
|
$merchant_id = self::$merchant_id;
|
|
|
|
$DistributorModel = new DistributorModel();
|
|
$distributor_id = $this->verifyDistributor($uid, $merchant_id);
|
|
|
|
if ($search) {
|
|
$where['dor.phone_number|rx.nickname'] = ['like', "%{$search}%"];;
|
|
}
|
|
|
|
$where['dor.pid'] = $distributor_id;
|
|
$where['dor.merchant_id'] = $merchant_id;
|
|
$where['dor.status'] = 0;
|
|
|
|
$field = [
|
|
'dor.id as distributor_id',
|
|
'dor.phone_number',
|
|
'dor.uid',
|
|
'dor.add_time',
|
|
'rx.nickname',
|
|
'rx.avatar',
|
|
];
|
|
$partnerList = $DistributorModel->getMyDistributor($where, $field, $page, $limit, 'dor.id desc');
|
|
|
|
if ($partnerList['list']) {
|
|
foreach ($partnerList['list'] as &$value) {
|
|
$value['add_time'] = date('Y-m-d H:i:s', $value['add_time']);
|
|
}
|
|
$res['status'] = 1;
|
|
$res['msg'] = '数据获取成功!';
|
|
$res['data'] = $partnerList;
|
|
} else {
|
|
$res['status'] = 1;
|
|
$res['msg'] = '暂无数据!';
|
|
$res['data'] = [];
|
|
}
|
|
$this->ajaxReturn($res);
|
|
}
|
|
|
|
/* ******************************************* 我的信息 End ******************************************** */
|
|
|
|
|
|
/* ******************************************* 提现相关Start ******************************************** */
|
|
// 获取提现基本信息
|
|
public function getWithdrawalConfig()
|
|
{
|
|
$ConfigModel = new ConfigModel();
|
|
$cMap = array(
|
|
'merchant_id' => I('post.merchant_id'),
|
|
);
|
|
$min_withdrawal_money = $ConfigModel->getFindField($cMap, 'withdrawal_money');
|
|
$dMap = array(
|
|
'uid' => I('post.uid'),
|
|
'status' => 0,
|
|
);
|
|
$DistributorModel = new DistributorModel();
|
|
$settable_amount = $DistributorModel->getFindField($dMap, 'settable_amount');
|
|
$this->ajaxReturn(array(
|
|
'status' => 1,
|
|
'msg' => '获取成功',
|
|
'data' => array(
|
|
'min_withdrawal_money' => $min_withdrawal_money,
|
|
'settable_amount' => $settable_amount,
|
|
),
|
|
));
|
|
}
|
|
|
|
// 提现账户设置
|
|
public function updateWithdrawalAccount()
|
|
{
|
|
$msg_arr['name'] = I('post.name');
|
|
$msg_arr['account'] = I('post.account');
|
|
$uid = self::$uid; //用户id
|
|
$merchant_id = self::$merchant_id;
|
|
|
|
$DistributorModel = new DistributorModel();
|
|
|
|
if (!$msg_arr['name'] || !$msg_arr['account']) {
|
|
$res['status'] = 1;
|
|
$res['msg'] = '必要参数缺失!';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
|
|
$msg = json_encode($msg_arr);
|
|
$result = $DistributorModel->updateOrder(['merchant_id' => $merchant_id, 'uid' => $uid], ['alipay_msg' => $msg]);
|
|
if ($result) {
|
|
$res['status'] = 1;
|
|
$res['msg'] = '操作成功';
|
|
} else {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '操作失败,请稍后!';
|
|
}
|
|
$this->ajaxReturn($res);
|
|
}
|
|
|
|
// 申请提现
|
|
public function userApplyWithdrawal()
|
|
{
|
|
$way = I('post.withdrawal_way');
|
|
$money = I('post.withdrawal_money');
|
|
$uid = self::$uid; //用户id
|
|
$merchant_id = self::$merchant_id;
|
|
|
|
$WithdrawalModel = new WithdrawalModel();
|
|
$DistributorModel = new DistributorModel();
|
|
$ConfigModel = new ConfigModel();
|
|
|
|
$distributor_id = $this->verifyDistributor($uid, $merchant_id);
|
|
|
|
if (!in_array($way, [0, 1, 2])) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '请选择正确的提现账户!';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
|
|
// 账户信息验证(未完成)
|
|
if ($way == 1) {
|
|
$alipay = $DistributorModel->getFind(['id' => $distributor_id], 'alipay_msg');
|
|
if (!$alipay) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '支付宝账号未设置!';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
$alipay_arr = json_decode($alipay['alipay_msg'], true);
|
|
if (!$alipay_arr['name'] || !$alipay_arr['account']) {
|
|
$res['status'] = 1;
|
|
$res['msg'] = '账号信息错误!';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
}
|
|
|
|
if (!verifyPrice($money)) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '请填写正确的金额!';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
|
|
// 金额判断 (用户金额是否足够-未完成)
|
|
$minMoney = $ConfigModel->getFind(['merchant_id' => $merchant_id], 'withdrawal_money');
|
|
if ($minMoney['withdrawal_money']) {
|
|
if ($money < $minMoney['withdrawal_money']) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '提现金额过低,请重新输入!';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
}
|
|
|
|
// 用户金额是否满足
|
|
$usrMoney = $DistributorModel->getFindField(['id' => $distributor_id], 'settable_amount');
|
|
if ($usrMoney < $money) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '您的余额不足!';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
|
|
$config = $ConfigModel->getFind(['merchant_id' => $merchant_id], 'clearing_form');
|
|
if ($config['clearing_form'] == 1) {
|
|
$data['status'] = 0;
|
|
} else {
|
|
$data['status'] = 2; // 无需审核
|
|
}
|
|
|
|
$data['withdrawal_way'] = $way;
|
|
$data['withdrawal_money'] = $money;
|
|
$data['merchant_id'] = $merchant_id;
|
|
$data['distributor_id'] = $distributor_id;
|
|
$data['add_time'] = time();
|
|
$result = $WithdrawalModel->addOrder($data);
|
|
if ($result) {
|
|
// 用户资金冻结
|
|
$DistributorModel->toSetDec(['id' => $distributor_id], 'settable_amount', $money);
|
|
if ($config['clearing_form'] == 0) { // 自动打款
|
|
if ($way == 1) { // 支付宝打款
|
|
|
|
} else {
|
|
// 微信打款
|
|
$remitRes = $WithdrawalModel->remitPay($result, $merchant_id);
|
|
if ($remitRes['status'] == 0) {
|
|
$this->failAjaxReturn($remitRes['msg']);
|
|
}
|
|
}
|
|
} else {
|
|
}
|
|
|
|
$this->successAjaxReturn('提现申请成功!', ['status' => $data['status']]);
|
|
} else {
|
|
$this->failAjaxReturn('申请失败,请稍后重试!!', []);
|
|
}
|
|
}
|
|
|
|
// 提现记录
|
|
public function userWithdrawalRecord()
|
|
{
|
|
$page = I('post.page') ? I('post.page') : 1;
|
|
$limit = I('post.limit') ? I('post.limit') : 10;
|
|
$uid = self::$uid;
|
|
$merchant_id = self::$merchant_id;
|
|
|
|
$WithdrawalModel = new WithdrawalModel();
|
|
|
|
$distributor_id = $this->verifyDistributor($uid, $merchant_id);
|
|
|
|
$where = [
|
|
'merchant_id' => $merchant_id,
|
|
'distributor_id' => $distributor_id
|
|
];
|
|
$field = [
|
|
'withdrawal_way',
|
|
'withdrawal_money',
|
|
'withdrawal_json',
|
|
'status',
|
|
'state',
|
|
'add_time',
|
|
];
|
|
|
|
$applyList['list'] = $WithdrawalModel->getSelect($where, $field, 'id DESC', $page . ',' . $limit);
|
|
$applyList['count'] = $WithdrawalModel->getCount($where);
|
|
if ($applyList['count']) {
|
|
foreach ($applyList['list'] as &$value) {
|
|
$value['add_time'] = date('Y-m-d H:i:s', $value['add_time']);
|
|
// $value['withdrawal_way'] = '微信支付';
|
|
// if($value['state'] == 0){
|
|
// $value['state'] = '未打款';
|
|
// } else if($value['state'] == 1){
|
|
// $value['state'] = '已到账';
|
|
// } else {
|
|
// $value['state'] = '打款中';
|
|
// }
|
|
|
|
// 账号信息结算
|
|
// $value['withdrawal_account'] = json_decode($value['withdrawal_json'], true);
|
|
// unset($value['withdrawal_json']);
|
|
}
|
|
$res['status'] = 1;
|
|
$res['msg'] = '数据获取成功!';
|
|
$res['data'] = $applyList;
|
|
} else {
|
|
$res['status'] = 1;
|
|
$res['msg'] = '暂无数据,请稍后重试!';
|
|
$res['data'] = [];
|
|
}
|
|
$this->ajaxReturn($res);
|
|
}
|
|
|
|
/* ******************************************* 提现相关End ******************************************** */
|
|
|
|
/* ******************************************* 商品Start ******************************************** */
|
|
// 获取分销商品
|
|
public function getDistributionGoods()
|
|
{
|
|
$page = I('post.page') ? I('post.page') : 1;
|
|
$limit = I('post.limit') ? I('post.limit') : 10;
|
|
$search = I('post.search');
|
|
$uid = self::$uid; //用户id
|
|
$merchant_id = self::$merchant_id;
|
|
|
|
$StoreModel = new StoreModel();
|
|
$RetailStoreModel = new RetailStoreModel();
|
|
$DistributorModel = new DistributorModel();
|
|
|
|
$distributor_id = $this->verifyDistributor($uid, $merchant_id);
|
|
|
|
if ($search) {
|
|
$where['rg.goods_name'] = ['like', "%{$search}%"];
|
|
}
|
|
$storeIdArr = $RetailStoreModel->getFindField(['merchantid' => $merchant_id, 'state' => 0], 'id', true);
|
|
$where['ds.merchant_id'] = $merchant_id;
|
|
$where['ds.goods_status'] = 1;
|
|
$where['rg.store_id'] = ['in', $storeIdArr];
|
|
$where['rg.status'] = 0;
|
|
$where['rg.online_state'] = 1;
|
|
|
|
$field = [
|
|
'ds.goods_id as id',
|
|
'ds.first_proportion',
|
|
'rg.goods_name',
|
|
'rg.store_id',
|
|
'rg.online_price',
|
|
'rg.goods_img_id'
|
|
];
|
|
|
|
$goodsList = $StoreModel->getDistributionGoods($where, $field, $page, $limit, 'ds.id desc');
|
|
|
|
// 获取我的佣金比例
|
|
$proportion = $DistributorModel->getUserPresent($distributor_id);
|
|
|
|
if ($goodsList['list']) {
|
|
foreach ($goodsList['list'] as &$value) {
|
|
if ($value['first_proportion']) {
|
|
$value['self_proportion'] = bcmul($value['online_price'], $value['first_proportion'], 2);
|
|
} else {
|
|
$value['self_proportion'] = bcmul($value['online_price'], $proportion, 2);
|
|
}
|
|
$value['retail_price'] = $value['online_price'];
|
|
$value['goods_img'] = getGoodsImage($value['goods_img_id'], self::$store_id);
|
|
unset($value['goods_img_id']);
|
|
}
|
|
|
|
$res['status'] = 1;
|
|
$res['msg'] = '数据获取成功!';
|
|
$res['data'] = $goodsList;
|
|
} else {
|
|
$res['status'] = 1;
|
|
$res['msg'] = '还没有分销商品哦!';
|
|
$res['data'] = [];
|
|
}
|
|
$this->ajaxReturn($res);
|
|
}
|
|
|
|
/**
|
|
* 商品分销佣金返回值
|
|
* @param uid 小程序用户id
|
|
* @param goods_id 商品id
|
|
* @param price 商品价格
|
|
*/
|
|
public function disStorePropReturn($uid, $goods_id, $price)
|
|
{
|
|
$StoreModel = new StoreModel();
|
|
$DistributorModel = new DistributorModel();
|
|
|
|
$distributor_id = $DistributorModel->getFindField(['uid' => $uid, 'status' => 0], 'id');
|
|
if ($distributor_id) {
|
|
// 获取我的佣金比例
|
|
$proportion = $DistributorModel->getUserPresent($distributor_id);
|
|
// 判断当前商品是否为分销商品
|
|
$storeMap = [
|
|
'goods_id' => $goods_id,
|
|
'goods_status' => ['neq', 2],
|
|
];
|
|
$distriStoreInfo = $StoreModel->getFind($storeMap, 'id,first_proportion');
|
|
if (!$distriStoreInfo['id']) return ['status' => 4, 'disStoreProp' => NULL];
|
|
|
|
if (empty($price)) {
|
|
//价格
|
|
return ['status' => 2, 'disStoreProp' => NULL];
|
|
} else {
|
|
if (!empty($distriStoreInfo['first_proportion'])) {
|
|
$prop = $distriStoreInfo['first_proportion'];
|
|
} else {
|
|
$prop = $proportion;
|
|
}
|
|
$disStoreProp = bcmul($price, $prop, 2);
|
|
if ($disStoreProp < 0.01) {
|
|
return ['status' => 5, 'disStoreProp' => NULL];
|
|
} else {
|
|
return ['status' => 1, 'disStoreProp' => $disStoreProp];
|
|
}
|
|
}
|
|
} else {
|
|
//非分销用户
|
|
return ['status' => 3, 'disStoreProp' => NULL];
|
|
}
|
|
}
|
|
|
|
/* ******************************************* 下面是一些方法 ******************************************** */
|
|
private function verifyDistributor($uid, $merchant_id)
|
|
{
|
|
$DistributorModel = new DistributorModel();
|
|
$distributor_id = $DistributorModel->verifyUserDistributor($uid, $merchant_id);
|
|
if ($distributor_id) {
|
|
return $distributor_id;
|
|
} else {
|
|
$res['status'] = 0;
|
|
$res['msg'] = 'Authentication failed,Please try again!';
|
|
$res['data'] = array(
|
|
'status' => $uid ? 1 : 0, //1不是分销员是普通用户 0不是分销员并且未登录
|
|
);
|
|
$this->ajaxReturn($res);
|
|
}
|
|
}
|
|
|
|
// 错误提示
|
|
private function failAjaxReturn($msg, $data = '')
|
|
{
|
|
$res['status'] = 0;
|
|
$res['msg'] = $msg;
|
|
$res['data'] = $data;
|
|
$this->ajaxReturn($res);
|
|
}
|
|
|
|
// 成功
|
|
private function successAjaxReturn($msg, $data = '')
|
|
{
|
|
$res['status'] = 1;
|
|
$res['msg'] = $msg;
|
|
$res['data'] = $data;
|
|
$this->ajaxReturn($res);
|
|
}
|
|
}
|