<?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.
*/
/**
* Device.php.
*
* @author allen05ren <allen05ren@outlook.com>
* @copyright 2016 overtrue <i@overtrue.me>
*
* @see https://github.com/overtrue
* @see http://overtrue.me
*/
namespace EasyWeChat\ShakeAround;
use EasyWeChat\Core\AbstractAPI;
use EasyWeChat\Core\Exceptions\InvalidArgumentException;
class Device extends AbstractAPI
{
const API_DEVICE_APPLYID = 'https://api.weixin.qq.com/shakearound/device/applyid';
const API_DEVICE_APPLYSTATUS = 'https://api.weixin.qq.com/shakearound/device/applystatus';
const API_DEVICE_UPDATE = 'https://api.weixin.qq.com/shakearound/device/update';
const API_DEVICE_BINDLOCATION = 'https://api.weixin.qq.com/shakearound/device/bindlocation';
const API_DEVICE_SEARCH = 'https://api.weixin.qq.com/shakearound/device/search';
public function apply($quantity, $reason, $comment = '', $poiId = null)
{
$params = [
'quantity' => intval($quantity),
'apply_reason' => $reason,
];
if (!empty($comment)) {
$params['comment'] = $comment;
}
if (!is_null($poiId)) {
$params['poi_id'] = intval($poiId);
}
return $this->parseJSON('json', [self::API_DEVICE_APPLYID, $params]);
}
public function getStatus($applyId)
{
$params = [
'apply_id' => intval($applyId),
];
return $this->parseJSON('json', [self::API_DEVICE_APPLYSTATUS, $params]);
}
public function update(array $deviceIdentifier, $comment)
{
$params = [
'device_identifier' => $deviceIdentifier,
'comment' => $comment,
];
return $this->parseJSON('json', [self::API_DEVICE_UPDATE, $params]);
}
public function bindLocation(array $deviceIdentifier, $poiId, $type = 1, $poiAppid = null)
{
$params = [
'device_identifier' => $deviceIdentifier,
'poi_id' => intval($poiId),
];
if (2 === $type) {
if (is_null($poiAppid)) {
throw new InvalidArgumentException('If value of argument #3 is 2, argument #4 is required.');
}
$params['type'] = 2;
$params['poi_appid'] = $poiAppid;
}
return $this->parseJSON('json', [self::API_DEVICE_BINDLOCATION, $params]);
}
public function fetchByIds(array $deviceIdentifiers)
{
$params = [
'type' => 1,
'device_identifiers' => $deviceIdentifiers,
];
return $this->fetch($params);
}
public function pagination($lastSeen, $count)
{
$params = [
'type' => 2,
'last_seen' => intval($lastSeen),
'count' => intval($count),
];
return $this->fetch($params);
}
public function fetchByApplyId($applyId, $lastSeen, $count)
{
$params = [
'type' => 3,
'apply_id' => intval($applyId),
'last_seen' => intval($lastSeen),
'count' => intval($count),
];
return $this->fetch($params);
}
private function fetch($params)
{
return $this->parseJSON('json', [self::API_DEVICE_SEARCH, $params]);
}
}