This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
namespace app\common\controller;
|
||||
|
||||
use app\common\controller\BaseController;
|
||||
use app\common\model\User;
|
||||
use app\common\model\UserGroupMembers;
|
||||
use app\common\traits\crud\CrudControllerTrait;
|
||||
use app\common\service\WordpressVisitorService;
|
||||
use support\Request;
|
||||
|
||||
class WordpressVisitorController extends BaseController
|
||||
{
|
||||
use CrudControllerTrait;
|
||||
|
||||
protected $noNeedLogin = ['whatsApp'];
|
||||
|
||||
protected $noNeedHeaderData = ['whatsApp'];
|
||||
|
||||
protected $service;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->service = new WordpressVisitorService();
|
||||
}
|
||||
|
||||
public function whatsApp(Request $request){
|
||||
try {
|
||||
$url = $this->service->whatsApp($request);
|
||||
return $this->success(['url' => $url]);
|
||||
} catch (\Throwable $e) {
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 列表页数据格式化
|
||||
*
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
// protected function indexFormat($data)
|
||||
// {
|
||||
// if (isset($data['created_at']))
|
||||
// $data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
|
||||
// if (isset($data['updated_at']))
|
||||
// $data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']);
|
||||
// return $data;
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\logic;
|
||||
|
||||
use app\common\traits\crud\CrudLogicTrait;
|
||||
use app\common\model\WordpressVisitor;
|
||||
|
||||
class WordpressVisitorLogic
|
||||
{
|
||||
use CrudLogicTrait;
|
||||
|
||||
/**
|
||||
* 指定当前要操作的模型
|
||||
*/
|
||||
protected string $modelClass = WordpressVisitor::class;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use support\Model;
|
||||
|
||||
class WordpressVisitor extends Model
|
||||
{
|
||||
/**
|
||||
* The connection name for the model.
|
||||
*
|
||||
* @var string|null
|
||||
*/
|
||||
protected $connection = 'mysql';
|
||||
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $table = 'wordpress_visitor';
|
||||
|
||||
/**
|
||||
* The primary key associated with the table.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $primaryKey = 'id';
|
||||
|
||||
/**
|
||||
* Indicates if the model should be timestamped.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $timestamps = true;
|
||||
|
||||
|
||||
protected $dateFormat = 'U';
|
||||
|
||||
/**
|
||||
* 允许自动填充模型字段
|
||||
*/
|
||||
protected $fillable = ['id', 'user_agent', 'accept_language', 'md5_unique', 'user_id', 'created_at', 'updated_at'];
|
||||
|
||||
/**
|
||||
* 不允许自动填充模型字段
|
||||
*/
|
||||
protected $guarded = [];
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\service;
|
||||
|
||||
use app\common\model\User;
|
||||
use app\common\model\UserGroupMembers;
|
||||
use app\common\validate\WordpressVisitorValidate;
|
||||
use app\common\traits\crud\CrudServiceTrait;
|
||||
use app\common\model\WordpressVisitor;
|
||||
use support\Redis;
|
||||
use support\Request;
|
||||
|
||||
class WordpressVisitorService
|
||||
{
|
||||
|
||||
use CrudServiceTrait;
|
||||
|
||||
/**
|
||||
* 指定当前要操作的模型
|
||||
*/
|
||||
protected string $modelClass = WordpressVisitor::class;
|
||||
|
||||
/**
|
||||
* 验证器
|
||||
*/
|
||||
protected $validator = WordpressVisitorValidate::class;
|
||||
|
||||
/**
|
||||
* 获取验证器
|
||||
*/
|
||||
public function getValidator()
|
||||
{
|
||||
return new $this->validator;
|
||||
}
|
||||
|
||||
public function whatsApp(Request $request){
|
||||
// $userAgent = $_SERVER['HTTP_USER_AGENT'];
|
||||
// $acceptLanguage = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
|
||||
|
||||
$userAgent = $request->header('User-Agent','');
|
||||
$acceptLanguage = $request->header('Accept-Language','');
|
||||
$referer = $request->header('referer');
|
||||
$md5Hash = md5($userAgent . '-' . $acceptLanguage);
|
||||
$visitor = WordpressVisitor::where('md5_unique', $md5Hash)->first();
|
||||
if (!empty($visitor)) {
|
||||
$visitor->updated_at = time();
|
||||
$visitor->save();
|
||||
$user = User::where('id',$visitor['user_id'])->first();
|
||||
//return $user['phone'];
|
||||
return $this->url($user['phone'], $referer);
|
||||
}
|
||||
|
||||
$lastId = Redis::get('wp_whatsApp_user');
|
||||
if($lastId){
|
||||
$last = WordpressVisitor::orderBy('id','desc')->first();
|
||||
$lastId = $last['id']??0;
|
||||
}
|
||||
|
||||
$userIds = User::where('role',1)
|
||||
->where('whatsapp','!=','')
|
||||
->orderBy('id','asc')
|
||||
->get();
|
||||
$userIds = array_column($userIds, 'id');
|
||||
$userIds = array_values($userIds);
|
||||
$id = 0;
|
||||
foreach ($userIds as $userId) {
|
||||
if($userId < $lastId){
|
||||
$id = $userId;
|
||||
}
|
||||
}
|
||||
if(empty($id)){
|
||||
$id = array_shift($userIds);
|
||||
}
|
||||
Redis::set('wp_whatsApp_user',$id);
|
||||
|
||||
$data = [
|
||||
'user_agent' => $userAgent,
|
||||
'accept_language' => $acceptLanguage,
|
||||
'md5_unique' => $md5Hash,
|
||||
'user_id' => $id,
|
||||
];
|
||||
WordpressVisitor::create($data);
|
||||
$user = User::where('id',$id)->first();
|
||||
//return $user['phone'];
|
||||
return $this->url($user['phone'], $referer);
|
||||
}
|
||||
|
||||
public function url($phone,$referer){
|
||||
$url = "https://wa.me/" . $phone;
|
||||
if(!empty($referer)){
|
||||
$url = $url . "?text=来自%20" . $referer;
|
||||
}
|
||||
return $url;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\validate;
|
||||
|
||||
use think\Validate;
|
||||
|
||||
class WordpressVisitorValidate extends Validate
|
||||
{
|
||||
// 规则
|
||||
protected $rule = [
|
||||
|
||||
];
|
||||
|
||||
// 提示
|
||||
protected $message = [
|
||||
|
||||
];
|
||||
|
||||
// 场景
|
||||
protected $scene = [
|
||||
'store' => [
|
||||
|
||||
],
|
||||
|
||||
'update' => [
|
||||
|
||||
]
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user