Payload.php 10.0 KB
<?php
/**
 * Payload.php
 *
 * @Author: Ghaoo
 * @Date  2020/7/27 0027
 */

namespace Weasy\Core;

use addons\qyexternal\model\Contacts;
use addons\qyexternal\model\Corp;
use addons\qyexternal\model\FollowUser;
use addons\qyexternal\model\GroupChat;
use addons\qyexternal\model\Logs;
use think\Log;
use Weasy\External\Api\CorpAPI;

class Payload
{
    public function __construct($msg)
    {
        if (!isset($msg['MsgType']) || $msg['MsgType'] != "event") {
            throw new \Exception("解析通知类消息失败!此消息非通知类消息。消息:\n$msg");
        }

        switch ($msg["Event"]) {
            case "change_contact": // 企业成员通知
                $this->changeContact($msg);
                break;
            case "change_external_contact": // 客户通知
                $this->changeExternalContact($msg);
                break;
            case "change_external_chat": // 客户群通知
                $this->changeExternalChat($msg);
                break;
            default:
        }
    }

    // 内部成员变更事件
    protected function changeContact($msg)
    {
        Log::info($msg);
        switch ($msg["ChangeType"]) {
            case "create_user":
                $this->createUser($msg);
                break;
            case "update_user":
                $this->updateUser($msg);
                break;
            case "delete_user":
                $this->deleteUser($msg);
                $this->saveLogs("删除企业成员事件", $msg);
                break;
            default:
        }
    }

    // 外部联系人事件
    protected function changeExternalContact($msg)
    {
        switch ($msg["ChangeType"]) {
            case "add_external_contact": // 添加企业客户事件
                $this->addExternalContact($msg);
                $this->saveLogs("添加企业客户事件", $msg);
                break;
            case "edit_external_contact": // 编辑企业客户事件,无法获取修改的内容,暂不做处理
                $this->saveLogs("编辑企业客户事件", $msg);
                break;
            case "del_external_contact": // 删除企业客户事件
                $this->delExternalContact($msg);
                $this->saveLogs("删除企业客户事件", $msg);
                break;
            case "del_follow_user": // 被外部联系人删除
                $this->delExternalContact($msg);
                $this->saveLogs("被外部联系人删除", $msg);
                break;
            default:
        }
    }

    // 客户群变更事件
    protected function changeExternalChat($msg)
    {
        $wxChat = $this->getExternalGroupChat($msg["ToUserName"], $msg["ChatId"]);
        $chat = GroupChat::where("corp_id", $msg["ToUserName"])->where("chat_id", $msg["ChatId"])->find();
        if (empty($wxChat["member_list"])) {
            $chat->delete();
            $this->saveLogs("客户群解散", $msg);
            return;
        }
        if (!$chat) {
            $chat = new GroupChat();
            $chat->corp_id = $msg["ToUserName"];
            $chat->corp_name = Corp::where("corp_id", $msg["ToUserName"])->find()->name;
            $chat->chat_id = $msg["ChatId"];
        }

        $chat->name = $wxChat["name"] ? $wxChat["name"] : $msg["ChatId"];
        $chat->owner = $wxChat["owner"];
        $chat->create_time = $wxChat["create_time"];
        if (isset($wxChat["notice"])) {
            $chat->notice = $wxChat["notice"];
        }

        $chat->save();

        $this->saveLogs("客户群变更事件", $msg);
    }

    /**
     * @Note: 新增外部联系人
     * @Author: Ghaoo
     * @param $msg
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function addExternalContact($msg)
    {
        $contact = $this->getExternalContact($msg["ToUserName"], $msg["ExternalUserID"]);

        if ($contact) {
            $external = $contact["external_contact"];

            $follow_user = [];
            if (isset($contact["follow_user"])) {
                foreach ($contact["follow_user"] as $follow) {
                    array_push($follow_user, $follow["userid"]);
                }
            }
        }

        $user = Contacts::where("corp_id", $msg["ToUserName"])->where("external_userid", $msg["ExternalUserID"])->find();
        if (!$user) {
            $user = new Contacts();
            $user->corp_id = $msg["ToUserName"];
            $user->corp_name = Corp::where("corp_id", $msg["ToUserName"])->find()->name;
            $user->external_userid = $msg["ExternalUserID"];
            $user->name = $msg["ExternalUserID"];
        }

        if (isset($external)) {
            $user->name = $external["name"];
            $user->type = $external["type"];
            $user->gender = $external["gender"];

            if (isset($external["avatar"])) {
                $user->avatar = $external["avatar"];
            }

            if (isset($external["unionid"])) {
                $user->unionid = $external["unionid"];
            }

            $user->follow_user = json_encode($follow_user);
        }

        $user->save();
    }

    /**
     * @Note: 主动或被动删除外部联系人
     * @Author: Ghaoo
     * @param $msg
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function delExternalContact($msg)
    {
        $user = Contacts::where("corp_id", $msg["ToUserName"])->where("external_userid", $msg["ExternalUserID"])->find();
        if ($user) {
            $follow = json_decode($user['follow_user'], true);
            $follow = array_diff($follow, [$msg["UserID"]]);
            if (count($follow) <= 0) {
                $user->delete();
            } else {
                $user->follow_user = json_encode($follow);
                $user->save();
            }
        }

    }

    /**
     * @Note: 新增企业成员事件
     * @param $msg
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function createUser($msg)
    {
        $user = new FollowUser();

        $user->corp_id = $msg["ToUserName"];
        $user->corp_name = Corp::where("corp_id", $msg["ToUserName"])->find()->name;
        $user->userid = $msg["UserID"];
        if (isset($msg["Name"])) {
            $user->name = $msg["Name"];
        }
        if (isset($msg["Mobile"])) {
            $user->mobile = $msg["Mobile"];
        }
        if (isset($msg["Email"])) {
            $user->email = $msg["Email"];
        }
        if (isset($msg["Avatar"])) {
            $user->avatar = $msg["Avatar"];
        }
        if (isset($msg["Gender"])) {
            $user->gender = $msg["Gender"];
        }
        if (isset($msg["Status"])) {
            $user->status = $msg["Status"];
        }

        $user->save();
        $this->saveLogs("新增企业成员事件", $msg);
    }

    /**
     * @Note: 更新企业成员事件
     * @Author: Ghaoo
     * @param $msg
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function updateUser($msg)
    {
        $user = FollowUser::where("corp_id", $msg["ToUserName"])->where("userid", $msg["UserID"])->find();

        if ($user) {
            $user->corp_id = $msg["ToUserName"];
            $user->corp_name = Corp::where("corp_id", $msg["ToUserName"])->find()->name;
            $user->userid = isset($msg["NewUserID"]) ? $msg["NewUserID"] : $msg["UserID"];
            if (isset($msg["Name"])) {
                $user->name = $msg["Name"];
            }
            if (isset($msg["Mobile"])) {
                $user->mobile = $msg["Mobile"];
            }
            if (isset($msg["Email"])) {
                $user->email = $msg["Email"];
            }
            if (isset($msg["Avatar"])) {
                $user->avatar = $msg["Avatar"];
            }
            if (isset($msg["Gender"])) {
                $user->gender = $msg["Gender"];
            }
            if (isset($msg["Status"])) {
                $user->status = $msg["Status"];
            }

            $user->save();
            $this->saveLogs("更新企业成员事件", $msg);
        }

    }

    /**
     * @Note: 删除成员事件
     * @Author: Ghaoo
     * @param $msg
     */
    protected function deleteUser($msg)
    {
        FollowUser::where("corp_id", $msg["ToUserName"])->where("userid", $msg["UserID"])->delete();
    }

    /**
     * @Note:
     * @Author: Ghaoo
     * @param $corpid
     * @param $userid
     * @return bool|null
     */
    protected function getExternalContact($corpid, $userid)
    {
        try {
            $corp = Corp::where("corp_id", $corpid)->find();
            $api = new CorpAPI($corpid, $corp->external_secret);
            return $api->ExternalContactGet($userid);
        } catch (\Exception $exception) {
            return false;
        }
    }

    /**
     * @Note:
     * @Author: Ghaoo
     * @param $corpid
     * @param $chatid
     * @return bool
     */
    protected function getExternalGroupChat($corpid, $chatid)
    {
        try {
            $corp = Corp::where("corp_id", $corpid)->find();
            $api = new CorpAPI($corpid, $corp->external_secret);
            return $api->ExternalContactGroupChatGet($chatid);
        } catch (\Exception $exception) {
            return false;
        }
    }

    protected function saveLogs($title, $msg)
    {
        $md5 = md5(json_encode($msg));
        $log = Logs::where("corp_id", $msg["ToUserName"])->where("md5", $md5)->count();
        if (!$log) {
            $log = new Logs();
            $log->corp_id = $msg["ToUserName"];
            $log->content = json_encode($msg);
            $log->title = $title;
            $log->md5 = $md5;
            $log->save();
        }
    }

}