<?php

//员工授权回跳
require '../../lib.php';
$ret = array('ret' => 0, 'msg' => '成功');
$oauth = OpenHelper::oauthUser();
//默认会从Get参数中拿到code去获得accesstoken
$result = $oauth->getAccessToken();
if ($result['ret'] == 0 && !empty($result['data'])) {
    //如果获取accesstoken成功,则对该用户鉴权通过,记下用户在示例app登录信息
    $access_tokens = $result['data'];
    $openId = $access_tokens['open_id'];
    $companyId = '';
    if (isset($access_tokens['company_id'])) {
        $companyId = $access_tokens['company_id'];
    }
    $model = new CSCorpModel();
    $model->saveOpenId($openId, $companyId);
    TestUser::user()->login($openId);
    header('Location:../../');
} else {
    header('Location:../../');
}
<?php

require '../../../lib.php';
$openId = TestUser::user()->id();
$companyId = TestUser::user()->companyId();
$model = new CSCorpModel();
$api = OpenHelper::api();
$userlist = $model->getUserList($companyId);
$before = array();
if ($userlist !== false) {
    $times = 100;
    do {
        $rand_index = rand(0, count($userlist) - 1);
        $before = $userlist[$rand_index];
    } while ($before['open_id'] == $openId && $times--);
}
if (isset($rand_index) && $before['open_id'] != $openId) {
    $result = $api->delUser($companyId, $model->getToken($companyId), getClientIp(), $before['open_id']);
}
$result = array('删除成员信息' => $before, '删除结果' => $result);
OpenUtils::outputJson($result);
<?php

//负责接收 设置第三方应用的开通状态 http://第三方应用/api/corporation/statuschange 的开启关闭状态
require '../../lib.php';
$companyId = $_GET['company_id'];
$hashkey = $_GET['hashkey'];
$status = $_GET['status'];
if ($hashkey == md5($companyId . OpenConfig::APPID . OpenConfig::APPSECRET)) {
    $model = new CSCorpModel();
    $model->setStatus($companyId, $status);
    $data = array('ret' => 0, 'msg' => "关闭成功,({$status})");
} else {
    $data = array('ret' => 1, 'msg' => "关闭失败,({$status})");
}
OpenUtils::outputJson($data);
 public function companyId()
 {
     $companyId = '';
     if ($this->id() !== '') {
         $model = new CSCorpModel();
         $companyId = $model->getCompanyIdByOpenId($this->id());
     } else {
         $open_ids = KeyValue::get(KEY_APP_ALL_CORPORATION);
         $companyId = $open_ids[0];
     }
     return $companyId;
 }
<?php

require '../../lib.php';
$openId = TestUser::user()->id();
$companyId = TestUser::user()->companyId();
$model = new CSCorpModel();
$api = OpenHelper::api();
$result = $api->sendTips($companyId, $model->getToken($companyId), getClientIp(), $openId, 'window_title_testapi', 'tips_title_testapi', 'tips_content_testapi,点击查看API文档', 'http://wiki.open.b.qq.com/api:start');
OpenUtils::outputJson($result);
<?php

require '../../lib.php';
$open_ids = KeyValue::get(KEY_APP_ALL_CORPORATION);
$model = new CSCorpModel();
$data = array();
if (empty($open_ids)) {
    $data = array(msg => '还没有任何公司主号开启该app');
} else {
    foreach ($open_ids as $open_id) {
        $a = $model->getCorpInfo($open_id);
        $data[$open_id]['info'] = $a;
        $tokens = $model->getAllToken($open_id);
        $data[$open_id]['tokens'] = $tokens;
    }
}
OpenUtils::outputJson($data);
<?php

require '../../lib.php';
$openId = TestUser::user()->id();
$companyId = TestUser::user()->companyId();
$model = new CSCorpModel();
$api = OpenHelper::api();
$result = $api->urlLong2Short($companyId, $model->getToken($companyId), getClientIp(), array("http://wiki.open.b.qq.com", "http://b.qq.com"));
OpenUtils::outputJson($result);
<?php

require '../../../lib.php';
$openId = TestUser::user()->id();
$companyId = TestUser::user()->companyId();
$model = new CSCorpModel();
$api = OpenHelper::api();
$result = $api->getDeptList($companyId, $model->getToken($companyId), getClientIp());
OpenUtils::outputJson($result);
<?php

require '../../../lib.php';
$openId = TestUser::user()->id();
$companyId = TestUser::user()->companyId();
$model = new CSCorpModel();
$api = OpenHelper::api();
$before = $model->getUserInfo($companyId, $openId);
$name = 'testrandom' . rand(1, 100);
@($result = $api->modUser($companyId, $model->getToken($companyId), getClientIp(), $openId, $before['account'], $name, null, null));
$after = $model->getUserInfo($companyId, $openId);
$result = array('修改前' => $before, '修改结果' => $result, '修改后' => $after);
OpenUtils::outputJson($result);
<?php

require '../../../lib.php';
$openId = TestUser::user()->id();
$companyId = TestUser::user()->companyId();
$model = new CSCorpModel();
$api = OpenHelper::api();
$accountrandom = 'testaccount' . rand(1, 999);
$namerandom = 'testname' . rand(1, 999);
$result = $api->addUser($companyId, $model->getToken($companyId), getClientIp(), $accountrandom, $namerandom);
$after = array();
if ($result['ret'] == 0) {
    $added_id = $result['data']['open_id'];
    $after = $model->getUserInfo($companyId, $added_id);
}
$result = array('添加结果' => $result, '添加成员信息' => $after);
OpenUtils::outputJson($result);
<?php

require '../../lib.php';
$company_ids = KeyValue::get(KEY_APP_ALL_CORPORATION);
$model = new CSCorpModel();
$data = array();
foreach ($company_ids as $company_id) {
    $tokens = KeyValue::get(KEY_ACCESSTOKEN . $company_id);
    $result = $model->refreshToken($tokens['refresh_token']);
    if ($result != false) {
        $refresh_tokens = $result;
        $refresh_tokens['company_id'] = $tokens['company_id'];
        $refresh_tokens['is_open'] = $tokens['is_open'];
        saveToken($refresh_tokens);
    }
    $tokens_after = KeyValue::get(KEY_ACCESSTOKEN . $company_id);
    $data[$company_id]['刷新前'] = $tokens;
    $data[$company_id]['刷新结果'] = $result;
    $data[$company_id]['刷新后'] = $tokens_after;
}
OpenUtils::outputJson($data);
<?php

require '../../lib.php';
$openId = TestUser::user()->id();
$companyId = TestUser::user()->companyId();
$model = new CSCorpModel();
$api = OpenHelper::api();
$result = $api->getWpaButton($companyId, $model->getToken($companyId), getClientIp(), $openId, 'rich', 'wpatitle', 'wpadesc富文本上下文', 'http://b.qq.com/', 'http://combo.b.qq.com/bqq/v4/images/logo.png', 1);
OpenUtils::outputJson($result);
<?php

require '../../lib.php';
$openId = TestUser::user()->id();
$companyId = TestUser::user()->companyId();
$model = new CSCorpModel();
$api = OpenHelper::api();
$result = $api->pushNotifyCenter2Client($companyId, $model->getToken($companyId), getClientIp(), $openId, array(array('ha', 1), array('fd', 5)));
OpenUtils::outputJson($result);
<?php

//单点登录,客户端点击第三方icon调转
require '../../lib.php';
$companyId = $_GET['company_id'];
$hashkey = $_GET['hashkey'];
$openId = $_GET['open_id'];
$to_open_id = $_GET['to_open_id'];
$hashskey = $_GET['hashskey'];
$returnurl = $_GET['returnurl'];
if ($hashkey == md5($companyId . OpenConfig::APPID . OpenConfig::APPSECRET) && $returnurl == 1) {
    $model = new CSCorpModel();
    $api = OpenHelper::api();
    $result = $api->verifyLoginHashskey($companyId, $model->getToken($companyId), getClientIp(), $openId, $hashskey);
    if ($result['ret'] == 0) {
        $api->pushNotifyCenter2Client($companyId, $model->getToken($companyId), getClientIp(), $openId);
        TestUser::user()->login($openId);
        header('Location:../../');
    } else {
        echo $result['msg'];
    }
} else {
    echo "单点登录失败,hashkey失效";
}
<?php

require '../../../lib.php';
$openId = TestUser::user()->id();
$companyId = TestUser::user()->companyId();
$model = new CSCorpModel();
$api = OpenHelper::api();
$deptlist = $model->getDeptList($companyId);
$before = array();
if ($deptlist !== false) {
    $times = 10;
    do {
        $rand_index = rand(0, count($deptlist) - 1);
        $before = $deptlist[$rand_index];
    } while ($before['dept_id'] == 1 && $times--);
}
if (isset($rand_index) && $before['dept_id'] != 1) {
    $result = $api->delDept($companyId, $model->getToken($companyId), getClientIp(), $before['dept_id']);
}
$result = array('删除组织架构信息' => $before, '删除结果' => $result);
OpenUtils::outputJson($result);
<?php

require '../../../lib.php';
$openId = TestUser::user()->id();
$companyId = TestUser::user()->companyId();
$model = new CSCorpModel();
$api = OpenHelper::api();
$userlist = $api->getUserList($companyId, $model->getToken($companyId), getClientIp());
OpenUtils::outputJson($userlist);
<?php

require '../../../lib.php';
$openId = TestUser::user()->id();
$companyId = TestUser::user()->companyId();
$model = new CSCorpModel();
$api = OpenHelper::api();
$namerandom = 'testdeptname' . rand(1, 999);
$result = $api->addDept($companyId, $model->getToken($companyId), getClientIp(), $namerandom);
$after = $api->getDeptList($companyId, $model->getToken($companyId), getClientIp());
$result = array('添加结果' => $result, '添加后组织架构列表' => $after);
OpenUtils::outputJson($result);
<?php

require '../../../lib.php';
$openId = TestUser::user()->id();
$companyId = TestUser::user()->companyId();
$model = new CSCorpModel();
$api = OpenHelper::api();
$result = $api->getUserMobile($companyId, $model->getToken($companyId), getClientIp(), $openId);
OpenUtils::outputJson($result);
<?php

require '../../lib.php';
$openId = TestUser::user()->id();
$companyId = TestUser::user()->companyId();
$model = new CSCorpModel();
$api = OpenHelper::api();
$result = $api->sendBroadCast($companyId, $model->getToken($companyId), getClientIp(), 'broadcast_title_testapi', 'broadcast_content_testapi', '1', '');
OpenUtils::outputJson($result);