| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- <?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;
- }
- }
- }
|