ExpressService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://demo.thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // +----------------------------------------------------------------------
  11. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  12. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  13. // +----------------------------------------------------------------------
  14. namespace app\common\service;
  15. use think\Db;
  16. /**
  17. * 商城邮费服务
  18. * Class ExpressService
  19. * @package app\store\service
  20. */
  21. class ExpressService
  22. {
  23. public static function getGoodsExpressPrice($goods_info,$address_info,$num)
  24. {
  25. if($goods_info['freight_type'] == 0){
  26. return ['code'=>200,'freight'=> $goods_info['postage'],'msg'=>'','id'=>0];
  27. } else if($goods_info['freight_id']){
  28. return self::getExpressPrice($address_info,$goods_info['freight_id'],$num) ; // 快递费用
  29. }else{
  30. return ['code'=>200,'freight'=> 0,'msg'=>'','id'=>0];
  31. }
  32. }
  33. /**
  34. * @param array $address_info 收货地址
  35. * @param $freight_id 运费模板id
  36. * @param int $num 商品数量
  37. * @return array
  38. * @throws \think\db\exception\DataNotFoundException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. * @throws \think\exception\DbException
  41. */
  42. public static function getExpressPrice($address_info,$freight_id,$num = 1)
  43. {
  44. $data = ['code'=>200,'freight'=>0 ,'msg'=>'','id'=>0];
  45. $template_info = Db::name('freight_template')->find($freight_id);
  46. if(empty($address_info)){
  47. $data['code'] = 201;
  48. $data['msg'] = '运费模板不存在';
  49. return $data;
  50. }
  51. $city_id = $address_info['city_id'];
  52. $no_express = explode(',',$template_info['no_express']);
  53. if(in_array($city_id,$no_express)) return ['code'=>202,'freight'=>0 ,'msg'=>'该收货地址不配送物流'];
  54. // 模板自定义分组
  55. $custom = json_decode($template_info['custom'],true);
  56. $base_num = $template_info['base_num'];// 基件数量
  57. $base_price = $template_info['base_price']; // 基件费用
  58. $base_keep_price = $template_info['base_keep_price']; // 续件费用
  59. foreach($custom as $cus_info) {
  60. $express =explode(',',$cus_info['express']);
  61. if(in_array($city_id,$express)) {
  62. $base_num = $cus_info['first_num'];
  63. $base_price = $cus_info['first_price'];
  64. $base_keep_price = $cus_info['keep_price'];
  65. break;
  66. }
  67. }
  68. if($base_num >= $num ) return $data = ['code'=>200,'freight'=>$base_price ,'msg'=>'ok'];
  69. $sub_keep = bcmul(bcsub($num,$base_num),$base_keep_price,2);
  70. return $data = ['code'=>200,'freight'=>bcadd($base_price,$sub_keep,2) ,'msg'=>'ok'];
  71. }
  72. }