TestService.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace App\Module\Test\Services;
  3. use App\Module\Test\Events\TestEvent;
  4. use App\Module\Test\Exceptions\TestException;
  5. use App\Module\Test\Models\Test as TestModel;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\Event;
  8. use Illuminate\Support\Facades\Validator;
  9. class TestService implements TestServiceInterface
  10. {
  11. /**
  12. * 创建测试数据
  13. *
  14. * @param array $data
  15. * @return TestModel
  16. * @throws TestException
  17. */
  18. public function create(array $data): TestModel
  19. {
  20. try {
  21. $this->validate($data);
  22. $test = TestModel::create($data);
  23. Event::dispatch(new TestEvent($test, 'created'));
  24. return $test;
  25. } catch (\Exception $e) {
  26. throw new TestException('创建测试数据失败:' . $e->getMessage());
  27. }
  28. }
  29. /**
  30. * 更新测试数据
  31. *
  32. * @param TestModel $test
  33. * @param array $data
  34. * @return bool
  35. * @throws TestException
  36. */
  37. public function update(TestModel $test, array $data): bool
  38. {
  39. try {
  40. $this->validate($data);
  41. $result = $test->update($data);
  42. if ($result) {
  43. Event::dispatch(new TestEvent($test, 'updated'));
  44. }
  45. return $result;
  46. } catch (\Exception $e) {
  47. throw new TestException('更新测试数据失败:' . $e->getMessage());
  48. }
  49. }
  50. /**
  51. * 删除测试数据
  52. *
  53. * @param TestModel $test
  54. * @return bool
  55. * @throws TestException
  56. */
  57. public function delete(TestModel $test): bool
  58. {
  59. try {
  60. $result = $test->delete();
  61. if ($result) {
  62. Event::dispatch(new TestEvent($test, 'deleted'));
  63. }
  64. return $result;
  65. } catch (\Exception $e) {
  66. throw new TestException('删除测试数据失败:' . $e->getMessage());
  67. }
  68. }
  69. /**
  70. * 恢复测试数据
  71. *
  72. * @param TestModel $test
  73. * @return bool
  74. * @throws TestException
  75. */
  76. public function restore(TestModel $test): bool
  77. {
  78. try {
  79. $result = $test->restore();
  80. if ($result) {
  81. Event::dispatch(new TestEvent($test, 'restored'));
  82. }
  83. return $result;
  84. } catch (\Exception $e) {
  85. throw new TestException('恢复测试数据失败:' . $e->getMessage());
  86. }
  87. }
  88. /**
  89. * 强制删除测试数据
  90. *
  91. * @param TestModel $test
  92. * @return bool
  93. * @throws TestException
  94. */
  95. public function forceDelete(TestModel $test): bool
  96. {
  97. try {
  98. $result = $test->forceDelete();
  99. if ($result) {
  100. Event::dispatch(new TestEvent($test, 'force_deleted'));
  101. }
  102. return $result;
  103. } catch (\Exception $e) {
  104. throw new TestException('强制删除测试数据失败:' . $e->getMessage());
  105. }
  106. }
  107. /**
  108. * 验证数据
  109. *
  110. * @param array $data
  111. * @return void
  112. * @throws TestException
  113. */
  114. protected function validate(array $data): void
  115. {
  116. $validator = Validator::make($data, [
  117. 'name' => 'required|string|max:255',
  118. 'code' => 'required|string|max:50|unique:test,code',
  119. 'description' => 'nullable|string',
  120. 'data' => 'nullable|array',
  121. 'status' => 'required|integer|in:0,1'
  122. ]);
  123. if ($validator->fails()) {
  124. throw new TestException('数据验证失败:' . $validator->errors()->first());
  125. }
  126. }
  127. public static function insertHash()
  128. {
  129. // 初始化salt
  130. $salt = 'uraus';
  131. // 初始化hash
  132. $hashInit = 'uraus';
  133. for ($i = 0; $i < 10000; $i++) {
  134. // 获取hash
  135. $lashHash = DB::table('lan_hash_test')
  136. ->where(['user_id' => 1])
  137. ->orderBy('id', 'desc')
  138. ->limit(1)
  139. ->value('hash');
  140. $lastHash = $lashHash ?? $hashInit;
  141. // 验证哈希链
  142. $isTrue = self::verify(1);
  143. if (!$isTrue) {
  144. echo '错误';
  145. return;
  146. }
  147. $num = 1;
  148. $time = time();
  149. $hashStr = "$time|$num|$lastHash|$salt";
  150. $hashValue = hash('sha256', $hashStr);
  151. $insert = [
  152. 'user_id' => 1,
  153. 'num' => $num,
  154. 'time' => $time,
  155. 'hash' => $hashValue
  156. ];
  157. DB::table('lan_hash_test')->insert($insert);
  158. echo $i . PHP_EOL;
  159. }
  160. return;
  161. }
  162. public static function verify($userId)
  163. {
  164. // 初始化salt
  165. $salt = 'uraus';
  166. // 初始化hash
  167. $hashInit = 'uraus';
  168. $logs = DB::table('lan_hash_test')->where(['user_id' => 1])->orderBy('id')->get()->toArray();
  169. foreach ($logs as $key => $value) {
  170. if (isset($logs[$key - 1])) {
  171. $lastHash = $logs[$key - 1]->hash;
  172. } else {
  173. $lastHash = $hashInit;
  174. }
  175. $hashStr = "$value->time|$value->num|$lastHash|$salt";
  176. $hashValue = hash('sha256', $hashStr);
  177. if ($hashValue === $value->hash) {
  178. echo $value->hash . PHP_EOL;
  179. echo $hashValue . PHP_EOL;
  180. echo '第'.$key.'次比对成功' . PHP_EOL;
  181. continue;
  182. }
  183. echo $value->hash . PHP_EOL;
  184. echo $hashValue . PHP_EOL;
  185. echo '比对失败' . PHP_EOL;
  186. return false;
  187. }
  188. }
  189. }