Team.php 21.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
<?php
/**
 * Created by PhpStorm.
 * User: 86132
 * Date: 2020/7/6
 * Time: 10:13
 */

namespace app\api\controller;

use app\common\model\Company;
use think\Db;
use app\common\controller\Api;


/**
 * 团队接口
 */
class Team extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }


    /**
     * @ApiTitle    (团队接口-类型,行业展示)
     * @ApiSummary  (类型,行业展示)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Team/TypeIndustryList)
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    "data": [
    {
    "type2": [
    {
    "type": "类型",
    "type_id": "id"
    ]
    },
    {
    "industrys": [
    {
    "industry": "行业",
    "industry_id": "id"
    }
    ]
    }
    ]
    })
     */
    public function TypeIndustryList()
    {
        $type = Db::name('type')->select();
        $industry = Db::name('industry')->select();
        foreach ($type as $k => $v) {
            $type1[$k]['type'] = $v['type'];
            $type1[$k]['type_id'] = $v['id'];
        }
        foreach ($industry as $k => $v) {
            $industry1[$k]['industry'] = $v['industry'];
            $industry1[$k]['industry_id'] = $v['id'];
        }
        $return[]['types'] = $type1;
        $return[]['industrys'] = $industry1;
        $this->success('成功', $return);
    }


    /**
     * @ApiTitle    (团队接口-公司申请状态)
     * @ApiSummary  (公司申请状态)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Team/CompanyStatus)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    "data": {
    "type": 审核状态:0=审核未通过,1=审核已通过,2=待审核,3=未申请过
    }
    })
     */
    public function CompanyStatus()
    {
        $user_id = $this->is_token($this->request->header());
        $is_company_holder = Db::name('company')->where(['company_holder' => $user_id])->find();
        if (empty($is_company_holder)) {
            $type = 3;
        } else {
            $type = $is_company_holder['status'];
        }
        if ($type == 1) {
            Db::name('user')->where(['id' => $user_id])->update(['user_level' => 3]);
        }
        $return = [
            'type' => $type
        ];
        $this->success('成功', $return);
    }


    /**
     * @ApiTitle    (团队接口-申请公司团队)
     * @ApiSummary  (申请公司团队)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Team/ApplicationCompanyTeam)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="company_name", type="integer", required=true, description="公司名")
     * @ApiParams   (name="credit", type="integer", required=true, description="公司信用代码")
     * @ApiParams   (name="company_address", type="string", required=true, description="公司地址")
     * @ApiParams   (name="address_con", type="string", required=true, description="公司详细地址")
     * @ApiParams   (name="invoice_address", type="string", required=true, description="开票地址")
     * @ApiParams   (name="bank_name", type="string", required=true, description="银行名称")
     * @ApiParams   (name="bank_num", type="string", required=true, description="银行账号")
     * @ApiParams   (name="company_tel", type="string", required=true, description="注册电话")
     * @ApiParams   (name="register_tel", type="string", required=true, description="公司电话")
     * @ApiParams   (name="type_id", type="int", required=true, description="类型")
     * @ApiParams   (name="industry_id", type="int", required=true, description="行业")
     * @ApiParams   (name="paragraph", type="int", required=true, description="税号")
     * @ApiParams   (name="business_avatar", type="string", required=true, description="营业执照")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1', //申请中 审核状态接口[CompanyStatus]//code:9991
    'msg':'返回成功'
    "data": "1"
    })
     */
    public function ApplicationCompanyTeam()
    {
        $user_id = $this->is_token($this->request->header());
        $is_company = Db::name('company')->where(['company_holder' => $user_id])->find();
        if (!empty($is_company)) {
            $this->success('申请过或正在申请中', 1, 9991);
        }
        $param = $this->request->param();
        $data = [
            'company_name' => $param['company_name'],
            'credit' => $param['credit'],
            'company_address' => $param['company_address'],
            'address_con' => $param['address_con'],
            'invoice_address' => $param['invoice_address'],
            'bank_name' => $param['bank_name'],
            'bank_num' => $param['bank_num'],
            'company_tel' => $param['company_tel'],
            'register_tel' => $param['register_tel'],
            'type_id' => $param['type_id'],
            'industry_id' => $param['industry_id'],
            'business_avatar' => $param['business_avatar'],
            'paragraph' => $param['paragraph'],
            'status' => 2,
            'company_holder' => $user_id
        ];
        $model = new Company();
        $res = $model->save($data);
        if ($res) {
            Db::name('company')->where(['company_holder' => $user_id])->where(['status' => 0])->delete();
            $this->success('成功', 1);
        } else {
            $this->error('失败', 0);
        }
    }


    /**
     * @ApiTitle    (团队接口-加入公司列表)
     * @ApiSummary  (加入公司列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Team/InsertCompanyList)
     * @ApiParams   (name="keywords", type="string", required=true, description="搜索关键词[不搜索为空]")
     * @ApiParams   (name="pages", type="int", required=true, description="分页")
     * @ApiParams   (name="rows", type="int", required=true, description="数量")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    "data": {
    "count": "数据总数",
    "list": [
    {
    "company_name": "天津银河百荣科技有限公司",
    "company_id": "公司ID"
    }
    ]
    }
    })
     */
    public function InsertCompanyList()
    {
        $param = $this->request->param();
        $map = [];
        if (!empty($param['keywords'] || $param['keywords'] != null || $param['keywords'] != "")) {
            $map['company_name'] = ['like', '%' . $param['keywords'] . '%'];
        }
        $company = Db::name('company')->where($map)->page($param['pages'], $param['rows'])->where(['status' => 1])->select();
        $count = Db::name('company')->where($map)->where(['status' => 1])->select();
        if (!empty($company)) {
            foreach ($company as $k => $v) {
                $return[$k]['company_name'] = $v['company_name'];
                $return[$k]['company_id'] = $v['id'];
            }
            $return1['count'] = count($count);
            $return1['list'] = $return;
            $this->success('成功', $return1);
        } else {
            $return1['count'] = [];
            $return1['list'] = [];
            $this->success('成功', $return1);
        }
    }


    /**
     * @ApiTitle    (团队接口-申请加入)
     * @ApiSummary  (申请加入)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Team/ApplyToJoin)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="company_id", type="int", required=true, description="要加入的公司ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1', //'您已申请过加入其他团队,请耐心等待审核结果'code:9995//'您已加入其他公司团队,暂时不能加入其他团队'code:9996//'您已申请公司团队,暂时不能加入其他团队'code:9997
    'msg':'返回成功'
    "data": "1"
    })
     */
    public function ApplyToJoin()
    {
        $user_id = $this->is_token($this->request->header());
        $company_id = input('company_id');
        $is_company_holder = Db::name('company')->where(['company_holder' => $user_id])->find();
        if ($is_company_holder) {
            $this->success('您已申请公司团队,暂时不能加入其他团队', 1, 9997);
        }
        $is_team = Db::name('team')->where(['user_id' => $user_id])->where(['status' => 1])->find();
        if ($is_team) {
            $this->success('您已加入其他公司团队,暂时不能加入其他团队', 1, 9996);
        }
        $is_status = Db::name('team')->where(['user_id' => $user_id])->where(['status' => 3])->find();
        if ($is_status) {
            $this->success('您已申请过加入其他团队,请耐心等待审核结果', 1, 9995);
        }
        $is_ApplyToJoin = Db::name('team')->insert(['company_id' => $company_id, 'user_id' => $user_id, 'createtime' => time(), 'updatetime' => time(), 'status' => 3]);
        if ($is_ApplyToJoin) {
            Db::name('team')->where(['user_id' => $user_id])->where(['status' => 2])->delete();
            $this->success('申请已提交', 1);
        } else {
            $this->error('申请失败', 0);
        }
    }


    /**
     * @ApiTitle    (团队接口-公司成员管理-申请列表)
     * @ApiSummary  (申请列表)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Team/CompanyMemberManagementApplicationList)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="pages", type="int", required=true, description="分页")
     * @ApiParams   (name="rows", type="int", required=true, description="数量")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',  //code为9998 此用户不是公司持有人
    'msg':'返回成功'
    "data": {
    "count": 总数,
    "list": [
    {
    "username": "姓名",
    "id": 用户ID
    }
    ]
    }
    })
     */
    public function CompanyMemberManagementApplicationList()
    {
        $user_id = $this->is_token($this->request->header());
        $param = $this->request->param();
        $company_id = Db::name('company')->where(['company_holder' => $user_id])->value('id');
        if (empty($company_id)) {
            $this->success('', '', 9998);
        }
        $user_id_arr = Db::name('team')->where(['company_id' => $company_id])->where(['status' => 3])->page($param['pages'], $param['rows'])->select();
        $count = Db::name('team')->where(['company_id' => $company_id])->where(['status' => 3])->select();
        if (empty($user_id_arr)) {
            $return['count'] = [];
            $return['list'] = [];
            $this->success('成功', $return);
        }
        foreach ($user_id_arr as $k => $v) {
            $user_data[$k] = Db::name('user')->where(['id' => $v['user_id']])->select();
        }
        $user_data_arr = $this->three_arr($user_data);
        foreach ($user_data_arr as $k => $v) {
            $data[$k]['username'] = $v['username'];
            $data[$k]['id'] = $v['id'];
        }
        $return['count'] = count($count);
        $return['list'] = $data;
        $this->success('成功', $return);
    }


    /**
     * @ApiTitle    (团队接口-公司成员管理-公司成员)
     * @ApiSummary  (公司成员)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Team/CompanyMemberManagementCompanyMember)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    "data": "1"
    })
     */
    public function CompanyMemberManagementCompanyMember()
    {
        $user_id = $this->is_token($this->request->header());
        $param = $this->request->param();
        $company_id = Db::name('company')->where(['company_holder' => $user_id])->value('id');
        if (empty($company_id)) {
            $this->success('', '', 9998);
        }
        $user_id_arr = Db::name('team')->where(['company_id' => $company_id])->where(['status' => 1])->page($param['pages'], $param['rows'])->select();
        $count = Db::name('team')->where(['company_id' => $company_id])->where(['status' => 1])->select();
        if (empty($user_id_arr)) {
            $return['count'] = [];
            $return['list'] = [];
            $this->success('成功', $return);
        }
        foreach ($user_id_arr as $k => $v) {
            $user_data[$k] = Db::name('user')->where(['id' => $v['user_id']])->select();
        }
        $user_data_arr = $this->three_arr($user_data);
        foreach ($user_data_arr as $k => $v) {
            $data[$k]['username'] = $v['username'];
            $data[$k]['id'] = $v['id'];
        }
        $return['count'] = count($count);
        $return['list'] = $data;
        $this->success('成功', $return);
    }


    /**
     * @ApiTitle    (团队接口-公司成员管理-申请列表-操作成员)
     * @ApiSummary  (操作成员)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Team/OperationMember)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="type", type="int", required=true, description="1=同意,2=拒绝")
     * @ApiParams   (name="id", type="int", required=true, description="用户ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    "data": "1"
    })
     */
    public function OperationMember()
    {
        $user_id = $this->is_token($this->request->header());
        $param = $this->request->param();
        $company_id = Db::name('company')->where(['company_holder' => $user_id])->value('id');
        if ($param['type'] == 1) {
            $type = 1;
            Db::name('user')->where(['id' => $param['id']])->update(['user_level' => 2]);
        }
        if ($param['type'] == 2) {
            $type = 2;
        }
        $res = Db::name('team')->where(['company_id' => $company_id])->where(['user_id' => $param['id']])->update(['status' => $type]);
        if ($res) {
            $this->success('成功', 1);
        } else {
            $this->error('失败', 0);
        }
    }


    /**
     * @ApiTitle    (团队接口-公司成员管理-公司成员-删除成员)
     * @ApiSummary  (删除成员)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Team/DeleteMember)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="int", required=true, description="用户ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    "data": "1"
    })
     */
    public function DeleteMember()
    {
        $user_id = $this->is_token($this->request->header());
        $param = $this->request->param();
        $company_id = Db::name('company')->where(['company_holder' => $user_id])->value('id');
        $res = Db::name('team')->where(['company_id' => $company_id])->where(['user_id' => $param['id']])->delete();
        Db::name('user')->where(['id' => $param['id']])->update(['user_level' => 1]);
        if ($res) {
            $this->success('成功', 1);
        } else {
            $this->error('失败', 0);
        }
    }


    /**
     * @ApiTitle    (团队接口-我的公司)
     * @ApiSummary  (我的公司)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Team/MyCompany)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1', //还没有创建公司或加入公司 code=99993
    'msg':'返回成功'
    "data": {
    "company_name": "天津银河百荣科技有限公司",
    "type": 1=管理员,2=普通成员
    }
    })
     */
    public function MyCompany()
    {
        $user_id = $this->is_token($this->request->header());
        $is_company = Db::name('company')->where(['company_holder' => $user_id])->where(['status' => 1])->find();
        if (empty($is_company)) {
            $is_team = Db::name('team')->where(['user_id' => $user_id])->where(['status' => 1])->value('company_id');
            if (empty($is_team)) {
                $this->success('还没有创建公司或加入公司', '', 99993);
            }
            $company_name = Db::name('company')->where(['id' => $is_team])->value('company_name');
            $return = [
                'company_name' => $company_name,
                'type' => 2
            ];
            $this->success('成功', $return);
        }
        $return = [
            'company_name' => $is_company['company_name'],
            'type' => 1
        ];
        $this->success('成功', $return);
    }


    /**
     * @ApiTitle    (团队接口-我的公司-注销或退出公司)
     * @ApiSummary  (注销或退出公司)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Team/WriteOffOrWithdrawFromTheCompany)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="type", type="int", required=true, description="1=管理员[注销公司],2=普通成员[退出公司]")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    "data": "1"
    })
     */
    public function WriteOffOrWithdrawFromTheCompany()
    {
        $user_id = $this->is_token($this->request->header());
        $param = $this->request->param();
        if ($param['type'] == 1) {
            $company_id = Db::name('company')->where(['company_holder' => $user_id])->value('id');
            Db::name('team')->where(['company_id' => $company_id])->delete();
            Db::name('company')->where(['company_holder' => $user_id])->delete();
            $this->success('成功', 1);
        }
        if ($param['type'] == 2) {
            Db::name('team')->where(['user_id' => $user_id])->delete();
            $this->success('成功', 1);
        }
    }


    /**
     * @ApiTitle    (团队接口-我的公司-转移管理渲染)
     * @ApiSummary  (转移管理渲染)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Team/TransferManagementRendering)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    "data": [
    {
    "username": "3323231",
    "id": 4
    }
    ]
    })
     */
    public function TransferManagementRendering()
    {
        $user_id = $this->is_token($this->request->header());
        $company_id = Db::name('company')->where(['company_holder' => $user_id])->value('id');
        $user_id_arr = Db::name('team')->where(['company_id' => $company_id])->select();
        if (empty($user_id_arr)) {
            $return = [];
            $this->error('公司没有其他成员', $return);
        }
        foreach ($user_id_arr as $k => $v) {
            $user_arr[$k] = Db::name('user')->where(['id' => $v['user_id']])->select();
        }
        $user = $this->three_arr($user_arr);
        foreach ($user as $k => $v) {
            $return[$k]['username'] = $v['username'];
            $return[$k]['id'] = $v['id'];
        }
        $this->success('成功', $return);
    }


    /**
     * @ApiTitle    (团队接口-我的公司-转移管理)
     * @ApiSummary  (转移管理)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/Team/TransferManagement)
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="int", required=true, description="要转移的用户ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturn   ({
    'code':'1',
    'msg':'返回成功'
    "data": {
    }
    })
     */

    public function TransferManagement()
    {
        $user_id = $this->is_token($this->request->header());
        $id = input('id');
        Db::name('company')->where(['company_holder' => $user_id])->update(['company_holder' => $id]);
        Db::name('user')->where('user_id', $user_id)->update(['user_level' => 1]);
        $res = Db::name('team')->where(['user_id' => $id])->delete();
        Db::name('user')->where('user_id', $id)->update(['user_level' => 3]);

        if ($res) {
            $this->success('成功', 1);
        } else {
            $this->error('失败', 0);
        }
    }
}