// +---------------------------------------------------------------------- namespace App\Services; use App\Models\ConfigModel; /** * 拍照图片-服务类 * @author wesmiler * @since 2020/11/11 * Class ItemService * @package App\Services */ class SnapshotService extends BaseService { protected $uuid = null; protected $appKey = null; protected $appSecret = null; protected $token = null; protected $movedCard = 5; protected $apiUrl = 'https://rds.xmeye.net/v1'; protected $apiUrls = [ // 设备登录 token参数 'login'=>'/rtc/device/login/%s', // 获取配置 token参数 'getconfig'=> '/rtc/device/getconfig/%s', // 获取拍照设置 token参数 'setconfig'=> '/rtc/device/setconfig/%s', ]; public static $instance = null; public function __construct() { $this->model = new ConfigModel(); $config = $this->model->where(['config_group_id'=> 5,'status'=> 1]) ->select(['value','code']) ->get() ->keyBy('code'); $config = $config? $config->toArray() : []; if(!$config){ throw new \Exception('缺少参数配置'); } $this->uuid = $config['snapshot_uuid']['value']? $config['snapshot_uuid']['value'] : ''; $this->appKey = $config['snapshot_appkey']['value']? $config['snapshot_appkey']['value'] : $this->appKey; $this->appSecret = $config['snapshot_appsecret']['value']? $config['snapshot_appsecret']['value'] : $this->appSecret; $this->apiUrl = $config['snapshot_apiurl']['value']? $config['snapshot_apiurl']['value'] : $this->apiUrl; } /** * 入口 * @return SnapshotService|null */ public static function make(){ if(self::$instance == null){ self::$instance = new SnapshotService(); } return self::$instance; } public function requestApi(string $apiName, array $params=[]){ if(!isset($this->apiUrls[$apiName])){ return false; } $url = $this->apiUrl.$this->apiUrls[$apiName]; } /** * 获取签名 * @param int $movedCard * @return string */ public function makeSign(int $movedCard = 0){ $movedCard = $movedCard? $movedCard : $this->movedCard; $signStr = $this->uuid.''.$this->appKey.''.$this->appSecret.'00000011461748332239'; // $signStr = $this->uuid.''.$this->appKey.''.$this->appSecret.$this->getTimeMillis(); $bytes = $this->getBytes($signStr); $changeBytes = $this->change($signStr, $movedCard); $mergeBytes = $this->mergeByte($bytes, $changeBytes); return md5($this->byteToStr($mergeBytes)); } public function getTimeMillis(){ $rand = rand(1,9999999); $len = mb_strlen($rand, 'utf8'); $rand = $len<7? str_repeat('0', 7-$len).$rand : $rand; return $rand.''.time().rand(100,999); } public function getBytes($string) { $bytes = array(); for($i = 0; $i < strlen($string); $i++){ $bytes[] = ord(iconv('utf8','ISO-8859-1', $string[$i])); } return $bytes; } public static function byteToStr($bytes) { $str = ''; foreach($bytes as $ch) { $str .= chr($ch); } return $str; } /** * 简单移位运算 * @param $str * @param int $moveCard * @return array */ public function change($str, int $moveCard ){ $bytes = $this->getBytes($str); $len = count($bytes); for ($i = 0; $i < $len; $i++) { $tempBytes = (($i % $moveCard) > (($len - $i) % $moveCard)) ? $bytes[$i] : $bytes[$len - ($i + 1)]; $bytes[$i] = $bytes[$len - ($i + 1)]; $bytes[$len - ($i + 1)] = $tempBytes; } return $bytes; } /** * 合并 * @param array $bytes * @param array $bytes1 * @return array */ public function mergeByte(array $bytes, array $bytes1 ){ $tempBytes = []; $len = count($bytes); $len1 = count($bytes1)*2; for ($i = 0; $i < $len; $i++) { $tempBytes[$i] = $bytes[$i]; $tempBytes[$len1 - 1 - $i] = $bytes1[$i]; } ksort($tempBytes); return $tempBytes; } }