function wx_updateUser($uids) { @set_time_limit(0); if (!getglobal('setting/CorpID') || !getglobal('setting/CorpSecret')) { return; } $uids = (array) $uids; $wx = new qyWechat(array('appid' => getglobal('setting/CorpID'), 'appsecret' => getglobal('setting/CorpSecret'), 'agentid' => 0)); $ret = 0; $syngids = array(); if ($syngid = getglobal('setting/synorgid')) { //设置的需要同步的部门 $syngids = getOrgidTree($syngid); } foreach ($uids as $uid) { if (!($user = C::t('user')->fetch($uid))) { continue; } $worgids = array(); if ($orgids = C::t('organization_user')->fetch_orgids_by_uid($uid)) { if ($syngids) { $orgids = array_intersect($orgids, $syngids); } if ($orgids) { foreach (C::t('organization')->fetch_all($orgids) as $value) { if ($value['worgid']) { $worgids[] = $value['worgid']; } else { if ($worgid = C::t('organization')->wx_update($value['orgid'])) { $worgids[] = $worgid; } } } } } if (!$worgids) { //用户不在机构和部门中,微信中应该禁用此用户 $data = array("userid" => "dzz-" . $user['uid'], "enable" => 0, "department" => 1); if ($wx->updateUser($data)) { $ret += 1; } else { $message = 'deleteUser:errCode:' . $wx->errCode . ';errMsg:' . $wx->errMsg; runlog('wxlog', $message); } } else { $profile = C::t('user_profile1')->fetch_all($user['uid']); if ($wxuser = $wx->getUserInfo('dzz-' . $user['uid'])) { //更新用户信息 $data = array("userid" => "dzz-" . $user['uid'], "name" => $user['username'], "enable" => 1, "email" => $user['email'], "enable" => $user['status'] ? 0 : 1); if (array_diff($wxuser['department'], $worgids)) { $data['department'] = $worgids; } if ($user['phone'] && $user['phone'] != $wxuser['mobile']) { $data['mobile'] = $user['phone']; } if ($user['weixinid'] && $wxuser['wechat_status'] == 4) { $data['weixinid'] = $user['weixinid']; } if ($profile['telephone'] && $profile['telephone'] != $wxuser['tel']) { $data['tel'] = $profile['telephone']; } if ($profile['gender'] && $profile['gender'] - 1 != $wxuser['gender']) { $data['gender'] = $profile['gender'] - 1; } if ($wx->updateUser($data)) { $ret += 1; } else { $message = 'updateUser:errCode:' . $wx->errCode . ';errMsg:' . $wx->errMsg; runlog('wxlog', $message); } $setarr = array('wechat_status' => $wxuser['status']); $setarr['weixinid'] = empty($wxuser['weixinid']) ? $user['weixinid'] : $wxuser['weixinid']; $setarr['phone'] = empty($user['phone']) ? $wxuser['phone'] : $user['phone']; $setarr['wechat_userid'] = 'dzz-' . $user['uid']; C::t('user')->update($user['uid'], $setarr); } else { //创建用户信息 $data = array("userid" => "dzz-" . $user['uid'], "name" => $user['username'], "department" => $worgids, "email" => $user['email'], "weixinid" => $user['wechat'], "enable" => $user['status'] ? 0 : 1); if ($user['phone']) { $data['mobile'] = $user['phone']; } if ($profile['telephone']) { $data['tel'] = $profile['telephone']; } if ($profile['gender']) { $data['gender'] = $profile['gender'] - 1; } if ($wx->createUser($data)) { C::t('user')->update($user['uid'], array('wechat_userid' => 'dzz-' . $user['uid'])); $ret += 1; } else { $message = 'createUser:errCode:' . $wx->errCode . ';errMsg:' . $wx->errMsg; runlog('wxlog', $message); } } } } return $ret; }
$worgids[] = $worgid; } } } } } if (!$worgids) { $data = array("userid" => "dzz-" . $user['uid'], "enable" => 0, "department" => 1); if ($wx->updateUser($data)) { exit(json_encode(array('msg' => 'continue', 'start' => $user['uid'], 'message' => $user['username'] . '<span class="info">不在同步范围,已禁用</span>'))); } else { exit(json_encode(array('msg' => 'continue', 'start' => $user['uid'], 'message' => $user['username'] . ' <span class="info">不在同步范围,忽略</span>'))); } } $profile = C::t('user_profile1')->fetch_all($user['uid']); if ($wxuser = $wx->getUserInfo('dzz-' . $user['uid'])) { //更新用户信息 $data = array("userid" => 'dzz-' . $user['uid'], "name" => $user['username'], "email" => $user['email'], "enable" => $user['status'] ? 0 : 1); if (array_diff($wxuser['department'], $worgids)) { $data['department'] = $worgids; } if ($user['phone'] && $user['phone'] != $wxuser['mobile']) { $data['mobile'] = $user['phone']; } if ($user['weixinid'] && $wxuser['wechat_status'] == 4) { $data['weixinid'] = $user['weixinid']; } if ($profile['telephone'] && $profile['telephone'] != $wxuser['tel']) { $data['tel'] = $profile['telephone']; } if ($profile['gender'] && $profile['gender'] - 1 != $wxuser['gender']) {