对接oa系统

 1. 根据 客户的需求,要从oa系统发起建单操作,那么先就要从客户系统的oa系统页面,发起一个获取我们系统token的接口,然后oa那边再将token放到请求头里,把json报文放在body体里,我们指定一个接口接收。完整代码如下:

package com.hand.hcf.app.expense.oareport.web;

import com.hand.hcf.app.expense.client.extraApi.CompanyLevelInterface;
import com.hand.hcf.app.expense.oareport.client.OrganizationForOAFeignClient;
import com.hand.hcf.app.expense.oareport.domain.ExpenseReportOaLog;
import com.hand.hcf.app.expense.oareport.domain.OaFormRespMessage;
import com.hand.hcf.app.expense.oareport.dto.*;
import com.hand.hcf.app.expense.oareport.service.*;
import com.hand.hcf.app.expense.report.domain.ExpenseReportHeader;
import com.hand.hcf.app.expense.oareport.service.ExpenseOAReportLineImportService;
import io.swagger.annotations.*;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.ZonedDateTime;


@RestController
@RequestMapping("/api/expense/oareport")
public class ExpenseOAReportController {
    @Autowired
    ExpenseOAReportHeaderService expenseOAReportHeaderService;
    @Autowired
    OrganizationForOAFeignClient organizationForOAFeignClient;
    @Autowired
    ExpenseOAReportLineImportService expenseOAReportLineImportService;
    @Autowired
    ExpenseReportOaLogService expenseReportOaLogService;

    @PostMapping("/save")
    @ApiOperation(value = "OA表单数据", notes = "OA表单数据 ")
    public ResponseEntity<OaFormRespMessage> saveExpenseOAReport(@ApiParam(value = "OA表单数据") @RequestBody @Valid ExpenseOAReportHeaderDTO dto,@RequestHeader("Authorization")  String token){
        ExpenseReportOaLog expenseReportOaLog = new ExpenseReportOaLog();
        //插入日志表
        JSONObject send_json = JSONObject.fromObject(dto);
        expenseReportOaLog.setCreatedDate(ZonedDateTime.now());
        OaFormRespMessage oaFormRespMessage = expenseOAReportHeaderService.saveOAExpenseReport(dto,token);
        JSONObject return_json = JSONObject.fromObject(oaFormRespMessage);
        expenseReportOaLog.setTaskId(dto.getTaskId());
        if (send_json.toString().length()>4000){
            expenseReportOaLog.setSendMsg(send_json.toString().substring(0,4000));
        }else{
            expenseReportOaLog.setSendMsg(send_json.toString());
        }
        expenseReportOaLog.setReturnMsg(return_json.toString());
        expenseReportOaLog.setLastUpdatedDate(ZonedDateTime.now());
        expenseReportOaLogService.insertOrUpdate(expenseReportOaLog);
        return ResponseEntity.ok(oaFormRespMessage);

    }


    @PostMapping("/next/save")
    @ApiOperation(value = "OA表单下一步", notes = "根据OA传过来的数据自动生成报销单")
    public ResponseEntity<ExpenseReportHeader> saveExpenseReportInfo(@ApiParam(value = "报账单头") @RequestBody @Valid ExpenseReportHeader expenseReportHeader,
                                      @ApiParam(value = "OA报账头id") @RequestParam Long headerId){
        return ResponseEntity.ok(expenseOAReportHeaderService.saveExpenseOAReportInfo(expenseReportHeader,headerId));

    }
    @PostMapping("/get/oatoken")
    @ApiOperation(value = "OA获取token", notes = "OA获取token")
    public String getOaToken(@ApiParam(value = "员工工号") @RequestParam String employeeCode, @ApiParam(value = "账套id") @RequestParam Long setOfBooksId){
        return expenseOAReportHeaderService.getOaToken(employeeCode,setOfBooksId);
    }


}

 

发送http请求:

package com.hand.hcf.app.expense.oareport.client;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;


public class OaToken {
    public  String doPostData(String url, String json) throws Exception {
        DefaultHttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(url);
        String result = "";
        HttpResponse res = null;
        try {
            StringEntity s = new StringEntity(json.toString(), "UTF-8");
            s.setContentType("application/json");
            post.setHeader("Accept", "application/json");
            post.setHeader("Content-type", "application/json; charset=utf-8");
            post.setEntity(s);
            res = client.execute(post);
            if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                result = EntityUtils.toString(res.getEntity());
                return result;
            }
        } catch (Exception e) {
            if(res == null) {
                return "HttpResponse 为 null!";
            }
            throw new RuntimeException(e);
        }
        if(res == null || res.getStatusLine() == null) {
            return "无响应";
        }
        return result;
    }
}

 

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乌托邦钢铁侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值