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; }
if ($wd[$org['worgid']]['order'] != $org['order']) { $data['order'] = $org['order']; } if ($data) { $data['id'] = $org['worgid']; } if ($data) { if (!$wx->updateDepartment($data)) { exit(json_encode(array('msg' => 'continue', 'start' => $org['orgid'], 'message' => $org['orgname'] . ' <span class="danger">' . $wx->errCode . ':' . $wx->errMsg . '</span>'))); } } exit(json_encode(array('msg' => 'continue', 'start' => $org['orgid'], 'message' => $org['orgname'] . ' <span class="success">更新成功</span>'))); } else { //创建机构信息 $data = array("name" => $org['orgname'], "parentid" => $org['forgid'] == 0 ? 1 : $forg['worgid'], "order" => $org['disp'] + 1); if ($ret = $wx->createDepartment($data)) { C::t('organization')->update($org['orgid'], array('worgid' => $ret['id'])); exit(json_encode(array('msg' => 'continue', 'start' => $org['orgid'], 'message' => $org['orgname'] . ' <span class="success">创建成功</span>'))); } 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'])); exit(json_encode(array('msg' => 'continue', 'start' => $org['orgid'], 'message' => $org['orgname'] . ' <span class="success">更新成功</span>'))); } } } exit(json_encode(array('msg' => 'continue', 'start' => $org['orgid'], 'message' => $org['orgname'] . ' <span class="danger">' . $wx->errCode . ':' . $wx->errMsg . '</span>'))); } }