Users.php
3.2 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
<?php
namespace app\admin\controller\facrm\workweixin;
use app\admin\model\Admin;
use app\common\controller\Backend;
use EasyWeChat\Factory;
use app\admin\model\facrm\qywx\User as QywxUser;
use fast\Random;
use think\Db;
use think\Exception;
/**
* 企业员工
* @icon fa fa-circle-o
*/
class Users extends Backend
{
protected $model = null;
protected $distinguish = true;
public function _initialize()
{
parent::_initialize();
$this->request->filter(['strip_tags']);
$this->model = new QywxUser();
}
/**
* 列表
* @return string|\think\response\Json
* @throws Exception
*/
public function index()
{
//设置过滤方法
$this->request->filter(['strip_tags']);
if ($this->request->isAjax()) {
//如果发送的来源是Selectpage,则转发到Selectpage
if ($this->request->request('keyField')) {
return $this->selectpage();
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$total = $this->model
->where($where)
->order($sort, $order)->fetchSql(false)
->count();
$list = $this->model
->where($where)
->field('weigh', true)
->with([
'admin' => function ($user) {
$user->field('id,username,nickname');
},
])
->order($sort, $order)
->limit($offset, $limit)->fetchSql(false)
->select();
$result = array("total" => $total, "rows" => $list);
return json($result);
}
return $this->view->fetch();
}
/**
* 同步员工
*/
public function syn()
{
set_time_limit(0);
$key = "work_weixin_set";
$settingModel = new \app\admin\model\facrm\Setting();
$row = $settingModel->where('key', $key)->find();
if (!$row) $this->error(__("企业微信未配置"));
$values = json_decode($row['values'], true);//获取器不知道为什么失效了
if (!$values) $this->error(__("企业微信未配置1"));
$config = [
'corp_id' => $values['corp_id'],
'secret' => $values['agent_secret'],
'agent_id' => $values['agent_id'],
//'agent_secret' => $values['agent_secret'],
];
try {
$app = Factory::work($config);
$result = $app->user->getDetailedDepartmentUsers(1, true);
} catch (\Exception $e) {
$this->error($e->getMessage());
}
if (!$result || $result['errcode'] != '0') {
$errmsg = isset($result['errmsg']) ? $result['errmsg'] : 'null';
$this->error(__("获取失败:" . $errmsg));
}
$userlist = $result['userlist'];
$result = 0;
foreach ($userlist as $user) {
//判断系统是否存在
$qywxUser = QywxUser::getUser($user['userid'], $values, $user);
if ($qywxUser) $result++;
}
$this->success("成功同步{$result}条");
}
}