SnapshotService.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Laravel框架 [ Laravel ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 Laravel研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: wesmiler <12345678@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. use App\Models\ConfigModel;
  13. /**
  14. * 拍照图片-服务类
  15. * @author wesmiler
  16. * @since 2020/11/11
  17. * Class ItemService
  18. * @package App\Services
  19. */
  20. class SnapshotService extends BaseService
  21. {
  22. protected $uuid = null;
  23. protected $appKey = null;
  24. protected $appSecret = null;
  25. protected $token = null;
  26. protected $movedCard = 5;
  27. protected $apiUrl = 'https://rds.xmeye.net/v1';
  28. protected $apiUrls = [
  29. // 设备登录 token参数
  30. 'login'=>'/rtc/device/login/%s',
  31. // 获取配置 token参数
  32. 'getconfig'=> '/rtc/device/getconfig/%s',
  33. // 获取拍照设置 token参数
  34. 'setconfig'=> '/rtc/device/setconfig/%s',
  35. ];
  36. public static $instance = null;
  37. public function __construct()
  38. {
  39. $this->model = new ConfigModel();
  40. $config = $this->model->where(['config_group_id'=> 5,'status'=> 1])
  41. ->select(['value','code'])
  42. ->get()
  43. ->keyBy('code');
  44. $config = $config? $config->toArray() : [];
  45. if(!$config){
  46. throw new \Exception('缺少参数配置');
  47. }
  48. $this->uuid = $config['snapshot_uuid']['value']? $config['snapshot_uuid']['value'] : '';
  49. $this->appKey = $config['snapshot_appkey']['value']? $config['snapshot_appkey']['value'] : $this->appKey;
  50. $this->appSecret = $config['snapshot_appsecret']['value']? $config['snapshot_appsecret']['value'] : $this->appSecret;
  51. $this->apiUrl = $config['snapshot_apiurl']['value']? $config['snapshot_apiurl']['value'] : $this->apiUrl;
  52. }
  53. /**
  54. * 入口
  55. * @return SnapshotService|null
  56. */
  57. public static function make(){
  58. if(self::$instance == null){
  59. self::$instance = new SnapshotService();
  60. }
  61. return self::$instance;
  62. }
  63. public function requestApi(string $apiName, array $params=[]){
  64. if(!isset($this->apiUrls[$apiName])){
  65. return false;
  66. }
  67. $url = $this->apiUrl.$this->apiUrls[$apiName];
  68. }
  69. /**
  70. * 获取签名
  71. * @param int $movedCard
  72. * @return string
  73. */
  74. public function makeSign(int $movedCard = 0){
  75. $movedCard = $movedCard? $movedCard : $this->movedCard;
  76. $signStr = $this->uuid.''.$this->appKey.''.$this->appSecret.'00000011461748332239';
  77. // $signStr = $this->uuid.''.$this->appKey.''.$this->appSecret.$this->getTimeMillis();
  78. $bytes = $this->getBytes($signStr);
  79. $changeBytes = $this->change($signStr, $movedCard);
  80. $mergeBytes = $this->mergeByte($bytes, $changeBytes);
  81. return md5($this->byteToStr($mergeBytes));
  82. }
  83. public function getTimeMillis(){
  84. $rand = rand(1,9999999);
  85. $len = mb_strlen($rand, 'utf8');
  86. $rand = $len<7? str_repeat('0', 7-$len).$rand : $rand;
  87. return $rand.''.time().rand(100,999);
  88. }
  89. public function getBytes($string) {
  90. $bytes = array();
  91. for($i = 0; $i < strlen($string); $i++){
  92. $bytes[] = ord(iconv('utf8','ISO-8859-1', $string[$i]));
  93. }
  94. return $bytes;
  95. }
  96. public static function byteToStr($bytes) {
  97. $str = '';
  98. foreach($bytes as $ch) {
  99. $str .= chr($ch);
  100. }
  101. return $str;
  102. }
  103. /**
  104. * 简单移位运算
  105. * @param $str
  106. * @param int $moveCard
  107. * @return array
  108. */
  109. public function change($str, int $moveCard ){
  110. $bytes = $this->getBytes($str);
  111. $len = count($bytes);
  112. for ($i = 0; $i < $len; $i++) {
  113. $tempBytes = (($i % $moveCard) > (($len - $i) % $moveCard)) ? $bytes[$i] : $bytes[$len - ($i + 1)];
  114. $bytes[$i] = $bytes[$len - ($i + 1)];
  115. $bytes[$len - ($i + 1)] = $tempBytes;
  116. }
  117. return $bytes;
  118. }
  119. /**
  120. * 合并
  121. * @param array $bytes
  122. * @param array $bytes1
  123. * @return array
  124. */
  125. public function mergeByte(array $bytes, array $bytes1 ){
  126. $tempBytes = [];
  127. $len = count($bytes);
  128. $len1 = count($bytes1)*2;
  129. for ($i = 0; $i < $len; $i++) {
  130. $tempBytes[$i] = $bytes[$i];
  131. $tempBytes[$len1 - 1 - $i] = $bytes1[$i];
  132. }
  133. ksort($tempBytes);
  134. return $tempBytes;
  135. }
  136. }