我已将访问令牌保存到DB,并循环每个用户以获取邮件。它对第一个用户有效,对于第二个用户,accesstoken已更新,但会提取第一个用户的邮件。我正在使用LaravelGmail插件
如果我与单个用户访问,则可以正常工作
public function sync()
{
$stafflist = Staff::all();
if (!empty($stafflist)) {
foreach ($stafflist as $key => $value) {
$this->service = new LaravelGmail;
$set_token = $this->setactiveToken($value->staffToken, $value->staffEmail);
try {
$messages = $this->service::message()->take(5)->all();
if (!empty($messages)) {
foreach ($messages as $loop) {
$detmsg = $this->service::message()->get($loop->getId());
$dettoken = $this->service::getAccessToken();
$maildate = $detmsg->getInternalDate();
$sender = $detmsg->getHeader('From');
preg_match('/<(.*)>/', $sender, $matches);
$sender_mail = isset($matches[1]) ? $matches[1] : $sender;
$result = Smailer::firstOrCreate(['mailReceiver' => $value->staffUID, 'mailThreadID' => $loop->getId()],
['mailSender' => $sender_mail, 'mailSubject' => $detmsg->getSubject(),
'mailContent' => $detmsg->getPlainTextBody(), 'mailHasAttachment' => $detmsg->hasAttachments(), 'mailReceivedDt' => $maildate]);
}
}
unset($this->service);
} catch (Google_Service_Exception $e) {
}
}
}
return redirect('control/mails');
}
private function setactiveToken($stoken = '', $email = '')
{
$this->service::setAccessToken($stoken);
if ($this->service::isAccessTokenExpired()) {
$this->service::fetchAccessTokenWithRefreshToken($this->service::getRefreshToken());
$token = $this->service::getAccessToken();
$token['email'] = $email;
$resp = Staff::where('staffEmail', $email)
->update(['staffToken' => json_encode($token)]);
}
}