Person.php 9.9 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//账户余额
            }
            })
     */
    public function getUserInfo(){
        if($this->request->isGet()){
            $data = Db::table('gc_user')
                ->where(['id'=>$this->user_id])
                ->field('id,username,mobile,type,address,id_num,id_img,company_name,license_num,license_img,money')
                ->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="image", 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());
        $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 .= '/uploads/'.$date.'/'.$info->getFilename().',';
            }else{
                // 上传失败获取错误信息
                $this->error($file->getError());
            }
        }
        $this->success('上传成功',rtrim($images,','));
    }

    /**
     * @ApiTitle    (账单明细)
     * @ApiSummary  (账单明细)
     * @ApiMethod   (POST)
     * @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::create(['uid'=>$this->user_id,'bank_num'=>$bank_num,'open_bank'=>$open_bank]);
            if($data){
                $this->success('保存成功');
            }else{
                $this->error('保存失败');
            }
        }else{
            $this->error('请求方式错误');
        }
    }

    /**
     * 根据规则增加积分
     * @param $share_uid
     * @param $type
     * @return bool
     */
    public function getScore($share_uid,$type){
        $score = config('site.'.$type);
        //给分享用户增加积分
        $userModel = new \app\admin\model\User();
        $user = $userModel->where(['id'=>$share_uid,'status'=>'normal'])->setInc('score', $score);
        //成为分享用户的下线
        $offlineModel = new Line();
        $off = $offlineModel->create(['uid'=>$share_uid,'s_score'=>$score,'off_uid'=>$this->user_id]);
        if($user && $off){
            return true;
        }
        return false;
    }

}