package com.cool.store.controller.webc;

import com.cool.store.context.PartnerUserHolder;
import com.cool.store.dto.wx.MiniProgramLoginDTO;
import com.cool.store.request.MobileUpdateRequest;
import com.cool.store.response.ResponseResult;
import com.cool.store.service.WechatMiniAppService;
import com.cool.store.vo.PartnerUserInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @FileName: MiniProgramAppController
 * @Description:
 * @date 2023-05-29 14:28
 */

@Api(tags = "微信小程序app接口")
@RestController
@RequestMapping({"/mini/miniProgram" })
public class MiniProgramAppController {

    @Resource
    private WechatMiniAppService wechatMiniAppService;

    @ApiOperation("判断用户是否存在的接口")
    @GetMapping("/getUserInfoByJsCode")
    public ResponseResult<PartnerUserInfoVO> getUserInfoByJsCode(@RequestParam(value = "jsCode",required = true)String jsCode) {
        return ResponseResult.success(wechatMiniAppService.getUserInfoByJsCode(jsCode));
    }

    @ApiOperation("小程序登录")
    @PostMapping("/login")
    public ResponseResult<PartnerUserInfoVO> login(@RequestBody @Valid MiniProgramLoginDTO param) {
        PartnerUserInfoVO userInfoVO = wechatMiniAppService.miniProgramLogin(param);
        return ResponseResult.success(userInfoVO);
    }

    @ApiOperation("获取手机号")
    @GetMapping("/getUserPhoneNumber")
    public ResponseResult<String> getUserPhoneNumber(@RequestParam(value = "mobileCode",required = true)String mobileCode) {
        return ResponseResult.success(wechatMiniAppService.getUserPhoneNumber(mobileCode));
    }

    @ApiOperation("更新手机号")
    @PostMapping("/updateUserPhoneNumber")
    public ResponseResult<String> updateUserPhoneNumber(@RequestBody @Valid MobileUpdateRequest request) {
        PartnerUserInfoVO userInfoVO = PartnerUserHolder.getUser();
        return ResponseResult.success(wechatMiniAppService.updateUserPhoneNumber(request, userInfoVO));
    }

    @ApiOperation("根据mobile和openId获取用户信息")
    @GetMapping("/getUserInfo")
    public ResponseResult<PartnerUserInfoVO> getUserInfo(){
        PartnerUserInfoVO userInfoVO = PartnerUserHolder.getUser();
        return ResponseResult.success(userInfoVO);
    }
}
