Corp.php
1.9 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
<?php
namespace addons\qyexternal\model;
use think\Model;
class Corp extends Model
{
//数据库
protected $connection = 'database';
// 表名
protected $name = 'qywx_corp';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = false;
protected $field = [
"id", "name", "admin_id", "corp_id", "agent_id", "external_secret", "contacts_secret", "token", "key", "remarks", "status", "weigh"
];
public function follow()
{
return $this->hasMany('FollowUser', 'corp_id', 'corp_id');
}
public function contacts()
{
return $this->hasMany('Contacts', 'corp_id', 'corp_id');
}
public function chats()
{
return $this->hasMany('GroupChat', 'corp_id', 'corp_id');
}
public static function init()
{
self::event('before_write', function ($corp) {
$corp->corp_id = trim($corp->corp_id);
$corp->external_secret = trim($corp->external_secret);
$corp->contacts_secret = trim($corp->contacts_secret);
$corp->token = trim($corp->token);
$corp->key = trim($corp->key);
});
self::event('after_write', function ($corp) {
//$corp = new \Weasy\Core\Corp($corp->id);
//$corp->sync();
});
self::event('after_delete', function ($corp) {
$has = Corp::where("corp_id", $corp->corp_id)->find();
if (! $has) {
Contacts::where("corp_id", $corp->corp_id)->delete();
FollowUser::where("corp_id", $corp->corp_id)->delete();
GroupChat::where("corp_id", $corp->corp_id)->delete();
UserBehavior::where("corp_id", $corp->corp_id)->delete();
Logs::where("corp_id", $corp->corp_id)->delete();
}
});
}
}