stanley-king 8 years ago
parent
commit
e2211915e3
2 changed files with 216 additions and 0 deletions
  1. 115 0
      core/framework/libraries/message_sender.php
  2. 101 0
      helper/push_sender.php

+ 115 - 0
core/framework/libraries/message_sender.php

@@ -0,0 +1,115 @@
+<?php
+/**
+ *
+ *
+ *
+ * @package    library* www.33hao.com 专业团队 提供售后服务
+ */
+
+require_once(BASE_ROOT_PATH . '/helper/push_sender.php');
+require_once(BASE_ROOT_PATH . '/helper/sms_helper.php');
+
+class message_sender
+{
+    private $code = '';
+    private $member_id = 0;
+    private $member_info = array();
+    private $mobile = '';
+    private $sms_param = '';
+
+    public function set($key,$value){
+        $this->$key = $value;
+    }
+
+    public function send($param = array())
+    {
+        $msg_tpl = rkcache('member_msg_tpl', true);
+        if (!isset($msg_tpl[$this->code]) || $this->member_id <= 0) {
+            return false;
+        }
+
+        $tpl_info = $msg_tpl[$this->code];
+
+        $setting_info = Model('member_msg_setting')->getMemberMsgSettingInfo(array('mmt_code' => $this->code, 'member_id' => $this->member_id), 'is_receive');
+        if (empty($setting_info) || $setting_info['is_receive'])
+        {
+            // 发送站内信
+            if ($tpl_info['mmt_message_switch']) {
+                $message = ncReplaceText($tpl_info['mmt_message_content'],$param);
+                $this->sendMessage($message);
+            }
+
+            $push_success = false;
+            if ($tpl_info['mmt_push_switch']) {
+                $message = ncReplaceText($tpl_info['mmt_push_content'],$param);
+                $info = push_helper::format_msg($this->member_id,$message);
+                $sender = new push_sender();
+                $push_success = $sender->send($info);
+            }
+
+            // 发送短消息
+            if ($push_success == false && $tpl_info['mmt_short_switch'])
+            {
+                $sms_code = $tpl_info['mmt_short_code'];
+                $this->getMemberInfo();
+                if (!empty($this->mobile)) {
+                    $this->member_info['member_mobile'] = $this->mobile;
+                }
+                $this->sendShort($this->member_info['member_mobile'], $sms_code, empty($this->sms_param) ? null : $this->sms_param);
+            }
+        }
+    }
+
+    /**
+     * 会员详细信息
+     */
+    private function getMemberInfo() {
+        if (empty($this->member_info)) {
+            $this->member_info = Model('member')->getMemberInfoByID($this->member_id);
+        }
+    }
+
+    /**
+     * 发送站内信
+     * @param unknown $message
+     */
+    private function sendMessage($message) {
+        //添加短消息
+        $model_message = Model('message');
+        $insert_arr = array();
+        $insert_arr['from_member_id'] = 0;
+        $insert_arr['member_id'] = $this->member_id;
+        $insert_arr['msg_content'] = $message;
+        $insert_arr['message_type'] = 1;
+
+        $model_message->saveMessage($insert_arr);
+    }
+
+    /**
+     * 发送短消息
+     * @param unknown $number
+     * @param unknown $message
+     */
+    private function sendShort($number,$smscode,$param) {
+        $sms = new Sms();
+        $sms->send_oper($number,$smscode,$param);
+    }
+
+//    /**
+//     * 发送邮件
+//     * @param unknown $number
+//     * @param unknown $subject
+//     * @param unknown $message
+//     */
+//    private function sendMail($number, $subject, $message) {
+//    	//即时发送邮箱 v3-b11
+//    	$email = new Email();
+//        $email->send_sys_email($number,$subject,$message);
+//        // 计划任务代码
+//        /*$insert = array();
+//        $insert['mail'] = $number;
+//        $insert['subject'] = $subject;
+//        $insert['contnet'] = $message;
+//        Model('mail_cron')->addMailCron($insert);*/
+//    }
+}

+ 101 - 0
helper/push_sender.php

@@ -0,0 +1,101 @@
+<?php
+
+require_once(BASE_DATA_PATH . '/api/notification/android/AndroidCustomizedcast.php');
+require_once(BASE_DATA_PATH . '/api/notification/ios/IOSCustomizedcast.php');
+
+class push_sender
+{
+    protected $appkey = NULL;
+    protected $appMasterSecret = NULL;
+    protected $timestamp = NULL;
+    protected $validation_token = NULL;
+
+    protected $android_alias_type = 'LRLZ';
+    protected $androd_appkey = '5631efd4e0f55a8770000027';
+    protected $androd_appmastersecret = 'r6w2a8z9x8zonh7qmk8ds2fvypu02wpj';
+
+    protected $ios_alias_type = 'kPandaMakeUpAlias';
+    protected $ios_appkey = '568b6727e0f55a06580011e7';
+    protected $ios_appmastersecret = 'q9rmi5zjvfpvkwauenbgvhnmdsqdjeso';
+
+    function __construct()
+    {
+        $this->timestamp = strval(time());
+    }
+
+    private function sendAndroidCustomizedcast(array $pushinfo)
+    {
+        $customizedcast = new AndroidCustomizedcast();
+        $customizedcast->setAppMasterSecret($this->androd_appmastersecret);
+        $customizedcast->setPredefinedKeyValue("appkey", $this->androd_appkey);
+        $customizedcast->setPredefinedKeyValue("timestamp", $this->timestamp);
+        // Set your alias here, and use comma to split them if there are multiple alias.
+        // And if you have many alias, you can also upload a file containing these alias, then
+        // use file_id to send customized notification.
+        $customizedcast->setPredefinedKeyValue("alias", $pushinfo['member_id']);
+        // Set your alias_type here
+        $customizedcast->setPredefinedKeyValue("alias_type", $this->android_alias_type);
+        $customizedcast->setPredefinedKeyValue("ticker", $pushinfo['text']);
+        $customizedcast->setPredefinedKeyValue("title", $pushinfo['text']);
+        $customizedcast->setPredefinedKeyValue("text", $pushinfo['text']);
+        $customizedcast->setPredefinedKeyValue("after_open", "go_app");
+        //Set extra fields
+        $customizedcast->setExtraField("go_type", $pushinfo['go_type']);
+
+        return $customizedcast->send();
+    }
+
+
+    private function sendIOSCustomizedcast(array $pushinfo)
+    {
+        $customizedcast = new IOSCustomizedcast();
+        $customizedcast->setAppMasterSecret($this->ios_appmastersecret);
+        $customizedcast->setPredefinedKeyValue("appkey", $this->ios_appkey);
+        $customizedcast->setPredefinedKeyValue("timestamp", $this->timestamp);
+
+        // Set your alias here, and use comma to split them if there are multiple alias.
+        // And if you have many alias, you can also upload a file containing these alias, then
+        // use file_id to send customized notification.
+        $customizedcast->setPredefinedKeyValue("alias", $pushinfo['member_id']);
+        // Set your alias_type here
+        $customizedcast->setPredefinedKeyValue("alias_type", $this->ios_alias_type);
+        $customizedcast->setPredefinedKeyValue("alert", $pushinfo['text']);
+        $customizedcast->setPredefinedKeyValue("badge", 0);
+        $customizedcast->setPredefinedKeyValue("sound", "chime");
+        // Set 'production_mode' to 'true' if your app is under production mode
+        $customizedcast->setPredefinedKeyValue("production_mode", "true");
+        //Set extra fields
+        $customizedcast->setCustomizedField("go_type", $pushinfo['go_type']);
+
+        return $customizedcast->send();
+    }
+
+    public function send(array $info)
+    {
+        try
+        {
+            $ret = $this->sendAndroidCustomizedcast($info);
+            $data = json_decode($ret,true);
+            if(strtoupper($data['ret']) == 'SUCCESS') {
+                return true;
+            }
+        }
+        catch (Exception $ex) {
+            Log::record("push_app sendAndroid error",Log::ERR);
+        }
+
+        try
+        {
+            $ret = $this->sendIOSCustomizedcast($info);
+            $data = json_decode($ret,true);
+            if(strtoupper($data['ret']) == 'SUCCESS') {
+                return true;
+            }
+        }
+        catch (Exception $ex) {
+            Log::record("push_app sendAndroid error",Log::ERR);
+        }
+
+        return false;
+    }
+}