TruckService.php 4.2 KB

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