路由
Route::get('post-moment-import', [\App\Http\Controllers\IndexController::class, 'postMomentImport']);
Route::get('post-moment', [\App\Http\Controllers\IndexController::class, 'postMoment']);
Route::get('edit-moment/{moment}', [\App\Http\Controllers\IndexController::class, 'editMoment']);
Route::get('get-moments', [\App\Http\Controllers\IndexController::class, 'getMoments']);
Route::get('issue-order', [\App\Http\Controllers\IndexController::class, 'IssueOrder']);
Route::get('grab-single', [\App\Http\Controllers\IndexController::class, 'GrabSingle']);
Collection
<?php
namespace App\Http\Controllers;
use App\Helps\api\redisLbs;
use App\Models\Moment;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Intervention\Image\Facades\Image;
class IndexController extends Controller
{
public function postMoment (Request $request, Moment $moment)
{
if (!$request->longitude){
return response()->json(['status' => 0, 'msg' => '缺少經度']);
}
if (!$request->latitude){
return response()->json(['status' => 0, 'msg' => '缺少緯度']);
}
$moment->name = $request->name;
$moment->longitude = $request->input('longitude');
$moment->latitude = $request->input('latitude');
if ($moment->save()){
$lbs = new redisLbs();
$result = $lbs->geoAdd($moment->id, $request->input('longitude'), $request->input('latitude'));
if ($result){
return response()->json(['status' => 1, 'msg' => '新增動態成功']);
}
return response()->json(['status' => 0, 'msg' => '動態新增成功,redis同步失敗']);
}
return response()->json(['status' => 0, 'msg' => '動態新增失敗']);
}
public function postMomentImport ()
{
$json = '[{"name":"xxx","poi":"118.96,42.2737"}]';
$json_arr = json_decode($json, JSON_UNESCAPED_UNICODE);
foreach (collect($json_arr)->chunk(300) ?? [] as $chunk)
{
foreach ($chunk ?? [] as $arr)
{
list($longitude, $latitude) = explode(',', $arr['poi']);
$moment = Moment::create([
'name' => $arr['name'],
'longitude' => $longitude,
'latitude' => $latitude,
]);
}
}
return response()->json(['status' => 1, 'msg' => '更新動態成功']);
}
public function editMoment (Request $request, Moment $moment)
{
if (!$request->longitude){
return response()->json(['status' => 0, 'msg' => '缺少經度']);
}
if (!$request->latitude){
return response()->json(['status' => 0, 'msg' => '缺少緯度']);
}
$moment->name = $request->name;
$moment->longitude = $request->input('longitude');
$moment->latitude = $request->input('latitude');
if ($moment->save()){
$lbs = new redisLbs();
$result = $lbs->geoAdd($moment->id, $request->input('longitude'), $request->input('latitude'));
if ($result){
return response()->json(['status' => 1, 'msg' => '更新動態成功']);
}
return response()->json(['status' => 0, 'msg' => '動態更新成功,redis同步失敗']);
}
return response()->json(['status' => 0, 'msg' => '動態更新失敗']);
}
public function getMoments (Request $request)
{
$lbs = new redisLbs();
$result = $lbs->geoNearFind($request->longitude,$request->latitude,1000);
$mid = $dist=[];
for($i=0;$i<count($result);$i++){
$dist[$i]['mid'] = $mid[$i] = $result[$i][0];
$dist[$i]['distance'] = round($result[$i][1], 2);
}
$moments = Moment::whereIn('id', $mid)->orderBy('created_at', 'desc')->select('id', 'name')->get()->toArray();
foreach ($dist ?? [] as $item)
{
foreach ($moments ?? [] as $k => $moment)
{
if ($item['mid'] == $moment['id'])
{
$moments[$k]['distance'] = $item['distance'];
}
}
}
$datas = collect($moments)->sortBy('distance')->values()->all();
return response()->json(['status' => 1, 'msg' => '成功獲取附近動態', 'data' => $datas]);
}
public function IssueOrder ()
{
$orderNum = 2000;
for ($i=1; $i <= $orderNum; $i++) {
$grab_orders = "grab_orders".$i;
Redis::lpush($grab_orders, 1);
}
return response()->json(['status' => 200, 'msg' => '釋出成功!']);
}
public function GrabSingle (Request $request)
{
$userId = rand(1, 9999);
$orderId = rand(1, 2000);
$goods = "grab_orders".$orderId;
$robSuccessUser = "success_user".$orderId;
$result = Redis::sismember($robSuccessUser,$userId);
if ($result) {
Log::info("使用者:{$userId},已經搶過了,訂單:{$goods}");
return response()->json(['status' => 20000, 'data' => '', 'msg' => '您已經搶購過了']);
}
$count = Redis::lpop($goods);
if (!$count) {
Log::info("使用者:{$userId},已經搶光了哦,訂單:{$goods},手慢了");
return response()->json(['status' => 20001, 'data' => '', 'msg' => '已經搶光了哦']);
}
$success = Redis::sadd($robSuccessUser, $userId);
if(!$success){
Redis::lpush($goods, 1);
Log::info("使用者:{$userId},已經搶購過了,訂單:{$goods},無需在槍");
return response()->json(['status' => 20002, 'data' => '', 'msg' => '已經搶購過了']);
}
Log::info("使用者:{$userId},搶單成功,訂單:{$goods}");
return response()->json(['status' => 200, 'data' => '', 'msg' => '搶單成功']);
}
}
RedisLbs
<?php
namespace App\Helps\api;
class redisLbs
{
private $redis;
public function __construct($config = array())
{
$host = isset($config['host']) ? $config['host'] : '127.0.0.1';
$port = isset($config['port']) ? $config['port'] : '6379';
$redis = new \Redis();
$redis->connect($host, $port);
if (env('APP_ENV') != 'local'){
$redis->auth('myRedis');
$redis ->set( "root" , "myRedis");
}
$this->setRedis($redis);
}
public function getRedis()
{
return $this->redis;
}
public function setRedis($redis)
{
$this->redis = $redis;
}
public function geoAdd($uin, $lon, $lat)
{
$redis = $this->getRedis();
$redis->geoAdd('moments', $lon, $lat, $uin);
return true;
}
public function geoNearFind($longitude , $latitude , $maxDistance = 0, $unit = 'm')
{
$redis = $this->getRedis();
$options = ['WITHDIST','ASC','WITHCOORD'];
$list = $redis->geoRadius('moments', $longitude, $latitude , $maxDistance, $unit, $options);
return $list;
}
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結