package com.flossom.miniProgram.controller;

import com.flossom.common.core.utils.StringUtils;
import com.flossom.common.core.web.controller.BaseController;
import com.flossom.common.core.web.domain.AjaxResult;
import com.flossom.miniProgram.domain.vo.UserMemberUpdateVo;
import com.flossom.miniProgram.service.IWxUserMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@RestController
@RequestMapping("/user")
public class UserMemberController extends BaseController {

    @Autowired
    private IWxUserMemberService wxUserMemberService;

    /**
     * 登录接口
     *
     * @param code 临时凭证
     * @return
     * @throws Exception
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestParam(value = "code") String code) throws Exception {
        if (StringUtils.isBlank(code)) {
            logger.error("登录code不能为空");
            return AjaxResult.error("登录失败");
        }
        return AjaxResult.success(wxUserMemberService.login(code));
    }

    /**
     * 添加手机号，升级会员
     * 1、不再需要提前调用 wx.login 进行登录
     * 2、getPhoneNumber 返回的 code 与 wx.login 返回的 code 作用是不一样的
     *
     * @param code
     * @return
     */
    @PostMapping("/upgradeMember")
    public AjaxResult upgradeMember(@RequestParam(value = "code") String code) throws Exception {
        if (StringUtils.isBlank(code)) {
            logger.error("获取手机号code不能为空");
            return AjaxResult.error("获取手机号失败");
        }
        return AjaxResult.success("获取成功", wxUserMemberService.upgradeMember(code));
    }

    /**
     * 完善用户信息
     *
     * @return
     */
    @PostMapping("/updateUser")
    public AjaxResult updateUser(@RequestBody @Validated UserMemberUpdateVo userMemberUpdateVo) {
        wxUserMemberService.updateUser(userMemberUpdateVo);
        return AjaxResult.success();
    }

}
