SysConfigFacadeRepository.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace App\Repositories\Eloquent;
  3. use App\Repositories\Contracts\SysConfigInterface;
  4. use App\Models\SysConfig;
  5. use Carbon\Carbon;
  6. use GuzzleHttp\Client as httpClient;
  7. use Illuminate\Support\Facades\Redis;
  8. use App\Models\PatientCard;
  9. /**
  10. * 用户
  11. * @author lilin
  12. *
  13. */
  14. class SysConfigFacadeRepository extends BaseRepository implements SysConfigInterface
  15. {
  16. public function scoketConfig($uid)
  17. {
  18. $user = PatientCard::where('uid',$uid)->where('is_chat', PatientCard::IS_CHAT_1)->first();
  19. if (!$user) {
  20. $this->error()->fail('请先创建"就诊人"或默认选中一个"就诊人"。栏目位置:我的-就诊人');
  21. }
  22. $data = [
  23. 'address' => config('console.third_consult.scoket_address'),
  24. 'data' =>[
  25. 'appid' => config('console.third_consult.appid'),
  26. 'app_type' => 1,
  27. 'customer_open_id' => $user->third_customer_openid,
  28. 'timestamp' => time()
  29. ]
  30. ];
  31. $signStr = config('console.third_consult.secret').$data['data']['app_type'].$data['data']['customer_open_id'].$data['data']['timestamp'];
  32. $data['data']['auth_sign'] = md5(md5($signStr));
  33. return $this->response($data);
  34. }
  35. public function getList(array $conditions, array $fields, string $sort, int $page, int $limit)
  36. {
  37. $result = SysConfig::where(function($query) use($conditions){
  38. if (isset($conditions['uid'])){
  39. $query->where('uid', $conditions['uid']);
  40. }
  41. })->orderByRaw($sort)->paginate($limit, $fields);
  42. return $this->response($result);
  43. }
  44. public function findBy(array $conditions, array $fields){
  45. $result = SysConfig::where(function($query) use($conditions){
  46. if (isset($conditions['id'])){
  47. $query->where('id', $conditions['id']);
  48. }
  49. if (isset($conditions['uid'])){
  50. $query->where('uid', $conditions['uid']);
  51. }
  52. })->first($fields);
  53. $carbon = Carbon::now();
  54. $result->now_time = $carbon->toDateTimeString();
  55. $result->today = $carbon->toDateString();
  56. $result->month = number2Chinese(intval($carbon->format('m')));
  57. $result->month_n = $carbon->format('m');
  58. $result->day = $carbon->format('d');
  59. $result->time = $carbon->format('H:i');
  60. $result->week = week2Chinese($carbon->dayOfWeek);
  61. $result->last12hour = $carbon->subHours(12)->toDateTimeString();
  62. return $result ? $this->response($result) : $this->error()->dataDoesNotExist();
  63. }
  64. public function create(array $data)
  65. {
  66. $result = SysConfig::create($data);
  67. return $result ? $this->response($result) : $this->error()->fail();
  68. }
  69. public function updateBy(array $conditions, array $data)
  70. {
  71. $result = SysConfig::where(function($query) use($conditions){
  72. if (isset($conditions['id'])){
  73. $query->where('id', $conditions['id']);
  74. }
  75. if (isset($conditions['uid'])){
  76. $query->where('uid', $conditions['uid']);
  77. }
  78. })->update($data);
  79. return $result ? $this->response($result) : $this->error()->fail();
  80. }
  81. public function deleteBy(array $conditions)
  82. {
  83. $result = SysConfig::where(function($query) use($conditions){
  84. if (isset($conditions['id'])){
  85. $query->where('id', $conditions['id']);
  86. }
  87. if (isset($conditions['uid'])){
  88. $query->where('uid', $conditions['uid']);
  89. }
  90. })->delete();
  91. return $result ? $this->response($result) : $this->error()->fail();
  92. }
  93. public function weather(string $cityid)
  94. {
  95. $redisKey = config('console.redis_key.weather');
  96. $rData = Redis::GET($redisKey);
  97. if ($rData){
  98. $this->setLog(self::TYPENAME.'获取天气 redis 得到', $this->startTime(), [$rData]);
  99. $data = json_decode($rData, true);
  100. return $this->response($data);
  101. }
  102. //请求地址
  103. $url = config('console.weather');
  104. try {
  105. $client = new httpClient();
  106. $res = $client->request('get', $url);
  107. $body = $res->getBody()->getContents();
  108. $data = json_decode($body, true);
  109. Redis::SET($redisKey, json_encode($data));
  110. Redis::EXPIRE($redisKey, 15*60);
  111. return $this->response($data);
  112. } catch (\Exception $e) {
  113. $this->setLog(self::TYPENAME.'weather 远程请求失败', $this->startTime, [$e->getMessage()]);
  114. $this->error()->fail();
  115. }
  116. }
  117. }