public function check_member() { $app = Model('app')->get_app(); $access_token = Model('app')->getAccessToken(); $weiXinObj = new weixinController(); if (!isset($_SESSION['wxid'])) { if (!isset($_GET['code'])) { $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; $url = $this->parseurl($url); header('Location:https://open.weixin.qq.com/connect/oauth2/authorize?appid=' . $app['appid'] . '&redirect_uri=' . $url . '&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect'); } else { $code = $_GET['code']; $arr = $weiXinObj->getUserByCode($app['appid'], $app['appsecret'], $code); $check_openid = Model('app')->get_count_openid($arr['openid']); if ($check_openid == 0) { $str = $weiXinObj->getUserInfo($arr['openid'], $arr['access_token']); $info['openid'] = $str['openid']; $info['nickname'] = $str['nickname']; $info['sex'] = $str['sex']; $info['headimgurl'] = $str['headimgurl']; $info['area_id'] = 0; $info['status'] = 1; $info['first'] = 0; $wxid = Model('app')->insert_into_wxuser($info); if (!$wxid) { echo "System Error! Please wait for a while!"; } } else { $wxid = Model('app')->get_wxid($arr['openid']); } $_SESSION['wxid'] = $wxid; } } }
/** * 微信公众平台与本系统连接的方法 */ public function weixin() { $wx = new weixinController(); $data = $wx->data; /*验证*/ if ($_GET["echostr"]) { $respon = $wx->valid(Model('wechat')->getToken()); if ($respon != '') { die($respon); } } $access_token = Model('wechat')->getAccessToken(); /*自定义关键词回复*/ if ($data['Content']) { $content = Model('wechat')->key_reply($data['Content']); if (!empty($content)) { if (!is_array($content)) { $wx->replyTextMsg($content); } else { $wx->replyImgMsg($content); } } else { $areply = Model('wechat')->get_dreply(); if (!is_array($areply)) { $wx->replyTextMsg($areply); } else { $wx->replyImgMsg($areply); } } } /*自定义菜单回复*/ if ($data['EventKey']) { if (strstr($data['EventKey'], 'event_')) { $pos = strpos($data['EventKey'], 'event_'); $key = intval(substr($data['EventKey'], $pos + 6)); $result = Model('wechat')->get_msg($key); if (!is_array($result)) { $wx->sendTextMsg($data['FromUserName'], $result); } else { if (!empty($result)) { foreach ($result as $k => $v) { $img_data[$k]['title'] = urlencode($v['title']); $img_data[$k]['description'] = urlencode($v['img_des']); $img_data[$k]['url'] = $v['url']; $img_data[$k]['picurl'] = 'http://' . $_SERVER['HTTP_HOST'] . '/' . $v['thumb']; } $wx->sendImgMsg($data['FromUserName'], $img_data); } } } } /*关注时回复*/ if ($data['Event'] == 'subscribe') { $areply = Model('wechat')->get_areply(); if (!is_array($areply)) { $wx->replyTextMsg($areply); } else { $wx->replyImgMsg($areply); } } elseif ($data['Event'] == 'unsubscribe') { } /*发送地址*/ // if($data['Location_X'] && $data['Location_Y']) // { // $img_one = array("url" => "http://".$_SERVER['HTTP_HOST']."/index.php?g=home&m=baidu&a=index&x=".$data['Location_X']."&y=".$data['Location_Y'], "thumb" => "Home/webroot/default/images/i96x66.jpg", "title" => "分店信息", "description" => "分店信息"); // $img_arr[] = $img_one; // $wx->replyImgMsg($img_arr); // } }
/** * 提交菜单 */ public function sumbit_menu() { $this->admin_priv('set_wx_menu', 2); $res = array('error' => 0, 'content' => ''); $wx = new weixinController(); $wxmu = Model('wxmenu'); $token = $wxmu->getAccessToken(); $button = array(); $data = array(); $menu = array(); $json_result = array('error' => '0', 'content' => ''); $cfg['is_show'] = 1; $temp = $wxmu->get_wxmenu($cfg); $temp = $this->format_menu($temp); $result = json_decode($wx->create_menu($temp, $token)); if ($result->errcode != 0) { $res['error'] = 1; } $res['content'] = $result->errmsg; $this->showMsg($res); }