Message.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\store\service\wxapp;
  10. use app\store\model\User as UserModel;
  11. use app\store\model\Wxapp as WxappModel;
  12. use app\common\library\wechat\WxTplMsg;
  13. use app\common\service\wxapp\FormId as FormIdService;
  14. /**
  15. * 推送模板消息服务类
  16. * Class Message
  17. * @package app\store\service\wxapp
  18. */
  19. class Message
  20. {
  21. // 分割符号
  22. const SEPARATOR = ',';
  23. /** @var array $stateSet 状态集 */
  24. private $stateSet = [];
  25. /** @var WxTplMsg $WxTplMsg 微信模板消息类 */
  26. private $WxTplMsg;
  27. /**
  28. * 构造方法
  29. * Message constructor.
  30. * @throws \app\common\exception\BaseException
  31. * @throws \think\exception\DbException
  32. */
  33. public function __construct()
  34. {
  35. // 实例化:微信模板消息类
  36. $config = WxappModel::getWxappCache();
  37. $this->WxTplMsg = new WxTplMsg($config['app_id'], $config['app_secret']);
  38. }
  39. /**
  40. * @param $data
  41. * @return bool
  42. * @throws \app\common\exception\BaseException
  43. * @throws \think\exception\DbException
  44. */
  45. public function send($data)
  46. {
  47. // 用户id集
  48. $userIdsArr = !strstr($data['user_id'], self::SEPARATOR) ? [$data['user_id']]
  49. : explode(self::SEPARATOR, $data['user_id']);
  50. // 批量发送
  51. foreach ($userIdsArr as $userId) {
  52. $this->sendTemplateMessage($userId, $data);
  53. }
  54. return true;
  55. }
  56. /**
  57. * 发送模板消息
  58. * @param $userId
  59. * @param $data
  60. * @return bool
  61. * @throws \app\common\exception\BaseException
  62. * @throws \think\exception\DbException
  63. */
  64. private function sendTemplateMessage($userId, $data)
  65. {
  66. // 获取formid
  67. if (!$formId = FormIdService::getAvailableFormId($userId)) {
  68. $this->recordState("用户[ID:$userId] 无可用formid,无法发送模板消息!");
  69. return false;
  70. }
  71. // 获取用户信息
  72. $user = UserModel::detail($data['user_id']);
  73. // 构建模板消息参数
  74. $params = [
  75. 'touser' => $user['open_id'],
  76. 'template_id' => $data['template_id'],
  77. 'page' => $data['page'],
  78. 'form_id' => $formId['form_id'],
  79. 'data' => []
  80. ];
  81. // 格式化模板内容
  82. foreach (array_filter($data['content']) as $key => $item) {
  83. $params['data']['keyword' . ($key + 1)] = $item;
  84. }
  85. // 请求微信api:发送模板消息
  86. if ($status = $this->WxTplMsg->sendTemplateMessage($params)) {
  87. $this->recordState("用户[ID:$userId] 发送成功!");
  88. }
  89. // 标记formid已使用
  90. FormIdService::setIsUsed($formId['id']);
  91. return $status;
  92. }
  93. /**
  94. * 获取状态集
  95. * @return array
  96. */
  97. public function getStateSet()
  98. {
  99. return $this->stateSet;
  100. }
  101. /**
  102. * 记录状态集
  103. * @param $content
  104. */
  105. private function recordState($content)
  106. {
  107. $this->stateSet[] = $content;
  108. }
  109. }