Message.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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
  25. * @throws \think\exception\DbException
  26. */
  27. public function __construct()
  28. {
  29. // 实例化:微信模板消息类
  30. $config = WxappModel::getWxappCache();
  31. $this->WxTplMsg = new WxTplMsg($config['app_id'], $config['app_secret']);
  32. }
  33. /**
  34. * 执行发送
  35. * @param $data
  36. * @return bool
  37. * @throws \app\common\exception\BaseException
  38. * @throws \think\exception\DbException
  39. */
  40. public function send($data)
  41. {
  42. // 用户id集
  43. $userIdsArr = !strstr($data['user_id'], self::SEPARATOR) ? [$data['user_id']]
  44. : explode(self::SEPARATOR, $data['user_id']);
  45. // 批量发送
  46. foreach ($userIdsArr as $userId) {
  47. $this->sendTemplateMessage($userId, $data);
  48. }
  49. return true;
  50. }
  51. /**
  52. * 发送模板消息
  53. * @param $userId
  54. * @param $data
  55. * @return bool
  56. * @throws \app\common\exception\BaseException
  57. * @throws \think\exception\DbException
  58. */
  59. private function sendTemplateMessage($userId, $data)
  60. {
  61. // 获取formid
  62. if (!$formId = FormIdService::getAvailableFormId($userId)) {
  63. $this->recordState("用户[ID:$userId] 无可用formid,无法发送模板消息!");
  64. return false;
  65. }
  66. // 获取用户信息
  67. $user = UserModel::detail($data['user_id']);
  68. // 构建模板消息参数
  69. $params = [
  70. 'touser' => $user['open_id'],
  71. 'template_id' => $data['template_id'],
  72. 'page' => $data['page'],
  73. 'form_id' => $formId['form_id'],
  74. 'data' => []
  75. ];
  76. // 格式化模板内容
  77. foreach (array_filter($data['content']) as $key => $item) {
  78. $params['data']['keyword' . ($key + 1)] = $item;
  79. }
  80. // 请求微信api:发送模板消息
  81. if ($status = $this->WxTplMsg->sendTemplateMessage($params)) {
  82. $this->recordState("用户[ID:$userId] 发送成功!");
  83. }
  84. // 标记formid已使用
  85. FormIdService::setIsUsed($formId['id']);
  86. return $status;
  87. }
  88. /**
  89. * 获取状态集
  90. * @return array
  91. */
  92. public function getStateSet()
  93. {
  94. return $this->stateSet;
  95. }
  96. /**
  97. * 记录状态集
  98. * @param $content
  99. */
  100. private function recordState($content)
  101. {
  102. $this->stateSet[] = $content;
  103. }
  104. }