• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

miaoxing / user / 3699122540

pending completion
3699122540

push

github

semantic-release-bot
chore(release): publish

14 of 22 branches covered (63.64%)

66 of 626 relevant lines covered (10.54%)

1.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/Service/UserVerify.php
1
<?php
2

3
namespace Miaoxing\User\Service;
4

5
use Miaoxing\Plugin\BaseService;
6

7
class UserVerify extends BaseService
8
{
9
    public const EXPIRE = 86400;
10

11
    public function generate($user)
12
    {
13
        $nonce = wei()->random->string(32);
×
14
        $timestamp = time();
×
15
        $userId = $user['id'];
×
16
        $password = $user['password'];
×
17
        $sign = md5($userId . $password . $timestamp . $nonce);
×
18

19
        return [
20
            'nonce' => $nonce,
×
21
            'timestamp' => $timestamp,
×
22
            'userId' => $userId,
×
23
            'sign' => $sign,
×
24
        ];
25
    }
26

27
    /**
28
     * @param $data
29
     * @param bool $isOnly 是否已访问过
30
     * @return array
31
     */
32
    public function verify($data, $isOnly = true)
33
    {
34
        // 1. 超时判断
35
        $timestamp = isset($data['timestamp']) ? $data['timestamp'] : 0;
×
36
        if ($timestamp < time() - static::EXPIRE) {
×
37
            return ['code' => -1, 'message' => '链接超时无效'];
×
38
        }
39

40
        // 2. 验证用户
41
        $userId = isset($data['userId']) ? $data['userId'] : 0;
×
42
        $user = wei()->user()->findOrInitById($userId);
×
43
        if ($user->isNew()) {
×
44
            return ['code' => -2, 'message' => '用户不存在'];
×
45
        }
46
        $password = $user['password'];
×
47

48
        // 3. 验证签名是否正确
49
        $nonce = $data['nonce'] ?: '';
×
50
        $sign = md5($data['userId'] . $password . $timestamp . $nonce);
×
51
        if ($sign != $data['sign']) {
×
52
            return ['code' => -3, 'message' => '链接验证无效'];
×
53
        }
54

55
        // 4. 验证码链接是否被用过
56
        if ($isOnly && !wei()->cache->add('user:verify:' . $sign, true, static::EXPIRE)) {
×
57
            return ['code' => -4, 'message' => '链接被访问过'];
×
58
        }
59

60
        return ['code' => 1, 'message' => '验证成功', 'user' => $user];
×
61
    }
62
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc