package com.community.huiju.controller;

import com.community.commom.mode.ResponseBean;
import com.community.huiju.service.WxLoginServiceI;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author FXF
 * @date 2019-03-29
 */
@RestController
@RefreshScope
@RequestMapping("/")
@Api(value = "微信登录 API", description = "微信登录 API")
public class WxLoginController {
	
	@Autowired
	private WxLoginServiceI wxLoginService;
	
	/**
	 * 微信登录
	 */
	@RequestMapping(value = "/wxLogin/{wxcode}", method = RequestMethod.POST)
	public ResponseBean autoLogin(@PathVariable String wxcode, HttpServletRequest request) {
		ResponseBean responseBean = new ResponseBean();
		if (StringUtils.isEmpty(wxcode)){
			responseBean.addError("fail");
			return responseBean;
		}
		//根据wxcode获取openid
		responseBean = wxLoginService.getOpenidByWxcode(wxcode);
		return responseBean;
	}

	/**
	 * 关联微信账号
	 * @return
	 */
	@RequestMapping(value = "/wx/associated", method = RequestMethod.POST)
	@ApiOperation(value = "手机号和验证码 关联微信账号", notes = "手机号和验证码 关联微信账号")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body", dataTypeClass = String.class, name = "parameter", value = "loginName手机号;code验证码;openid微信标识;userInfo微信用户信息")
	})
	public ResponseBean associatedWX(@RequestBody String parameter) {
		ResponseBean responseBean = new ResponseBean();
		responseBean = wxLoginService.associatedWX(parameter);
		return responseBean;
	}

	@ApiOperation(value = "注册", notes = "注册")
	@ApiImplicitParams({
			@ApiImplicitParam(paramType = "body",dataType = "String",name = "parameter",value = "userName用户姓名;idCard身份证;gender性别(1：男  2：女);loginName手机号;code验证码;openid微信标识;userInfo微信用户信息")
	})
	@RequestMapping(value = "/wx/register",method = RequestMethod.POST)
	public ResponseBean register(@RequestBody String parameter) {
		ResponseBean responseBean = new ResponseBean();
		responseBean = wxLoginService.register(parameter);
		return responseBean;
	}
}
