Browse Source

短信提醒/用户信息

zhangxiaobin 1 year ago
parent
commit
4c0da9a279

+ 18 - 5
application/api/controller/PreOrder.php

@@ -3,6 +3,7 @@
 namespace app\api\controller;
 
 use app\common\controller\Api;
+use app\common\service\PreOrderService;
 use app\common\service\UserService;
 use think\Db;
 use think\Exception;
@@ -70,6 +71,8 @@ class PreOrder extends Api
             $id = $this->request->param('id',0);
             $carId = $this->request->param('car_id',0);
             $preTime = $this->request->param('pre_time','');
+            $name = $this->request->param('name', '');
+            $mobile = $this->request->param('mobile', '');
             $userId = $this->auth->id;
             $companyId = $this->auth->company_id;
             $scene = !empty($id) ? 'edit' : 'add';
@@ -86,13 +89,14 @@ class PreOrder extends Api
                 throw new Exception('未找到车辆信息');
             }
             $time = time();
+            $carNo = isset($userCar['car_number']) ? $userCar['car_number'] : '';
             $data = [
-                'name'           => $this->request->param('name', ''),
-                'mobile'         => $this->request->param('mobile', ''),
-                'address'         => $this->request->param('address', ''),
+                'name'           => $name,
+                'mobile'         => $mobile,
+                'address'        => $this->request->param('address', ''),
                 'remark'         => $this->request->param('remark', ''),
                 'car_id'         => $carId,
-                'car_number'     => isset($userCar['car_number']) ? $userCar['car_number'] : '',
+                'car_number'     => $carNo,
                 'servicetype_id' => $this->request->param('servicetype_id', 0),
                 'pre_time'       => $preTime,
             ];
@@ -108,12 +112,21 @@ class PreOrder extends Api
                 $userParams = [
                     'user_id' => $userId,
                     'company_id' => $companyId,
-                    'comefrom' => '',//来源
+                    'comefrom' => '平台引流',//来源
                 ];
                 $userBindRes = $userService->userWallet($userParams);
                 if (!$userBindRes['status']) {
                     throw new Exception($userBindRes['msg']);
                 }
+                //用户预约发送短信通知
+                $service = new PreOrderService();
+                $params = [
+                    'company_id' => $companyId,//门店ID
+                    'name'       => $name,//联系人
+                    'mobile'     => $mobile,//手机号
+                    'pre_time'   => $preTime,//预约时间
+                ];
+                $service->preOrderToUser($params);
             } else {
                 $data['updatetime'] = $time;
                 $where['id'] = $id;

+ 66 - 0
application/api/controller/Sms.php

@@ -5,7 +5,11 @@ namespace app\api\controller;
 use app\common\controller\Api;
 use app\common\library\Sms as Smslib;
 use app\common\model\User;
+use app\common\service\SmsService;
+use think\Db;
+use think\Exception;
 use think\Hook;
+use think\Log;
 
 /**
  * 手机短信接口
@@ -101,4 +105,66 @@ class Sms extends Api
             $this->error(__('验证码不正确'));
         }
     }
+
+    /**
+     * 保养提醒短信发送
+     * @return void
+     */
+    public function remindSend()
+    {
+        try {
+            $startTime = strtotime('00:00:00'); //今天开始时间戳
+            $endTime = strtotime('+ 1day',$startTime);
+            $where['remind_time'] = ['between',[$startTime,$endTime]];
+            $where['status'] = 1;
+            $where['is_remind'] = 0;
+            $remindCount = Db::name('user_car_remind')->where($where)->count();
+            if (!empty($remindCount)) {
+                $remindIds = [];
+                $limit = 10;
+                $total = intval(ceil($remindCount / $limit));
+                $service = new SmsService();
+                $templateArr = config('ali_sms_template');
+                $template = isset($templateArr['service_expire']) ? $templateArr['service_expire'] : '';
+                for ($i=1; $i <= $total;$i++) {
+                    $page = $i;
+                    $offset = ($page - 1) * $limit;
+                    $remindData = model('UserCarRemind')->with(['order'=>function($oQuery){
+                        $oQuery->field('id,user_mobile,user_name');
+                    }])->where($where)->limit($offset,$limit)->select();
+                    $remindData = collection($remindData)->toArray();
+                    if (!empty($remindData)) {
+                        //尊敬的${name}先生/女士,您的爱车${chepai}要到保养周期了,预计保养时间为:${time},不要错过爱车的黄金保养时间哦!
+                        foreach ($remindData as $key => $value) {
+                            $order  = isset($value['order']) ? $value['order'] : [];
+                            $mobile = isset($order['user_mobile']) ? $order['user_mobile'] : '';//手机号
+                            $name   = isset($order['user_name']) ? $order['user_name'] : '';    //联系人
+                            $params = [
+                                'template'    => $template,//短息模版
+                                'mobile'      => $mobile,  //手机号
+                                'data_params' => [
+                                    'name'   => $name,     //联系人
+                                    'chepai' => $value['car_number'], //车牌号
+                                    'time'   => date('Y-m-d',$value['upkeep_time']), //保养时间
+                                ],//短信参数
+                            ];
+                            $smsRes = $service->send($params);
+                            if (!$smsRes['status']) {
+                                Log::error('短信发送失败:params:'.json_encode($params));
+                            } else {
+                                $remindIds[] = $value['id'];
+                            }
+                        }
+                    }
+                }
+                if (!empty($remindIds)) {
+                    $updateWhere['id'] = ['in',$remindIds];
+                    $remindRes = model('UserCarRemind')->where($updateWhere)->update(['is_remind'=>1]);
+                }
+            }
+            $this->success();
+        } catch (Exception $e) {
+            $this->error($e->getMessage());
+        }
+    }
 }

+ 17 - 0
application/api/controller/company/Order.php

@@ -3,11 +3,13 @@
 namespace app\api\controller\company;
 
 use app\common\controller\Apic;
+use app\common\service\SmsService;
 use app\common\service\UserService;
 use think\Db;
 
 use alipaysdkphpallmaster\aop\AopClient;
 use alipaysdkphpallmaster\aop\request\AlipayTradePayRequest;
+use think\Log;
 
 /**
  * 订单管理
@@ -192,6 +194,21 @@ class Order extends Apic
         ];
 
         Db::name('order')->where('id',$id)->update($data);
+        //记录保养时间(后续提醒客户保养)
+        $smsService = new SmsService();
+        $nextDate = !empty($next_date) ? strtotime($next_date) : 0;
+        $params = [
+            'order_id'    => $info['id'],
+            'company_id'  => $info['company_id'],
+            'user_id'     => $info['user_id'],
+            'car_id'      => $info['user_car_id'],
+            'car_number'  => $info['user_car_number'],
+            'upkeep_time' => $nextDate,
+        ];
+        $orderRes = $smsService->carRemindSave($params);
+        if (!$orderRes['status']) {
+            Log::error('记录保养短信提醒失败:params:'.json_encode($params));
+        }
         $this->success('操作成功');
 
     }

+ 1 - 1
application/api/library/ExceptionHandle.php

@@ -13,7 +13,7 @@ class ExceptionHandle extends Handle
 
     public function render(Exception $e)
     {
-        //return parent::render($e);
+        return parent::render($e);
         $statuscode = $code = 500;
         $msg = $e->getMessage();
         // 验证异常

+ 5 - 1
application/common/library/Auth.php

@@ -417,7 +417,11 @@ class Auth
 
         //追加
         $userinfo['avatar'] = one_domain_image($userinfo['avatar']);
-        $userinfo['money'] = model('wallet')->getWallet($this->id,$this->company_id,'money');
+        $companyId = $this->company_id;
+        if (empty($this->company)) {
+            $companyId = $this->temp_company_id;
+        }
+        $userinfo['money'] = model('wallet')->getWallet($this->id,$companyId,'money');
 
         return $userinfo;
     }

+ 4 - 0
application/common/model/CompanyStaff.php

@@ -13,4 +13,8 @@ class CompanyStaff extends Model
     // 表名
     protected $name = 'company_staff';
 
+    public function company()
+    {
+        return $this->hasOne('Company', 'id', 'company_id',[],'LEFT');
+    }
 }

+ 28 - 0
application/common/model/UserCarRemind.php

@@ -0,0 +1,28 @@
+<?php
+
+namespace app\common\model;
+
+use think\Model;
+
+class UserCarRemind extends Model
+{
+    // 表名
+    protected $name = 'user_car_remind';
+
+    // 自动写入时间戳字段
+    protected $autoWriteTimestamp = 'integer';
+
+    // 定义时间戳字段名
+    protected $createTime = 'createtime';
+    protected $updateTime = false;
+    protected $deleteTime = false;
+
+    // 追加属性
+    protected $append = [
+    ];
+
+    public function order()
+    {
+        return $this->hasOne('Order', 'id', 'order_id',[],'LEFT');
+    }
+}

+ 119 - 0
application/common/service/PreOrderService.php

@@ -0,0 +1,119 @@
+<?php
+
+namespace app\common\service;
+
+use think\Db;
+use think\Exception;
+use think\Log;
+
+class PreOrderService
+{
+    private $model =  null;
+
+    /**
+     * 初始化方法
+     */
+    public function __construct()
+    {
+        $this->model = Db::name('pre_order');
+    }
+
+    /**
+     * 预约发送短息
+     * @return void
+     */
+    public function preOrderToUser($params=[])
+    {
+        $result = [
+            'status' => 1,
+            'msg' => '操作成功',
+            'data' => [],
+        ];
+        try {
+            $companyId  = isset($params['company_id']) ? $params['company_id'] : 0;
+            $name    = isset($params['name']) ? $params['name'] : '';
+            $mobile  = isset($params['mobile']) ? $params['mobile'] : '';
+            $preTime = isset($params['pre_time']) ? $params['pre_time'] : 0;
+            $templatePreorder = config('ali_sms_template');
+            $template = isset($templatePreorder['pro_order_u']) ? $templatePreorder['pro_order_u'] : '';
+            if (!empty($template)) {
+                if (!empty($mobile)) {
+                    //尊敬的${name}先生/女士,您在中蓝养车保养预约成功,预约门店:${mendian},预约时间:${time}
+                    $service = new SmsService();
+                    $where['id'] = $companyId;
+                    $company = model('Company')->where($where)->find();
+                    $companyName = isset($company['name']) ? $company['name'] : '';
+                    $params = [
+                        'template'    => $template,//短息模版
+                        'mobile'      => $mobile,  //手机号
+                        'data_params' => [
+                            'name'    => $name,     //联系人
+                            'mendian' => $companyName,//门店
+                            'time'    => date('Y-m-d H:i:s',$preTime),//预约时间
+                        ],//短信参数
+                    ];
+                    $smsRes = $service->send($params);
+                    if (!$smsRes['status']) {
+                        Log::error('短信发送失败:params:'.json_encode($params));
+                    }
+                }
+            }
+        } catch (Exception $e) {
+            $result['status'] = 0;
+            $result['msg'] = $e->getMessage();
+        }
+        return $result;
+    }
+
+    /**
+     * 预约发送短息
+     * @return void
+     */
+    public function preOrderToShop($params=[])
+    {
+        $result = [
+            'status' => 1,
+            'msg' => '操作成功',
+            'data' => [],
+        ];
+        try {
+            $companyId = isset($params['company_id']) ? $params['company_id'] : 0;
+            $name      = isset($params['name']) ? $params['name'] : '';
+            $preTime   = isset($params['pre_time']) ? $params['pre_time'] : 0;
+            $templatePreorder = config('ali_sms_template');
+            $template = isset($templatePreorder['pro_order_c']) ? $templatePreorder['pro_order_c'] : '';
+            if (!empty($template)) {
+                $staffWhere = [
+                  'status'     => 1,
+                  'type'       => 1,//类型:1=商家,2=员工
+                  'company_id' => $companyId,
+                ];
+                $companyStaff = model('CompanyStaff')->with(['company'])->where($staffWhere)->find();
+                if (!empty($companyStaff)) {
+                    //尊敬的${name}先生/女士,您在中蓝养车保养预约成功,预约门店:${mendian},预约时间:${time}
+                    $service = new SmsService();
+                    $mobile = $companyStaff['mobile'];  //商家手机号
+                    $company = isset($companyStaff['company']) ? $companyStaff['company'] : [];
+                    $companyName = isset($company['name']) ? $company['name'] : '';
+                    $params = [
+                        'template'    => $template,//短息模版
+                        'mobile'      => $mobile,  //手机号
+                        'data_params' => [
+                            'name'    => $name,     //联系人
+                            'mendian' => $companyName,//门店
+                            'time'    => $preTime,    //预约时间
+                        ],//短信参数
+                    ];
+                    $smsRes = $service->send($params);
+                    if (!$smsRes['status']) {
+                        Log::error('短信发送失败:params:'.json_encode($params));
+                    }
+                }
+            }
+        } catch (Exception $e) {
+            $result['status'] = 0;
+            $result['msg'] = $e->getMessage();
+        }
+        return $result;
+    }
+}

+ 111 - 0
application/common/service/SmsService.php

@@ -0,0 +1,111 @@
+<?php
+
+namespace app\common\service;
+
+use app\common\library\Alisms;
+use think\Db;
+use think\Exception;
+
+class SmsService
+{
+    private $model =  null;
+
+    /**
+     * 初始化方法
+     */
+    public function __construct()
+    {
+        $this->model = Db::name('sms');
+    }
+
+    /**
+     * 发送短息
+     * @return void
+     */
+    public function send($params=[])
+    {
+        $result = [
+            'status' => 1,
+            'msg' => '操作成功',
+            'data' => [],
+        ];
+        try {
+            $dataParams = isset($params['data_params']) ? $params['data_params'] : [];
+            //配置
+            $config = config('alisms');
+            if (isset($params['template'])) {
+                $config['template'] = $params['template'];
+            }
+            $alisms = new Alisms();
+            $result = $alisms->mobile($params['mobile'])
+                ->template($config['template'])
+                ->param($dataParams)
+                ->send();
+            if (!$result) {
+                throw new Exception('发送消息失败');
+            }
+        } catch (Exception $e) {
+            $result['status'] = 0;
+            $result['msg'] = $e->getMessage();
+        }
+        return $result;
+    }
+
+    /**
+     * 保养提醒保存
+     * @return void
+     */
+    public function carRemindSave($params=[])
+    {
+        $result = [
+            'status' => 1,
+            'msg' => '操作成功',
+            'data' => [],
+        ];
+        try {
+            $orderId    = isset($params['order_id']) ? $params['order_id'] : 0;
+            $companyId  = isset($params['company_id']) ? $params['company_id'] : 0;
+            $userId     = isset($params['user_id']) ? $params['user_id'] : 0;
+            $carId      = isset($params['car_id']) ? $params['car_id'] : 0;
+            $carNo      = isset($params['car_number']) ? $params['car_number'] : '';
+            $upkeepTime = isset($params['upkeep_time']) ? $params['upkeep_time'] : 0;
+            $remindTime = !empty($upkeepTime) ? $upkeepTime - 86400 : 0;
+            $time = time();
+            if ($remindTime > 0 && $upkeepTime > $time) {
+                $data = [
+                    'order_id'    => $orderId,   //订单ID
+                    'company_id'  => $companyId, //门店ID
+                    'user_id'     => $userId,    //用户ID
+                    'car_id'      => $carId,     //车辆ID
+                    'car_number'  => $carNo,     //车牌号
+                    'upkeep_time' => $upkeepTime,//保养时间
+                    'remind_time' => $remindTime,//提醒时间
+                ];
+                $remindWhere['user_id'] = $userId;
+                $remindWhere['company_id'] = $companyId;
+                $remindWhere['car_id'] = $carId;
+                $userCarRemind = Db::name('user_car_remind')->where($remindWhere)->find();
+                if (!empty($userCarRemind)) {
+                    if ($upkeepTime != $userCarRemind['upkeep_time']) {
+                        $data['is_remind']  = 0;
+                        $data['updatetime'] = $time;
+                        $result = Db::name('user_car_remind')->where($remindWhere)->update($data);
+                        if (!$result) {
+                            throw new Exception('更新保养提醒失败');
+                        }
+                    }
+                } else {
+                    $data['createtime'] = $time;
+                    $result = Db::name('user_car_remind')->insertGetId($data);
+                    if (!$result) {
+                        throw new Exception('记录保养提醒失败');
+                    }
+                }
+            }
+        } catch (Exception $e) {
+            $result['status'] = 0;
+            $result['msg'] = $e->getMessage();
+        }
+        return $result;
+    }
+}

+ 5 - 0
application/config.php

@@ -330,4 +330,9 @@ return [
         'key' => 'LTAI5tGhFauMXWNWoLJsH6qK',
         'secret' => 'FwLSFS7QtyBpQglFjtEEQXL0VftRs0',
     ],
+    //阿里云短信模版
+    'ali_sms_template' => [
+        'pro_order_u'    => 'SMS_461410697', //用户预约通知
+        'service_expire' => 'SMS_461325805', //保养到期提醒用户
+    ],
 ];