<?php

namespace App\Services\WeChat;

class Fans extends Base
{

    public function __construct($publicId)
    {
        parent::__construct($publicId);
    }


    /**
     * 获取用户基本信息
     * @param string $openid
     * @return mixed
     * @throws WeChatException
     */
    public function info($openid)
    {
        $access_token = $this->accessToken;
        $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token={$access_token}&openid={$openid}&lang=zh_CN";
        $response = json_decode(getUTF8Sting(\Curl::to($url)->get()), true);
        if (isset($response['errcode']) && $response['errcode'] != 0) {
            throw new WeChatException($response['errmsg'], $response['errcode']);
        }

        return $response;
    }


    /**
     * 获取全部用户信息
     * @param  string  $next_openid
     * @return mixed
     * @throws WeChatException
     */
    public function getAll($next_openid = '')
    {
        $access_token = $this->accessToken;
        $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token={$access_token}&next_openid={$next_openid}";
        $response = \Curl::to($url)->asJson(true)->get();
        if (isset($response['errcode']) && $response['errcode'] != 0) {
            throw new WeChatException($response['errmsg'], $response['errcode']);
        }

        return $response;
    }


    /**
     * 批量获取用户基本信息
     * @param string|string[] $openid_list
     * @return mixed
     * @throws WeChatException
     */
    public function batchGet($openid_list)
    {
        $user_list = [];
        foreach ($openid_list as $openid) {
            array_push($user_list, [
                'openid' => $openid,
                'lang' => 'zh_CN',
            ]);
        }
        $access_token = $this->accessToken;
        $url = "https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token={$access_token}";

        $response = \Curl::to($url)->withData([
            'user_list' => $user_list,
        ])->asJson(true)->post();

        if (isset($response['errcode']) && $response['errcode'] != 0) {
            throw new WeChatException($response['errmsg'], $response['errcode']);
        }
        $user_info_list = $response['user_info_list'];

        return $user_info_list;
    }


    /**
     * 设置用户备注名
     * @param string $openid
     * @param string $remark
     * @return bool
     * @throws WeChatException
     */
    public function updateRemark($openid, $remark)
    {
        $post = [
            'openid' => $openid,
            'remark' => $remark,
        ];
        $access_token = $this->accessToken;
        $url = "https://api.weixin.qq.com/cgi-bin/user/info/updateremark?access_token={$access_token}";
        $response = \Curl::to($url)->withData($post)->asJson(true)->post();
        if (isset($response['errcode']) && $response['errcode'] != 0) {
            throw new WeChatException($response['errmsg'], $response['errcode']);
        }

        return true;
    }


    /**
     * 获取标签下粉丝列表
     * @param int $tag_id
     * @param  string  $next_openid
     * @return mixed
     * @throws WeChatException
     */
    public function tagUser($tag_id, $next_openid = '')
    {
        $access_token = $this->accessToken;
        $url = "https://api.weixin.qq.com/cgi-bin/user/tag/get?access_token={$access_token}";
        $response = \Curl::to($url)->withData([
            'tagid' => $tag_id,
            'next_openid' => $next_openid,
        ])->asJson(true)->post();
        if (isset($response['errcode']) && $response['errcode'] != 0) {
            throw new WeChatException($response['errmsg'], $response['errcode']);
        }

        return $response;
    }
}
