Person.php 14.5 KB
<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\admin\model\Bank;
use app\admin\model\off\Line;
use think\Db;
use think\Validate;
/**
 * 个人信息接口**
 */
class Person extends Api
{
    protected  $noNeedLogin = ['getScore'];
    protected $noNeedRight = ['getScore'];
    protected $person = 0;//个人
    protected $company = 1;//企业
    protected $user_id = '';//token存贮user_id
    public function _initialize()
    {
        parent::_initialize();
        $this->user_id = $this->auth->getUserId();
    }

    /**
     * @ApiTitle    (获取会员信息)
     * @ApiSummary  (获取会员信息)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/person/getUserInfo)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturn ({
            "code": 1,
            "msg": "成功",
            "time": "1553838039",
            "data": {
                "id": 26,
                "username": "风起时呀",//用户名
                "mobile": "",//联系方式
                "type": 0,//类型 0:个人 1:企业
                "address": "", //上门地址
                "id_num": "",//身份证号
                "id_img": "",//身份证件
                "company_name": "",//公司名字
                "license_num": "",//公司编码
                "license_img": ""//公司营业执照
                "money":100//账户余额
                "avatar"://头像
            }
            })
     */
    public function getUserInfo(){
        if($this->request->isGet()){
            $data = Db::table('gc_user')
                ->where(['id'=>$this->user_id])
                ->field('id,nickname,username,mobile,type,address,id_num,id_img,company_name,license_num,license_img,money,avatar')
                ->find();
            $this->success('成功', $data);
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (修改会员信息)
     * @ApiSummary  (修改会员信息)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/person/updateInfo)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="username", type="string", required=true, description="用户名(姓名)")
     * @ApiParams   (name="mobile", type="string", required=true, description="联系电话")
     * @ApiParams   (name="address", type="string", required=true, description="上门地址")
     * @ApiParams   (name="type", type="integer", required=true, description="用户类型(0:个人,1企业)")
     * @ApiParams   (name="id_num", type="string", required=true, description="身份证号(个人必须)")
     * @ApiParams   (name="id_img", type="string", required=true, description="身份证件(个人必须)")
     * @ApiParams   (name="company_name", type="string", required=true, description="公司名称(企业必须)")
     * @ApiParams   (name="license_num", type="string", required=true, description="公司编号(企业必须)")
     * @ApiParams   (name="license_img", type="string", required=true, description="公司营业执照(企业必须)")
     * @ApiReturn({
            "code": 1,
            "msg": "保存成功",
            "time": "1553838137",
            "data": null
            })
     */
    public function updateInfo(){
        if ($this->request->isPost()) {
            $raw_data = $this->request->post();
            if(empty($raw_data['type'])){
                $this->error('请选择用户类型');
            }
            $type = $raw_data['type'];
            if($type == $this->person){
                //验证类型为个人的数据
                $rule = config('site.person');
                $validate = new Validate($rule['rule'],$rule['msg']);
                if (!$validate->check($raw_data)) {
                    $this->error($validate->getError());
                }
            }else if($type == $this->company){
                //验证类型为企业的数据
                $company_rule = config('site.company');
                $company_validate = new Validate($company_rule['rule'],$company_rule['msg']);
                if (!$company_validate->check($raw_data)) {
                    $this->error($company_validate->getError());
                }
            }else{
                $this->error('用户类型不合法');
            }
            $user = new \app\admin\model\User();
            $data = $user->where(['id'=>$this->user_id])->update($raw_data);
            if($data){
                $this->success('保存成功');
            }else{
                $this->error('保存失败');
            }
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (上传文件)
     * @ApiSummary  (上传文件)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/person/upload)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="images[]", type="file", required=true, description="上传文件(图片)")
     * @ApiReturn ({
            "code": 1,
            "msg": "上传成功",
            "time": "1553777266",
            "data":[
                {
                    "/uploads/20190328/0179b6b111307ef8be3c832ef808e1c1.jpg,/uploads/20190328/42bbc2f2692023d43957c3ee0b580443.jpg"
                }
            ]
            })
     */
    public function upload(){
        // 获取表单上传文件
        $files = request()->file('images');
        if (empty($files)) {
            $this->error('未检出文件上传');
        }
        $countFile = count($files);
        if($countFile > 9){
            $this->error('最多上传9张图片');
        }
        $date = date('Ymd',time());
        $host = \app\admin\model\User::DOMAIN;
        $images = '';
        foreach($files as $file){
            //移动到框架应用根目录/public/uploads/ 目录下
            //允许文件大小200k
            $info = $file->validate(['size'=>204800,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                $images .= $host.'/uploads/'.$date.'/'.$info->getFilename().',';
            }else{
                // 上传失败获取错误信息
                $this->error($file->getError());
            }
        }
        $this->success('上传成功',rtrim($images,','));
    }

    /**
     * @ApiTitle    (账单明细)
     * @ApiSummary  (账单明细)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/person/accountDetail)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="page", type="integer", required=true, description="分页页码")
     * @ApiReturn ({
            "code": 1,
            "msg": "成功",
            "time": "1554270371",
            "data": [
                {
                    "id": 1,
                    "type": 0,//类型 0:充值 1:提现
                    "money": 0.01,//充值或提现金额
                    "createtime": "2019-04-03 13:03:57"//时间
                }
            ]
            })
     */
    public function accountDetail(){
        if($this->request->isGet()){
            $page = $this->request->get('page');//分页页码
            $limit = config('site.page_limit');//分页限制数量
            $rule = config('site.gift_pages');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['page'=>$page])) {
                $this->error($validate->getError());
            }
            $data = Db::table('gc_account')
                ->where(['uid'=>$this->user_id])
                ->page($page,$limit)
                ->field('id,type,money,createtime')
                ->select();
            foreach($data as &$value){
                $value['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
            }
            $this->success('成功', $data);
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (绑定银行卡)
     * @ApiSummary  (绑定银行卡)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/person/bindBank)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="bank_num", type="integer", required=true, description="银行卡号")
     * @ApiParams   (name="open_bank", type="string", required=true, description="开户行")
     * @ApiReturn ({
        "code": 1,
        "msg": "保存成功",
        "time": "1554270371",
        "data": null
        })
     */
    public function bindBank(){
        if($this->request->isPost()) {
            $bank_num = $this->request->post('bank_num');//银行卡号
            $open_bank = $this->request->post('open_bank');//开户行
            $rule = config('site.bank');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['bank_num'=>$bank_num,'open_bank'=>$open_bank])) {
                $this->error($validate->getError());
            }
            $bank = new Bank();
            $data = $bank->where(['uid'=>$this->user_id,'bank_num'=>$bank_num])->find();
            if($data){
                $this->error('不能重复绑定');
            }
            $data1 = $bank::create(['uid'=>$this->user_id,'bank_num'=>$bank_num,'open_bank'=>$open_bank]);
            if($data1){
                $this->success('保存成功');
            }else{
                $this->error('保存失败');
            }
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (银行卡列表)
     * @ApiSummary  (银行卡列表)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/person/bankList)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturn ([
            {
                "id": 3,//银行卡id
                "bank_num": "6228480028807779179",//银行卡号
                "open_bank": "农业银行"//开户行
            },
            {
                "id": 7,
                "bank_num": "6228480028306448000",
                "open_bank": "open_bank"
            }
            ])
     */
    public function bankList(){
        if($this->request->isGet()){
            $data = Db::table('gc_bank')
                ->where(['uid'=>$this->user_id])
                ->field('id,bank_num,open_bank')
                ->order('id desc')
                ->select();
            $this->success('成功', $data);
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * @ApiTitle    (修改银行卡)
     * @ApiSummary  (修改银行卡)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/person/updateBank)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="integer", required=true, description="银行卡id")
     * @ApiParams   (name="bank_num", type="integer", required=true, description="银行卡号")
     * @ApiParams   (name="open_bank", type="string", required=true, description="开户行")
     * @ApiReturn ({
        "code": 1,
        "msg": "修改成功",
        "time": "1555652055",
        "data": null
        })
     */
    public function updateBank(){
        if($this->request->isPost()) {
            $id = $this->request->post('id');//银行卡id
            $bank_num = $this->request->post('bank_num');//银行卡号
            $open_bank = $this->request->post('open_bank');//开户行
            $rule = config('site.update_bank');
            $validate = new Validate($rule['rule'],$rule['msg']);
            if (!$validate->check(['id'=>$id,'bank_num'=>$bank_num,'open_bank'=>$open_bank])) {
                $this->error($validate->getError());
            }
            $data = Db::table('gc_bank')->where('id','neq',$id)->where(['uid'=>$this->user_id,'bank_num'=>$bank_num])->find();
            if($data){
                $this->error('此银行卡已绑定');
            }
            $bank = new Bank();
            $data1 = $bank->where(['id'=>$id,'uid'=>$this->user_id])->update(['open_bank'=>$open_bank,'bank_num'=>$bank_num]);
            if($data1){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * 根据规则增加积分
     * @param $share_uid
     * @param $type
     * @return bool
     */
    public function getScore($share_uid,$type){
        $class = '';
        if($type == 'share_purchase'){
            $scoreRule = Db::table('gc_sscore')
                ->where('set_rule','like','%购买%')
                ->field('set_score')
                ->find();
            $class = 1;
        }else if($type == 'share_sell'){
            $scoreRule = Db::table('gc_sscore')
                ->where('set_rule','like','%卖废品%')
                ->field('set_score')
                ->find();
            $class = 0;
        }
//        $score = config('site.'.$type);
        $score = 0;
        if($scoreRule){
            $score = $scoreRule['set_score'];
        }
        $offlineModel = new Line();
        //此处判断分享用户否已有上线,如果有,则不能成为其他的下线
        $each_other = $offlineModel->where(['off_uid'=>$share_uid])->select();
        //判断本登录用户是否有上线
        $other = $offlineModel->where(['off_uid'=>$this->user_id])->select();
        //分享人是自己无效
        if($share_uid != $this->user_id){
            //分享用户有上线
            if($each_other){
                $on_uid = array_unique(array_column($each_other,'uid'))[0];
                //给上线增加积分
                $userModel = new \app\admin\model\User();
                $userModel->where(['id'=>$on_uid,'status'=>'normal'])->setInc('score', $score);
                //创建上线用户的积分记录
                $offlineModel->create(['uid'=>$on_uid,'s_score'=>$score,'off_uid'=>$share_uid,'type'=>$class]);
            }else{
                //本登录用户没有上线
                if(!$other){
                    //给分享用户增加积分
                    $userModel = new \app\admin\model\User();
                    $userModel->where(['id'=>$share_uid,'status'=>'normal'])->setInc('score', $score);
                    //成为分享用户的下线并创建积分记录
                    $offlineModel->create(['uid'=>$share_uid,'s_score'=>$score,'off_uid'=>$this->user_id,'type'=>$class]);
                }
            }
        }
    }

}