kk Blog —— 通用基础

date [-d @int|str] [+%s|"+%F %T"]

企业微信重建部门并移动员工

先将员工移动到临时部门中,删除所有部门,再重建部门,移动员工

https://work.weixin.qq.com/api/doc/90000/90135/90204

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
<?php

	function queryUrl($url, $header, $postfields)
	{   
		if (function_exists('curl_init') != 1)
			throw new Exception("Please install curl plugin", 1); //请安装curl插件!

		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
		curl_setopt($curl, CURLOPT_TIMEOUT, 5); 
		curl_setopt($curl, CURLOPT_POST, 1); 
		curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields);
//        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0');
		$result = curl_exec($curl);
		curl_close($curl);

		return $result;
	}

	// TODO
	$app_access_token = '';
	$header = array('Content-Type: application/json');

	$fp = fopen('bmh_db', 'r');
	while (!feof($fp)) {
		$line = fgets($fp);
		if ($line == false)
			break;
		$line = explode(' ', trim($line));

		// 创建部门
		$msg = array(
			'id' => $line[0],
			'name' => $line[1], 
			'parentid' => $line[2],
			'order' => $line[3]
		);
		print $line[0];
		echo "\n";
		print(queryUrl("https://qyapi.weixin.qq.com/cgi-bin/department/create?access_token=$app_access_token", $header, json_encode($msg)));
		echo "\n";

		// 移动员工到部门
		$order = 1000000;
		for ($i = 4; $i < count($line); $i ++) {
			echo ' ', $line[$i];
			$order -= 100;
			$msg2 = array(
				'userid' => $line[$i],
				'department' => array($line[0]),
				'order' => array($order)
			);
			print(queryUrl("https://qyapi.weixin.qq.com/cgi-bin/user/update?access_token=$app_access_token", $header, json_encode($msg2)));
			echo "\n";
		}
	}

bmh_db 格式: 部门号 部门名 上级部门号 排序号 员工1 员工2 …

1
2
3
4
100 事业部1 1 2000000
10001 业务组1 100 1999000 1234 1235
200 事业部2 1 1000000
20001 业务组2 200 999000 2345 2346 2347