Fare.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 运费设置
  7. */
  8. namespace app\fastshop\controller;
  9. use app\common\controller\Manage;
  10. use think\facade\Config;
  11. class Fare extends Manage{
  12. public function initialize() {
  13. parent::initialize();
  14. if(!model('auth')->getAuth($this->user->id,0)){
  15. $this->error('无权限,你非【超级管理员】');
  16. }
  17. $this->assign('pathMaps',[['name'=>'运费设置','url'=>url("fastshop/fare/index")]]);
  18. }
  19. /**
  20. * 运费管理
  21. */
  22. public function index(){
  23. $view['info'] = model('Fare')->get(['member_miniapp_id' => $this->member_miniapp_id]);
  24. return view('index',$view);
  25. }
  26. /**
  27. * 编辑/保存
  28. */
  29. public function save(){
  30. if(request()->isAjax()){
  31. $data = [
  32. 'first_weight' => input('post.first_weight/d'),
  33. 'first_price' => input('post.first_price/d'),
  34. 'second_weight' => input('post.second_weight/d'),
  35. 'second_price' => input('post.second_price/d'),
  36. ];
  37. $validate = $this->validate($data,'fare.save');
  38. if(true !== $validate){
  39. return json(['code'=>0,'msg'=>$validate]);
  40. }
  41. $rel = model('Fare')->get(['member_miniapp_id' => $this->member_miniapp_id]);
  42. if(empty($rel)){
  43. $data['member_miniapp_id'] = $this->member_miniapp_id;
  44. $result = model('Fare')->insert($data);
  45. }else{
  46. $result = model('Fare')->save($data,['member_miniapp_id' => $this->member_miniapp_id]);
  47. }
  48. if($result){
  49. return json(['code'=>200,'data' => ['url' => url('fastshop/fare/index')],'msg'=>'操作成功']);
  50. }else{
  51. return json(['code'=>0,'msg'=>'操作失败']);
  52. }
  53. }
  54. }
  55. /**
  56. * 计算运费多少钱
  57. * @param array $item [计算参数]
  58. * @return array 商品价格信息
  59. */
  60. public static function realAmount($item,$member_miniapp_id){
  61. $fare = self::where(['member_miniapp_id' => $member_miniapp_id])->find();
  62. $real_amount = 0; //商品总价
  63. $real_freight = 0; //运费总价
  64. $total = 0; //单SKU运费
  65. foreach($item as $value){
  66. $real_amount += $value['amount'];
  67. $weight = $value['weight'] * $value['num'];
  68. if($weight <= $fare['first_weight'] || 0 == $fare['second_weight']){
  69. $total = $fare['first_price'];
  70. }else{
  71. $weight = $weight - $fare['second_weight'];
  72. $total = $fare['first_price'] + ceil($weight/$fare['second_weight']) * $fare['second_price'];
  73. }
  74. $real_freight += $total;
  75. }
  76. $data['real_amount'] = money($real_amount); //商品价格
  77. $data['real_freight'] = money($real_freight); //运费
  78. $data['order_amount'] = money($real_freight+$real_amount); //商品总价+运费
  79. return $data;
  80. }
  81. }