乐高玩具

阿里快递查询

2018-03-27  本文已影响13人  wanggs
package app.front.api;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import app.base.dao.legouser.OrdersDao;
import app.base.entity.legouser.User;
import app.front.UserContext;
import app.front.api.params.TrackingNumber;
import app.front.express.HttpUtils;
import component.common.msg.Ret;
import component.common.msg.Rets;
import component.common.utils.PropertyUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Wgs on 2017/5/16.
 */

@RestController
@RequestMapping("/api/express")
public class ExpressInquiryApi {

    @Autowired
    private OrdersDao ordersDao;

    @RequestMapping(value = "/expressInquiry", method = RequestMethod.POST)
    public Ret expressInquiry(@RequestBody TrackingNumber number) {
        String host = PropertyUtil.getProperty("express.host");
        String path = PropertyUtil.getProperty("express.path");
        String method = PropertyUtil.getProperty("express.method");
        String appcode = PropertyUtil.getProperty("express.appcode");

        Map<String, String> headers = new HashMap<String, String>();
        // 最后在header中的格式(中间是英文空格)为Authorization:APPCODE
        // 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);

        // 1. 判断是否是自己的单号 logisticsNo
        User user = UserContext.getLoginUser();

        String logisticsNo = ordersDao.getByProperty("userId", user.getId()).getLogisticsNo();
        if (logisticsNo.equals(number.getNumber())) {
            Map<String, String> querys = new HashMap<String, String>();
            querys.put("number", number.getNumber());
            querys.put("type", "auto");

            try {
                /**
                 * 重要提示如下: HttpUtils请从
                 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
                 * 下载
                 *
                 * 相应的依赖请参照
                 * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
                 */
                HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
                // 获取response的body
                String json = (EntityUtils.toString(response.getEntity()));

                JSONObject jsonObj = JSON.parseObject(json);

                return Rets.successData(jsonObj.get("result"));
            } catch (Exception e) {
                e.printStackTrace();

                return Rets.errorMsg(e.getMessage());
            }

        } else {
            return Rets.errorMsg("单号有误");
        }

    }

}

上一篇下一篇

猜你喜欢

热点阅读