Files
2021-08-20 21:11:43 +08:00

171 lines
5.5 KiB
PHP

<?php
/**
* Created by Vim
* User: lcc
* Desc: 分销申请
* Date: 2021.08.19
* Time: 14:08
*/
require_once APPPATH . 'controllers/wxapp/Wxapp.php';
class Apply extends Wxapp{
function __construct($inputs, $app_key){
parent::__construct($inputs, $app_key);
$this->login_white = '';//
$this->check_status = array();//用户状态校验
$this->check_mobile = array();//需要手机号
$this->check_headimg = array();//授权微信信息
$this->majia_white = array('get');//超级管理员披上马甲可操作权限
$this->load->model('app/deal_apply_model','apply_model');
}
protected function get(){
$uid = $this->session['uid'];
$page = $this->input_param('page');
$size = $this->input_param('size');
!$page && $page = 1;
!$size && $size = 20;
$where = [
'app_id' => $this->app_id,
'opt_uid' => $uid,
'status' => 0
];
$rows = $this->apply_model->select($where,'id desc',$page,$size,'id,remark,status,app_uid');
$total = $this->apply_model->count($where);
$list = [];
if($rows){
$app_uids = array_column($rows,'app_uid');
$user_rows = $this->app_user_model->get_map_by_ids($app_uids,'id,mobile');
foreach($rows as $key => $val){
$list[] = [
'id' => $val['id'],
'mobile' => $user_rows[$val['app_uid']] ? $user_rows[$val['app_uid']][0]['mobile']:'',
'remark' => $val['remark'],
];
}
}
$data = [
'list' => $list,
'total' => $total
];
if($page==1){
$data['statistics'] = $this->statistics();
}
return $data;
}
protected function put(){
$uid = $this->session['uid'];
$id = $this->input_param('id');
$status = $this->input_param('status');
$row = $this->apply_model->get(['id'=>$id,'status'=>0]);
if(!$row){
throw new Exception('参数错误', API_CODE_INVILD_PARAM);
}
$update = [
'status' => $status==1 ? 1 : -1
];
$res = $this->apply_model->update($update,['id'=>$row['id']]);
if($res){
if($status==1){
$this->app_user_model->update(['deal_uid'=>$uid,'dealer'=>1],['id'=>$row['app_uid']]);
}
throw new Exception('保存成功', API_CODE_SUCCESS);
}else{
throw new Exception('保存失败', API_CODE_FAIL);
}
}
protected function post(){
$uid = $this->session['uid'];
$f_uid = $this->input_param('from_uid');
$remark = $this->input_param('remark');
if(!$f_uid || !$remark){
throw new Exception('参数错误', API_CODE_INVILD_PARAM);
}
if($this->session['dealer']){
throw new Exception('您已是分销人员', API_CODE_FAIL);
}
$where = [
'app_id' => $this->app_id,
'app_uid' => $uid,
'status' => 0
];
$row = $this->apply_model->get($where);
if($row){
throw new Exception('申请成功', API_CODE_SUCCESS);
}
$add_data = [
'app_id' => $this->app_id,
'app_uid' => $uid,
'opt_uid' => $f_uid,
'remark' => $remark,
'c_time' => time()
];
$this->apply_model->add($add_data);
throw new Exception('申请成功', API_CODE_SUCCESS);
}
//获取我的分销用户
protected function get_dealer(){
$page = $this->input_param('page');
$size = $this->input_param('size');
!$page && $page = 1;
!$size && $size = 20;
$where = [
'deal_uid' => $this->session['uid'],
'dealer' => 1
];
$rows = $this->app_user_model->select($where,'id desc',$page,$size,'id,mobile');
$total = $this->app_user_model->count($where);
$list = [];
if($rows){
foreach($rows as $key => $val){
$apply = $this->apply_model->get(['app_id'=>$this->app_id,'app_uid'=>$val['id'],'status'=>1],'remark');
$list[] = [
'id' => $val['id'],
'mobile' => $val['mobile'],
'remark' => $apply['remark']
];
}
}
$data = [
'list' => $list,
'total' => $total
];
if($page==1){
$data['statistics'] = $this->statistics();
}
return $data;
}
//解除分销关系
protected function put_dealer(){
$deal_uid = $this->session['uid'];
$id = $this->input_param('id');
$row = $this->app_user_model->get(['deal_uid'=>$deal_uid,'id'=>$id,'dealer'=>1]);
if(!$row){
throw new Exception('参数错误', API_CODE_INVILD_PARAM);
}
$res = $this->app_user_model->update(['deal_uid'=>0,'dealer'=>0],['id'=>$row['id']]);
if($res){
throw new Exception('保存成功', API_CODE_SUCCESS);
}else{
throw new Exception('保存失败', API_CODE_FAIL);
}
}
private function statistics(){
$uid = $this->session['uid'];
$dealers = $this->app_user_model->count(['deal_uid'=>$uid,'dealer'=>1]);
$applying = $this->apply_model->count(['status'=>0,'opt_uid'=>$uid]);
return ['dealer' => $dealers,'applying' => $applying];
}
}