Browse Source

自动处理iOS下对于text文字回复\r\n两次换行的问题

dodgepudding 10 years ago
parent
commit
749238b82a
2 changed files with 24 additions and 2 deletions
  1. 12 1
      Thinkphp/Wechat.class.php
  2. 12 1
      wechat.class.php

+ 12 - 1
Thinkphp/Wechat.class.php

@@ -105,6 +105,7 @@ class Wechat
 	private $_msg;
 	private $_funcflag = false;
 	private $_receive;
+	private $_text_filter = true;
 	public $debug =  false;
 	public $errCode = 40001;
 	public $errMsg = "no access";
@@ -483,6 +484,16 @@ class Wechat
 	}
 	
 	/**
+	 * 过滤文字回复\r\n换行符
+	 * @param string $text
+	 * @return string|mixed
+	 */
+	private function _auto_text_filter($text) {
+		if (!$this->_text_filter) return $text;
+		return str_replace("\r\n", "\n", $text);
+	}
+	
+	/**
 	 * 设置回复消息
 	 * Examle: $obj->text('hello')->reply();
 	 * @param string $text
@@ -494,7 +505,7 @@ class Wechat
 			'ToUserName' => $this->getRevFrom(),
 			'FromUserName'=>$this->getRevTo(),
 			'MsgType'=>self::MSGTYPE_TEXT,
-			'Content'=>$text,
+			'Content'=>$this->_auto_text_filter($text),
 			'CreateTime'=>time(),
 			'FuncFlag'=>$FuncFlag
 		);

+ 12 - 1
wechat.class.php

@@ -105,6 +105,7 @@ class Wechat
 	private $_msg;
 	private $_funcflag = false;
 	private $_receive;
+	private $_text_filter = true;
 	public $debug =  false;
 	public $errCode = 40001;
 	public $errMsg = "no access";
@@ -478,6 +479,16 @@ class Wechat
 	}
 	
 	/**
+	 * 过滤文字回复\r\n换行符
+	 * @param string $text
+	 * @return string|mixed
+	 */
+	private function _auto_text_filter($text) {
+		if (!$this->_text_filter) return $text;
+		return str_replace("\r\n", "\n", $text);
+	}
+	
+	/**
 	 * 设置回复消息
 	 * Examle: $obj->text('hello')->reply();
 	 * @param string $text
@@ -489,7 +500,7 @@ class Wechat
 			'ToUserName' => $this->getRevFrom(),
 			'FromUserName'=>$this->getRevTo(),
 			'MsgType'=>self::MSGTYPE_TEXT,
-			'Content'=>$text,
+			'Content'=>$this->_auto_text_filter($text),
 			'CreateTime'=>time(),
 			'FuncFlag'=>$FuncFlag
 		);