love_php/app/Jobs/SyncScore.php

124 lines
3.7 KiB
PHP
Raw Normal View History

2026-04-02 09:20:51 +08:00
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\Models\User;
use App\Utils\Http;
use App\Utils\Messenger;
use Illuminate\Container\Container;
use App\Repositories\Eloquent\SmsRepository as Sms;
class SyncScore implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* 任务最大尝试次数。
*
* @var int
*/
public $tries = 5;
protected $user_id;
protected $score;
protected $type;
protected $message;
protected $other_user_id;
protected $status;
protected $mobile = '15872844805';
/**
*
* Create a new job instance.
*
* @return void
*/
public function __construct($array)
{
$this->user_id = $array['user_id'];
$this->other_user_id = $array['other_user_id'];
$this->score = $array['score'];
$this->type = $array['type'];
$this->message = $array['message'];
$this->status = isset($array['status']) ? $array['status'] : 0;
$container = new Container();
$this->sms = new Sms($container);
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
try {
$uid = $this->getAccountUserID($this->user_id);
$other_uid = $this->getAccountUserID($this->other_user_id);
if (empty($uid)) {
return;
}
$data = [
'mp'=>'LOVE',
'score'=>$this->score,
'type'=>$this->type,
'message'=>$this->message,
'other_uid'=>$other_uid,
'status'=>$this->status,
];
$url = config('app.account_url').'/api/change/user/'.$uid.'/score';
$result = json_decode(Http::http($url, $data, 'POST'),true);
//\Log::info('syncScore success');
} catch (\Exception $e) {
//\Log::info('syncScore failure');
$message = '用户id '.$this->user_id.'同步积分失败';
$this->sms->sentMessage($this->mobile, $message);
}
}
public function getAccountUserID($user_id)
{
$user = User::with('wechat')->where('id', $user_id)->first();
if (empty($user)) {
return $uid = 0;
}
if ($user->uid) {
$uid = $user->uid;
}else{
if (empty($user->wechat)) {
return;
}
$url = config('app.account_url').'/api/user';
$data = [
'mobile' => $user->mobile,
'name' => $user->name,
'nickname' => $user->wechat->nickname,
'gender' => $user->wechat->gender,
'city' => $user->wechat->city,
'province' => $user->wechat->province,
'country' => $user->wechat->country,
'unionid' => $user->wechat->unionid,
'avatar' => $user->wechat->avatar2?$user->wechat->avatar2:$user->wechat->avatar,
];
try {
$account_user = json_decode(Http::http($url, $data, 'POST'), true);
} catch (Exception $e) {
$message = $e->getMessage();
Messenger::sendSMS('15872844805', $message);
}
if (isset($account_user['id'])) {
$uid = $account_user['id'];
$user->uid = $uid;
$user->save();
}else{
$uid = 0;
}
}
return $uid;
}
}