SmsService.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. namespace app\common\service;
  3. use app\common\library\Alisms;
  4. use app\common\model\CompanyStaff;
  5. use think\Db;
  6. use think\Exception;
  7. use think\Log;
  8. class SmsService
  9. {
  10. private $model = null;
  11. /**
  12. * 初始化方法
  13. */
  14. public function __construct()
  15. {
  16. $this->model = Db::name('sms');
  17. }
  18. /**
  19. * 发送短息
  20. * @return void
  21. */
  22. public function send($params=[])
  23. {
  24. $result = [
  25. 'status' => 1,
  26. 'msg' => '操作成功',
  27. 'data' => [],
  28. ];
  29. try {
  30. $dataParams = isset($params['data_params']) ? $params['data_params'] : [];
  31. //配置
  32. $config = config('alisms');
  33. if (isset($params['template'])) {
  34. $config['template'] = $params['template'];
  35. }
  36. $alisms = new Alisms();
  37. $result = $alisms->mobile($params['mobile'])
  38. ->template($config['template'])
  39. ->param($dataParams)
  40. ->send();
  41. if (!$result) {
  42. throw new Exception('发送消息失败');
  43. }
  44. } catch (Exception $e) {
  45. $result['status'] = 0;
  46. $result['msg'] = $e->getMessage();
  47. }
  48. return $result;
  49. }
  50. /**
  51. * 保养提醒保存
  52. * @return void
  53. */
  54. public function carRemindSave($params=[])
  55. {
  56. $result = [
  57. 'status' => 1,
  58. 'msg' => '操作成功',
  59. 'data' => [],
  60. ];
  61. try {
  62. $orderId = isset($params['order_id']) ? $params['order_id'] : 0;
  63. $companyId = isset($params['company_id']) ? $params['company_id'] : 0;
  64. $userId = isset($params['user_id']) ? $params['user_id'] : 0;
  65. $carId = isset($params['car_id']) ? $params['car_id'] : 0;
  66. $carNo = isset($params['car_number']) ? $params['car_number'] : '';
  67. $upkeepTime = isset($params['upkeep_time']) ? $params['upkeep_time'] : 0;
  68. $remindTime = !empty($upkeepTime) ? $upkeepTime - 86400 : 0;
  69. $time = time();
  70. if ($remindTime > 0 && $upkeepTime > $time) {
  71. $data = [
  72. 'order_id' => $orderId, //订单ID
  73. 'company_id' => $companyId, //门店ID
  74. 'user_id' => $userId, //用户ID
  75. 'car_id' => $carId, //车辆ID
  76. 'car_number' => $carNo, //车牌号
  77. 'upkeep_time' => $upkeepTime,//保养时间
  78. 'remind_time' => $remindTime,//提醒时间
  79. ];
  80. $remindWhere['user_id'] = $userId;
  81. $remindWhere['company_id'] = $companyId;
  82. $remindWhere['car_id'] = $carId;
  83. $userCarRemind = Db::name('user_car_remind')->where($remindWhere)->find();
  84. if (!empty($userCarRemind)) {
  85. if ($upkeepTime != $userCarRemind['upkeep_time']) {
  86. $data['is_remind'] = 0;
  87. $data['updatetime'] = $time;
  88. $res = Db::name('user_car_remind')->where($remindWhere)->update($data);
  89. if (!$res) {
  90. throw new Exception('更新保养提醒失败');
  91. }
  92. }
  93. } else {
  94. $data['createtime'] = $time;
  95. $res = Db::name('user_car_remind')->insertGetId($data);
  96. if (!$res) {
  97. throw new Exception('记录保养提醒失败');
  98. }
  99. }
  100. }
  101. } catch (Exception $e) {
  102. $result['status'] = 0;
  103. $result['msg'] = $e->getMessage();
  104. }
  105. return $result;
  106. }
  107. /**
  108. * 预约通知商家
  109. * @return void
  110. */
  111. public function preOrderToShop($params=[])
  112. {
  113. $result = [
  114. 'status' => 1,
  115. 'msg' => '操作成功',
  116. 'data' => [],
  117. ];
  118. try {
  119. $companyId = isset($params['company_id']) ? $params['company_id'] : 0;
  120. $preOrderId = isset($params['id']) ? $params['id'] : 0;
  121. $templatePreorder = config('ali_sms_template');
  122. $template = isset($templatePreorder['order_to_c']) ? $templatePreorder['order_to_c'] : '';
  123. if (!empty($template)) {
  124. //商家店长
  125. $companyStaffParams = ['type'=>1,'company_id'=>$companyId];
  126. $companyStaff = CompanyStaff::getStaffList($companyStaffParams);
  127. $mobile = isset($companyStaff[0]) ? $companyStaff[0]['mobile'] : '';
  128. //预约信息
  129. $preOrderWhere['id'] = $preOrderId;
  130. $preOrder = model('PreOrder')->where($preOrderWhere)->find();
  131. $name = isset($preOrder['name']) ? $preOrder['name'] : '';
  132. $orderNo = isset($preOrder['pre_order_no']) ? $preOrder['pre_order_no'] : '';
  133. $preTime = !empty($preOrder['pre_time']) ? date('Y-m-d H:i:s',$preOrder['pre_time']) : '';
  134. $params = [
  135. 'template' => $template,//短息模版
  136. 'mobile' => $mobile, //手机号
  137. 'data_params' => [
  138. 'name' => $name, //联系人
  139. 'order_no' => $orderNo, //单号
  140. 'time' => date('Y-m-d H:i:s',$preTime),//预约时间
  141. ],//短信参数
  142. ];
  143. $smsRes = $this->send($params);
  144. if (!$smsRes['status']) {
  145. Log::error('预约通知商家短信发送失败:params:'.json_encode($params));
  146. }
  147. }
  148. } catch (Exception $e) {
  149. $result['status'] = 0;
  150. $result['msg'] = $e->getMessage();
  151. }
  152. return $result;
  153. }
  154. /**
  155. * 订单通知商家
  156. * @return void
  157. */
  158. public function orderToShop($params=[])
  159. {
  160. $result = [
  161. 'status' => 1,
  162. 'msg' => '操作成功',
  163. 'data' => [],
  164. ];
  165. try {
  166. $companyId = isset($params['company_id']) ? $params['company_id'] : 0;
  167. $orderId = isset($params['id']) ? $params['id'] : 0;
  168. $templatePreorder = config('ali_sms_template');
  169. $template = isset($templatePreorder['order_to_c']) ? $templatePreorder['order_to_c'] : '';
  170. if (!empty($template)) {
  171. //商家店长
  172. $companyStaffParams = ['type'=>1,'company_id'=>$companyId];
  173. $companyStaff = CompanyStaff::getStaffList($companyStaffParams);
  174. $mobile = isset($companyStaff[0]) ? $companyStaff[0]['mobile'] : '';
  175. //预约信息
  176. $preOrderWhere['id'] = $orderId;
  177. $preOrder = model('Order')->where($preOrderWhere)->find();
  178. $name = isset($preOrder['user_name']) ? $preOrder['user_name'] : '';
  179. $orderNo = isset($preOrder['orderno']) ? $preOrder['orderno'] : '';
  180. $orderTime = !empty($preOrder['createtime']) ? date('Y-m-d H:i:s',$preOrder['createtime']) : '';
  181. $params = [
  182. 'template' => $template,//短息模版
  183. 'mobile' => $mobile, //手机号
  184. 'data_params' => [
  185. 'name' => $name, //联系人
  186. 'order_no' => $orderNo,//单号
  187. 'time' => date('Y-m-d H:i:s',$orderTime),//时间
  188. ],//短信参数
  189. ];
  190. $smsRes = $this->send($params);
  191. if (!$smsRes['status']) {
  192. Log::error('预约通知商家短信发送失败:params:'.json_encode($params));
  193. }
  194. }
  195. } catch (Exception $e) {
  196. $result['status'] = 0;
  197. $result['msg'] = $e->getMessage();
  198. }
  199. return $result;
  200. }
  201. }