437 行
16 KiB
PHP
可执行文件
437 行
16 KiB
PHP
可执行文件
<?php
|
|
|
|
/**
|
|
* 小程序--分销接口
|
|
* @author nianyi
|
|
* @create_time 2020年07月10日
|
|
*/
|
|
|
|
namespace Distribution\Controller;
|
|
|
|
use Distribution\Model\ApplyModel;
|
|
use Distribution\Model\DistributorModel;
|
|
use Distribution\Model\ConfigModel;
|
|
use Distribution\Model\GradeModel;
|
|
|
|
use Common\Model\RetailSmallProgramUserModel;
|
|
use Common\Model\RetailMessageModel;
|
|
use Distribution\Model\LinkModel;
|
|
|
|
class MiniApplyController extends \SmallProgramApi\Controller\BaseController
|
|
{
|
|
public function _initialize()
|
|
{
|
|
parent::_initialize();
|
|
if (empty(self::$uid)) {
|
|
$res['status'] = -1;
|
|
$res['msg'] = '用户未登录';
|
|
$this->ajaxReturn($res);
|
|
}
|
|
}
|
|
|
|
|
|
// 获取招募文案
|
|
public function getInviteCopy()
|
|
{
|
|
$merchant_id = self::$merchant_id;
|
|
|
|
$ConfigModel = new ConfigModel();
|
|
$official = $ConfigModel->getFind(['merchant_id' => $merchant_id], 'official_writer, official_btn_color, official_text_color');
|
|
if ($official['official_writer']) {
|
|
|
|
if (!empty($official['official_writer'])) {
|
|
$official['official_writer'] = str_replace('img src="/', 'img src="' . $this->REQUEST_URL . '/', $official['official_writer']);
|
|
$official['official_writer'] = str_replace("img src="/", "img src="" . $this->REQUEST_URL . '/', $official['official_writer']);
|
|
$official['official_writer'] = str_replace("style=''", "", $official['official_writer']);
|
|
$official['official_writer'] = str_replace('style=""', "", $official['official_writer']);
|
|
$official['official_writer'] = str_replace('src="/Uploads/', 'src="' . $this->REQUEST_URL . '/Uploads/', $official['official_writer']);
|
|
}
|
|
|
|
$res['status'] = 1;
|
|
$res['msg'] = '文案设置获取成功!';
|
|
$res['data'] = $official;
|
|
} else {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '信息获取失败,商户未设置文案!';
|
|
$res['data'] = (object)[];
|
|
}
|
|
$this->ajaxReturn($res);
|
|
}
|
|
|
|
// 获取分享文案
|
|
public function getShareCopy()
|
|
{
|
|
$merchant_id = self::$merchant_id;
|
|
$uid = self::$uid;
|
|
|
|
$ConfigModel = new ConfigModel();
|
|
$SmallProgramUserModel = new RetailSmallProgramUserModel();
|
|
$DistributorModel = new DistributorModel();
|
|
|
|
$field = 'share_writer, share_picture, share_color, invite_under_color, invite_code_color';
|
|
$official = $ConfigModel->getFind(['merchant_id' => $merchant_id], $field);
|
|
$user = $SmallProgramUserModel->getFind(['id' => $uid], 'nickname, avatar');
|
|
$distributor = $DistributorModel->getFind(['uid' => $uid, 'merchant_id' => $merchant_id, 'status' => 0], 'invite_code');
|
|
if ($official) {
|
|
$official['nickname'] = $user['nickname'];
|
|
$official['avatar'] = $user['avatar'];
|
|
$official['uid'] = $uid;
|
|
$official['invite_code'] = $distributor['invite_code'];
|
|
|
|
$res['status'] = 1;
|
|
$res['msg'] = '文案获取成功!';
|
|
$res['data'] = $official;
|
|
} else {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '信息获取失败,请稍后重试!';
|
|
$res['data'] = (object)[];
|
|
}
|
|
$this->ajaxReturn($res);
|
|
}
|
|
|
|
// 用户申请
|
|
public function userApplyDistributor()
|
|
{
|
|
$distributor_id = I('post.distributor_id') ? I('post.distributor_id') : 0;
|
|
$mobile = I('post.mobile');
|
|
$invite_code = I('post.invite_code');
|
|
$uid = self::$uid; //用户id
|
|
$merchant_id = self::$merchant_id;
|
|
|
|
if (!$mobile || !$uid) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '必要信息缺失';
|
|
$res['data'] = (object)[];
|
|
$this->ajaxReturn($res);
|
|
}
|
|
|
|
$DistributorModel = new DistributorModel();
|
|
$ApplyModel = new ApplyModel();
|
|
$ConfigModel = new ConfigModel();
|
|
$GradeModel = new GradeModel();
|
|
$MessageModel = new RetailMessageModel();
|
|
$LinkModel = new LinkModel();
|
|
|
|
|
|
// 判断此用户是否是分销员
|
|
$identity = $DistributorModel->getFind(['uid' => $uid, 'status' => 0], 'id');
|
|
if ($identity) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '您已经是分销员,请勿重新申请!';
|
|
$res['data'] = (object)[];
|
|
$this->ajaxReturn($res);
|
|
}
|
|
|
|
//分销设置
|
|
$disConfigField = [
|
|
'is_customer_protect', //是否开启客户保护 1关闭 2开启
|
|
'customer_protect_type', //客户保护方式 1永久保护 2固定时间保护
|
|
'protect_date', //固定保护天数
|
|
];
|
|
$disConfig = $ConfigModel->getFind(['merchant_id' => $merchant_id], $disConfigField);
|
|
$hasLinkData = $LinkModel->getOrderFind(['uid' => $uid], '', 'add_time desc');
|
|
// 开启客户保护
|
|
if ($disConfig['is_customer_protect'] == 2) {
|
|
// 固定时间保护
|
|
if ($disConfig['customer_protect_type'] == 2) {
|
|
$protectEndTime = bcadd($hasLinkData['add_time'], $disConfig['protect_date'] * 86400);
|
|
if ($protectEndTime > time()) {
|
|
// 验证分销员是否被删除
|
|
$distributorId = $DistributorModel->getFindField(['id' => $hasLinkData['distributor_id'], 'status' => 0], 'id');
|
|
if ($distributorId) {
|
|
unset($invite_code);
|
|
$distributor_id = $hasLinkData['distributor_id'];
|
|
}
|
|
}
|
|
// 永久保护
|
|
} elseif ($disConfig['customer_protect_type'] == 1) {
|
|
// 验证分销员是否被删除
|
|
$distributorId = $DistributorModel->getFindField(['id' => $hasLinkData['distributor_id'], 'status' => 0], 'id');
|
|
if ($distributorId) {
|
|
unset($invite_code);
|
|
$distributor_id = $hasLinkData['distributor_id'];
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($invite_code) {
|
|
$superior = $DistributorModel->getFind(['invite_code' => $invite_code, 'merchant_id' => $merchant_id, 'status' => 0], 'id');
|
|
if (!$superior['id']) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '错误的邀请码!';
|
|
$res['data'] = (object)[];
|
|
$this->ajaxReturn($res);
|
|
}
|
|
|
|
$distributor_id = $superior['id'];
|
|
$distribution['join_type'] = 2;
|
|
}
|
|
|
|
// 手机号是否使用--有无申请信息
|
|
$mobile_filtrate['phone_number'] = $mobile;
|
|
$mobile_filtrate['merchant_id'] = $merchant_id;
|
|
$mobile_filtrate['status'] = ['in', [0, 1]];
|
|
$isMobile = $ApplyModel->getOrderFind($mobile_filtrate, 'id, status');
|
|
if ($isMobile) {
|
|
|
|
$res['status'] = 0;
|
|
$res['msg'] = '该手机号已有申请信息';
|
|
$res['data'] = (object)[];
|
|
|
|
if ($isMobile['status'] == 1 || $isMobile['status'] == 2) { // 后台是否删除此用户,删除后可以重新申请
|
|
$isDel = $ApplyModel->getFind(['phone_number' => $mobile, 'status' => 1, 'merchant_id' => $merchant_id]);
|
|
if (!$isDel) {
|
|
$this->ajaxReturn($res);
|
|
}
|
|
} else {
|
|
$this->ajaxReturn($res);
|
|
}
|
|
}
|
|
|
|
// 手机号是否使用--查询手机号有无分销员信息
|
|
$isUse = $ApplyModel->getFind(['phone_number' => $mobile, 'status' => 0, 'merchant_id' => $merchant_id]);
|
|
if ($isUse) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '该手机号已被使用!';
|
|
$res['data'] = (object)[];
|
|
$this->ajaxReturn($res);
|
|
}
|
|
|
|
// 是否已有申请信息
|
|
$audit_filtrate['merchant_id'] = $merchant_id;
|
|
$audit_filtrate['uid'] = $uid;
|
|
// $audit_filtrate['status'] = ['NEQ', -1];
|
|
$audit_filtrate['status'] = 0;
|
|
$detail = $ApplyModel->getOrderFind($audit_filtrate);
|
|
if ($detail) {
|
|
$res['status'] = 0;
|
|
$res['msg'] = '正在申请中,请勿重复申请!';
|
|
$res['data'] = (object)[];
|
|
$this->ajaxReturn($res);
|
|
}
|
|
|
|
$data['merchant_id'] = $merchant_id;
|
|
$data['uid'] = $uid;
|
|
$data['phone_number'] = $mobile;
|
|
$data['inviter_uid'] = $distributor_id;
|
|
$data['add_time'] = time();
|
|
|
|
// 查看是否需要审核
|
|
$isAudit = $ConfigModel->getFind(['merchant_id' => $merchant_id], 'is_check');
|
|
if ($isAudit['is_check'] == 1) {
|
|
$data['status'] = 0;
|
|
} else {
|
|
$data['status'] = 2;
|
|
}
|
|
|
|
$audit = $ApplyModel->addOrder($data);
|
|
if ($data['status'] == 2) { // 无需审核,分销员记录添加
|
|
// 获取最低的分销员等级
|
|
$grade = $GradeModel->getOrderFind(['merchant_id' => $merchant_id, 'status' => 0], 'id', 'proportion ASC');
|
|
if ($grade['id']) {
|
|
$distribution['grade_level_id'] = $grade['id'];
|
|
}
|
|
|
|
$distribution['merchant_id'] = $merchant_id;
|
|
$distribution['phone_number'] = $mobile;
|
|
$distribution['uid'] = $uid;
|
|
$distribution['pid'] = $distributor_id;
|
|
$distribution['invite_code'] = $DistributorModel->createInviteCode($merchant_id);
|
|
|
|
$distribution['add_time'] = time();
|
|
$distribution['join_type'] = 3;
|
|
$DistributorModel->addOrder($distribution);
|
|
|
|
if ($distributor_id) {
|
|
$DistributorModel->toSetInc(['id' => $distributor_id], 'total_invite', 1);
|
|
}
|
|
|
|
// 消息推送
|
|
$MessageData = [
|
|
'type' => 6,
|
|
'uid' => $uid,
|
|
'title' => '分销员审核通过',
|
|
'content' => '恭喜您,审核成功',
|
|
'link_path' => '/pages/distribution/center/center',
|
|
'add_time' => time(),
|
|
];
|
|
$MessageModel->addOrder($MessageData);
|
|
}
|
|
if ($audit) {
|
|
return $this->successAjaxReturn('申请成功', $data);
|
|
} else {
|
|
return $this->failAjaxReturn('申请失败,请稍后重试', (object)[]);
|
|
}
|
|
}
|
|
|
|
// 获取用户当前申请状态--前端判断用
|
|
public function getUserStatus()
|
|
{
|
|
$merchant_id = self::$merchant_id;
|
|
$uid = self::$uid; //用户id
|
|
|
|
$DistributorModel = new DistributorModel();
|
|
$ApplyModel = new ApplyModel();
|
|
$LinkModel = new LinkModel();
|
|
$ConfigModel = new ConfigModel();
|
|
|
|
// 用户是否是分销员
|
|
$isDistributor = $DistributorModel->getFind(['merchant_id' => $merchant_id, 'uid' => $uid, 'status' => 0], 'id');
|
|
if ($isDistributor['id']) {
|
|
$res['status'] = 1;
|
|
$res['msg'] = '您已经是分销员';
|
|
$res['data'] = 1;
|
|
$this->ajaxReturn($res);
|
|
}
|
|
//分销设置
|
|
$disConfigField = [
|
|
'is_open', //是否开启分销 0关闭 1开启
|
|
'is_customer_protect', //是否开启客户保护 1关闭 2开启
|
|
'customer_protect_type', //客户保护方式 1永久保护 2固定时间保护
|
|
'protect_date', //固定保护天数
|
|
'binding_type', //客户绑定方式 1购买商品绑定 2点击链接绑定
|
|
];
|
|
$disConfig = $ConfigModel->getFind(['merchant_id' => $merchant_id], $disConfigField);
|
|
$hasLinkData = $LinkModel->getOrderFind(['uid' => $uid], '', 'add_time desc');
|
|
$res['isInviteCodeDisplay'] = 1; //前端是否展示填写邀请码 1是 2否
|
|
// 开启客户保护
|
|
if ($disConfig['is_customer_protect'] == 2) {
|
|
// 固定时间保护
|
|
if ($disConfig['customer_protect_type'] == 2) {
|
|
$protectEndTime = bcadd($hasLinkData['add_time'], $disConfig['protect_date'] * 86400);
|
|
if ($protectEndTime > time()) {
|
|
// 验证分销员是否被删除
|
|
$isDelDistributor = $DistributorModel->getFind(['id' => $hasLinkData['distributor_id'], 'status' => 0]);
|
|
if ($isDelDistributor) {
|
|
$res['isInviteCodeDisplay'] = 2;
|
|
}
|
|
}
|
|
} elseif ($disConfig['customer_protect_type'] == 1) {
|
|
// 永久保护
|
|
$res['isInviteCodeDisplay'] = 2;
|
|
}
|
|
}
|
|
// 申请状态
|
|
$audit_filtrate['merchant_id'] = $merchant_id;
|
|
$audit_filtrate['uid'] = $uid;
|
|
$apply_status = $ApplyModel->getOrderFind($audit_filtrate, 'status', 'id desc');
|
|
if ($apply_status['status'] === 0 || $apply_status['status'] === '0') {
|
|
$res['status'] = 1;
|
|
$res['msg'] = '申请审核中,请耐心等待...';
|
|
$res['data'] = 2;
|
|
$this->ajaxReturn($res);
|
|
}
|
|
$res['status'] = 1;
|
|
$res['msg'] = '您可以申请!';
|
|
$res['data'] = 3;
|
|
$this->ajaxReturn($res);
|
|
}
|
|
|
|
// 获取用户申请记录
|
|
public function getUserApplyRecord()
|
|
{
|
|
$page = I('post.page') ? I('post.page') : 1;
|
|
$limit = I('post.limit') ? I('post.limit') : 10;
|
|
$uid = self::$uid; //用户id
|
|
$merchant_id = self::$merchant_id;
|
|
|
|
$ApplyModel = new ApplyModel();
|
|
|
|
$where = [
|
|
'uid' => $uid,
|
|
'merchant_id' => $merchant_id
|
|
];
|
|
$field = [
|
|
'add_time',
|
|
'status',
|
|
'back_reason',
|
|
'edit_time',
|
|
];
|
|
$applyList['list'] = $ApplyModel->getSelect($where, $field, 'id DESC', $page . ',' . $limit);
|
|
$applyList['count'] = $ApplyModel->getCount($where);
|
|
if ($applyList['count']) {
|
|
// 申请状态处理
|
|
foreach ($applyList['list'] as &$value) {
|
|
$value['status_msg'] = $this->getApplyStatusText($value['status']);
|
|
}
|
|
$this->successAjaxReturn('数据获取成功!', $applyList);
|
|
} else {
|
|
$this->failAjaxReturn('尚未拥有申请记录!');
|
|
}
|
|
}
|
|
|
|
// 通过邀请码绑定上级
|
|
public function bindSuperiorByInviteCode()
|
|
{
|
|
$invite_code = I('post.invite_code');
|
|
$uid = self::$uid; //用户id
|
|
$merchant_id = self::$merchant_id;
|
|
|
|
$DistributorModel = new DistributorModel();
|
|
|
|
$distributor = $DistributorModel->getFind(['uid' => $uid, 'merchant_id' => $merchant_id, 'status' => 0], 'id, pid');
|
|
|
|
if ($distributor['pid']) {
|
|
$this->failAjaxReturn('请勿重复绑定!');
|
|
}
|
|
|
|
$superior = $DistributorModel->getFind(['invite_code' => $invite_code, 'merchant_id' => $merchant_id], 'id');
|
|
if (!$superior) {
|
|
$this->failAjaxReturn('错误的邀请码!');
|
|
}
|
|
|
|
if ($superior['id'] == $distributor['id']) {
|
|
$this->failAjaxReturn('这是您自己的邀请码哦!');
|
|
}
|
|
|
|
$result = $DistributorModel->updateOrder(['uid' => $uid, 'merchant_id' => $merchant_id], ['pid' => $superior['id']]);
|
|
if ($result) {
|
|
$DistributorModel->toSetInc(['id' => $superior['id']], 'total_invite', 1);
|
|
|
|
$this->successAjaxReturn('关联成功!');
|
|
} else {
|
|
$this->failAjaxReturn('关联失败,请稍后重试!');
|
|
}
|
|
}
|
|
|
|
/* ******************************************* 下面是一些方法 ******************************************** */
|
|
// 获取当前申请状态文案
|
|
private function getApplyStatusText($status)
|
|
{
|
|
switch ((int)$status) {
|
|
case 0:
|
|
return 'Your application is under review, please wait!!';
|
|
break;
|
|
case 1:
|
|
return 'Your application has been approved!';
|
|
break;
|
|
case -1:
|
|
return 'Your application has been rejected!';
|
|
break;
|
|
case 2:
|
|
return '您已是分销员,请勿重新申请!';
|
|
break;
|
|
default:
|
|
return 'System maintenance, please try again later';
|
|
}
|
|
}
|
|
|
|
// 错误提示
|
|
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);
|
|
}
|
|
}
|