Basics.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace app\common\service\message;
  3. use app\common\model\Wxapp as WxappModel;
  4. use app\common\model\Setting as SettingModel;
  5. use app\common\library\sms\Driver as SmsDriver;
  6. use app\common\library\wechat\WxSubMsg;
  7. /**
  8. * 消息通知服务[基类]
  9. * Class Basics
  10. * @package app\common\service\message
  11. */
  12. abstract class Basics extends \app\common\service\Basics
  13. {
  14. // 参数列表
  15. protected $param = [];
  16. /**
  17. * 发送消息通知
  18. * @param array $param 参数
  19. * @return mixed
  20. */
  21. abstract public function send($param);
  22. /**
  23. * 发送短信提醒
  24. * @param $msgType
  25. * @param $templateParams
  26. * @param $wxappId
  27. * @return bool
  28. * @throws \think\Exception
  29. */
  30. protected function sendSms($msgType, $templateParams, $wxappId)
  31. {
  32. $smsConfig = SettingModel::getItem('sms', $wxappId);
  33. return (new SmsDriver($smsConfig))->sendSms($msgType, $templateParams);
  34. }
  35. /**
  36. * 发送微信订阅消息
  37. * @param $wxappId
  38. * @param $params
  39. * @return mixed
  40. * @throws \app\common\exception\BaseException
  41. * @throws \think\Exception
  42. * @throws \think\exception\DbException
  43. */
  44. protected function sendWxSubMsg($wxappId, $params)
  45. {
  46. // 获取小程序配置
  47. $wxConfig = WxappModel::getWxappCache($wxappId);
  48. // 请求微信api执行发送
  49. $WxSubMsg = new WxSubMsg($wxConfig['app_id'], $wxConfig['app_secret']);
  50. return $WxSubMsg->sendTemplateMessage($params);
  51. }
  52. /**
  53. * 字符串截取前20字符
  54. * [用于兼容thing数据类型]
  55. * @param $content
  56. * @param int $length
  57. * @return bool|string
  58. */
  59. protected function getSubstr($content, $length = 20)
  60. {
  61. return str_substr($content, $length);
  62. }
  63. }