diff --git a/app/common/controller/WordpressVisitorController.php b/app/common/controller/WordpressVisitorController.php new file mode 100644 index 0000000..33c78ef --- /dev/null +++ b/app/common/controller/WordpressVisitorController.php @@ -0,0 +1,49 @@ +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; + // } +} \ No newline at end of file diff --git a/app/common/logic/WordpressVisitorLogic.php b/app/common/logic/WordpressVisitorLogic.php new file mode 100644 index 0000000..ec67253 --- /dev/null +++ b/app/common/logic/WordpressVisitorLogic.php @@ -0,0 +1,17 @@ +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; + } +} \ No newline at end of file diff --git a/app/common/validate/WordpressVisitorValidate.php b/app/common/validate/WordpressVisitorValidate.php new file mode 100644 index 0000000..b9bf4b0 --- /dev/null +++ b/app/common/validate/WordpressVisitorValidate.php @@ -0,0 +1,29 @@ + [ + + ], + + 'update' => [ + + ] + ]; +} \ No newline at end of file diff --git a/config/route.php b/config/route.php index b9337f4..d724040 100644 --- a/config/route.php +++ b/config/route.php @@ -75,4 +75,9 @@ Route::group('/common', function () { Route::any('/info', [app\common\controller\UserController::class, 'show']); Route::any('/edit', [app\common\controller\UserController::class, 'edit']); }); + + //分配whatsApp + Route::group('/wordpress', function () { + Route::any('/whatsApp', [app\common\controller\WordpressVisitorController::class, 'whatsApp']); + }); }); \ No newline at end of file