Message.php 3.1 KB

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