作者 chencheng

1

... ... @@ -163,6 +163,7 @@ class Wechat extends Api
if(empty(request()->param('code'))){
return $this->error('非法操作');
}
$_REQUEST['code'] = request()->param('code');
try{
$app = WehcatModule::getInstance()->initWechat();
$oauth = $app->oauth;
... ... @@ -208,23 +209,37 @@ class Wechat extends Api
/**注册 分销关系 如 https://coupon.xp.yn.cn?store_id=1&activity_id=3&share_id=2
/**注册 分销关系 如 https://coupon.cc/index.php/api/v1/wechat/register_relation?store_id=1&activity_id=3&share_id=2
* store_id,activity_id
* 关系 user表 id 和 pid
*/
public function register_relation(){
//参数
$param = request()->param();
if(empty($param['share_id'])){return $this->error('信息错误');}
if(empty($param['store_id'])){return $this->error('门店信息错误');}
if(empty($param['activity_id'])){return $this->error('活动信息错误');}
if(!request()->isPost()){
return $this->error('请求错误');
}
$my_user_id = $this->auth->id;
if(empty($my_user_id)){return $this->error('用户信息错误');}
if(empty($param['store_id'])){return $this->error('门店信息错误');}
//记录门店浏览统计
$this->store_record($param['store_id'],$my_user_id);
if(empty($param['share_id'])){return $this->error('信息错误');}
if(empty($param['activity_id'])){return $this->error('活动信息错误');}
//检测是存在关系
$w['id'] = $my_user_id;
$myInfo = Db::name('user')->where($w)->find();
if(empty($myInfo['pid'])){
//更新分销关系
//本身是自己门店的代理角色,则无效,如果点击别的门店分销,则对于别的门店来说,不是代理,则要统计分销
$w1['user_id'] = $my_user_id;
$w1['verification_store_id'] = $param['store_id'];
$role = Db::name('verification_staff')->where($w1)->find();
if(in_array($role['type'],['1','3'])){
return $this->error('我是代理不记录');
}
Db::name('user')->where($w)->update(['id'=>$myInfo['pid']]);
return $this->success('更新关系');
}else{
... ... @@ -238,8 +253,16 @@ class Wechat extends Api
}
}
/*
门店分销记录
*/
private function store_record($store_id,$user_id){
$data['verification_store_id'] = $store_id;
$data['user_id'] = $user_id;
$data['createtime'] = time();
Db::name('verification_store_user_record')->insert($data);
}
/*
... ... @@ -318,4 +341,63 @@ class Wechat extends Api
file_put_contents("1_cc.log",json_encode($xmlarray),FILE_APPEND);
*/
}
/** 支付成功 则进入分销链条 2级分销
* 1. 上级 是代理人 则直接获得佣金 企业付款
* 2. 上级 是普通人 则直接获得某个门店的所有礼品券
* 3. 上级的上级 是代理人则直接获得佣金 企业付款
* 4. 上级的上级 是普通人,则直接获得某个门店的所有礼品券
* A -> B -> C 下单
* $order_info 支付成功后的订单信息
*/
public function sales_distribution($order_info){
$w['id'] = $order_info['user_id'];
$myInfo = Db::name('user')->where($w)->find();
//是否与上级
if(!empty($myInfo['pid'])){
//上级
$this->getParent($myInfo['pid'],$order_info['verification_store_id'],$order_info['id']);
//上级的上级
$w1['id'] = $myInfo['pid'];
$myInfoP = Db::name('user')->where($w1)->find();
$this->getParent($myInfoP['id'],$order_info['verification_store_id'],$order_info['id']);
}
}
//查询上级
private function getParent($pid,$verification_store_id,$order_id){
//上级身份
$time = time();
$w1['user_id'] = $pid;
$w1['verification_store_id'] = $verification_store_id;
$role = Db::name('verification_staff')->where($w1)->find();
if(in_array($role['type'],['1','3'])){
//属于代理 企业付款到佣金
return '';
}else{
//属于普通用户 发送代金券
$w1['verification_store_id'] = $verification_store_id;
$w1['closetime'] = ['<',$time];
$coupon = Db::name('verification_coupon')->where($w1)->select();
if(!empty($coupon)){
foreach($coupon as $k=>$v){
$save = [];
$save['verification_coupon_id'] = $v['id'];
$save['verification_store_id'] = $v['verification_store_id'];
$save['user_id'] = $pid;
$save['type'] = 0;
$save['closetime'] = $v['closetime'];
$save['receive_no'] = $time.uniqid();
$res = (new \app\api\controller\v1\Index())->build($save['receive_no']);
$save['qr_code'] = $res;//二维码
$save['createtime'] = $time;
$save['verification_order_id'] = $order_id;
}
}
}
}
}
... ...