package com.platform.modules.app.controller;


import com.platform.common.utils.R;
import com.platform.common.validator.ValidatorUtils;
import com.platform.modules.app.entity.UserEntity;
import com.platform.modules.app.form.RegisterForm;
import com.platform.modules.app.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
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.Date;

/**
 * 注册
 */
@RestController
@RequestMapping("/app")
@Api(value = "APP注册接口", tags = "APP注册接口")
public class AppRegisterController {
    @Autowired
    private UserService userService;

    @PostMapping("register")
    @ApiOperation("注册")
    public R register(@RequestBody RegisterForm form){
        //表单校验
        ValidatorUtils.validateEntity(form);

        UserEntity user = new UserEntity();
        user.setMobile(form.getMobile());
        user.setUsername(form.getMobile());
        //sha256加密
        String salt = RandomStringUtils.randomAlphanumeric(15);
//        user.setPassword(DigestUtils.sha256Hex(form.getPassword()));
        user.setPassword(new Sha256Hash(form.getPassword(), salt).toHex());
        user.setSalt(salt);
        user.setCreateTime(new Date());
        userService.insert(user);

        return R.ok();
    }
}
