public function wx_update($orgid)
 {
     global $_G;
     if (!$this->_wxbind) {
         return;
     }
     if (!($org = parent::fetch($orgid))) {
         return false;
     }
     $wx = new qyWechat(array('appid' => $_G['setting']['CorpID'], 'appsecret' => $_G['setting']['CorpSecret'], 'agentid' => 0));
     $wd = array();
     if ($wxdepart = $wx->getDepartment()) {
         foreach ($wxdepart['department'] as $value) {
             $wd[$value['id']] = $value;
         }
     } else {
         return false;
     }
     if ($org['forgid']) {
         if (($forg = parent::fetch($org['forgid'])) && !$forg['worgid']) {
             if ($worgid = self::wx_update($forg['orgid'])) {
                 $forg['worgid'] = $worgid;
             } else {
                 return;
             }
         }
     }
     $parentid = $org['forgid'] == 0 ? 1 : $forg['worgid'];
     if ($org['worgid'] && $wd[$org['worgid']] && $parentid == $wd[$org['worgid']]['parentid']) {
         //更新机构信息
         $data = array("id" => $org['worgid']);
         if ($wd[$org['worgid']]['name'] != $org['orgname']) {
             $data['name'] = $org['orgname'];
         }
         if ($wd[$org['worgid']]['parentid'] != $parentid) {
             $data['parentid'] = $parentid;
         }
         if ($wd[$org['worgid']]['order'] != $org['order']) {
             $data['order'] = $org['order'];
         }
         if ($data) {
             $data['id'] = $org['worgid'];
         }
         if ($data) {
             if (!$wx->updateDepartment($data)) {
                 $message = 'updateDepartment:errCode:' . $wx->errCode . ';errMsg:' . $wx->errMsg;
                 runlog('wxlog', $message);
                 return false;
             }
         }
         return $org['worgid'];
     } else {
         $data = array("name" => $org['orgname'], "parentid" => $org['forgid'] == 0 ? 1 : $forg['worgid'], "order" => $org['disp'] + 1);
         if ($ret = $wx->createDepartment($data)) {
             parent::update($orgid, array('worgid' => $ret['id']));
             return $ret['id'];
         } else {
             if ($wx->errCode == '60008') {
                 //部门的worgid不正确导致的问题
                 foreach ($wd as $value) {
                     if ($value['name'] == $data['name'] && ($value['parentid'] = $data['parentid'])) {
                         C::t('organization')->update($org['orgid'], array('worgid' => $value['id']));
                         return $value['id'];
                     }
                 }
             }
             $message = 'createDepartment:errCode:' . $wx->errCode . ';errMsg:' . $wx->errMsg;
             runlog('wxlog', $message);
             return false;
         }
     }
     return false;
 }
Example #2
0
//error_reporting(E_ALL);
include_once libfile('function/organization');
$navtitle = '数据同步';
$do = $_GET['do'];
if (submitcheck('synsubmit')) {
} elseif ($do == 'qiwechat_syn_org') {
    $i = intval($_GET['i']);
    $wx = new qyWechat(array('appid' => $_G['setting']['CorpID'], 'appsecret' => $_G['setting']['CorpSecret'], 'agentid' => 0));
    if ($i < 1) {
        $data = array("id" => 1, "name" => $_G['setting']['sitename']);
        if (!$wx->updateDepartment($data)) {
            runlog('wxlog', '更新顶级部门为站点名称, errCode:' . $wx->errCode . '; errMsg:' . $wx->errMsg);
        }
    }
    $wd = array();
    if ($wxdepart = $wx->getDepartment()) {
        foreach ($wxdepart['department'] as $value) {
            $wd[$value['id']] = $value;
        }
    } else {
        exit(json_encode(array('error' => ' <p class="danger">获取微信企业号的机构和部门信息出错,无法继续同步!请检查是否正确绑定了企业号的CorpID和CorpSecret</p><p class="info">' . $wx->errCode . ':' . $wx->errMsg . '</p>')));
    }
    if ($org = DB::fetch_first("select * from %t where orgid>%d  order by orgid ", array('organization', $i))) {
        if ($org['forgid']) {
            if (($forg = C::t('organization')->fetch($org['forgid'])) && !$forg['worgid']) {
                if ($worgid = C::t('organization')->wx_update($forg['orgid'])) {
                    $forg['worgid'] = $worgid;
                } else {
                    exit(json_encode(array('msg' => 'continue', 'start' => $org['orgid'], 'message' => $org['orgname'] . ' <span class="danger">创建父机构失败,忽略</span>')));
                }
            }