Msg.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <?php
  2. namespace plugins\weixin\util;
  3. use app\common\model\Msg AS MsgModel;
  4. class Msg
  5. {
  6. protected static $wxid;
  7. protected static $user;
  8. protected static $content;
  9. protected static function getUrl($array=[]){
  10. if (empty($array[1])) {
  11. return ;
  12. }
  13. $token = '';
  14. $user = self::$user;
  15. if ($user) {
  16. $token = md5( self::$wxid . time() . $user['lastvist'] . rands(5) );
  17. cache2(md5($token),"{$user['uid']}\t{$user['username']}\t".mymd5($user['password'],'EN')."\t",3600*24);
  18. }
  19. //once=1 出于安全考虑,限制只有一次有效
  20. if(strstr($array[2],'?')){
  21. $array[2].='&once=1&token='.$token;
  22. }else{
  23. $array[2].='?once=1&token='.$token;
  24. }
  25. return $array[1].':'.$array[2].$array[3];
  26. }
  27. protected static function stringLength($string){
  28. $re = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
  29. preg_match_all($re, $string, $match);
  30. return count($match[0]);
  31. }
  32. protected static function sendmsg($content=''){
  33. $info = MsgModel::where('touid',self::$user['uid'])->order('id desc')->find();
  34. if (!self::$content) {
  35. self::$content = $content;
  36. }
  37. if ( (time()-strtotime($info['create_time']))>60 || substr(del_html($info['content']) , 0 , 30) != substr(del_html(self::$content) , 0 , 30) ){
  38. $data = [
  39. 'touid'=>self::$user['uid'],
  40. 'title'=>'微信消息',
  41. 'content'=>self::$content,
  42. 'create_time'=>time(),
  43. ];
  44. $result = MsgModel::create($data);
  45. $id = $result->id;
  46. }else{
  47. $id = $info['id'];
  48. }
  49. $user = self::$user;
  50. $token = md5( self::$wxid . time() . $user['lastvist'] . rands(5) );
  51. cache2(md5($token),"{$user['uid']}\t{$user['username']}\t".mymd5($user['password'],'EN')."\t",3600*24);
  52. $url = get_url(murl('member/msg/show',['id'=>$id])).'?once=1&token='.$token;
  53. return $url;
  54. }
  55. /**
  56. * 公众号订阅消息
  57. * @param unknown $openid
  58. * @param unknown $content
  59. * @param string $access_token
  60. * @param array $array
  61. */
  62. public static function mp_subscribe($openid,$content,$array=[],$access_token=''){
  63. $first = "你好!";
  64. $subject = "来自《".config('webdb.webname')."》的消息";
  65. $content = stripslashes($content);
  66. preg_match("/(http|https):([^ ]+)(\"|')/is",$content,$_array);
  67. $url = $_array[2] ? "$_array[1]:$_array[2]" : request()->domain();
  68. $content = preg_replace('/<([^<]*)>/is',"",$content);
  69. if (self::stringLength($content)>20) {
  70. if (!$array['sendmsg']) { //不是私信的情况
  71. $_url = self::sendmsg($content);
  72. if ($_url) {
  73. $url = $_url;
  74. }
  75. }
  76. $content = mb_substr($content,0,17,'utf-8').'...';
  77. }
  78. if (self::stringLength($subject)>20) {
  79. $subject = mb_substr($subject,0,17,'utf-8').'...';
  80. }
  81. $time = date('Y年m月d日 H:i');
  82. $tinfo = [];
  83. if($array['template_data']){
  84. $tinfo = fun('msg@template',$array['template_data']['key_word']);
  85. if (!$tinfo || $tinfo['type']==0) {
  86. return ;
  87. }
  88. }
  89. if ($tinfo) {
  90. $_data = [];
  91. foreach(json_decode($tinfo['data_field'],true) AS $rs){
  92. $_data[$rs['title3']] = [
  93. 'value'=>$array['template_data'][$rs['title2']],
  94. 'color'=>'#0000ff',
  95. ];
  96. }
  97. $array = [
  98. 'touser'=>$openid,
  99. 'template_id'=>$tinfo['template_id'],
  100. 'url'=>$array['template_data']['page_url'] ?: $url,
  101. 'page'=>$array['template_data']['page_url'] ?: $url,
  102. 'data'=>$_data,
  103. ];
  104. $data = json_encode($array);
  105. }else{
  106. $data=" {
  107. \"touser\":\"$openid\",
  108. \"template_id\":\"".config('webdb.mp_subscribe_template_id')."\",
  109. \"page\":\"$url\",
  110. \"data\":{
  111. \"thing2\": {
  112. \"value\":\"$content\",
  113. \"color\":\"#0000ff\"
  114. },
  115. \"thing4\":{
  116. \"value\":\"$content\",
  117. \"color\":\"#666666\"
  118. },
  119. \"thing3\":{
  120. \"value\":\"$content\",
  121. \"color\":\"#666666\"
  122. },
  123. \"time3\": {
  124. \"value\":\"$time\",
  125. \"color\":\"#666666\"
  126. },
  127. \"thing1\":{
  128. \"value\":\"$content\",
  129. \"color\":\"#0000ff\"
  130. }
  131. }
  132. }";
  133. }
  134. $surl = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/bizsend?access_token=';
  135. if($tinfo['type']==2){
  136. $surl = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=';
  137. }
  138. $string = http_Curl($surl.$access_token,$data);
  139. if(strstr($string,'ok')){
  140. return true;
  141. }
  142. }
  143. /**
  144. * 小程序订阅消息
  145. * @param unknown $openid
  146. * @param unknown $content
  147. * @param array $array
  148. * @param string $access_token
  149. * @param string $template_id
  150. * @return boolean
  151. */
  152. public static function wxapp_subscribe($openid,$content,$array=[],$access_token='',$template_id=''){
  153. $first = "你好!";
  154. $subject = "来自《".config('webdb.webname')."》的消息";
  155. $content = stripslashes($content);
  156. preg_match("/(http|https):([^ ]+)(\"|')/is",$content,$_array);
  157. $url = $_array[2] ? "$_array[1]:$_array[2]" : request()->domain();
  158. $content = preg_replace('/<([^<]*)>/is',"",$content);
  159. if (self::stringLength($content)>20) {
  160. if (!$array['sendmsg']) { //不是私信的情况
  161. $_url = self::sendmsg($content);
  162. if ($_url) {
  163. $url = $_url;
  164. }
  165. }
  166. $content = mb_substr($content,0,17,'utf-8').'...';
  167. }
  168. if (self::stringLength($subject)>20) {
  169. $subject = mb_substr($subject,0,17,'utf-8').'...';
  170. }
  171. $url = urlencode($url);
  172. $time = date('Y年m月d日 H:i');
  173. $tinfo = [];
  174. if($array['template_data']){
  175. $tinfo = fun('msg@template',$array['template_data']['key_word']);
  176. if (!$tinfo || $tinfo['type']!=0) {
  177. return ;
  178. }
  179. }
  180. if ($tinfo) {
  181. $_data = [];
  182. foreach(json_decode($tinfo['data_field'],true) AS $rs){
  183. $_data[$rs['title3']] = [
  184. 'value'=>$array['template_data'][$rs['title2']],
  185. 'color'=>'#0000ff',
  186. ];
  187. }
  188. $array = [
  189. 'touser'=>$openid,
  190. 'template_id'=>$tinfo['template_id'],
  191. 'page'=>'pages/hy/web/index?url='.$array['template_data']['page_url']?urlencode($array['template_data']['page_url']):$url,
  192. 'data'=>$_data,
  193. ];
  194. $data = json_encode($array);
  195. }else{
  196. $data=" {
  197. \"touser\":\"$openid\",
  198. \"template_id\":\"".($template_id?:config('webdb.wxapp_subscribe_template_id'))."\",
  199. \"page\":\"pages/hy/web/index?url=$url\",
  200. \"data\":{
  201. \"thing3\": {
  202. \"value\":\"$content\",
  203. \"color\":\"#0000ff\"
  204. },
  205. \"thing4\":{
  206. \"value\":\"$content\",
  207. \"color\":\"#666666\"
  208. },
  209. \"thing2\":{
  210. \"value\":\"$content\",
  211. \"color\":\"#666666\"
  212. },
  213. \"time3\": {
  214. \"value\":\"$time\",
  215. \"color\":\"#666666\"
  216. },
  217. \"thing1\":{
  218. \"value\":\"$content\",
  219. \"color\":\"#0000ff\"
  220. }
  221. }
  222. }";
  223. }
  224. $string = http_Curl("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=".$access_token,$data);
  225. if(strstr($string,'ok')){
  226. return true;
  227. }
  228. }
  229. /**
  230. * 给用户发送公众号或者是小程序消息
  231. * @param unknown $openid 有可能是公众号,也有可能是小程序的ID,将要弃用
  232. * @param unknown $content
  233. * @param array $array
  234. * @param array $user 用户的信息
  235. * @return void|boolean|mixed
  236. */
  237. public function send($openid,$content,$array=[],$user=[]){
  238. if(empty($openid) && empty($user)){
  239. return ;
  240. }
  241. self::$wxid = $openid?:$user['uid'];
  242. self::$user = $user;
  243. self::$content = $content;
  244. $content = preg_replace_callback("/(http|https):([^ ]+)(\"|')/is",array(self,'getUrl'),$content); //加入用户登录信息,让用户直接登录
  245. if($array['type']=='image'){
  246. $data="{
  247. \"touser\":\"$openid\",
  248. \"msgtype\":\"image\",
  249. \"image\":
  250. {
  251. \"media_id\":\"$array[id]\"
  252. }
  253. }";
  254. }elseif($array['type']=='voice'){
  255. $data="{
  256. \"touser\":\"$openid\",
  257. \"msgtype\":\"voice\",
  258. \"voice\":
  259. {
  260. \"media_id\":\"$array[id]\"
  261. }
  262. }";
  263. }elseif($array['type']=='video'){
  264. $array['thumb_media_id'] || $array['thumb_media_id']=$array['id'];
  265. $data="{
  266. \"touser\":\"$openid\",
  267. \"msgtype\":\"video\",
  268. \"video\":
  269. {
  270. \"media_id\":\"$array[id]\",
  271. \"thumb_media_id\":\"$array[thumb_media_id]\",
  272. \"title\":\"$array[title]\",
  273. \"description\":\"$array[description]\"
  274. }
  275. }";
  276. }else{
  277. strstr($content,'"') && $content = addslashes($content);
  278. $data="{
  279. \"touser\":\"$openid\",
  280. \"msgtype\":\"text\",
  281. \"text\":
  282. {
  283. \"content\":\"$content\"
  284. }
  285. }";
  286. }
  287. $ac = wx_getAccessToken();
  288. if(in_array($array['type'],['image','voice','video'])&&strstr( http_curl("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=$ac",$data) ,'ok')){
  289. return true;
  290. }
  291. //圈子小程序消息的处理
  292. if (get_wxappAppid() && $user['qun_msg_dy'] && $user['qun_msg_dy'][get_wxappAppid()]) {
  293. $template_id = wxapp_open_cfg(get_wxappAppid())['wxapp_subscribe_template_id'];
  294. if ($template_id && self::wxapp_subscribe($user['qun_msg_dy'][get_wxappAppid()], $content,$array,wx_getAccessToken(false,true,get_wxappAppid()),$template_id)===true) {
  295. return true;
  296. }
  297. }elseif(!$user['subscribe_wxapp'] && $user['qun_msg_dy']){
  298. foreach($user['qun_msg_dy'] AS $qun_appid=>$qun_openid){
  299. $template_id = wxapp_open_cfg($qun_appid)['wxapp_subscribe_template_id'];
  300. if ( $template_id && self::wxapp_subscribe($qun_openid, $content,$array,wx_getAccessToken(false,true,$qun_appid),$template_id)===true) {
  301. return true;
  302. }
  303. }
  304. }
  305. // if ($user['subscribe_qun_wxapp']) {
  306. // $info = \app\qun\model\Weixin::where('uid',$user['uid'])->where('if_dy',1)->find();
  307. // if ($info && self::wxapp_subscribe($info['wxapp_api'], $content,$array,wx_getAccessToken(false,true,$info['wxapp_appid']))===true) {
  308. // return true;
  309. // }
  310. // }
  311. //个性模板消息优先
  312. if(!get_wxappAppid() && $array['template_data'] && ($tinfo = fun('msg@template',$array['template_data']['key_word'])) ){
  313. if($tinfo['type']==0 && $user['wxapp_api'] && self::wxapp_subscribe($user['wxapp_api'], $content,$array,wx_getAccessToken(false,true))===true ){
  314. return true;
  315. }elseif($user['weixin_api'] && self::mp_subscribe($user['weixin_api'], $content,$array,$ac)===true ){
  316. return true;
  317. }
  318. }
  319. if($user['wx_attention'] && self::gz_mp_msg($user['weixin_api'],$content,$ac)===true){
  320. return true;
  321. }
  322. //通用模板消息的处理
  323. if($user['subscribe_wxapp'] && self::wxapp_subscribe($user['wxapp_api'], $content,$array,wx_getAccessToken(false,true))===true){
  324. return true;
  325. //}elseif($user['subscribe_mp'] && self::mp_subscribe($user['weixin_api'], $content,$array,$ac)===true){
  326. }elseif( self::mp_subscribe($user['weixin_api'], $content,$array,$ac)===true ){
  327. return true;
  328. }elseif(strstr( http_curl("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=$ac",$data) ,'ok')){
  329. return true;
  330. }elseif($user['weixin_api'] && self::gz_mp_msg($user['weixin_api'],$content,$ac)===true){
  331. return true;
  332. }
  333. }
  334. /**
  335. * 关注了公众号,才能使用的模板消息。非公众号订阅消息
  336. * @param string $openid
  337. * @param string $content
  338. * @param string $ac
  339. * @return boolean
  340. */
  341. private static function gz_mp_msg($openid='',$content='',$ac=''){
  342. $first = "你好!";
  343. $subject = "来自《".config('webdb.webname')."》的消息";
  344. $sender = "系统消息";
  345. $content = stripslashes($content);
  346. preg_match("/(http|https):([^ ]+)(\"|')/is",$content,$array);
  347. $url = $array[2] ? "$array[1]:$array[2]" : request()->url(true);
  348. $content = preg_replace('/<([^<]*)>/is',"",$content);
  349. $content = addslashes($content);
  350. $field_name = config('webdb.weixin_msgfield')?:'keyword2';
  351. $data=" {
  352. \"touser\":\"$openid\",
  353. \"template_id\":\"".config('webdb.weixin_msg_template_id')."\",
  354. \"url\":\"$url\",
  355. \"data\":{
  356. \"first\": {
  357. \"value\":\"$content\",
  358. \"color\":\"#0000ff\"
  359. },
  360. \"{$field_name}\":{
  361. \"value\":\"$content\",
  362. \"color\":\"#000000\"
  363. },
  364. \"subject\":{
  365. \"value\":\"$subject\",
  366. \"color\":\"#666666\"
  367. },
  368. \"sender\": {
  369. \"value\":\"$sender\",
  370. \"color\":\"#666666\"
  371. },
  372. \"remark\":{
  373. \"value\":\"$content\",
  374. \"color\":\"#0000ff\"
  375. }
  376. }
  377. }";
  378. $string = http_Curl("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=$ac",$data);
  379. if(strstr($string,'ok')){
  380. return true;
  381. }
  382. }
  383. }