package com.ruoyi.api;


import cn.hutool.json.JSONObject;
import com.ruoyi.busi.utils.WeChatUtils;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.GzhLoginBody;
import com.ruoyi.member.service.IMemberUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping("/wxApi")
public class WxApi {
    @Autowired
    private WeChatUtils weChatUtils;
    @Autowired
    private IMemberUserService memberUserService;


    @GetMapping("/getCodeUrl")
    @Anonymous
    public Map<String, String> getCodeUrl(String userType, String url) {
        Map<String, String> res = new HashMap<>();
        res.put("codeUrl", weChatUtils.getCodeUrl("https://www.ddtg.site/" + url, userType));
        return res;
    }

    @GetMapping("/getWebAccessTokenAndOpenid")
    @Anonymous
    public JSONObject getWebAccessTokenAndOpenid(String code) {
        return weChatUtils.getWebAccessTokenAndOpenid(code);
    }

    @PostMapping("/gzhLogin")
    @Anonymous
    public AjaxResult gzhLogin(@RequestBody GzhLoginBody gzhLoginBody) {
        JSONObject userInfo = weChatUtils.getUserInfo(gzhLoginBody.getAccess_token(), gzhLoginBody.getOpenid());
        //如果解析成功,获取token
        String token = memberUserService.gzhLogin(gzhLoginBody.getOpenid(), userInfo);
        AjaxResult ajax = AjaxResult.success();
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

}
