package com.xunwang.ucenter.controller;

import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.xunwang.commonutils.JwtUtils;
import com.xunwang.commonutils.R;
import com.xunwang.ucenter.service.MiniProgramOpenidUidService;
import com.xunwang.ucenter.utils.WxConstantUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Long
 * @since 2022-11-09
 */
@RestController
@RequestMapping("/ucenter/mini-program-openid-uid")
@Api("用户控制器")
@ApiSupport(author = "Long")
public class MiniProgramOpenidUidController {

    @Autowired
    private MiniProgramOpenidUidService uidService;

    @ApiOperation("小程序登录")
    @GetMapping("wxlogin")
    public R wxloginUser(HttpServletRequest request) throws Exception {
        String code = request.getParameter("code");
        Map map = uidService.getOpenId(request, WxConstantUtils.APPID, WxConstantUtils.SECRET, code, WxConstantUtils.GRANT_TYPE);
        return R.ok().data(map);
    }

    @ApiOperation("修改昵称和头像")
    @GetMapping("wxAlias")
    public R wxEditAlias(HttpServletRequest request) {
        String uid = JwtUtils.getUIdByJwtToken(request);
        String alias = request.getParameter("alias");
        String avatar = request.getParameter("avatar");
        uidService.editAlias(uid,alias,avatar);
        return R.ok().message("更新昵称成功");
    }

    @ApiOperation("获取微信手机号")
    @GetMapping("wxGetPhone")
    public R wxPhone(HttpServletRequest request) throws Exception {
        String uid = JwtUtils.getUIdByJwtToken(request);
        String code = request.getParameter("code");
        String phone = uidService.getUserPhone(uid, code);
        return R.ok().data("phone", phone);
    }

}

