Wechat.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的saas管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace core\template;
  12. use app\service\core\wechat\CoreWechatService;
  13. use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
  14. use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
  15. use EasyWeChat\Kernel\Support\Collection;
  16. use GuzzleHttp\Exception\GuzzleException;
  17. use Psr\Http\Message\ResponseInterface;
  18. class Wechat extends BaseTemplate
  19. {
  20. /**
  21. * @param array $config
  22. * @return void
  23. */
  24. protected function initialize(array $config = [])
  25. {
  26. parent::initialize($config);
  27. }
  28. /**
  29. * 消息发送
  30. * @param array $data
  31. * @return array|Collection|object|ResponseInterface|string
  32. * @throws GuzzleException
  33. * @throws InvalidArgumentException
  34. * @throws InvalidConfigException
  35. */
  36. public function send(array $data)
  37. {
  38. $openid = $data[ 'openid' ];
  39. $template_id = $data[ 'template_id' ];
  40. $template_data = $data[ 'data' ];
  41. $first = $data[ 'first' ];
  42. $remark = $data[ 'remark' ];
  43. $url = $data[ 'url' ];
  44. $miniprogram = $data[ 'miniprogram' ] ?? [];
  45. $temp_data = [];
  46. foreach($template_data as $k => $v){
  47. $temp_data[$k] = ['value' => $v];
  48. }
  49. if (!empty($first)) $data[ 'first' ] = $first;
  50. if (!empty($remark)) $data[ 'remark' ] = $remark;
  51. $api = CoreWechatService::appApiClient();
  52. $param = [
  53. 'touser' => $openid,
  54. 'template_id' => $template_id,
  55. 'url' => $url,
  56. 'miniprogram' => $miniprogram,
  57. 'data' => $temp_data,
  58. ];
  59. if(!empty($client_msg_id)){
  60. $param['client_msg_id'] = $client_msg_id;
  61. }
  62. return $api->postJson('cgi-bin/message/template/send', $param);
  63. }
  64. /**
  65. * 添加模板消息
  66. * @param array $data
  67. * @return array|Collection|object|ResponseInterface|string
  68. * @throws GuzzleException
  69. * @throws InvalidConfigException
  70. */
  71. public function addTemplate(array $data)
  72. {
  73. $api = CoreWechatService::appApiClient();
  74. return $api->postJson('cgi-bin/template/api_add_template', [
  75. 'template_id_short' => $data[ 'shortId' ],
  76. 'keyword_name_list' => $data[ 'keyword_name_list' ]
  77. ]);
  78. }
  79. /**
  80. * 删除
  81. * @param array $data
  82. * @return array|Collection|object|ResponseInterface|string
  83. * @throws GuzzleException
  84. * @throws InvalidConfigException
  85. */
  86. public function delete(array $data)
  87. {
  88. $api = CoreWechatService::appApiClient();
  89. return $api->postJson('cgi-bin/template/del_private_template', [
  90. 'template_id' => $data[ 'templateId' ],
  91. ]);
  92. }
  93. /**
  94. * 获取
  95. * @return void
  96. */
  97. public function get()
  98. {
  99. }
  100. }