message.inc.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. global $_GPC, $_W;
  3. $GLOBALS['frames'] = $this->getMainMenu();
  4. $item=pdo_get('zhtc_system',array('uniacid'=>$_W['uniacid']));
  5. $time=time()-60*60*24*7;
  6. $sql=" select distinct user_id from".tablename('zhtc_userformid')." where uniacid={$_W['uniacid']} and UNIX_TIMESTAMP(time)>={$time} ";
  7. $user=pdo_fetchall($sql);
  8. if(checksubmit('submit')){
  9. function getaccess_token($_W){
  10. $res=pdo_get('zhtc_system',array('uniacid'=>$_W['uniacid']));
  11. $appid=$res['appid'];
  12. $secret=$res['appsecret'];
  13. $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret."";
  14. $ch = curl_init();
  15. curl_setopt($ch, CURLOPT_URL,$url);
  16. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  17. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
  18. $data = curl_exec($ch);
  19. curl_close($ch);
  20. $data = json_decode($data,true);
  21. return $data['access_token'];
  22. }
  23. //设置与发送模板信息
  24. function set_msg($_W, $_GPC,$user){
  25. $access_token = getaccess_token($_W);
  26. $res=pdo_get('zhtc_sms',array('uniacid'=>$_W['uniacid']));
  27. $userinfo=pdo_get('zhtc_user',array('id'=>$user));
  28. $time=time()-60*60*24*7;
  29. if($_POST['dz']){
  30. $_POST['dz']=$_POST['dz'];
  31. }else{
  32. $_POST['dz']="zh_tcwq/pages/index/index";
  33. }
  34. if($_POST['sj']){
  35. $_POST['sj']=$_POST['sj'];
  36. }else{
  37. $_POST['sj']=date("Y-m-d H:i:s");
  38. }
  39. $form=pdo_fetch("select * from ".tablename('zhtc_userformid')." where user_id={$user} and UNIX_TIMESTAMP(time)>={$time} order by id asc");
  40. $formwork ='{
  41. "touser": "'.$userinfo["openid"].'",
  42. "template_id": "'.$res["qf_tid"].'",
  43. "page":"'.$_POST['dz'].'",
  44. "form_id":"'.$form['form_id'].'",
  45. "data": {
  46. "keyword1": {
  47. "value": "'.$_POST['bz'].'",
  48. "color": "#173177"
  49. },
  50. "keyword2": {
  51. "value": "'.$_POST['ly'].'",
  52. "color": "#173177"
  53. },
  54. "keyword3": {
  55. "value":"'.$_POST['nr'].'",
  56. "color": "#173177"
  57. },
  58. "keyword4": {
  59. "value":"'.$_POST['sj'].'",
  60. "color": "#173177"
  61. }
  62. },
  63. "emphasis_keyword": "keyword1.DATA"
  64. }';
  65. // $formwork=$data;
  66. $url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=".$access_token."";
  67. $ch = curl_init();
  68. curl_setopt($ch, CURLOPT_URL,$url);
  69. curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  70. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
  71. curl_setopt($ch, CURLOPT_POST,1);
  72. curl_setopt($ch, CURLOPT_POSTFIELDS,$formwork);
  73. $data = curl_exec($ch);
  74. curl_close($ch);
  75. //return $data;
  76. pdo_delete('zhtc_userformid',array('id'=>$form['id']));
  77. }
  78. for($i=0;$i<count($user);$i++){
  79. echo set_msg($_W,$_GPC,$user[$i]['user_id']);
  80. }
  81. $data['note']=$_GPC['bz'];
  82. $data['source']=$_GPC['ly'];
  83. $data['content']=$_GPC['nr'];
  84. if($_GPC['sj']){
  85. $data['time']=$_GPC['sj'];
  86. }else{
  87. $data['time']=date("Y-m-d H:i:s");
  88. }
  89. if($_GPC['dz']){
  90. $data['src']=$_GPC['dz'];
  91. }else{
  92. $data['src']="首页";
  93. }
  94. $data['fs_time']=date("Y-m-d H:i:s");
  95. $data['uniacid']=$_W['uniacid'];
  96. pdo_insert('zhtc_message',$data);
  97. message('发送成功',$this->createWebUrl('message',array()),'success');
  98. }
  99. include $this->template('web/message');