ConfigService.php 5.7 KB

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