ConfigService.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace App\Services;
  3. use App\Constants\HttpStatus;
  4. use App\Services\BaseService;
  5. use App\Models\Config;
  6. use Exception;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Collection;
  9. use Illuminate\Support\Facades\Cache;
  10. use Illuminate\Support\Facades\Log;
  11. /**
  12. * 配置
  13. */
  14. class ConfigService extends BaseService
  15. {
  16. public static string $MODEL = Config::class;
  17. /**
  18. * @description: 模型
  19. * @return {string}
  20. */
  21. public static function model(): string
  22. {
  23. return Config::class;
  24. }
  25. /**
  26. * @description: 枚举
  27. * @return {*}
  28. */
  29. public static function enum(): string
  30. {
  31. return '';
  32. }
  33. /**
  34. * @description: 获取查询条件
  35. * @param {array} $search 查询内容
  36. * @return {array}
  37. */
  38. public static function getWhere(array $search = []): array
  39. {
  40. $where = [];
  41. if (isset($search['field']) && !empty($search['field'])) {
  42. $where[] = ['field', '=', $search['field']];
  43. }
  44. if (isset($search['group_id']) && !empty($search['group_id'])) {
  45. if (!is_array($search['group_id'])) {
  46. $where[] = ['group_id', '=', $search['group_id']];
  47. }
  48. }
  49. if (isset($search['id']) && !empty($search['id'])) {
  50. $where[] = ['id', '=', $search['id']];
  51. }
  52. return $where;
  53. }
  54. public static function getWhereIn(array $search = []): array
  55. {
  56. $where = [];
  57. if (isset($search['in_group_id']) && is_array($search['in_group_id'])) {
  58. $where['group_id'] = $search['in_group_id'];
  59. }
  60. return $where;
  61. }
  62. /**
  63. * @description: 查询单条数据
  64. * @param array $search
  65. * @return \App\Models\Coin|null
  66. */
  67. public static function findOne(array $search): ?Config
  68. {
  69. return self::model()::where(self::getWhere($search))->first();
  70. }
  71. /**
  72. * @description: 查询所有数据
  73. * @param array $search
  74. * @return \Illuminate\Database\Eloquent\Collection
  75. */
  76. public static function findAll(array $search = [])
  77. {
  78. return self::model()::where(self::getWhere($search))->get();
  79. }
  80. /**
  81. * @description: 分页查询
  82. * @param array $search
  83. * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator
  84. */
  85. public static function paginate(array $search = [])
  86. {
  87. $limit = isset($search['limit']) ? $search['limit'] : 15;
  88. $query = self::model()::where(self::getWhere($search));
  89. $where = self::getWhereIn($search);
  90. foreach ($where as $key => $value) {
  91. $query = $query->whereIn($key, $search['in_group_id']);
  92. }
  93. $paginator = $query->paginate($limit);
  94. return ['total' => $paginator->total(), 'data' => $paginator->items()];
  95. }
  96. /**
  97. * @description:
  98. * @param {*} $params
  99. * @return {*}
  100. */
  101. public static function submit($params = [])
  102. {
  103. $result = false;
  104. $msg['code'] = self::NOT;
  105. $msg['msg'] = '';
  106. // 2. 判断是否是更新
  107. if (!empty($params['id'])) {
  108. // 更新
  109. $info = self::findOne(['id' => $params['id']]);
  110. if (!$info) {
  111. $msg['msg'] = '配置不存在!';
  112. } else {
  113. unset($params['field']);
  114. $result = $info->update($params);
  115. $id = $params['id'];
  116. }
  117. } else {
  118. // 创建
  119. $result = $info = self::model()::create($params);
  120. $id = $result->id;
  121. }
  122. if ($result) {
  123. $msg['code'] = self::YES;
  124. $msg['msg'] = '设置成功';
  125. } else {
  126. $msg['msg'] = empty($msg['msg']) ? '操作失败' : $msg['msg'];
  127. }
  128. return $msg;
  129. }
  130. public static function syncExchangeRate()
  131. {
  132. $url = "https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@latest/v1/currencies/usd.json";
  133. $result = file_get_contents($url, false, stream_context_create([
  134. 'http' => [
  135. 'method' => 'GET',
  136. 'header' => "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3\r\n"
  137. ]
  138. ]));
  139. $result = json_decode($result, true);
  140. $rate = number_format($result['usd']['cnh'], 2);
  141. $info = self::findOne(['field' => 'exchange_rate_rmb']);
  142. $info->val = $rate;
  143. $info->save();
  144. return $result;
  145. }
  146. public static function getVal(string $field)
  147. {
  148. $config = static::$MODEL::where('field', $field)->first();
  149. if ($config) return $config->val;
  150. throw new Exception("参数不存在", HttpStatus::CUSTOM_ERROR);
  151. }
  152. /**
  153. * 每天把19:56–20:30这段时间的开奖切换成 极速PC28
  154. */
  155. public function autoSwitchGameType(): void
  156. {
  157. $day = date('Y-m-d');
  158. $start = strtotime("$day 19:56:00");
  159. $end = strtotime("$day 20:30:00");
  160. $now = time();
  161. if ($start <= $now && $now <= $end) {
  162. Cache::put('pc28_switch', 1);
  163. } else {
  164. Cache::put('pc28_switch', 0);
  165. }
  166. }
  167. }