Company.php 9.9 KB
<?php
namespace app\mobile\controller;

use think\Validate;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use app\common\controller\Api;
use app\common\library\Sms as Smslib;
use app\mobile\controller\Sms;
use app\mobile\model\CompanyUser;
use app\mobile\model\Agreement;
use app\mobile\model\Exam;
use app\mobile\model\CourseOrder;
use app\mobile\model\CourseAppraise;
use app\mobile\model\SecretOrder;
use app\mobile\model\ScoreSpec;
use app\mobile\model\ScoreOrder;
use app\mobile\model\QuestionCollect;
use app\mobile\model\CourseCollect;
use app\mobile\model\QuestionNote;
use app\mobile\model\Message;
use app\mobile\model\Feedback;
use app\mobile\model\Problem;
use addons\epay\library\Service;

/**
 * 我的(企业)接口
 * @ApiWeigh (65)
 */
class Company extends Api
{
	protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];
    protected $company_id = 0;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\mobile\model\Company;
        if(!$this->company_id){
            $user = $this->auth->getUser();
            if($user['group_id'] == 0){
                $this->error('您还不是企业管理员');
            }
            $this->company_id = $this->model->where('user_id',$user['id'])->value('id');
        }
    }

    /**
     * @ApiWeigh (67)
     * @ApiTitle    (我的密卷)
     * @ApiSummary  (我的密卷)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
        "code": 1, 
        "msg": "成功", 
        "time": "1600499807", 
        "data": [
            {
                "id": 10, //密卷订单ID
                "pay_price": "500.00", //实际支付金额
                "people_num": 20, //密卷规格人数(斜杠后数字)
                "is_top": "1", //是否顶配:0=否,1=是
                "secret": {
                    "title": "测试密卷", 
                    "do_num": 20
                }, 
                "user_count": 3 //当前企业下员工含管理员数量(斜杠前数字)
            }
        ]
    })
     */
    public function mySecret(){
        // 查找所有密卷
        $list = SecretOrder::with(['secret'])
            ->where('company_id',$this->company_id)
            ->where('user_id',$this->auth->id)
            ->where('pay_status','1')
            ->select();
        $user_count = CompanyUser::where('company_id',$this->company_id)
                ->where('status','1')
                ->count();
        foreach ($list as $v) {
            // 当前企业下员工含管理员人数
            $v['user_count'] = $user_count + 1;
            $v->visible(['id','pay_price','people_num','is_top','secret'])->append(['user_count']);
            $v->getRelation('secret')->visible(['title']);
        }
        $this->success('成功',$list);
    }

    /**
     * @ApiWeigh (65)
     * @ApiTitle    (我的课程)
     * @ApiSummary  (我的课程)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1600081718",
        "data": [{
            "id": 9, //密卷订单ID
            "pay_price": "50.00", //实际支付金额
            "people_num": 20, //限制人数
            "is_top": "0", //是否顶配:0=否,1=是
            "secret": { //密卷信息
                "title": "测试密卷", //密卷标题
                "do_num": "" //做过人数
            },
            "is_have_qi": 0 //是否有企字:0=否,1=是
        }]
    })
     */
    public function myCourse(){
        // 查找所有密卷
        $list = CourseOrder::with(['course'])
            ->where('company_id',$this->company_id)
            ->where('user_id',$this->auth->id)
            ->where('pay_status','1')
            ->select();
        $user_count = CompanyUser::where('company_id',$this->company_id)
                ->where('status','1')
                ->count();
        foreach ($list as $v) {
            // 当前企业下员工含管理员人数
            $v['user_count'] = $user_count + 1;
            $v->visible(['id','pay_price','people_num','is_top','course'])->append(['user_count']);
            $v->getRelation('course')->visible(['cover','title']);
        }
        $this->success('成功',$list);
    }

    /**
     * @ApiWeigh (99)
     * @ApiTitle    (企业成员管理)
     * @ApiSummary  (企业成员管理)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="status", type="string", required=false, description="0=未审核,1=已通过")
     * @ApiParams   (name="page", type="inter", required=false, description="当前页(默认1)")
     * @ApiParams   (name="page_num", type="inter", required=false, description="每页显示数据个数(默认10)")
     *
     * @ApiReturn({
        "code": 1, 
        "msg": "成功", 
        "time": "1600511589", 
        "data": {
            "total": 2, //数据总数
            "list": [
                {
                    "id": 1, //申请ID
                    "name": "何先生", //申请人名称
                    "createtime": "2020.09.12" //申请时间
                }
            ]
        }
    })
     */
    public function companyUser()
    {
        $status = $this->request->param('status','0');
        $page = $this->request->param('page', 1, 'intval');
        $page_num = $this->request->param('page_num', 10, 'intval');
        $data = CompanyUser::where('company_id',$this->company_id)
            ->where('status',$status)
            ->paginate($page_num,false,['page'=>$page])
            ->each(function($v){
                $v['createtime'] = date('Y.m.d',$v['createtime']);
                $v->visible(['id','name','createtime']);
            })->toArray();
        $this->success('成功',['total'=>$data['total'],'list'=>$data['data']]);
    }

    /**
     * @ApiWeigh (99)
     * @ApiTitle    (企业成员管理-同意加入)
     * @ApiSummary  (企业成员管理-同意加入)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="company_user_id", type="string", required=false, description="申请ID")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1600167441",
        "data": null
    })
     */
    public function companyUserAgree()
    {
        $company_user_id = $this->request->param('company_user_id');
        empty($company_user_id) && $this->error('缺少必需参数');
        $info = CompanyUser::get($company_user_id);
        empty($info) && $this->error('申请信息不存在');
        $info->save(['status'=>'1']);
        $this->success('同意加入成功');
    }

    /**
     * @ApiWeigh (99)
     * @ApiTitle    (企业成员管理-拒绝加入)
     * @ApiSummary  (企业成员管理-拒绝加入)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="company_user_id", type="string", required=false, description="申请ID")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1600167441",
        "data": null
    })
     */
    public function companyUserRefuse()
    {
        $company_user_id = $this->request->param('company_user_id');
        empty($company_user_id) && $this->error('缺少必需参数');
        $info = CompanyUser::get($company_user_id);
        empty($info) && $this->error('申请信息不存在');
        $info->save(['status'=>'2']);
        $this->success('拒绝成功');
    }

    /**
     * @ApiWeigh (99)
     * @ApiTitle    (企业成员管理-移出团队)
     * @ApiSummary  (企业成员管理-移出团队)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="company_user_id", type="string", required=false, description="申请ID")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1600167441",
        "data": null
    })
     */
    public function companyUserRemove()
    {
        $company_user_id = $this->request->param('company_user_id');
        empty($company_user_id) && $this->error('缺少必需参数');
        $info = CompanyUser::get($company_user_id);
        empty($info) && $this->error('申请信息不存在');
        $info->delete();
        $this->success('移出成功');
    }

    /**
     * @ApiWeigh (99)
     * @ApiTitle    (企业成员管理-企业邀请码)
     * @ApiSummary  (企业成员管理-企业邀请码)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1600167441",
        "data": {
            "invite_code": 376581, //企业邀请码
        }
    })
     */
    public function inviteCode()
    {
        $invite_code = $this->model->where('id',$this->company_id)->value('invite_code');
        $this->success('成功',compact('invite_code'));
    }

    /**
     * @ApiWeigh (99)
     * @ApiTitle    (企业成员管理-更新企业邀请码)
     * @ApiSummary  (企业成员管理-更新企业邀请码)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1600167441",
        "data": {
            "invite_code": 376581, //企业邀请码
        }
    })
     */
    public function setInviteCode()
    {
        $invite_code = (new User)->setInviteCode($this->company_id);
        $this->success('更新邀请码成功',compact('invite_code'));
    }
}