sendStoremsg.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace sendmsg;
  3. use think\facade\Db;
  4. class sendStoremsg {
  5. private $code = '';
  6. private $store_id = 0;
  7. /**
  8. * 设置
  9. *
  10. * @param mixed $key
  11. * @param mixed $value
  12. */
  13. public function set($key,$value){
  14. $this->$key = $value;
  15. }
  16. public function send($param = array(),$weixin_param = array(),$ali_param = array(),$ten_param = array()) {
  17. $msg_tpl = rkcache('storemsgtpl', true);
  18. if (!isset($msg_tpl[$this->code]) || $this->store_id <= 0) {
  19. return false;
  20. }
  21. $tpl_info = $msg_tpl[$this->code];
  22. $setting_info = model('storemsgsetting')->getStoremsgsettingInfo(array('storemt_code' => $this->code, 'store_id' => $this->store_id));
  23. // 发送站内信
  24. if ($tpl_info['storemt_message_switch'] && ($tpl_info['storemt_message_forced'] || $setting_info['storems_message_switch'])) {
  25. $message = ds_replace_text($tpl_info['storemt_message_content'],$param);
  26. $this->sendMessage($message);
  27. }
  28. // 发送短消息
  29. if ($tpl_info['storemt_short_switch'] && $setting_info['storems_short_number'] != '' && ($tpl_info['smt_short_forced'] || $setting_info['storems_short_switch'])) {
  30. $message = ds_replace_text($tpl_info['storemt_short_content'],$param);
  31. $smslog_param=array(
  32. 'ali_template_code'=>$tpl_info['ali_template_code'],
  33. 'ali_template_param'=>$ali_param,
  34. 'ten_template_code'=>$tpl_info['ten_template_code'],
  35. 'ten_template_param'=>$ten_param,
  36. 'message'=>$message,
  37. );
  38. $this->sendShort($setting_info['storems_short_number'], $smslog_param);
  39. }
  40. // 发送邮件
  41. if ($tpl_info['storemt_mail_switch'] && $setting_info['storems_mail_number'] != '' && ($tpl_info['storemt_mail_forced'] || $setting_info['storems_mail_switch'])) {
  42. $param['site_name'] = config('ds_config.site_name');
  43. $param['mail_send_time'] = date('Y-m-d H:i:s');
  44. $subject = ds_replace_text($tpl_info['storemt_mail_subject'],$param);
  45. $message = htmlspecialchars_decode(ds_replace_text($tpl_info['storemt_mail_content'],$param));
  46. $this->sendMail($setting_info['storems_mail_number'], $subject, $message);
  47. }
  48. // 发送微信模板消息
  49. if(!empty($weixin_param) && $tpl_info['storemt_weixin_switch'] && $tpl_info['storemt_weixin_code'] && ($tpl_info['storemt_weixin_forced'] || $setting_info['storems_weixin_switch'])){
  50. $param['site_name'] = config('ds_config.site_name');
  51. $member_id=Db::name('store')->where(array('store_id'=>$this->store_id))->value('member_id');
  52. if($member_id){
  53. $openid=Db::name('member')->where(array('member_id'=>$member_id))->value('member_wxopenid');
  54. if($openid){
  55. $tm_data = array(
  56. "first" => array(
  57. "value" => $tpl_info['storemt_name'],
  58. "color" => "#ff7007"
  59. ),
  60. "remark" => array(
  61. "value" => ds_replace_text($tpl_info['storemt_short_content'],$param),
  62. "color" => "#333"
  63. )
  64. );
  65. $wechat_model=model('wechat');
  66. $wechat_model->getOneWxconfig();
  67. $wechat_model->sendMessageTemplate($openid, $tpl_info['storemt_weixin_code'], $weixin_param['url'], array_merge($tm_data,$weixin_param['data']));
  68. }
  69. }
  70. }
  71. }
  72. /**
  73. * 发送站内信
  74. * @param unknown $message
  75. */
  76. private function sendMessage($message) {
  77. $insert = array();
  78. $insert['storemt_code'] = $this->code;
  79. $insert['store_id'] = $this->store_id;
  80. $insert['storemsg_content'] = $message;
  81. model('storemsg')->addStoremsg($insert);
  82. }
  83. /**
  84. * 发送短消息
  85. * @param unknown $number
  86. * @param unknown $message
  87. */
  88. private function sendShort($number, $message) {
  89. model('smslog')->sendSms($number, $message,'','','0','',true);
  90. }
  91. /**
  92. * 发送邮件
  93. * @param unknown $number
  94. * @param unknown $subject
  95. * @param unknown $message
  96. */
  97. private function sendMail($number, $subject, $message) {
  98. // 计划任务代码
  99. $insert = array();
  100. $insert['mailcron_address'] = $number;
  101. $insert['mailcron_subject'] = $subject;
  102. $insert['mailcron_contnet'] = $message;
  103. model('mailcron')->addMailCron($insert);
  104. }
  105. }