package com.ebts.web.controller.wechat;

import com.alibaba.fastjson.JSONObject;
import com.ebts.common.annotation.Log;
import com.ebts.common.constant.Constants;
import com.ebts.common.core.entity.AjaxResult;
import com.ebts.common.core.entity.entity.User;
import com.ebts.common.core.entity.model.LoginBody;
import com.ebts.common.enums.BusinessType;
import com.ebts.common.utils.HttpClient;
import com.ebts.common.utils.StringUtils;
import com.ebts.framework.config.WeChatConfig;
import com.ebts.framework.web.service.WeChatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Author 18209
 * @Date 2021/6/5 8:44
 * @Email clay@hchyun.com
 * @Version 1.0
 */
@RestController
@Api(value = "微信登录接口", tags = "微信登录接口")
@RequestMapping("/elecadmin/applet/login")
public class WeChatController {

    @Autowired
    private WeChatService weChatService;

    /**
     * 微信授权登录
     *
     * @param loginBody
     * @return
     */
    @ApiOperation("微信登录")
    @Log(title = "微信登录", businessType = BusinessType.EXPORT)
    @PostMapping("/authorization")
    public AjaxResult LoginByWeChat(@RequestBody LoginBody loginBody) {
        String loginUrl = getUrl(loginBody.getType(), loginBody.getCode());
        String rul = HttpClient.doGet(loginUrl);
        JSONObject wechat = JSONObject.parseObject(rul);
        String openid = wechat.getString("openid");
        String unionid = wechat.getString("unionid");
        AjaxResult ajax = AjaxResult.success();
        ajax.put("sessionKey", wechat.getString("session_key"));
        if (StringUtils.isEmpty(openid) || StringUtils.isEmpty(unionid)) {
            return AjaxResult.error("授权失败");
        }
        Map<String, Object> mode = weChatService.loginWeCat(new User(openid, unionid, loginBody.getType(), loginBody.getNickName(), loginBody.getAvatar()));
        User user = (User) mode.get("user");
        ajax.put(Constants.TOKEN, mode.get("token"));
        ajax.put("activate", mode.get("activate"));
        ajax.put("user", user);
        return ajax;
    }

    public String getUrl(String type, String code) {
        String loginUrl = "";
        switch (type) {
            case "01":
                loginUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=" + WeChatConfig.getUserAppid() + "&secret=" + WeChatConfig.getUserSecret() + "&js_code=" + code + "&grant_type=" + WeChatConfig.getGrant_type();
                break;
            default:
                break;
        }
        return loginUrl;
    }

    @ApiOperation("账号密码登录")
    @PostMapping
    public AjaxResult login(@RequestBody LoginBody loginBody) {
        AjaxResult ajax = AjaxResult.success();
        Map<String, Object> mode = weChatService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getType());
        ajax.put(Constants.TOKEN, mode.get("token"));
        User user = (User) mode.get("user");
        if (StringUtils.isEmpty(user.getPhonenumber())) {
            ajax.put("activate", false);
        } else {
            ajax.put("activate", true);
        }
        ajax.put("user", user);
        return ajax;
    }

}
