ExpressService.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace app\data\service;
  3. use app\data\model\BasePostageRegion;
  4. use app\data\model\BasePostageTemplate;
  5. use think\admin\extend\DataExtend;
  6. use think\admin\Service;
  7. use think\admin\service\InterfaceService;
  8. /**
  9. * 快递查询数据服务
  10. * Class ExpressService
  11. * @package app\data\service
  12. */
  13. class ExpressService extends Service
  14. {
  15. /**
  16. * 模拟计算快递费用
  17. * @param array $codes 模板编号
  18. * @param string $provName 省份名称
  19. * @param string $cityName 城市名称
  20. * @param integer $truckCount 邮费基数
  21. * @return array [邮费金额, 计费基数, 模板编号, 计费描述]
  22. */
  23. public static function amount(array $codes, string $provName, string $cityName, int $truckCount = 0): array
  24. {
  25. if (empty($codes)) return [0, $truckCount, '', '邮费模板编码为空!'];
  26. $map = [['status', '=', 1], ['deleted', '=', 0], ['code', 'in', $codes]];
  27. $template = BasePostageTemplate::mk()->where($map)->order('sort desc,id desc')->findOrEmpty();
  28. if ($template->isEmpty()) return [0, $truckCount, '', '邮费模板编码无效!'];
  29. $rule = json_decode($template['normal'] ?: '[]', true) ?: [];
  30. foreach (json_decode($template['content'] ?: '[]', true) ?: [] as $item) {
  31. if (isset($item['city']) && is_array($item['city'])) foreach ($item['city'] as $city) {
  32. if ($city['name'] === $provName && in_array($cityName, $city['subs'])) {
  33. $rule = $item['rule'];
  34. break 2;
  35. }
  36. }
  37. }
  38. [$firstNumber, $firstAmount] = [$rule['firstNumber'] ?: 0, $rule['firstAmount'] ?: 0];
  39. [$repeatNumber, $repeatAmount] = [$rule['repeatNumber'] ?: 0, $rule['repeatAmount'] ?: 0];
  40. if ($truckCount <= $firstNumber) {
  41. return [$firstAmount, $truckCount, $template['code'], "首件计费,不超过{$firstNumber}件"];
  42. } else {
  43. $amount = $repeatNumber > 0 ? $repeatAmount * ceil(($truckCount - $firstNumber) / $repeatNumber) : 0;
  44. return [$firstAmount + $amount, $truckCount, $template['code'], "续件计费,超出{$firstNumber}件续件{$amount}元"];
  45. }
  46. }
  47. /**
  48. * 获取快递模板数据
  49. * @return array
  50. */
  51. public static function templates(): array
  52. {
  53. $query = BasePostageTemplate::mk()->where(['status' => 1, 'deleted' => 0]);
  54. return $query->order('sort desc,id desc')->column('code,name,normal,content', 'code');
  55. }
  56. /**
  57. * 配送区域树型数据
  58. * @param integer $level 最大等级
  59. * @param null|integer $status 状态筛选
  60. * @return array
  61. */
  62. public static function region(int $level = 3, ?int $status = null): array
  63. {
  64. $query = BasePostageRegion::mk();
  65. if (is_numeric($level)) $query->where('level', '<=', $level);
  66. if (is_numeric($status)) $query->where(['status' => $status]);
  67. $items = DataExtend::arr2tree($query->column('id,pid,name,status', 'id'), 'id', 'pid', 'subs');
  68. // 排序子集为空的省份和城市
  69. foreach ($items as $ik => $item) {
  70. foreach ($item['subs'] as $ck => $city) {
  71. if (isset($city['subs']) && empty($city['subs'])) unset($items[$ik]['subs'][$ck]);
  72. }
  73. if (isset($item['subs']) && empty($item['subs'])) unset($items[$ik]);
  74. }
  75. return $items;
  76. }
  77. /**
  78. * 楚才开放平台快递查询
  79. * @param string $code 快递公司编号
  80. * @param string $number 快递配送单号
  81. * @return array
  82. * @throws \think\admin\Exception
  83. */
  84. public static function query(string $code, string $number): array
  85. {
  86. return static::getInterface()->doRequest('api.auth.express/query', [
  87. 'type' => 'free', 'express' => $code, 'number' => $number,
  88. ]);
  89. }
  90. /**
  91. * 楚才开放平台快递公司
  92. * @return array
  93. * @throws \think\admin\Exception
  94. */
  95. public static function company(): array
  96. {
  97. return static::getInterface()->doRequest('api.auth.express/getCompany');
  98. }
  99. /**
  100. * 获取楚才开放平台接口实例
  101. * @return InterfaceService
  102. */
  103. private static function getInterface(): InterfaceService
  104. {
  105. $service = InterfaceService::instance();
  106. // 测试的账号及密钥随时可能会变更,请联系客服更新
  107. $service->getway('https://open.cuci.cc/user/');
  108. $service->setAuth("6998081316132228", "193fc1d9a2aac78475bc8dbeb9a5feb1");
  109. return $service;
  110. }
  111. }