| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- // +----------------------------------------------------------------------
- // | Laravel框架 [ Laravel ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 Laravel研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: wesmiler <12345678@qq.com>
- // +----------------------------------------------------------------------
- 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;
- }
- }
|