Email.php
5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
/**
* 邮箱
* @time 2022/7/29 15:09
* Class Email
* @package App\Mail\Models
*/
class Email extends Model {
// 启用
const STATUS_ACTIVE = 1;
// 禁用
const STATUS_DISABLED = 0;
/**
* @param string $v
* @return mixed|string
* @time 2022/7/29 17:34
*/
public function getPasswordAttribute($v)
{
return $v ? decrypt($v) : '';
}
/**
* 添加邮箱
* @param int $user_id
* @param string $email
* @param string $password
* @param string $email_name
* @param array $host
* @return false|static
* @time 2022/8/17 11:12
*/
public static function _add(int $user_id, string $email, string $password='', string $email_name = '',$host = []) {
// 这里可以用事务提交,允许断层
// 是否已存在邮箱
$model = static::where('email',$email)->first();
if(!$model){
// 新增
$model = new static();
$model->email = $email;
}
$password && $model->password = encrypt($password);
$model->status = self::STATUS_ACTIVE;
$model->email_name = $email_name;
if(!empty($host['imap'])){
$model->imap = $host['imap'];
}
if(!empty($host['smtp'])){
$model->smtp = $host['smtp'];
}
if(!$model->save()){
return false;
}
// 是否已绑定
$userEmail = EmailJoinUser::isBind($user_id,$model->id);
if(!$userEmail){
if(!EmailJoinUser::insert([
'user_id' => $user_id,
'email_id' => $model->id
])){
return false;
};
}
return $model;
}
/**
* 修改密码
* @param string $email
* @param string $password
* @return bool
* @time 2022/7/29 17:32
*/
public static function _changePwd(string $email, string $password):bool {
$data = static::where([
'email' => $email
])
->first();
if($data){
$data->password = encrypt($password);
$data->pwd_error = 0;
$data->save();
}
return false;
}
/**
* 读取列表
* @param int $user_id
* @param int $status
* @return array
* @time 2022/8/1 9:43
*/
public static function _get(int $user_id, $status = self::STATUS_ACTIVE,$field=['e.id','e.email','e.status','e.email_name']):array {
$lists = DB::table('email_join_users as eu')
->leftJoin('emails as e','eu.email_id','=','e.id')
->where([
// 'eu.user_id'=>$user_id,
'e.status' => $status
])->get($field)
->toArray();
return json_decode(json_encode($lists),true);
}
public static function _getById($user_id){
return DB::table('email_join_users as eu')
->leftJoin('emails as e','eu.email_id','=','e.id')
->where([
// 'eu.user_id'=>$user_id,
'e.status' => self::STATUS_ACTIVE
])->get(['e.id'])
->toArray();
}
/**
* 读取一条
* @param string $email
* @return mixed
* @author:dc
* @time 2023/2/4 15:50
*/
public static function _first(string $email) {
return static::where([
'email' => $email
])
->first();
}
/**
* 根据主键获取数据
* @param $id
* @return array
* @author:dc
* @time 2022/10/24 11:38
*/
public static function _firstById($id):array {
$data = static::where('id',$id)->first();
return $data ? $data->toArray() : [];
}
/**
* 禁用邮箱
* @param string $email
* @return bool
* @time 2022/7/29 16:18
*/
public static function disabled(string $email):bool{
$data = static::where([
'email' => $email,
])->first();
if ($data){
$data->status = self::STATUS_DISABLED;
return $data->save();
}
return false;
}
/**
* 启用
* @param string $email
* @return bool
* @time 2022/7/29 16:20
*/
public static function active(string $email):bool{
$data = static::where([
'email' => $email,
])->first();
if ($data){
$data->status = self::STATUS_ACTIVE;
return $data->save();
}
return false;
}
/**
* 更新
* @param $where
* @param $data
* @return mixed
* @time 2022/8/1 15:54
*/
public static function _update($where,$data){
return static::where($where)->update($data);
}
/**
* @param $email
* @return mixed
* @time 2022/8/17 11:38
*/
public static function _getIds($email){
return static::where('email',is_array($email)?'in':'=',$email)->get(['id'])->pluck('id')->toArray();
}
}