public function wx_sendMsg($data) { if (!getglobal('setting/CorpID') || !getglobal('setting/CorpSecret')) { return false; } $user = C::t('user')->fetch($data['uid']); if (!$user['wechat_userid'] || $user['wechat_status'] != 1) { C::t('notification')->update($data['id'], array('wx_new' => $data['wx_new'] + 1)); return false; } $agentid = 0; if ($data['from_idtype'] == 'app' && $data['from_id'] && ($wxapp = C::t('wx_app')->fetch($data['from_id']))) { if ($wxapp['agentid'] && $wxapp['status'] < 1) { $agentid = $wxapp['agentid']; } } $wx = new qyWechat(array('appid' => getglobal('setting/CorpID'), 'appsecret' => getglobal('setting/CorpSecret'))); $msg = array("touser" => "dzz-" . $data['uid'], "safe" => 0, "agentid" => $agentid, "msgtype" => "news", "news" => array("articles" => array(array("title" => $data['title'], "description" => getstr($data['wx_note'], 0, 0, 0, 0, -1), "url" => $wx->getOauthRedirect(getglobal('siteurl') . 'index.php?mod=system&op=wxredirect&url=' . dzzencode($data['redirecturl'])))))); if ($ret = $wx->sendMessage($msg)) { C::t('notification')->update($data['id'], array('wx_new' => 0)); return true; } else { C::t('notification')->update($data['id'], array('wx_new' => $data['wx_new'] + 1)); $message = 'wx_notification:errCode:' . $wx->errCode . ';errMsg:' . $wx->errMsg; runlog('wxlog', $message); return false; } }
exit(json_encode(array('msg' => 'success'))); } elseif ($_GET['action'] == 'menu_publish') { //发布到微信 $data = $setting['menu'] = array('button' => $_GET['menu']); C::t('setting')->update('feed_wxsetting', $setting); if ($appid) { C::t('wx_app')->update($appid, array('menu' => serialize($data))); } updatecache('setting'); //发布菜单到微信 if (getglobal('setting/CorpID') && getglobal('setting/CorpSecret') && $setting['agentid']) { $wx = new qyWechat(array('appid' => getglobal('setting/CorpID'), 'appsecret' => getglobal('setting/CorpSecret'))); //处理菜单数据,所有本站链接添加oauth2地址 foreach ($data['button'] as $key => $value) { if ($value['url'] && strpos($value['url'], $_G['siteurl']) === 0) { $data['button'][$key]['url'] = $wx->getOauthRedirect(getglobal('siteurl') . 'index.php?mod=system&op=wxredirect&url=' . dzzencode($value['url'])); } elseif ($value['sub_button']) { foreach ($value['sub_button'] as $key1 => $value1) { if ($value1['url'] && strpos($value1['url'], $_G['siteurl']) === 0) { $data['button'][$key]['sub_button'][$key1]['url'] = $wx->getOauthRedirect(getglobal('siteurl') . 'index.php?mod=system&op=wxredirect&url=' . dzzencode($value1['url'])); } } } } if ($wx->createMenu($data, $setting['agentid'])) { exit(json_encode(array('msg' => 'success'))); } else { exit(json_encode(array('error' => '发布失败,errCode:' . $wx->errCode . ',errMsg:' . $wx->errMsg))); } } else { exit(json_encode(array('error' => '发布失败,应用还没有创建微信agentid')));
function getOauthRedirect($url) { //获取链接的auth地址 $wx = new qyWechat(array('appid' => getglobal('setting/CorpID'), 'appsecret' => getglobal('setting/CorpSecret'))); return $wx->getOauthRedirect(getglobal('siteurl') . 'index.php?mod=system&op=wxredirect&url=' . dzzencode($url)); }