/** * 将用户信息同步到数据库 */ public function synchronizedUserInfo($userInfo, $Event) { global $db; $openid = $userInfo["openid"]; $subscribe = $userInfo["subscribe"]; $currtime = DateUtil::getCurrentTime(); $sql = ""; if ($Event == "subscribe") { LogUtil::logs("TypeEvent synchronizedUserInfo insert=====> ", getLogFile("/business.log")); $nickname = $userInfo["nickname"]; $nickname = base64_encode($nickname); //$nickname = str_replace("🌻", "*", $nickname); $sex = $userInfo["sex"]; $headimgurl = $userInfo["headimgurl"]; $sql = $sql . ", nickname='{$nickname}', sex='{$sex}'"; LogUtil::logs("TypeEvent.php synchronizedUserInfo :nickname ====>" . $nickname, getLogFile('/business.log')); // 关注的情况有关注和重新关注,所以使用on duplicate的方法 $sql = "INSERT INTO `wx_user_info` (openid, nickname, sex, subscribe, subscribe_time, headimgurl) \r\n\t\t\tVALUES ('{$openid}', '{$nickname}', '{$sex}', '{$subscribe}', '{$currtime}', '{$headimgurl}') \r\n\t\t\tON DUPLICATE KEY UPDATE subscribe='{$subscribe}', nickname='{$nickname}', subscribe_time='{$currtime}', headimgurl='{$headimgurl}'"; } else { // 取消关注 LogUtil::logs("TypeEvent synchronizedUserInfo update=====> ", getLogFile("/business.log")); $sql = "update `wx_user_info` set subscribe_time='{$currtime}', subscribe='{$subscribe}' where openid = '{$openid}' "; } $db->exec($sql); }
/** * save token to db */ public static function initWxTokenToDB() { global $db; $url = wxTokenUrl() . "&appid=" . appid() . "&secret=" . secret(); // 请求微信token //LogUtil::logs("initWxTokenToDB tokenurl====>".$url, getLogFile('/business.log')); $arr = RequestUtil::httpGet($url); $newToken = $arr['access_token']; //LogUtil::logs("initWxTokenToDB newToken====>".$newToken, getLogFile('/business.log')); // 请求微信ticket $newTicket = self::initTicket($newToken); $updatetime = DateUtil::getCurrentTime(); // 加锁文件 if (file_exists($lockfile)) { //LogUtil::logs("initWxTokenToDB ====> file in writing, only can read\r\n", getLogFile('/business.log')); exit; } // save or update; $db->exec("INSERT INTO wx_token(id, token, updatetime, ticket) \r\n\t\tvalues(1, '{$newToken}','{$updatetime}','{$newTicket}') \r\n\t\tON DUPLICATE KEY UPDATE token='{$newToken}', updatetime='{$updatetime}', ticket='{$newTicket}'"); // 关闭锁文件 fclose($lockTemp); // 删除锁文件 unlink($lockfile); return $newToken; }
function addone() { // $_SESSION['cn_sysadmin']['user_id'] = $user_id; // $_SESSION['cn_sysadmin']['role_name'] = $role['name']; // $_SESSION['cn_sysadmin']['role_id'] = $role['id']; global $db; $createby = $_SESSION['cn_sysadmin']['user_id']; $createtime = DateUtil::getCurrentTime(); $groupname = empty($_POST["groupname"]) ? '' : $_POST["groupname"]; if ($groupname != "") { $backup = empty($_POST["backup"]) ? '' : $_POST["backup"]; $sql = "insert into wx_group_info (createby, createtime, groupname, backup) values ('{$createby}', '{$createtime}', '{$groupname}', '{$backup}')"; $db->exec($sql); } // 添加后,重新再查一遍 showlist(); }
function reply() { // msgid 是回复的目标消息id 对应表 的 replyid $msgid = $_POST["msgid"]; if (empty($msgid)) { return; } $content = $_POST["content"]; if (empty($content)) { return; } $openid = $_POST["createby"]; if (empty($openid)) { return; } // 回复消息要做2件事 // 1 调微信接口 $paramContent = array(); $contentTemp = array("content" => "管理员回复:\r\n" . $content); $paramContent = array("msgtype" => "text", "text" => $contentTemp); $data = JsonUtil::getJsonStrFromArray(array_merge($paramContent, array("touser" => $openid))); //LogUtil::logs("queryGroupUserAndReplyMsg data ====>".$data, getLogFile("/business.log")); $tp = new TypeParent(); $response = $tp->sendMsgByService($data); if ($response["errcode"] == 0) { // 回复成功 // 2 保存回复的消息 $msgtype = "1"; // 消息类型: 0表示用户发送 1表示管理员回复 2表示管理员群发消息 3 自动回复 4聊天室信息 $status = "0"; // 消息状态 :0: 消息发送成功 1: 发送中 2 发送失败, 保存成功 3 发送成功, 保存失败 4 表示这条信息是用户送的,并且已经得到回复 $createtime = DateUtil::getCurrentTime(); $createby = $_SESSION['cn_sysadmin']['user_id']; // 消息类型: 0表示用户发送 1表示管理员回复 2表示管理员群发消息 3 自动回复 4聊天室信息 DBUtil::saveMsg($createby, $content, $createtime, $msgid, $msgtype, $status); // 原来设计是将状态改为4用来标识已回复,现在能查到回复内容,就取消这个方案了 // $sql = "update wx_user_msg set status = '4' where id ='$msgid'"; // DBUtil::updateMsg($sql); } showlist(); }
/** * 查到用户组里面的所有组员,再向其发送消息 */ function queryGroupUserAndReplyMsg($userGroupId, $postData) { global $db; // 发消息的人自己 $userSelfOpenid = $postData["FromUserName"]; $content = $postData["Content"]; $createtime = DateUtil::getCurrentTime(); $mediaid = $postData["MediaId"]; // 用户发送的信息,要保存到数据库 // 消息类型: 0表示用户发送 1表示管理员回复 2表示管理员群发消息 3 自动回复 4聊天室信息 DBUtil::saveMsg($userSelfOpenid, "图片消息", $createtime, "", "4", "0"); // 查询所有的组员 $arr = array(); // TODO 这个$res可以缓存到文件中 $res = $db->query("SELECT * FROM wx_group_user where groupid = '{$userGroupId}' and userisin = '0' "); $row = $db->fetch_all($res); foreach ($row as $val) { // 循环每个人推送一条消息 $openid = $val['openid']; // 从组中除去发信息者自己 if ($userSelfOpenid != $openid) { // 拼接 /*{ "touser":"******", "msgtype":"image", "image": { "media_id":"MEDIA_ID" } }*/ $paramContent = array("touser" => $openid, "msgtype" => "image", "image" => array("media_id" => $mediaid)); $data = JsonUtil::getJsonStrFromArray($paramContent); LogUtil::logs("queryGroupUserAndReplyMsg data ====>" . $data, getLogFile("/business.log")); parent::sendMsgByService($data); } } return getSuccessStr(); }
function transactionSave($user_info_list) { $errorOpenids = ""; $successcount = 0; $failcount = 0; LogUtil::logs("要保存到数据库的用户信息数量====> " . count($user_info_list), getLogFile("/business.log")); //echo print_r($user_info_list); global $db; if (!empty($user_info_list)) { mysql_query('START TRANSACTION'); foreach ($user_info_list as $userinfo) { /*if(($successcount + $failcount) % 5000 == 0) { mysql_query('START TRANSACTION'); }*/ $openid = $userinfo["openid"]; if (!empty($openid)) { //echo print_r($userinfo); $nickname = base64_encode($userinfo["nickname"]); //$nickname = $userinfo["nickname"]; // 默认是/0 表示640*640的尺寸 有0、46、64、96、132 $headimgurl = $userinfo["headimgurl"]; $sex = $userinfo["sex"]; $subscribe = $userinfo["subscribe"]; $currtime = DateUtil::getCurrentTime(); $sql = "INSERT INTO `wx_user_info` (openid, nickname, sex, subscribe, subscribe_time, headimgurl) \r\n\t\t\t\t\tVALUES ('{$openid}', '{$nickname}', '{$sex}', '{$subscribe}', '{$currtime}', '{$headimgurl}')"; $execres = $db->exec($sql); if (!$execres) { $errorOpenids .= $openid . "; "; $failcount = $failcount + 1; } else { $successcount = $successcount + 1; } } /*if(($successcount + $failcount) % 5000 == 0) { mysql_query('COMMIT'); }*/ } // mysql_query('ROLLBACK '); mysql_query('COMMIT'); if (!empty($errorOpenids)) { LogUtil::logs("====>同步错误用户名单:" . $errorOpenids, getLogFile("/db.log")); } LogUtil::logs("事务提交:成功" . $successcount . "条,失败" . $failcount . "条", getLogFile("/business.log")); } }
/** * 普通消息公共调用 * MsgType : text image voice video 等 */ function autoReplay($postData) { $openid = $postData["FromUserName"]; $content = $postData["Content"]; $createtime = DateUtil::getCurrentTime(); $msgData = array(); if ($postData['MsgType'] == 'text' && !empty($content)) { // 判断是否修改信息 // $strrule = "/^更新信息+.*/"; $strrule = "/^备注#.*/"; LogUtil::logs("autoReplay content ====>" . $content, getLogFile("/business.log")); if (preg_match($strrule, $content)) { LogUtil::logs("autoReplay content ====>" . $content, getLogFile("/business.log")); //$arr = explode("+", $content); $arr = explode("#", $content); $returnmsg = ""; $backup = $arr[1]; if (!preg_match("/^.{0,30}\$/", $backup)) { $returnmsg .= " 亲备注信息不要超过30个字符哦/:,@-D"; $paramsData['Content'] = $returnmsg; $paramsData['MsgType'] = 'text'; return parent::packageData($postData, $paramsData); } /*$mobile = $arr[2]; if(!preg_match("/^1[3|4|5|7|8][0-9]\\d{8}$/", $mobile)){ $returnmsg .= " 手机号格式不正确"; }*/ if (empty($returnmsg)) { $returnmsg = "信息更新申请成功"; } global $db; //$db -> exec("update wx_user_info set localnickname='$localnickname', mobile='$mobile' where openid='$openid'"); $db->exec("update wx_user_info set backup='{$backup}', mobile='{$mobile}' where openid='{$openid}'"); // 用户申请更新信息 $paramsData['Content'] = $returnmsg; $paramsData['MsgType'] = 'text'; return parent::packageData($postData, $paramsData); } else { // 不更新信息的情况下,只要保存到后台即可 DBUtil::saveMsg($openid, $content, $createtime, "", "5", "0"); return getSuccessStr(); } // 用户发送的信息,要保存到数据库// 消息类型: 0表示用户发送 1表示管理员回复 2表示管理员群发消息 3 自动回复 4聊天室信息 5用户私聊管理员 } }