作者 chencheng

1

@@ -163,6 +163,7 @@ class Wechat extends Api @@ -163,6 +163,7 @@ class Wechat extends Api
163 if(empty(request()->param('code'))){ 163 if(empty(request()->param('code'))){
164 return $this->error('非法操作'); 164 return $this->error('非法操作');
165 } 165 }
  166 + $_REQUEST['code'] = request()->param('code');
166 try{ 167 try{
167 $app = WehcatModule::getInstance()->initWechat(); 168 $app = WehcatModule::getInstance()->initWechat();
168 $oauth = $app->oauth; 169 $oauth = $app->oauth;
@@ -208,23 +209,37 @@ class Wechat extends Api @@ -208,23 +209,37 @@ class Wechat extends Api
208 209
209 210
210 211
211 - /**注册 分销关系 如 https://coupon.xp.yn.cn?store_id=1&activity_id=3&share_id=2 212 + /**注册 分销关系 如 https://coupon.cc/index.php/api/v1/wechat/register_relation?store_id=1&activity_id=3&share_id=2
212 * store_id,activity_id 213 * store_id,activity_id
213 * 关系 user表 id 和 pid 214 * 关系 user表 id 和 pid
214 */ 215 */
215 public function register_relation(){ 216 public function register_relation(){
216 //参数 217 //参数
217 $param = request()->param(); 218 $param = request()->param();
218 - if(empty($param['share_id'])){return $this->error('信息错误');}  
219 - if(empty($param['store_id'])){return $this->error('门店信息错误');}  
220 - if(empty($param['activity_id'])){return $this->error('活动信息错误');} 219 + if(!request()->isPost()){
  220 + return $this->error('请求错误');
  221 + }
221 $my_user_id = $this->auth->id; 222 $my_user_id = $this->auth->id;
  223 +
222 if(empty($my_user_id)){return $this->error('用户信息错误');} 224 if(empty($my_user_id)){return $this->error('用户信息错误');}
  225 + if(empty($param['store_id'])){return $this->error('门店信息错误');}
  226 + //记录门店浏览统计
  227 + $this->store_record($param['store_id'],$my_user_id);
  228 +
  229 + if(empty($param['share_id'])){return $this->error('信息错误');}
  230 + if(empty($param['activity_id'])){return $this->error('活动信息错误');}
223 //检测是存在关系 231 //检测是存在关系
224 $w['id'] = $my_user_id; 232 $w['id'] = $my_user_id;
225 $myInfo = Db::name('user')->where($w)->find(); 233 $myInfo = Db::name('user')->where($w)->find();
226 if(empty($myInfo['pid'])){ 234 if(empty($myInfo['pid'])){
227 //更新分销关系 235 //更新分销关系
  236 + //本身是自己门店的代理角色,则无效,如果点击别的门店分销,则对于别的门店来说,不是代理,则要统计分销
  237 + $w1['user_id'] = $my_user_id;
  238 + $w1['verification_store_id'] = $param['store_id'];
  239 + $role = Db::name('verification_staff')->where($w1)->find();
  240 + if(in_array($role['type'],['1','3'])){
  241 + return $this->error('我是代理不记录');
  242 + }
228 Db::name('user')->where($w)->update(['id'=>$myInfo['pid']]); 243 Db::name('user')->where($w)->update(['id'=>$myInfo['pid']]);
229 return $this->success('更新关系'); 244 return $this->success('更新关系');
230 }else{ 245 }else{
@@ -238,8 +253,16 @@ class Wechat extends Api @@ -238,8 +253,16 @@ class Wechat extends Api
238 } 253 }
239 } 254 }
240 255
  256 + /*
  257 + 门店分销记录
241 258
242 - 259 + */
  260 + private function store_record($store_id,$user_id){
  261 + $data['verification_store_id'] = $store_id;
  262 + $data['user_id'] = $user_id;
  263 + $data['createtime'] = time();
  264 + Db::name('verification_store_user_record')->insert($data);
  265 + }
243 266
244 267
245 /* 268 /*
@@ -318,4 +341,63 @@ class Wechat extends Api @@ -318,4 +341,63 @@ class Wechat extends Api
318 file_put_contents("1_cc.log",json_encode($xmlarray),FILE_APPEND); 341 file_put_contents("1_cc.log",json_encode($xmlarray),FILE_APPEND);
319 */ 342 */
320 } 343 }
  344 +
  345 +
  346 + /** 支付成功 则进入分销链条 2级分销
  347 + * 1. 上级 是代理人 则直接获得佣金 企业付款
  348 + * 2. 上级 是普通人 则直接获得某个门店的所有礼品券
  349 + * 3. 上级的上级 是代理人则直接获得佣金 企业付款
  350 + * 4. 上级的上级 是普通人,则直接获得某个门店的所有礼品券
  351 + * A -> B -> C 下单
  352 + * $order_info 支付成功后的订单信息
  353 + */
  354 +
  355 + public function sales_distribution($order_info){
  356 + $w['id'] = $order_info['user_id'];
  357 + $myInfo = Db::name('user')->where($w)->find();
  358 + //是否与上级
  359 + if(!empty($myInfo['pid'])){
  360 + //上级
  361 + $this->getParent($myInfo['pid'],$order_info['verification_store_id'],$order_info['id']);
  362 + //上级的上级
  363 + $w1['id'] = $myInfo['pid'];
  364 + $myInfoP = Db::name('user')->where($w1)->find();
  365 + $this->getParent($myInfoP['id'],$order_info['verification_store_id'],$order_info['id']);
  366 +
  367 + }
  368 + }
  369 +
  370 + //查询上级
  371 + private function getParent($pid,$verification_store_id,$order_id){
  372 + //上级身份
  373 + $time = time();
  374 + $w1['user_id'] = $pid;
  375 + $w1['verification_store_id'] = $verification_store_id;
  376 + $role = Db::name('verification_staff')->where($w1)->find();
  377 + if(in_array($role['type'],['1','3'])){
  378 + //属于代理 企业付款到佣金
  379 + return '';
  380 + }else{
  381 + //属于普通用户 发送代金券
  382 + $w1['verification_store_id'] = $verification_store_id;
  383 + $w1['closetime'] = ['<',$time];
  384 + $coupon = Db::name('verification_coupon')->where($w1)->select();
  385 + if(!empty($coupon)){
  386 + foreach($coupon as $k=>$v){
  387 + $save = [];
  388 + $save['verification_coupon_id'] = $v['id'];
  389 + $save['verification_store_id'] = $v['verification_store_id'];
  390 + $save['user_id'] = $pid;
  391 + $save['type'] = 0;
  392 + $save['closetime'] = $v['closetime'];
  393 + $save['receive_no'] = $time.uniqid();
  394 + $res = (new \app\api\controller\v1\Index())->build($save['receive_no']);
  395 + $save['qr_code'] = $res;//二维码
  396 + $save['createtime'] = $time;
  397 + $save['verification_order_id'] = $order_id;
  398 + }
  399 + }
  400 + }
  401 + }
  402 +
321 } 403 }