<?php

/**
 * Description of JwUser.php
 *
 * @author haiming
 * Date: 2021/5/21
 * Time: 10:32 PM
 */

namespace  Api\PhpServices\JwUser;

use Api\PhpUtils\Common\TimeOut;
use Api\PhpUtils\Http\Request;
use App\Exception\custom\CodeSpecialException;

class JwUser
{
    /**
     * 获取简网用户信息
     *
     *@param $params['mobile'] 手机号
     *
     */
    public function getUserInfo($params)
    {
        $url = config('interface', 'service.jw_user.get_user_info');
        if (!$url) {
            throw new CodeSpecialException("failed");
        }

        if (empty($params['mobile'])) {
            throw new CodeSpecialException("failed");
        }

        $params = ["mobilePhone" => $params['mobile']];
        $user_info  = (new TimeOut())->runGet($url, $params);
        if (!$user_info) {
            throw new CodeSpecialException("timeout");
        }
        return $user_info;
    }
    /**
     * 获取单条信息
     *
     */
    public function getUserByUserId($params)
    {
        $url = config('interface', 'service.jw_user.get_user_info_by_user_id');
        if (!$url) {
            throw new CodeSpecialException("failed");
        }

        if (empty($params['user_id'])) {
            throw new CodeSpecialException("failed");
        }

        $params = ["userId" => $params['user_id']];
        $user_info  = (new TimeOut())->runPost($url, $params);
        if (!$user_info) {
            throw new CodeSpecialException("timeout");
        }
        return $user_info;
    }

    public function getUserList($params)
    {
        $url = config('interface', 'service.jw_user.get_user_list');
        if (!$url) {
            throw new CodeSpecialException("failed");
        }

        if (empty($params['user_id'])) {
            throw new CodeSpecialException("failed");
        }

        $params = ["userIds" => $params['user_id']];
        $user_info  = (new TimeOut())->runPost($url, $params);
        if (!$user_info) {
            throw new CodeSpecialException("timeout");
        }
        return $user_info;
    }


    /**
     * 获取城市树接口
     *
     *
     */
    public function getAllCityTree()
    {
        $url = config('interface', 'service.jw_user.get_all_city_tree');
        if (!$url) {
            throw new CodeSpecialException("failed");
        }
        $params = [];
        $city_list  = (new TimeOut())->runGet($url, $params);
        if (!$city_list) {
            throw new CodeSpecialException("timeout");
        }
        return $city_list;
    }


    /**
     * 保存用户与token的对应关系接口
     * 支持的token prefix：MMPP 小米推送，UMPP 友盟推送，GTPP 个推
     *
     */
    public function savePushToken($params)
    {
        $url = config('interface', 'service.jw_user.save_push_token');
        if (!$url) {
            throw new CodeSpecialException("failed");
        }

        if (empty($params)) {
            throw new CodeSpecialException("failed");
        }
        $pushToken  = (new TimeOut())->runPost($url, $params);
        if (!$pushToken) {
            throw new CodeSpecialException("timeout");
        }
        return $pushToken;
    }

    /**
     * 获取简网用户动态
     */

    public static function getUserListWithDynamic($params)
    {
        $url = config('interface', 'service.jw_user.get_user_dynamic');
        if (!$url) {
            throw new CodeSpecialException("failed");
        }

        if (empty($params['user_id'])) {
            throw new CodeSpecialException("failed");
        }

        $params = ["userIds" => $params['user_id']];
        $list  = (new TimeOut())->runPost($url, $params);
        if (!$list) {
            throw new CodeSpecialException("timeout");
        }
        return $list;
    }

    /**
     * 获取简网用户历史赞
     */

    public static function getUserDigg($params)
    {
        $url = config('interface', 'service.jw_user.get_user_digg');
        if (!$url) {
            throw new CodeSpecialException("failed");
        }

        if (empty($params['user_id'])) {
            throw new CodeSpecialException("failed");
        }

        $params = ["userId" => $params['user_id']];
        $list  = (new TimeOut())->runPost($url, $params);
        if (!$list) {
            throw new CodeSpecialException("timeout");
        }
        return $list;
    }

    /**
     * 给简网同步生活号
     */

    public static function saveUpdateLife($params)
    {
        $url = config('interface', 'service.jw_user.save_update_life');
        if (!$url) {
            throw new CodeSpecialException("failed");
        }

        $ret  = (new TimeOut())->runPost($url, $params);
        if (!$ret) {
            throw new CodeSpecialException("timeout");
        }
        return $ret;
    }
}
