|
|
@@ -0,0 +1,204 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Module\Test\Services;
|
|
|
+
|
|
|
+use App\Module\Test\Events\TestEvent;
|
|
|
+use App\Module\Test\Exceptions\TestException;
|
|
|
+use App\Module\Test\Models\Test as TestModel;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+use Illuminate\Support\Facades\Event;
|
|
|
+use Illuminate\Support\Facades\Validator;
|
|
|
+
|
|
|
+class TestService implements TestServiceInterface
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * 创建测试数据
|
|
|
+ *
|
|
|
+ * @param array $data
|
|
|
+ * @return TestModel
|
|
|
+ * @throws TestException
|
|
|
+ */
|
|
|
+ public function create(array $data): TestModel
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $this->validate($data);
|
|
|
+ $test = TestModel::create($data);
|
|
|
+ Event::dispatch(new TestEvent($test, 'created'));
|
|
|
+ return $test;
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ throw new TestException('创建测试数据失败:' . $e->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新测试数据
|
|
|
+ *
|
|
|
+ * @param TestModel $test
|
|
|
+ * @param array $data
|
|
|
+ * @return bool
|
|
|
+ * @throws TestException
|
|
|
+ */
|
|
|
+ public function update(TestModel $test, array $data): bool
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $this->validate($data);
|
|
|
+ $result = $test->update($data);
|
|
|
+ if ($result) {
|
|
|
+ Event::dispatch(new TestEvent($test, 'updated'));
|
|
|
+ }
|
|
|
+ return $result;
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ throw new TestException('更新测试数据失败:' . $e->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除测试数据
|
|
|
+ *
|
|
|
+ * @param TestModel $test
|
|
|
+ * @return bool
|
|
|
+ * @throws TestException
|
|
|
+ */
|
|
|
+ public function delete(TestModel $test): bool
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $result = $test->delete();
|
|
|
+ if ($result) {
|
|
|
+ Event::dispatch(new TestEvent($test, 'deleted'));
|
|
|
+ }
|
|
|
+ return $result;
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ throw new TestException('删除测试数据失败:' . $e->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 恢复测试数据
|
|
|
+ *
|
|
|
+ * @param TestModel $test
|
|
|
+ * @return bool
|
|
|
+ * @throws TestException
|
|
|
+ */
|
|
|
+ public function restore(TestModel $test): bool
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $result = $test->restore();
|
|
|
+ if ($result) {
|
|
|
+ Event::dispatch(new TestEvent($test, 'restored'));
|
|
|
+ }
|
|
|
+ return $result;
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ throw new TestException('恢复测试数据失败:' . $e->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 强制删除测试数据
|
|
|
+ *
|
|
|
+ * @param TestModel $test
|
|
|
+ * @return bool
|
|
|
+ * @throws TestException
|
|
|
+ */
|
|
|
+ public function forceDelete(TestModel $test): bool
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $result = $test->forceDelete();
|
|
|
+ if ($result) {
|
|
|
+ Event::dispatch(new TestEvent($test, 'force_deleted'));
|
|
|
+ }
|
|
|
+ return $result;
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ throw new TestException('强制删除测试数据失败:' . $e->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证数据
|
|
|
+ *
|
|
|
+ * @param array $data
|
|
|
+ * @return void
|
|
|
+ * @throws TestException
|
|
|
+ */
|
|
|
+ protected function validate(array $data): void
|
|
|
+ {
|
|
|
+ $validator = Validator::make($data, [
|
|
|
+ 'name' => 'required|string|max:255',
|
|
|
+ 'code' => 'required|string|max:50|unique:test,code',
|
|
|
+ 'description' => 'nullable|string',
|
|
|
+ 'data' => 'nullable|array',
|
|
|
+ 'status' => 'required|integer|in:0,1'
|
|
|
+ ]);
|
|
|
+
|
|
|
+ if ($validator->fails()) {
|
|
|
+ throw new TestException('数据验证失败:' . $validator->errors()->first());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function insertHash()
|
|
|
+ {
|
|
|
+ // 初始化salt
|
|
|
+ $salt = 'uraus';
|
|
|
+ // 初始化hash
|
|
|
+ $hashInit = 'uraus';
|
|
|
+
|
|
|
+ for ($i = 0; $i < 10000; $i++) {
|
|
|
+ // 获取hash
|
|
|
+ $lashHash = DB::table('lan_hash_test')
|
|
|
+ ->where(['user_id' => 1])
|
|
|
+ ->orderBy('id', 'desc')
|
|
|
+ ->limit(1)
|
|
|
+ ->value('hash');
|
|
|
+ $lastHash = $lashHash ?? $hashInit;
|
|
|
+
|
|
|
+
|
|
|
+ // 验证哈希链
|
|
|
+ $isTrue = self::verify(1);
|
|
|
+ if (!$isTrue) {
|
|
|
+ echo '错误';
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ $num = 1;
|
|
|
+ $time = time();
|
|
|
+ $hashStr = "$time|$num|$lastHash|$salt";
|
|
|
+ $hashValue = hash('sha256', $hashStr);
|
|
|
+ $insert = [
|
|
|
+ 'user_id' => 1,
|
|
|
+ 'num' => $num,
|
|
|
+ 'time' => $time,
|
|
|
+ 'hash' => $hashValue
|
|
|
+ ];
|
|
|
+ DB::table('lan_hash_test')->insert($insert);
|
|
|
+ echo $i . PHP_EOL;
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function verify($userId)
|
|
|
+ {
|
|
|
+ // 初始化salt
|
|
|
+ $salt = 'uraus';
|
|
|
+ // 初始化hash
|
|
|
+ $hashInit = 'uraus';
|
|
|
+ $logs = DB::table('lan_hash_test')->where(['user_id' => 1])->orderBy('id')->get()->toArray();
|
|
|
+ foreach ($logs as $key => $value) {
|
|
|
+ if (isset($logs[$key - 1])) {
|
|
|
+ $lastHash = $logs[$key - 1]->hash;
|
|
|
+ } else {
|
|
|
+ $lastHash = $hashInit;
|
|
|
+ }
|
|
|
+
|
|
|
+ $hashStr = "$value->time|$value->num|$lastHash|$salt";
|
|
|
+ $hashValue = hash('sha256', $hashStr);
|
|
|
+ if ($hashValue === $value->hash) {
|
|
|
+ echo $value->hash . PHP_EOL;
|
|
|
+ echo $hashValue . PHP_EOL;
|
|
|
+ echo '第'.$key.'次比对成功' . PHP_EOL;
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ echo $value->hash . PHP_EOL;
|
|
|
+ echo $hashValue . PHP_EOL;
|
|
|
+ echo '比对失败' . PHP_EOL;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|