1 <?php 2 namespace Admin\Controller; 3 use Think\Controller; 4 //header(‘content-type:text‘);/////噢噢噢噢噢噢噢噢哦哦 没实名认证前被加了警告的ban代码 影响了token的验证 5 class WechatController extends Controller { 6 public function init() { 7 Vendor("Wechat.wechat#class"); 8 9 $config = M ( "Wxconfig" )->where ( array ( 10 "id" => "1" 11 ) )->find (); 12 13 $options = array ( 14 ‘token‘ => $config ["token"], // 填写你设定的key 15 ‘encodingaeskey‘ => $config ["encodingaeskey"], // 填写加密用的EncodingAESKey 16 ‘appid‘ => $config ["appid"], // 填写高级调用功能的app id 17 ‘appsecret‘ => $config ["appsecret"], // 填写高级调用功能的密钥 18 ); 19 $weObj = new \Wechat ( $options ); 20 return $weObj; 21 } 22 public function index() { 23 $weObj = $this->init (); 24 $weObj->valid (); 25 $type = $weObj->getRev ()->getRevType (); 26 switch ($type) { 27 case \Wechat::MSGTYPE_TEXT : 28 $key = $weObj->getRev()->getRevContent(); 29 $replay = M("Wxmessage")->where(array("key"=>$key))->select(); 30 31 for ($i = 0; $i < count($replay); $i++) { 32 if ($replay[$i]["type"]==0) { 33 $appUrl = "http://" . I("server.HTTP_HOST") . __ROOT__; 34 $newsArr[$i] = array( 35 ‘Title‘ => $replay[$i]["title"], 36 ‘Description‘ => $replay[$i]["description"], 37 ‘PicUrl‘ => $appUrl . ‘/Public‘.$replay[$i]["savepath"].$replay[$i]["picurl"], 38 ‘Url‘ => $replay[$i]["url"].‘&uid=‘ . $weObj->getRevFrom () 39 ); 40 }else{ 41 $weObj->text ( $replay[$i]["title"] )->reply (); 42 exit (); 43 } 44 } 45 $weObj->getRev ()->news ( $newsArr )->reply (); 46 exit (); 47 break; 48 case \Wechat::MSGTYPE_EVENT : 49 $eventype = $weObj->getRev ()->getRevEvent (); 50 file_put_contents("123.txt",$eventype); 51 if ($eventype [‘event‘] == "CLICK") { 52 $appUrl = "http://" . I("server.HTTP_HOST") . __ROOT__; 53 54 $news = M ( "Wxmessage" )->where ( array ( 55 "key" => $eventype [‘key‘], 56 "type" => 0 57 ) )->select (); 58 59 if ($news) { 60 for($i = 0; $i < count ( $news ); $i ++) { 61 $newsArr[$i] = array( 62 ‘Title‘ => $news[$i]["title"], 63 ‘Description‘ => $news[$i]["description"], 64 ‘PicUrl‘ => $appUrl . ‘/Public‘.$news[$i]["savepath"].$news[$i]["picurl"], 65 ‘Url‘ => $news[$i]["url"].‘&uid=‘ . $weObj->getRevFrom () 66 ); 67 } 68 $weObj->getRev ()->news ( $newsArr )->reply (); 69 } 70 71 }elseif ($eventype[‘event‘] == "subscribe") { 72 $weObj->text ( "欢迎您关注wemall商城!" )->reply (); 73 } 74 exit (); 75 break; 76 default : 77 $weObj->text ( "help info" )->reply (); 78 } 79 } 80 81 public function createMenu() { 82 $menu = M("Wxmenu")->select(); 83 $newmenu["button"] = array(); 84 for($i = 0; $i < count($menu); $i++){ 85 if($menu[$i]["type"] == "view"){ 86 array_push($newmenu["button"] , array(‘type‘=>‘view‘,‘name‘=>$menu[$i]["name"],‘url‘=>$menu[$i]["url"])); 87 }else{ 88 array_push($newmenu["button"] , array(‘type‘=>‘click‘,‘name‘=>$menu[$i]["name"],‘key‘=>$menu[$i]["key"])); 89 } 90 } 91 $weObj = $this->init(); 92 $weObj->createMenu($newmenu); 93 $this->success("重新创建菜单成功!"); 94 } 95 }
参考来源http://bbs.csdn.net/topics/391022271