Commit d783e8bf by xj

附近的站点

虚拟卡
parent 8641d689
<?php
namespace App\Http\Controllers\V1\Wx;
use App\Http\Controllers\Controller;
use App\Model\UnrealCardModel;
use Illuminate\Http\Request;
use Validator;
class UnrealCardsController extends Controller
{
public function index()
{
$userId = \Auth::id();
$unrealCards = UnrealCardModel::where('user_id', $userId)->paginate(15);
return $this->response->array($this->getResponseCode() + ['data' => $unrealCards]);
}
public function store(Request $request)
{
$validator = Validator::make($request->all(), [
'mac' => 'required',
]);
if ($validator->fails()) {
abort(400, $validator->errors()->first());
}
// 验证卡号有效性
$mac = strtoupper($request->get('mac'));
if (strlen($mac) != 10) {
$this->resultCode = 'FAIL';
return $this->response->array($this->getResponseCode() + ['data' => ['message' => '卡号不能低于10位']]);
}
$temp = substr($mac, 0, 2);
for ($i = 2; $i < 8; $i += 2) {
$temp = dechex(hexdec($temp) ^ hexdec(substr($mac, $i, 2)));
}
if (strtoupper($temp) != substr($mac, -2)) {
$this->resultCode = 'FAIL';
return $this->response->array($this->getResponseCode() + ['data' => ['message' => '卡号错误']]);
}
$unrealCard = UnrealCardModel::where('mac', $mac)->first();
if ($unrealCard) {
$this->resultCode = 'FAIL';
return $this->response->array($this->getResponseCode() + ['data' => ['message' => '虚拟卡已经被使用']]);
}
// 保存
$unrealCard = new UnrealCardModel();
$unrealCard->user_id = \Auth::id();
$unrealCard->mac = $mac;
$unrealCard->save();
return $this->response->array($this->getResponseCode() + ['data' => ['message' => '绑定成功']]);
}
public function destroy($id)
{
$unrealCard = UnrealCardModel::find($id);
if (!$unrealCard) {
$this->resultCode = 'FAIL';
return $this->response->array($this->getResponseCode() + ['data' => ['message' => 'Not Found']]);
}
// 判断用户是否有权限删除
if (!$unrealCard->user_id || $unrealCard->user_id != \Auth::id()) {
$this->resultCode = 'FAIL';
return $this->response->array($this->getResponseCode() + ['data' => ['message' => '无效数据']]);
}
$unrealCard->delete();
return $this->response->array($this->getResponseCode() + ['data' => ['message' => '删除成功']]);
}
}
......@@ -25,7 +25,7 @@ class CorsMiddleware
$headers = [
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Methods' => 'POST, GET, OPTIONS',
'Access-Control-Allow-Methods' => 'POST, GET, OPTIONS, DELETE',
'Access-Control-Allow-Credentials' => 'true',
'Access-Control-Max-Age' => '86400',
'Access-Control-Allow-Headers' => 'Content-Type, Authorization, X-Requested-With'
......
<?php
namespace App\Model;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class UnrealCardModel extends Model
{
use SoftDeletes;
protected $table = 'unreal_cards';
protected $dates = ['deleted_at'];
}
\ No newline at end of file
......@@ -134,7 +134,7 @@ class GeoHashServices
{
// 搜索该点附近的站点
$geoHashCode = $this->getGeoHash($latitude, $longitude);
$stations = StationModel::select(['id', 'name', 'longitude', 'latitude', 'geohash'])
$stations = StationModel::select(['id', 'address', 'name', 'longitude', 'latitude', 'geohash'])
->where('geohash', 'like', substr($geoHashCode, 0, 4) . '%')
->get();
if (!$stations)
......@@ -144,6 +144,15 @@ class GeoHashServices
$stations[$key]['distance'] = $this->getDistance($latitude, $longitude, $station['latitude'], $station['longitude']);
}
array_multisort($stations, SORT_ASC, SORT_NUMERIC, array_column($stations, 'distance'), SORT_ASC, SORT_NUMERIC);
$redis = RedisSingle::getRedis();
foreach ($stations as $key => $station) {
if ($station['distance'] > $scale) {
unset($stations[$key]);
continue;
}
$stations[$key]['freeCount'] = count($redis->zRangeByScore("cdz:station:port:state:" . $station['id'] . ':zset', 11, 11, ['withscores' => TRUE]));
}
return $stations;
}
}
\ No newline at end of file
......@@ -114,6 +114,11 @@ $api->version('v1', ['namespace' => 'App\Http\Controllers\V1', 'prefix' => 'api/
// 查找附近的站点
$api->post('station/nearby', 'Station\StationController@nearby');
// 虚拟卡
$api->get('unreal/cards', 'Wx\UnrealCardsController@index');
$api->post('unreal/cards', 'Wx\UnrealCardsController@store');
$api->delete('unreal/cards/{id}', 'Wx\UnrealCardsController@destroy');
});
$api->post('refresh/token', ['as' => 'wx.refresh.token', 'uses' => 'Wx\AccessController@refreshToken']);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment