ExpressService.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2018/01/10
  6. */
  7. namespace crmeb\services;
  8. use crmeb\services\express\Express;
  9. class ExpressService
  10. {
  11. protected static $api = [
  12. 'query' => 'https://wuliu.market.alicloudapi.com/kdi'
  13. ];
  14. protected static $express = [];
  15. /**
  16. * @param string $type
  17. * @return Express|mixed
  18. */
  19. public static function init($type = 'express')
  20. {
  21. if (isset(self::$express['express_' . $type])) {
  22. return self::$express['express_' . $type];
  23. }
  24. $config = [];
  25. $config['account'] = sys_config('sms_account', '');
  26. $config['secret'] = sys_config('sms_token', '');
  27. return self::$express['express_' . $type] = new Express($type, $config);
  28. }
  29. public static function expressList()
  30. {
  31. $key = md5('crmeb_plat_express_list');
  32. $express = CacheService::get($key);
  33. if (!$express) {
  34. $expressInit = self::init();
  35. try {
  36. $express = $expressInit->express(1, 1, 100);
  37. $express = $express['data'] ?? [];
  38. CacheService::set($key, $express, 86400);
  39. } catch (\Throwable $e) {
  40. $express = [];
  41. }
  42. }
  43. return $express;
  44. }
  45. public static function query($no, $com = '')
  46. {
  47. $expressInit = self::init();
  48. $express = self::expressList();
  49. $code = '';
  50. if ($express) {
  51. $express = array_combine(array_column($express, 'name'), $express);
  52. $code = $express[$com]['code'] ?? '';
  53. }
  54. try {
  55. if ($code) {
  56. $data = $expressInit->query($code, $no);
  57. $result['status'] = 0;
  58. $result['msg'] = 'ok';
  59. $result['result'] = [
  60. 'number' => $data['num'],
  61. 'type' => $data['com'],
  62. 'list' => $data['content']
  63. ];
  64. } else {
  65. $result = self::oldquery($no);
  66. }
  67. } catch (\Throwable $e) {
  68. $result = [];
  69. }
  70. return $result;
  71. }
  72. public static function oldquery($no, $type = '')
  73. {
  74. $appCode = sys_config('system_express_app_code');
  75. if (!$appCode) return false;
  76. $res = HttpService::getRequest(self::$api['query'], compact('no', 'type'), ['Authorization:APPCODE ' . $appCode]);
  77. $result = json_decode($res, true) ?: false;
  78. return $result;
  79. }
  80. }