예제 #1
0
function initUser()
{
    $remoteTemp = WxUtil::getUserList();
    //echo print_r($remoteTemp);
    if (!empty($remoteTemp)) {
        $remoteUsers = $remoteTemp["data"];
        if (!empty($remoteUsers)) {
            global $db;
            $res = $db->query("SELECT openid FROM wx_user_info ");
            $rowList = $db->fetch_all($res);
            $user_info_list = array();
            // 每次最多查询100个用户
            $itemp = 0;
            $remoteOpenidArray = $remoteUsers["openid"];
            $openidArray = array();
            // 先过滤一遍
            foreach ($remoteOpenidArray as $openid) {
                if (!in_array(array("openid" => $openid), $rowList)) {
                    // 只取openid不在数据库中的
                    $openidArray[] = $openid;
                }
            }
            LogUtil::logs("微信总用户数: ====> " . count($openidArray), getLogFile("/business.log"));
            $saveTimes = 1;
            foreach ($openidArray as $openid) {
                if (!in_array(array("openid" => $openid), $rowList)) {
                    //echo $openid." is not in<br />";
                    // 组装一个信息
                    if ($itemp % 100 == 0) {
                        $user_to_query = array();
                    }
                    $toqueryuser = array("openid" => $openid, "lang" => "zh-CN");
                    $user_to_query[] = $toqueryuser;
                    //LogUtil::logs("itemp====> ".$itemp, getLogFile("/business.log"));
                    if (count($user_to_query) == 100 || $itemp == count($openidArray) - 1) {
                        // 每满100条,查询一次
                        LogUtil::logs("====> 第" . $saveTimes . "次发起微信查询", getLogFile("/business.log"));
                        $pdata = JsonUtil::getJsonStrFromArray(array("user_list" => $user_to_query));
                        $batchUserInfo = WxUtil::getBatchUserInfo($pdata);
                        if (empty($batchUserInfo)) {
                            // 先检查是否存在特殊字符
                        }
                        if (!empty($batchUserInfo)) {
                            $user_info_list = $batchUserInfo["user_info_list"];
                            transactionSave($user_info_list);
                            $countres = $db->query("SELECT count(1) as totalcount from wx_user_info a");
                            $countObj = $db->fetch($countres);
                            $nums = $countObj["totalcount"];
                            LogUtil::logs("已保存用户数 ====> " . $nums, getLogFile("/business.log"));
                        } else {
                            LogUtil::logs("====> 第" . $saveTimes . "次发起微信查询出错", getLogFile("/business.log"));
                            LogUtil::logs("查询的用户openid请求参数====> " . $pdata, getLogFile("/business.log"));
                            LogUtil::logs("查询的用户openid结果====>" . JsonUtil::getJsonStrFromArray($batchUserInfo), getLogFile("/business.log"));
                        }
                        $saveTimes++;
                    }
                    $itemp++;
                }
            }
            return "同步完成";
        }
    }
}