<?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;
    }
 
}
