Message.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\wechat\model;
  11. use app\model\BaseModel;
  12. use addon\weapp\model\Config as WeappConfig;
  13. use addon\mobileshop\model\Config as MobileShopConfig;
  14. /**
  15. * 微信消息模板
  16. */
  17. class Message extends BaseModel
  18. {
  19. /**
  20. * 发送模板消息
  21. * @param array $param
  22. */
  23. public function sendMessage(array $param)
  24. {
  25. try {
  26. $site_id = $param['site_id'];
  27. $support_type = $param["support_type"] ?? [];
  28. if (!empty($support_type) && !in_array("wechat", $support_type)) return $this->success();
  29. if (empty($param['openid'])) return $this->success('缺少必需参数openid');
  30. $message_info = $param['message_info'];
  31. if ($message_info['wechat_is_open'] == 0) return $this->error('未启用模板消息');
  32. $wechat_info = json_decode($message_info['wechat_json'], true);
  33. if (!isset($message_info['wechat_template_id']) || empty($message_info['wechat_template_id'])) return $this->error('未配置模板消息');
  34. $template_data = [
  35. 'first' => [
  36. 'value' => $wechat_info['headtext'],
  37. 'color' => !empty($wechat_info['headtextcolor']) ? $wechat_info['headtextcolor'] : '#f00'
  38. ],
  39. 'remark' => [
  40. 'value' => $wechat_info['bottomtext'],
  41. 'color' => !empty($wechat_info['bottomtextcolor']) ? $wechat_info['bottomtextcolor'] : '#333'
  42. ]
  43. ];
  44. if (!empty($param['template_data'])) $template_data = array_merge($template_data, $param['template_data']);
  45. $data = [
  46. 'openid' => $param['openid'],
  47. 'template_id' => $message_info['wechat_template_id'],
  48. 'data' => $template_data,
  49. 'miniprogram' => [],
  50. 'url' => ""
  51. ];
  52. if (!empty($param['page'])) {
  53. // 商家消息
  54. if ($message_info['message_type'] == 2 && addon_is_exit('mobileshop', $site_id)) {
  55. $config = new MobileShopConfig();
  56. $weapp_config = $config->getWeAppConfig($site_id)['data']["value"];
  57. if (!empty($weapp_config['appid'])) {
  58. $data['miniprogram'] = [
  59. 'appid' => $weapp_config['appid'],
  60. 'pagepath' => $param['page']
  61. ];
  62. }
  63. $mshop_config = $config->getMShopDomainName($site_id)['data']['value'];
  64. $data['url'] = $mshop_config['domain_name_mobileshop'] . '/' . $param['page'];
  65. } else {
  66. $template_config_model = new Config();
  67. $template_config = $template_config_model->getTemplateMessageConfig($site_id);
  68. $template_config = $template_config['data']['value'];
  69. if ($template_config['is_jump_weapp']) {
  70. // 小程序配置
  71. $weapp_config = new WeappConfig();
  72. $weapp_config_result = $weapp_config->getWeAppConfig($site_id);
  73. $weapp_config = $weapp_config_result['data']["value"];
  74. if (!empty($weapp_config['appid'])) {
  75. $data['miniprogram'] = [
  76. 'appid' => $weapp_config['appid'],
  77. 'pagepath' => $param['page']
  78. ];
  79. }
  80. }
  81. $data['url'] = getH5Domain() . '/' . $param['page'];
  82. }
  83. }
  84. $wechat = new Wechat($site_id);
  85. $res = $wechat->sendTemplateMessage($data);
  86. return $res;
  87. } catch (\Exception $e) {
  88. return $this->error('', "模板消息发送失败");
  89. }
  90. }
  91. }