function weixin_robot_update_user($weixin_openid, $weixin_user) { // 更新自定义字段 global $wpdb; $old_user = weixin_robot_get_user($weixin_openid); if ($old_user) { $weixin_user = wp_parse_args($weixin_user, $old_user); $wpdb->update($wpdb->weixin_users, $weixin_user, array('openid' => $weixin_openid)); wp_cache_delete($weixin_openid, 'weixin_user'); } return $weixin_user; }
function weixin_robot_add_credit($arg) { if (!is_array($arg) || count($arg) < 1) { wp_die('系统错误(1000),请通知管理员。'); } global $wpdb; $default_args = array('type' => '', 'post_id' => 0, 'weixin_openid' => 0, 'operator_id' => 0, 'credit_change' => 0, 'exp_change' => false, 'note' => '', 'multiple' => 1); extract(wp_parse_args($arg, $default_args)); if (!$type) { wp_die('未知动态类型。'); } if (!$weixin_openid) { wp_die('weixin_openid 为空或非法。'); } $weixin_user = weixin_robot_get_user($weixin_openid); $old_credit = weixin_robot_get_credit($weixin_openid); $old_exp = weixin_robot_get_exp($weixin_openid); $credit_change = intval($credit_change) * intval($multiple); if ($exp_change === false) { // 传递进来 0 就不加 $exp_change = $credit_change; } $limit = 0; if ($credit_change > 0 && $operator_id == 0) { // 有 operator_id 就不检测每日上限 $today_credit_sum = (int) $wpdb->get_var($wpdb->prepare("SELECT SUM(credit_change) FROM {$wpdb->weixin_credits} WHERE weixin_openid=%s AND time<=%s AND time>=%s AND credit_change > 0 AND operator_id = 0", $weixin_openid, date('Y-m-d', current_time('timestamp')) . ' 23:59:59', date('Y-m-d', current_time('timestamp')) . ' 00:00:00')); if ($today_credit_sum >= weixin_robot_get_setting('weixin_day_credit_limit')) { $credit_change = 0; $limit = 1; } } $credit = $old_credit + $credit_change; $exp = $old_exp + $exp_change; // 积分变化,需要清理用户缓存 wp_cache_delete($weixin_openid, 'weixin_user_credit'); wp_cache_delete($weixin_openid, 'weixin_user_exp'); $data = array('weixin_openid' => $weixin_openid, 'operator_id' => $operator_id, 'credit_change' => $credit_change, 'credit' => $credit, 'exp_change' => $exp_change, 'exp' => $exp, 'type' => $type, 'post_id' => $post_id, 'note' => $note, 'limit' => $limit, 'time' => current_time('mysql'), 'url' => $_SERVER['REQUEST_URI']); $format = array('%s', '%d', '%d', '%d', '%d', '%d', '%s', '%d', '%s', '%s', '%s'); $wpdb->insert($wpdb->weixin_credits, $data, $format); do_action('weixin_credit', $arg); return $credit_change; }
} $weixin_openid = weixin_robot_get_user_openid(); if ($weixin_openid == false) { wp_die('非法访问!'); } if (isset($_GET['update'])) { if (isset($_POST['update']) && wp_verify_nonce($_POST['weixin_user_act'], 'weixin_user')) { $weixin_user_new = array('name' => trim(wp_strip_all_tags($_POST['name'])), 'address' => trim(wp_strip_all_tags($_POST['address'])), 'phone' => trim(wp_strip_all_tags($_POST['phone']))); weixin_robot_update_user($weixin_openid, $weixin_user_new); $success = "ok"; } } else { global $wpdb; $weixin_credits = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->weixin_credits} WHERE weixin_openid=%s ORDER BY id DESC LIMIT 0,30;", $weixin_openid)); } $weixin_user = weixin_robot_get_user($weixin_openid); if (empty($weixin_user['name']) && !empty($weixin_user['nickname'])) { $weixin_user['name'] = $weixin_user['nickname']; } ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>用户中心</title> <meta name="HandheldFriendly" content="True"> <meta name="MobileOptimized" content="320"> <meta name="viewport" content="width=device-width, initial-scale=1"> <style type="text/css"> ul{padding-left: 0;} li{margin-left: 0px;}
function weixin_robot_credit_page() { global $plugin_page, $current_user; if (isset($_POST['weixin_robot_credit_nonce']) && wp_verify_nonce($_POST['weixin_robot_credit_nonce'], 'weixin_robot')) { $weixin_openid = stripslashes(trim($_POST['weixin_openid'])); $credit_change = stripslashes(trim($_POST['credit_change'])); $note = stripslashes(trim($_POST['note'])); if (empty($weixin_openid) || empty($credit_change)) { $err_msg = '微信 OpenID 和 积分不能为空'; } elseif (weixin_robot_get_user($weixin_openid, 'local') === false) { $err_msg = '微信OpenID不存在'; } elseif (!is_numeric($credit_change)) { $err_msg = '积分必须为数字'; } if (empty($err_msg)) { $args = array('type' => 'manual', 'weixin_openid' => $weixin_openid, 'operator_id' => $current_user->ID, 'credit_change' => $credit_change, 'exp_change' => 0, 'note' => $note); weixin_robot_add_credit($args); $succeed_msg = '修改成功'; } } ?> <div class="wrap"> <div id="icon-weixin-robot" class="icon32"><br></div> <h2> <?php if (isset($_GET['action']) && $_GET['action'] == 'add') { ?> 手工修改积分 <a href="<?php echo admin_url('admin.php?page=' . $plugin_page); ?> " class="add-new-h2">返回列表</a> <?php } else { ?> 微信积分记录 <a href="<?php echo admin_url('admin.php?page=' . $plugin_page); ?> &action=add" class="add-new-h2">手工修改</a> <?php } ?> </h2> <?php if (!empty($succeed_msg)) { ?> <div class="updated"> <p><?php echo $succeed_msg; ?> </p> </div> <?php } ?> <?php if (!empty($err_msg)) { ?> <div class="error" style="color:red;"> <p>错误:<?php echo $err_msg; ?> </p> </div> <?php } ?> <?php if (isset($_GET['action']) && $_GET['action'] == 'add') { weixin_robot_credit_add(); } else { weixin_robot_credit_list(); } ?> </div> <?php }
function weixin_robot_messages_page() { ?> <div class="wrap"> <h2>最新消息</h2> <p>下面是你公众号上最新的消息,你可以直接删除(WordPress 本地删除,公众号后台不受影响)!</p> <?php global $wpdb, $plugin_page; if ($_SERVER['REQUEST_METHOD'] == 'POST') { if (!wp_verify_nonce($_POST['weixin_robot_send_user_nonce'], 'weixin_robot')) { ob_clean(); wp_die('非法操作'); } $weixin_openid = stripslashes(trim($_POST['weixin_openid'])); $reply_id = stripslashes(trim($_POST['reply_id'])); $reply_type = stripslashes(trim($_POST['reply_type'])); $content = stripslashes(trim($_POST['content'])); if ($weixin_openid && $message_id && $content) { $data = array('MsgType' => 'manual', 'FromUserName' => $weixin_openid, 'CreateTime' => current_time('timestamp') - get_option('gmt_offset') * 3600, 'Content' => $content); $insert_id = $wpdb->insert($wpdb->weixin_messages, $data); $wpdb->update($wpdb->weixin_messages, array('Response' => $wpdb->insert_id), array('id' => $reply_id)); $succeed_msg = weixin_rebot_sent_user($weixin_openid, $content, $reply_type); } } $response_types = weixin_robot_get_response_types(); $types = weixin_robot_stats_get_types(); unset($types['subscribe']); unset($types['unsubscribe']); $types['manual'] = '需要人工回复'; $type = weixin_robot_stats_get_type(); if (!$type) { $type = 'total'; } $Response = isset($_REQUEST['Response']) ? $_REQUEST['Response'] : ''; if (isset($_GET['delete']) && isset($_GET['id']) && $_GET['id']) { $wpdb->query("DELETE FROM {$wpdb->weixin_messages} WHERE id = {$_GET['id']}"); } $current_page = isset($_GET['paged']) ? $_GET['paged'] : 1; $number_per_page = 100; $start_count = ($current_page - 1) * $number_per_page; $limit = 'LIMIT ' . $start_count . ',' . $number_per_page; if ($type == 'total') { $where = ''; } elseif ($type == 'manual') { $where = "AND Response in('not-found','too-long')"; } else { $where = "AND MsgType = '{$type}'"; } if (isset($_GET['openid'])) { $where = "AND FromUserName = '******'openid']}'"; } $sql = "SELECT SQL_CALC_FOUND_ROWS * FROM {$wpdb->weixin_messages} WHERE 1=1 {$where} AND MsgType != 'manual' AND Event!= 'subscribe' AND Event != 'unsubscribe' ORDER BY CreateTime DESC " . $limit; $weixin_messages = $wpdb->get_results($sql); $total_count = $wpdb->get_var("SELECT FOUND_ROWS();"); ?> <?php if (!empty($succeed_msg)) { ?> <div class="updated"> <p><?php echo $succeed_msg; ?> </p> </div> <?php } ?> <ul class="subsubsub"> <?php foreach ($types as $key => $value) { ?> <li class="<?php echo $key; ?> "><a href="<?php echo admin_url('admin.php?page=' . $plugin_page . '&type=' . $key); ?> " <?php if ($type == $key) { ?> class="current"<?php } ?> ><?php echo $value; ?> </a> |</li> <?php } ?> <?php /* <li class="not-found"><a href="<?php echo admin_url('admin.php?page='.$plugin_page.'&Response=not-found')?>" <?php if($Response == 'need-manual') {?> class="current"<?php } ?>>需要回复</a></li>*/ ?> </ul> <table class="widefat" cellspacing="0"> <thead> <tr> <?php if (weixin_robot_get_setting('weixin_advanced_api') && strpos($wpdb->weixin_messages, 'weixin')) { ?> <th colspan="2">用户</th> <?php } else { ?> <th>用户</th> <?php } ?> <th style="min-width:200px;width:40%;">内容</th> <th>类型</th> <th>回复类型</th> <th>操作</th> </tr> </thead> <tbody> <?php $alternate = ''; foreach ($weixin_messages as $weixin_message) { $MsgType = $weixin_message->MsgType; $alternate = $alternate ? '' : 'alternate'; $weixin_openid = $weixin_message->FromUserName; ?> <tr id="<?php echo $weixin_message->id; ?> " class="<?php echo $alternate; ?> "> <?php if (weixin_robot_get_setting('weixin_advanced_api') && strpos($wpdb->weixin_messages, 'weixin')) { ?> <?php $weixin_user = weixin_robot_get_user($weixin_openid); ?> <?php if ($weixin_user['subscribe']) { ?> <td> <?php $weixin_user_avatar = ''; if (!empty($weixin_user['headimgurl'])) { $weixin_user_avatar = $weixin_user['headimgurl']; ?> <a href="<?php echo admin_url('admin.php?page=' . $plugin_page . '&openid=' . $weixin_openid); ?> "><img src="<?php echo $weixin_user_avatar; ?> " width="32" /></a> <?php } ?> </td> <td> <?php echo $weixin_user['nickname']; ?> (<?php if ($weixin_user['sex'] == 1) { echo '男'; } elseif ($weixin_user['sex'] == 2) { echo '女'; } else { echo "未知"; } ?> )<br /> <?php echo $weixin_user['country'] . ' ' . $weixin_user['province'] . ' ' . $weixin_user['city']; ?> <br /> </td> <?php } else { ?> <td colspan="2"> <span style="color:red;">*已经取消关注</span> </td> <?php } ?> <?php } else { ?> <td><?php echo $weixin_openid; ?> </td> <?php } ?> <td class="content"> <?php if ($MsgType == 'text') { echo $weixin_message->Content; } elseif ($MsgType == 'link') { echo '<a href="' . $weixin_message->Url . '" target="_blank">' . $weixin_message->Title . '</a>'; } elseif ($MsgType == 'image') { echo '<a href="' . $weixin_message->PicUrl . '" target="_blank" title="' . $weixin_message->MediaId . '"><img src="' . $weixin_message->PicUrl . '" alt="' . $weixin_message->MediaId . '" width="100px;"></a>'; if (isset($_GET['debug'])) { echo '<br />MediaId:' . $weixin_message->MediaId; } } elseif ($MsgType == 'location') { echo '<a href="http://ditu.google.cn/maps?q=' . urlencode($weixin_message->label) . '&ll=' . $weixin_message->Location_X . ',' . $weixin_message->Location_Y . '&source=embed" target="_blank">' . $weixin_message->label . '</a>'; } elseif ($MsgType == 'event') { echo '[' . $weixin_message->Event . '] ' . $weixin_message->EventKey; } elseif ($MsgType == 'voice') { if ($weixin_message->Recognition) { echo '语音识别成:'; echo $weixin_message->Recognition; } else { echo '未识别'; } if (isset($_GET['debug'])) { echo '<br />MediaId:' . $weixin_message->MediaId; } } else { echo $MsgType; echo '该类型的内容无法显示,请直接访问微信公众号后台进行操作!'; } if (is_numeric($weixin_message->Response)) { $weixin_reply_message = weixin_robot_get_message($weixin_message->Response); echo '<br /><span style="background-color:yellow; padding:2px; ">人工回复:' . $weixin_reply_message->Content . '</span>'; } ?> </td> <td><?php echo $types[$MsgType]; ?> <br /><?php echo date('Y-m-d H:i:s', $weixin_message->CreateTime + get_option('gmt_offset') * 3600); ?> </td> <td> <?php if (is_numeric($weixin_message->Response)) { echo '人工回复'; } elseif (isset($response_types[$weixin_message->Response])) { echo $response_types[$weixin_message->Response]; } ?> </td> <td class="action"> <?php if (weixin_robot_get_setting('weixin_advanced_api') && strpos($wpdb->weixin_messages, 'weixin') && current_time('timestamp') - $weixin_message->CreateTime < (48 + get_option('gmt_offset')) * 3600) { ?> <?php if (is_numeric($weixin_message->Response)) { ?> <span>已经回复</span> <?php } elseif ($weixin_user['subscribe']) { ?> <span class="reply"><a href="javascript:;" onclick="reply_to_weixin('<?php echo $weixin_openid; ?> ', '<?php echo $weixin_message->id; ?> ')">回复</a></span> <?php } ?> <?php } else { ?> <span class="delete"><a href="<?php echo admin_url('admin.php?page=weixin-robot-messages&delete&id=' . $weixin_message->id); ?> ">删除</a></span> <?php } ?> </td> </tr> <?php } ?> <?php if (weixin_robot_get_setting('weixin_advanced_api') && strpos($wpdb->weixin_messages, 'weixin')) { ?> <tr id="reply_form" style="display:none;" > <td colspan="2"> </td> <td colspan="4"> <form action="<?php echo admin_url('admin.php?page=' . $plugin_page); ?> " method="POST"> <p> <textarea name="content" id="content" rows="5" class="large-text code"></textarea> </p> <p> <select name="reply_type" id="reply_type" > <option value="text">文本回复</option> <option value="img">图文回复</option> </select> </p> <input type="hidden" name="weixin_openid" id="weixin_openid" value="" /> <input type="hidden" name="reply_id" id="reply_id" value="" /> <?php wp_nonce_field('weixin_robot', 'weixin_robot_send_user_nonce'); ?> <p><input type="submit" name="submit" id="submit" class="button button-primary" value="回复用户" style="float:right; margin-right:20px;"></p> </form> </td> </tr> <?php } ?> </tbody> </table> <?php wpjam_admin_pagenavi($total_count, $number_per_page); ?> <?php if (weixin_robot_get_setting('weixin_advanced_api') && strpos($wpdb->weixin_messages, 'weixin')) { ?> <script type="text/javascript"> function reply_to_weixin(weixin_openid, id){ jQuery('input#weixin_openid')[0].value = weixin_openid; jQuery('input#reply_id')[0].value = id; jQuery('tr#'+id).after(jQuery('#reply_form')); jQuery('tr#reply_form').show(); } jQuery(function(){ jQuery('form').submit(function( event ) { var reply_id = jQuery('input#reply_id')[0].value; var reply_type = jQuery('select#reply_type')[0].value; var weixin_openid = jQuery('input#weixin_openid')[0].value; var reply_content = jQuery('textarea#content')[0].value; if(jQuery('textarea#content')[0].value != ''){ jQuery.ajax({ type: 'post', url: '<?php echo admin_url('admin-ajax.php'); ?> ', data: { action: 'weixin_reply', weixin_openid: weixin_openid, reply_id: reply_id, reply_type: reply_type, content: reply_content, _ajax_nonce: '<?php echo wp_create_nonce('weixin_robot_ajax_nonce'); ?> ' }, success: function(html){ reply_content = jQuery('tr#'+reply_id+' td.content').html()+'<br /><span style="background-color:yellow; padding:2px; ">人工回复:'+reply_content+'</span>'; jQuery('tr#'+reply_id+' td.content').html(reply_content); jQuery('tr#'+reply_id+' td.action').html('已经回复'); jQuery('textarea#content')[0].value = ''; jQuery('tr#reply_form').hide(); } }); }else{ alert('回复的内容不能为空'); jQuery('textarea#content').focus(); } event.preventDefault(); }); }); </script> <?php wpjam_confim_delete_script(); ?> <?php } }