SchoolClient.php 16.1 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
<?php

/*
 * This file is part of the overtrue/wechat.
 *
 * (c) overtrue <i@overtrue.me>
 *
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

namespace EasyWeChat\Work\ExternalContact;

use EasyWeChat\Kernel\BaseClient;

/**
 * Class Client.
 *
 * @author MillsGuo <millsguo@gmail.com>
 */
class SchoolClient extends BaseClient
{
    /**
     * 创建部门
     * @see https://work.weixin.qq.com/api/doc/90000/90135/92340
     * @param string $name
     * @param int $parentId
     * @param int $type
     * @param int $standardGrade
     * @param int $registerYear
     * @param int $order
     * @param array $departmentAdmins [['userid':'139','type':1],['userid':'1399','type':2]]
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function createDepartment(string $name, int $parentId, int $type, int $standardGrade, int $registerYear, int $order, array $departmentAdmins)
    {
        $params = [
            'name' => $name,
            'parentid' => $parentId,
            'type' => $type,
            'standard_grade' => $standardGrade,
            'register_year' => $registerYear,
            'order' => $order,
            'department_admins' => $departmentAdmins
        ];

        return $this->httpPostJson('cgi-bin/school/department/create', $params);
    }

    /**
     * 更新部门
     * @see https://work.weixin.qq.com/api/doc/90000/90135/92341
     * @param int $id
     * @param string $name
     * @param int $parentId
     * @param int $type
     * @param int $standardGrade
     * @param int $registerYear
     * @param int $order
     * @param array $departmentAdmins [['op':0,'userid':'139','type':1],['op':1,'userid':'1399','type':2]] OP=0表示新增或更新,OP=1表示删除管理员
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function updateDepartment(int $id, string $name, int $parentId, int $type, int $standardGrade, int $registerYear, int $order, array $departmentAdmins)
    {
        $params = [
            'id' => $id,
            'name' => $name,
            'parentid' => $parentId,
            'type' => $type,
            'standard_grade' => $standardGrade,
            'register_year' => $registerYear,
            'order' => $order,
            'department_admins' => $departmentAdmins
        ];
        $params = $this->filterNullValue($params);

        return $this->httpPostJson('cgi-bin/school/department/update', $params);
    }

    /**
     * 删除部门
     * @see https://work.weixin.qq.com/api/doc/90000/90135/92342
     * @param int $id
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function deleteDepartment(int $id)
    {
        return $this->httpGet('cgi-bin/school/department/delete', [
            'id' => $id
        ]);
    }

    /**
     * 获取部门列表
     * @see https://work.weixin.qq.com/api/doc/90000/90135/92343
     * @param int $id 如果ID为0,则获取全量组织架构
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function getDepartments(int $id)
    {
        if ($id > 0) {
            $params = [
                'id' => $id
            ];
        } else {
            $params = [];
        }

        return $this->httpGet('cgi-bin/school/department/list', $params);
    }

    /**
     * 创建学生
     * @see https://work.weixin.qq.com/api/doc/90000/90135/92325
     * @param string $userId
     * @param string $name
     * @param array $department
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function createStudent(string $userId, string $name, array $department)
    {
        $params = [
            'student_userid' => $userId,
            'name' => $name,
            'department' => $department
        ];

        return $this->httpPostJson('cgi-bin/school/user/create_student', $params);
    }

    /**
     * 删除学生
     * @see https://work.weixin.qq.com/api/doc/90000/90135/92326
     * @param string $userId
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function deleteStudent(string $userId)
    {
        return $this->httpGet('cgi-bin/school/user/delete_student', [
            'userid' => $userId
        ]);
    }

    /**
     * 更新学生
     * @see  https://work.weixin.qq.com/api/doc/90000/90135/92327
     * @param string $userId
     * @param string $name
     * @param string $newUserId
     * @param array $department
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function updateStudent(string $userId, string $name, string $newUserId, array $department)
    {
        $params = [
            'student_userid' => $userId
        ];
        if (!empty($name)) {
            $params['name'] = $name;
        }
        if (!empty($newUserId)) {
            $params['new_student_userid'] = $newUserId;
        }
        if (!empty($department)) {
            $params['department'] = $department;
        }

        return $this->httpPostJson('cgi-bin/school/user/update_student', $params);
    }

    /**
     * 批量创建学生,学生最多100个
     * @see https://work.weixin.qq.com/api/doc/90000/90135/92328
     * @param array $students 学生格式:[[student_userid:'','name':'','department':[1,2]],['student_userid':'','name':'','department':[1,2]]]
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function batchCreateStudents(array $students)
    {
        $params = [
            'students' => $students
        ];

        return $this->httpPostJson('cgi-bin/school/user/batch_create_student', $params);
    }

    /**
     * 批量删除学生,每次最多100个学生
     * @see https://work.weixin.qq.com/api/doc/90000/90135/92329
     * @param array $useridList 学生USERID,格式:['zhangsan','lisi']
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function batchDeleteStudents(array $useridList)
    {
        return $this->httpPostJson('cgi-bin/school/user/batch_delete_student', [
            'useridlist' => $useridList
        ]);
    }

    /**
     * 批量更新学生,每次最多100个
     * @see https://open.work.weixin.qq.com/api/doc/90001/90143/92042
     * @param array $students 格式:[['student_userid':'lisi','new_student_userid':'lisi2','name':'','department':[1,2]],.....]
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function batchUpdateStudents(array $students)
    {
        return $this->httpPostJson('cgi-bin/school/user/batch_update_student', [
            'students' => $students
        ]);
    }

    /**
     * 创建家长
     * @see https://open.work.weixin.qq.com/api/doc/90001/90143/92077
     * @param string $userId
     * @param string $mobile
     * @param bool $toInvite
     * @param array $children
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function createParent(string $userId, string $mobile, bool $toInvite, array $children)
    {
        $params = [
            'parent_userid' => $userId,
            'mobile' => $mobile,
            'to_invite' => $toInvite,
            'children' => $children
        ];

        return $this->httpPostJson('cgi-bin/school/user/create_parent', $params);
    }

    /**
     * 删除家长
     * @see https://open.work.weixin.qq.com/api/doc/90001/90143/92079
     * @param string $userId
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function deleteParent(string $userId)
    {
        return $this->httpPostJson('cgi-bin/school/user/delete_parent', [
            'userid' => $userId
        ]);
    }

    /**
     * 更新家长
     * @see https://open.work.weixin.qq.com/api/doc/90001/90143/92081
     * @param string $userId
     * @param string $mobile
     * @param string $newUserId
     * @param array $children 格式:[['student_userid':'','relation':''],[]]
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function updateParent(string $userId, string $mobile, string $newUserId, array $children)
    {
        $params = [
            'parent_userid' => $userId
        ];
        if (!empty($newUserId)) {
            $params['new_parent_userid'] = $newUserId;
        }
        if (!empty($mobile)) {
            $params['mobile'] = $mobile;
        }
        if (!empty($children)) {
            $params['children'] = $children;
        }

        return $this->httpPostJson('cgi-bin/school/user/update_parent', $params);
    }

    /**
     * 批量创建家长 每次最多100个
     * @see https://open.work.weixin.qq.com/api/doc/90001/90143/92078
     * @param array $parents [['parent_userid':'','mobile':'','to_invite':true,'children':['student_userid':'','relation':'']],.....]
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function batchCreateParents(array $parents)
    {
        return $this->httpPostJson('cgi-bin/school/user/batch_create_parent', [
            'parents' => $parents
        ]);
    }

    /**
     * 批量删除家长,每次最多100个
     * @see https://open.work.weixin.qq.com/api/doc/90001/90143/92080
     * @param array $userIdList 格式:['chang','lisi']
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function batchDeleteParents(array $userIdList)
    {
        return $this->httpPostJson('cgi-bin/school/user/batch_delete_parent', [
            'useridlist' => $userIdList
        ]);
    }

    /**
     * 批量更新家长,每次最多100个
     * @see https://open.work.weixin.qq.com/api/doc/90001/90143/92082
     * @param array $parents 格式:[['parent_userid':'','new_parent_userid':'','mobile':'','children':[['student_userid':'','relation':''],...]],.....]
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function batchUpdateParents(array $parents)
    {
        return $this->httpPostJson('cgi-bin/school/user/batch_update_parent', [
            'parents' => $parents
        ]);
    }

    /**
     * 读取学生或家长
     * @see https://open.work.weixin.qq.com/api/doc/90001/90143/92038
     * @param string $userId 学生或家长的userid
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function getUser(string $userId)
    {
        return $this->httpGet('cgi-bin/school/user/get', [
            'userid' => $userId
        ]);
    }

    /**
     * 获取部门成员详情
     * @see https://open.work.weixin.qq.com/api/doc/90001/90143/92038
     * @param int $departmentId
     * @param bool $fetchChild
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function getStudents(int $departmentId, bool $fetchChild)
    {
        $params = [
            'department_id' => $departmentId
        ];
        if ($fetchChild) {
            $params['fetch_child'] = 1;
        } else {
            $params['fetch_child'] = 0;
        }

        return $this->httpGet('cgi-bin/school/user/list', $params);
    }

    /**
     * 获取学校通知二维码
     * @see https://open.work.weixin.qq.com/api/doc/90001/90143/92197
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function getSubscribeQrCode()
    {
        return $this->httpGet('cgi-bin/externalcontact/get_subscribe_qr_code');
    }

    /**
     * 设置关注学校通知的模式
     * @see https://open.work.weixin.qq.com/api/doc/90001/90143/92290
     * @param int $mode 关注模式,1可扫码填写资料加入,2禁止扫码填写资料加入
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function setSubscribeMode(int $mode)
    {
        return $this->httpPostJson('cgi-bin/externalcontact/set_subscribe_mode', [
            'subscribe_mode' => $mode
        ]);
    }

    /**
     * 获取关注学校通知的模式
     * @see https://open.work.weixin.qq.com/api/doc/90001/90143/92290
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function getSubscribeMode()
    {
        return $this->httpGet('cgi-bin/externalcontact/get_subscribe_mode');
    }

    /**
     * 设置【老师可查看班级】的模式
     * @see https://open.work.weixin.qq.com/api/doc/90001/90143/92652
     * @param int $mode
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function setTeacherViewMode(int $mode)
    {
        return $this->httpPostJson('cgi-bin/school/set_teacher_view_mode', [
            'view_mode' => $mode
        ]);
    }

    /**
     * 获取【老师可查看班级】的模式
     * @see https://open.work.weixin.qq.com/api/doc/90001/90143/92652
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function getTeacherViewMode()
    {
        return $this->httpGet('cgi-bin/school/get_teacher_view_mode');
    }

    /**
     * 外部联系人OPENID转换
     * @param string $userId
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function convertOpenid(string $userId)
    {
        return $this->httpGet('cgi-bin/externalcontact/convert_to_openid', [
            'external_userid' => $userId
        ]);
    }

    /**
     * 过滤数组中值为NULL的键
     * @param array $data
     * @return array
     */
    protected function filterNullValue(array $data)
    {
        $returnData = [];
        foreach ($data as $key => $value) {
            if ($value !== null) {
                $returnData[$key] = trim($value);
            }
        }

        return $returnData;
    }
}