package com.uas.platform.b2b.manage.openapi;

import com.uas.platform.b2b.manage.model.AccessToken;
import com.uas.platform.b2b.manage.service.AccessTokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 获取及验证token接口
 * 
 * @author yingp
 *
 */
@RestController("openapi.AccessTokenController")
@RequestMapping("/public/token")
public class AccessTokenController {

	@Autowired
	private AccessTokenService accessTokenService;

	/**
	 * 通过access_token获取验证信息
	 * @param access_token
	 * @return ResponseEntity
	 */
	@RequestMapping(method = RequestMethod.GET)
	public ResponseEntity<ModelMap> getUserByToken(String access_token) {
		ModelMap data = new ModelMap();
		AccessToken token = accessTokenService.findOne(access_token);
		if (token == null) {
			data.put("error", "验证信息错误或已过期");
			return new ResponseEntity<>(data, HttpStatus.NOT_FOUND);
		}
		data.put("user", token.getUser());
		data.put("bind", token.getBind());
		accessTokenService.delete(token.getId());
		return new ResponseEntity<>(data, HttpStatus.OK);
	}

}
