123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- <?php
- namespace app\common\service;
- use app\common\library\Alisms;
- use app\common\model\CompanyStaff;
- use think\Db;
- use think\Exception;
- use think\Log;
- 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;
- $res = Db::name('user_car_remind')->where($remindWhere)->update($data);
- if (!$res) {
- throw new Exception('更新保养提醒失败');
- }
- }
- } else {
- $data['createtime'] = $time;
- $res = Db::name('user_car_remind')->insertGetId($data);
- if (!$res) {
- throw new Exception('记录保养提醒失败');
- }
- }
- }
- } 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;
- $preOrderId = isset($params['id']) ? $params['id'] : 0;
- $templatePreorder = config('ali_sms_template');
- $template = isset($templatePreorder['order_to_c']) ? $templatePreorder['order_to_c'] : '';
- if (!empty($template)) {
- //商家店长
- $companyStaffParams = ['type'=>1,'company_id'=>$companyId];
- $companyStaff = CompanyStaff::getStaffList($companyStaffParams);
- $mobile = isset($companyStaff[0]) ? $companyStaff[0]['mobile'] : '';
- //预约信息
- $preOrderWhere['id'] = $preOrderId;
- $preOrder = model('PreOrder')->where($preOrderWhere)->find();
- $name = isset($preOrder['name']) ? $preOrder['name'] : '';
- $orderNo = isset($preOrder['pre_order_no']) ? $preOrder['pre_order_no'] : '';
- $preTime = !empty($preOrder['pre_time']) ? date('Y-m-d H:i:s',$preOrder['pre_time']) : '';
- $params = [
- 'template' => $template,//短息模版
- 'mobile' => $mobile, //手机号
- 'data_params' => [
- 'name' => $name, //联系人
- 'order_no' => $orderNo, //单号
- 'time' => date('Y-m-d H:i:s',$preTime),//预约时间
- ],//短信参数
- ];
- $smsRes = $this->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 orderToShop($params=[])
- {
- $result = [
- 'status' => 1,
- 'msg' => '操作成功',
- 'data' => [],
- ];
- try {
- $companyId = isset($params['company_id']) ? $params['company_id'] : 0;
- $orderId = isset($params['id']) ? $params['id'] : 0;
- $templatePreorder = config('ali_sms_template');
- $template = isset($templatePreorder['order_to_c']) ? $templatePreorder['order_to_c'] : '';
- if (!empty($template)) {
- //商家店长
- $companyStaffParams = ['type'=>1,'company_id'=>$companyId];
- $companyStaff = CompanyStaff::getStaffList($companyStaffParams);
- $mobile = isset($companyStaff[0]) ? $companyStaff[0]['mobile'] : '';
- //预约信息
- $preOrderWhere['id'] = $orderId;
- $preOrder = model('Order')->where($preOrderWhere)->find();
- $name = isset($preOrder['user_name']) ? $preOrder['user_name'] : '';
- $orderNo = isset($preOrder['orderno']) ? $preOrder['orderno'] : '';
- $orderTime = !empty($preOrder['createtime']) ? date('Y-m-d H:i:s',$preOrder['createtime']) : '';
- $params = [
- 'template' => $template,//短息模版
- 'mobile' => $mobile, //手机号
- 'data_params' => [
- 'name' => $name, //联系人
- 'order_no' => $orderNo,//单号
- 'time' => date('Y-m-d H:i:s',$orderTime),//时间
- ],//短信参数
- ];
- $smsRes = $this->send($params);
- if (!$smsRes['status']) {
- Log::error('预约通知商家短信发送失败:params:'.json_encode($params));
- }
- }
- } catch (Exception $e) {
- $result['status'] = 0;
- $result['msg'] = $e->getMessage();
- }
- return $result;
- }
- }
|