Utils.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. namespace App\Libs;
  3. use App\Cache\CacheKeys;
  4. use App\Exceptions\ApiException;
  5. use Hashids;
  6. use Illuminate\Support\Arr;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Facades\Redis;
  9. class Utils
  10. {
  11. /**
  12. * 异常报错
  13. * @param $errorData
  14. * @throws ApiException
  15. */
  16. public static function throwError($errorData)
  17. {
  18. // 分解错误码、错误信息
  19. $arr = explode(':', (string)$errorData);
  20. $code = (int)$arr[0];
  21. $msg = (string)$arr[1];
  22. throw new ApiException($code, $msg);
  23. }
  24. /**
  25. * 解码加密id
  26. * @param $idStr
  27. * @return int
  28. */
  29. public static function getDecodeId($idStr)
  30. {
  31. if (!is_numeric($idStr)) {
  32. $idArr = Hashids::decode($idStr);
  33. $id = isset($idArr[0]) ? (int)$idArr[0] : 0;
  34. } else {
  35. $id = $idStr;
  36. }
  37. return $id;
  38. }
  39. /**
  40. * 加密id
  41. * @param $id
  42. * @return mixed
  43. */
  44. public static function getEncodeId($id)
  45. {
  46. return Hashids::encode($id);
  47. }
  48. /**
  49. * 加密批量bid
  50. * @param $bids
  51. * @return array
  52. */
  53. public static function getEncodeIds($bids)
  54. {
  55. return array_map(function ($bid) {
  56. return Hashids::encode($bid);
  57. }, $bids);
  58. }
  59. /**
  60. * 审核状态判断,true为审核状态,false默认
  61. * @param $package
  62. * @param $brand
  63. * @param $codeVersion
  64. * @return bool
  65. */
  66. public static function checkIsAudit($package, $brand, $codeVersion): bool
  67. {
  68. $audit = getProp(config('audit'), $package, []);
  69. if ($audit && $codeVersion === getProp($audit, 'codeVersion') && strtolower($brand) === getProp($audit, 'brand')) {
  70. return true;
  71. }
  72. return false;
  73. }
  74. /**
  75. * redis key组装
  76. * @param $keyStr
  77. * @param $args
  78. * @return string
  79. */
  80. public static function getCacheKey($keyStr, $args = [])
  81. {
  82. $stdStr = Arr::get(CacheKeys::$all, $keyStr, '');
  83. if ($args) {
  84. $stdStr = sprintf($stdStr, ...$args);
  85. }
  86. return $stdStr;
  87. }
  88. /**
  89. * 展示友好数字
  90. * @param $num
  91. * @return string
  92. */
  93. public static function showNiceNumber($num)
  94. {
  95. if ($num >= 10000) {
  96. $num = sprintf('%.1f', round($num / 10000 * 100) / 100) . '万';
  97. } elseif ($num >= 1000) {
  98. $num = sprintf('%.1f', round($num / 1000 * 100) / 100) . '千';
  99. }
  100. return $num;
  101. }
  102. /**
  103. * 根据日期获得这周的周一
  104. * @param $date
  105. * @param int $d
  106. * @param string $format
  107. * @return false|string
  108. */
  109. public static function firstOfWeek($date, $d = 0, $format = 'Ymd')
  110. {
  111. $now = strtotime($date) - $d * 86400; //当时的时间戳
  112. $number = date("w", $now); //当时是周几
  113. $number = $number == 0 ? 7 : $number; //如遇周末,将0换成7
  114. $diff_day = $number - 1; //求到周一差几天
  115. return date($format, $now - ($diff_day * 60 * 60 * 24));
  116. }
  117. /**
  118. * 老原创的签名
  119. * @param $time
  120. * @return string
  121. */
  122. public static function getOldYcSign($time): string
  123. {
  124. $privateKey = env('EXTERNAL_PRIVATE_KEY');
  125. return md5(md5($time . $privateKey) . $privateKey);
  126. }
  127. /**
  128. * @param array $data
  129. * @param array $colHeaders
  130. * @param bool $asString
  131. * @return bool|string
  132. */
  133. public static function toCSV(array $data, array $colHeaders = array(), $asString = false)
  134. {
  135. $stream = $asString ? fopen('php://temp/maxmemory', 'wb+') : fopen('php://output', 'wb');
  136. if (!empty($colHeaders)) {
  137. fputcsv($stream, $colHeaders);
  138. }
  139. foreach ($data as $record) {
  140. fputcsv($stream, $record);
  141. }
  142. if ($asString) {
  143. rewind($stream);
  144. $returnVal = stream_get_contents($stream);
  145. fclose($stream);
  146. return $returnVal;
  147. }
  148. fclose($stream);
  149. return true;
  150. }
  151. /**
  152. * 设备信息
  153. * @return string
  154. */
  155. public static function getDeviceType(): string
  156. {
  157. //全部变成小写字母
  158. $agent = strtolower(getProp($_SERVER, 'HTTP_USER_AGENT'));
  159. $type = 'other';
  160. //分别进行判断
  161. if (strpos($agent, 'iphone') || strpos($agent, 'ipad')) {
  162. $type = 'ios';
  163. }
  164. if (strpos($agent, 'android')) {
  165. $type = 'android';
  166. }
  167. return $type;
  168. }
  169. /**
  170. * 是否在微信内打卡
  171. * @return bool
  172. */
  173. public static function isOpenedInWechat(): bool
  174. {
  175. //全部变成小写字母
  176. $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
  177. //判断
  178. if (strpos($agent, 'micromessenger')) {
  179. return true;
  180. }
  181. return false;
  182. }
  183. /**
  184. * 组装meta数据
  185. * @param $page
  186. * @param $total
  187. * @param int $pageSize
  188. * @return array
  189. */
  190. public static function buildMeta($page, $total, $pageSize = 15): array
  191. {
  192. $lastPage = (int)ceil($total / $pageSize);
  193. return [
  194. 'current_page' => (int)$page,
  195. 'next_page' => ++$page,
  196. 'last_page' => $lastPage ?? 1,
  197. 'per_page' => (int)$pageSize,
  198. 'total' => (int)$total,
  199. 'next_page_url' => '',
  200. 'prev_page_url' => ''
  201. ];
  202. }
  203. /**
  204. * 获取token保存的信息
  205. * @param $token
  206. * @return mixed
  207. */
  208. public static function getTokenData($token) {
  209. $tokenJson = Redis::get($token);
  210. return json_decode($tokenJson, true);
  211. }
  212. /**
  213. * 获取口令码
  214. * @param $uid
  215. * @param $bid
  216. * @return int|mixed|null
  217. */
  218. public static function getPasswordCode($uid, $bid) {
  219. $code = DB::table('user_book_code')->where(['uid'=>$uid, 'bid'=>$bid])->value('id');
  220. if (!$code) {
  221. $code = DB::table('user_book_code')->insertGetId([
  222. 'uid' => $uid,
  223. 'bid' => $bid,
  224. 'created_at' => date('Y-m-d H:i:s'),
  225. 'updated_at' => date('Y-m-d H:i:s'),
  226. ]);
  227. }
  228. return '请在抖音搜索“火猫小说”后搜索口令'.$code.',开始阅读吧!';
  229. }
  230. }