sendMemberMsg.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace sendmsg;
  3. class sendMemberMsg
  4. {
  5. private $code = '';
  6. private $member_id = 0;
  7. private $member_info = array();
  8. private $mobile = '';
  9. private $email = '';
  10. /**
  11. * 设置
  12. *
  13. * @param mixed $key
  14. * @param mixed $value
  15. */
  16. public function set($key, $value)
  17. {
  18. $this->$key = $value;
  19. }
  20. public function send($param = array(),$weixin_param = array(),$ali_param = array(),$ten_param = array())
  21. {
  22. $msg_tpl = rkcache('membermsgtpl', true);
  23. if (!isset($msg_tpl[$this->code]) || $this->member_id <= 0) {
  24. return false;
  25. }
  26. $tpl_info = $msg_tpl[$this->code];
  27. $setting_info = model('membermsgsetting')->getMembermsgsettingInfo(array('membermt_code' => $this->code,'member_id' => $this->member_id), 'membermt_isreceive');
  28. if (empty($setting_info) || $setting_info['membermt_isreceive']) {
  29. // 发送站内信
  30. if ($tpl_info['membermt_message_switch']) {
  31. $message = ds_replace_text($tpl_info['membermt_message_content'], $param);
  32. $this->sendMessage($message);
  33. }
  34. // 发送短消息
  35. if ($tpl_info['membermt_short_switch']) {
  36. $this->getMemberInfo();
  37. if (!empty($this->mobile))
  38. $this->member_info['member_mobile'] = $this->mobile;
  39. if ($this->member_info['member_mobilebind'] && !empty($this->member_info['member_mobile'])) {
  40. $message = ds_replace_text($tpl_info['membermt_short_content'], $param);
  41. if(session('member_msg_short')==md5($message.'@'.$this->code.'@'.$this->member_id)){//如果发送过相同的消息则停止再发送
  42. return false;
  43. }else{
  44. session('member_msg_short',md5($message.'@'.$this->code.'@'.$this->member_id));
  45. }
  46. $smslog_param=array(
  47. 'ali_template_code'=>$tpl_info['ali_template_code'],
  48. 'ali_template_param'=>$ali_param,
  49. 'ten_template_code'=>$tpl_info['ten_template_code'],
  50. 'ten_template_param'=>$ten_param,
  51. 'message'=>$message,
  52. );
  53. $this->sendShort($this->member_info['member_mobile'], $smslog_param);
  54. }
  55. }
  56. // 发送邮件
  57. if ($tpl_info['membermt_mail_switch']) {
  58. $this->getMemberInfo();
  59. if (!empty($this->email))
  60. $this->member_info['member_email'] = $this->email;
  61. if ($this->member_info['member_emailbind'] && !empty($this->member_info['member_email'])) {
  62. $param['site_name'] = config('ds_config.site_name');
  63. $param['mail_send_time'] = date('Y-m-d H:i:s');
  64. $subject = ds_replace_text($tpl_info['membermt_mail_subject'], $param);
  65. $message = htmlspecialchars_decode(ds_replace_text($tpl_info['membermt_mail_content'], $param));
  66. $this->sendMail($this->member_info['member_email'], $subject, $message);
  67. }
  68. }
  69. // 发送微信模板消息
  70. if(!empty($weixin_param) && $tpl_info['membermt_weixin_switch'] && $tpl_info['membermt_weixin_code']){
  71. $param['site_name'] = config('ds_config.site_name');
  72. $this->getMemberInfo();
  73. if($this->member_info['member_wxopenid']){
  74. $tm_data = array(
  75. "first" => array(
  76. "value" => $tpl_info['membermt_name'],
  77. "color" => "#ff7007"
  78. ),
  79. "remark" => array(
  80. "value" => ds_replace_text($tpl_info['membermt_short_content'],$param),
  81. "color" => "#333"
  82. )
  83. );
  84. $wechat_model=model('wechat');
  85. $wechat_model->getOneWxconfig();
  86. $wechat_model->sendMessageTemplate($this->member_info['member_wxopenid'], $tpl_info['membermt_weixin_code'], $weixin_param['url'], array_merge($tm_data,$weixin_param['data']));
  87. }
  88. }
  89. }
  90. }
  91. /**
  92. * 会员详细信息
  93. */
  94. private function getMemberInfo()
  95. {
  96. if (empty($this->member_info)) {
  97. $this->member_info = model('member')->getMemberInfoByID($this->member_id);
  98. }
  99. }
  100. /**
  101. * 发送站内信
  102. * @param unknown $message
  103. */
  104. private function sendMessage($message)
  105. {
  106. //添加短消息
  107. $message_model = model('message');
  108. $insert_arr = array();
  109. $insert_arr['from_member_id'] = 0;
  110. $insert_arr['member_id'] = $this->member_id;
  111. $insert_arr['msg_content'] = $message;
  112. $insert_arr['message_type'] = 1;
  113. $message_model->addMessage($insert_arr);
  114. }
  115. /**
  116. * 发送短消息
  117. * @param unknown $number
  118. * @param unknown $message
  119. */
  120. private function sendShort($number, $message)
  121. {
  122. model('smslog')->sendSms($number, $message,'','','0','',true);
  123. }
  124. /**
  125. * 发送邮件
  126. * @param unknown $number
  127. * @param unknown $subject
  128. * @param unknown $message
  129. */
  130. private function sendMail($number, $subject, $message)
  131. {
  132. // 计划任务代码
  133. $insert = array();
  134. $insert['mailcron_address'] = $number;
  135. $insert['mailcron_subject'] = $subject;
  136. $insert['mailcron_contnet'] = $message;
  137. model('mailcron')->addMailCron($insert);
  138. }
  139. }