Browse Source

门店小程序码和保养列表等

zhangxiaobin 1 year ago
parent
commit
d78f3e5e77

+ 2 - 0
application/api/controller/Companys.php

@@ -115,6 +115,7 @@ class Companys extends Api
         Db::startTrans();
         try {
             $id = $this->request->param('id',0);
+            $comeform = $this->request->param('comeform','');//'自然进店','平台引流','线下新客','老带新'
             $where['id'] = $id;
             $where['status'] = 1;
             $company = $this->model->where($where)->find();
@@ -140,6 +141,7 @@ class Companys extends Api
                     'user_id' => $userId,
                     'company_id' => $id,
                     'money' => 0.00,
+                    'comefrom' => $comeform,
                 ];
                 $walletRes = model('UserWallet')->insertGetId($walletData);
                 if (!$walletRes) {

+ 44 - 0
application/api/controller/Order.php

@@ -157,4 +157,48 @@ class Order extends Api
             $this->error($e->getMessage());
         }
     }
+    
+    /**
+     * 保养列表
+     * @return void
+     */
+    public function upkeepList()
+    {
+        try {
+            $serviceTypeId = input('servicetype_id',0);
+            $carId = input('car_id',0);
+            $carNumber = input('car_number','');
+            if (!empty($serviceTypeId)) {
+                $where['servicetype_id'] = $serviceTypeId;
+            }
+            if (!empty($carId)) {
+                $where['car_id'] = $carId;
+            }
+            if (empty($carNumber)) {
+                $where['user_car_number'] = $carNumber;
+            }
+            $where['user_id'] = $this->auth->id;
+            $where['status'] = 3;//状态:2=待处理,3=已完成,4=已取消
+
+            $o = 'order';
+            $st = 'servicetype';
+            $field = $o.'.id,servicetype_id,server_info,finish_time,next_date,next_carlicheng,pay_fee,appen_fee,'.$st.'.title as `service_title`';
+            $result = $this->model->alias($o)->field($field)
+                ->join($st,$st.'.id = '.$o.'.servicetype_id','LEFT')
+                ->where($where)->order($o.'.finish_time desc')->autopage()->select();
+            if (!empty($result)) {
+                $timeArr = ['finish_time'];
+                foreach ($result as $key => &$value) {
+                    foreach ($timeArr as $k => $v) {
+                        $value[$v] = !empty($value[$v]) ? date('Y-m-d', $value[$v]) : '';
+                    }
+                    $value['total_amounts'] = bcadd($value['pay_fee'],$value['appen_fee'],2);
+                }
+            }
+            
+            $this->success('获取成功',$result);
+        } catch (Exception $e) {
+            $this->error($e->getMessage());
+        }
+    }
 }

+ 1 - 0
application/api/controller/Pay.php

@@ -4,6 +4,7 @@ namespace app\api\controller;
 
 use app\common\controller\Api;
 use app\common\service\OrderService;
+use app\common\service\UserService;
 use think\Db;
 use addons\epay\library\Service;
 use think\Exception;

+ 39 - 16
application/api/controller/User.php

@@ -13,6 +13,7 @@ use think\Validate;
 
 use think\Db;
 use miniprogram\wxBizDataCrypt;
+use GuzzleHttp\Client;
 
 /**
  * 会员接口
@@ -669,22 +670,44 @@ class User extends Api
     public function getMiniCode()
     {
         try {
-            $companyId = 3;
-            $tk = getAccessToken();
-            $urlss = 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$tk;
-            $ch = curl_init();
-            $dataArr = ["page"=>"pages/index/index","env_version"=>"develop", "scene"=>"shopId=".$companyId];
-            $datass = json_encode($dataArr);
-            curl_setopt($ch, CURLOPT_URL, $urlss);
-            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
-            curl_setopt($ch, CURLOPT_POSTFIELDS, $datass);
-            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
-            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
-            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-            curl_setopt($ch, CURLOPT_HEADER, false);
-            $output = curl_exec($ch);
-            curl_close($ch);
-            $this->success('获取成功',$output);
+            $companyId = $this->request->param('company_id',0);
+            if (empty($companyId)) {
+                throw new Exception('参数错误');
+            }
+            $companyWhere['id'] = $companyId;
+            $companyWhere['status'] = 1;
+            $company = Db::name('company')->where($companyWhere)->find();
+            if (empty($company)) {
+                throw new Exception('未找到门店信息');
+            }
+            $httpStr = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'];
+            if (empty($company['mini_code'])) {
+                $client = new Client();
+                $tk = getAccessToken();
+                $res2 = $client->request('POST', 'https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token='.$tk, [
+                    'json' => [
+                        //'page' => 'pages/home/index',
+                        'env_version'=>'trial',
+                        'scene' => 'shopId='.$companyId,
+                    ]
+                ]);
+                $fileName = md5($companyId);
+                $fileUrl = '/uploads/company/'.$fileName.'.png';
+                $code = $res2->getBody()->getContents();
+                file_put_contents(ROOT_PATH.'/public'.$fileUrl,$code);
+                $companyData['mini_code'] = $fileUrl;
+                $companyRes = Db::name('company')->where($companyWhere)->update($companyData);
+                if (!$companyRes) {
+                    throw new Exception('更新门店信息失败');
+                }
+                $miniCode = $httpStr.$fileUrl;
+            } else {
+                $miniCode = $httpStr.$company['mini_code'];
+            }
+            $result = [
+                'mini_code' => $miniCode,
+            ];
+            $this->success('获取成功',$result);
         } catch (Exception $e) {
             $this->error($e->getMessage());
         }

+ 26 - 2
application/api/controller/UserCar.php

@@ -3,6 +3,7 @@
 namespace app\api\controller;
 
 use app\common\controller\Api;
+use app\common\service\OrderService;
 use think\Db;
 use think\Exception;
 
@@ -26,10 +27,23 @@ class UserCar extends Api
     {
         try {
             $userId = $this->auth->id;
+            $companyId = $this->auth->company_id;
             $field = 'id,car_number,car_model';
             $where['user_id'] = $userId;
             $result = $this->model->field($field)->where($where)->order('createtime desc')->autopage()->select();
-
+            if (!empty($result)) {
+                $ids = array_column($result,'id');
+                $params = [
+                    'user_id' => $userId,
+                    'company_id' => $companyId,
+                    'car_id' => $ids,
+                ];
+                $orderService = new OrderService();
+                $orderRes = $orderService->getCarNextDate($params);
+                foreach ($result as $key => &$value) {
+                    $value['next_date'] = isset($orderRes['data'][$value['id']]) ? $orderRes['data'][$value['id']]['next_date'] : '';
+                }
+            }
             $this->success('获取成功',$result);
         } catch (Exception $e) {
             $this->error($e->getMessage());
@@ -45,11 +59,21 @@ class UserCar extends Api
         try {
             $id = $this->request->param('id',0);
             $userId = $this->auth->id;
+            $companyId = $this->auth->company_id;
             $field = 'id,car_number,car_model';
             $where['user_id'] = $userId;
             $where['id'] = $id;
             $result = $this->model->field($field)->where($where)->find();
-
+            if (!empty($result)) {
+                $params = [
+                    'user_id' => $userId,
+                    'company_id' => $companyId,
+                    'car_id' => [$id],
+                ];
+                $orderService = new OrderService();
+                $orderRes = $orderService->getCarNextDate($params);
+                $result['next_date'] = isset($orderRes['data'][$id]) ? $orderRes['data'][$id]['next_date'] : '';
+            }
             $this->success('获取成功',$result);
         } catch (Exception $e) {
             $this->error($e->getMessage());

+ 37 - 2
application/common/service/OrderService.php

@@ -198,12 +198,12 @@ class OrderService
                 'servicetype_id'  => isset($package['servicetype_id']) ? $package['servicetype_id'] : 0,//服务类型
                 'server_info'     => isset($package['info']) ? $package['info'] : '',//套餐内容
                 'server_images'   => isset($package['images']) ? $package['images'] : '',//套餐图片
-                'pay_fee'         => isset($package['price']) ? $package['price'] : 0,//套餐价格
+                'pay_fee'         => isset($package['price']) ? $package['price'] : 0.00,//套餐价格
                 'package_endtime' => $packageEndtime,//套餐到期时间
                 'status'          => 2,//状态:1=待支付,2=待处理,3=已完成,4=已取消
                 'pay_time'        => $time,
                 'paytype'         => $orderPayType,//支付方式:1=线下,2=余额,3=微信
-                'pay_fee'         => isset($package['price']) ? $package['price'] : 0.00,//支付总额
+                'total_fee'       => isset($package['price']) ? $package['price'] : 0.00,//支付总额
                 'createtime'      => $time,//下单时间
                 'pay_order_id'    => $payOrderId,//支付ID
             ];
@@ -221,4 +221,39 @@ class OrderService
         }
         return $result;
     }
+    
+    /**
+     * 获取车辆预计下次保养时间
+     * @return void
+     */
+    public function getCarNextDate($params=[])
+    {
+        $result = [
+            'status' => 1,
+            'msg' => '获取成功',
+            'data' => [],
+        ];
+        try {
+            $userId = isset($params['user_id']) ? $params['user_id'] : 0;
+            $companyId = isset($params['company_id']) ? $params['company_id'] : 0;
+            $carId = isset($params['car_id']) ? $params['car_id'] : [];
+            $orderWhere['user_id'] = $userId;
+            $orderWhere['company_id'] = $companyId;
+            $orderWhere['user_car_id'] = ['in',$carId];
+            $orderWhere['servicetype_id'] = 2;
+            $orderWhere['status'] = 3;//状态:2=待处理,3=已完成,4=已取消
+            $order = Db::name('order')->field('id,user_car_id,next_date')->where($orderWhere)->group('user_car_id')->order('finish_time desc')->select();
+            $row = [];
+            if (!empty($order)) {
+                foreach ($order as $key => $value) {
+                    $row[$value['user_car_id']]['next_date'] = $value['next_date'];
+                }
+            }
+            $result['data'] = $row;
+        } catch (Exception $e) {
+            $result['status'] = 0;
+            $result['msg'] = $e->getMessage();
+        }
+        return $result;
+    }
 }

+ 94 - 0
application/common/service/UserService.php

@@ -0,0 +1,94 @@
+<?php
+
+namespace app\common\service;
+
+use think\Db;
+
+class UserService
+{
+    private $model =  null;
+
+    /**
+     * 初始化方法
+     */
+    public function __construct()
+    {
+        $this->model = Db::name('user');
+    }
+
+    /**
+     * 用户换绑
+     * @return void
+     */
+    public function userBindCompany($params=[])
+    {
+        $result = [
+            'status' => 1,
+            'msg' => '操作成功',
+            'data' => [],
+        ];
+        try {
+            $userId = isset($params['user_id']) ? $params['user_id'] : 0;
+            $companyId = isset($params['company_id']) ? $params['company_id'] : 0;
+            $where['id'] = $userId;
+            $user = Db::name('user')->where($where['id'])->find();
+            if (!empty($user)) {
+                if ($user['company_id'] != $companyId) {
+                    $userData['company_id'] = $companyId;
+                    $userData['updatetime'] = time();
+                    $userRes = Db::name('user')->where($where['id'])->update($userData);
+                    if (!$userRes) {
+                        throw new Exception('操作失败');
+                    }
+                }
+            }
+        } catch (Exception $e) {
+            $result['status'] = 0;
+            $result['msg'] = $e->getMessage();
+        }
+        return $result;
+    }
+
+    /**
+     * 用户绑定门店和钱包
+     * @return void
+     */
+    public function userWallet($params=[])
+    {
+        $result = [
+            'status' => 1,
+            'msg' => '操作成功',
+            'data' => [],
+        ];
+        try {
+            $userId = isset($params['user_id']) ? $params['user_id'] : 0;
+            $companyId = isset($params['company_id']) ? $params['company_id'] : 0;
+            $comefrom = isset($params['comefrom']) ? $params['comefrom'] : '';
+            $bindRes = $this->userBindCompany($params);//绑定门店
+            if (!$bindRes['status']) {
+                throw new Exception($bindRes['msg']);
+            }
+            $where['user_id'] = $userId;
+            $where['company_id'] = $companyId;
+            $userWallet = Db::name('user_wallet')->where($where['id'])->find();
+            $time = time();
+            if (empty($userWallet)) {
+                $userWalletData = [
+                    'user_id' => $userId,
+                    'company_id' => $companyId,
+                    'money' => 0.00,
+                    'createtime' => $time,
+                    'comefrom' => $comefrom,
+                ];
+                $userWalletRes = Db::name('user_wallet')->insertGetId($userWalletData);
+                if (!$userWalletRes) {
+                    throw new Exception('生成钱包失败');
+                }
+            }
+        } catch (Exception $e) {
+            $result['status'] = 0;
+            $result['msg'] = $e->getMessage();
+        }
+        return $result;
+    }
+}