package com.czg.controller.admin;

import cn.hutool.core.util.StrUtil;
import com.czg.account.service.CommonService;
import com.czg.resp.CzgResult;
import com.czg.service.account.util.AliOssUtil;
import jakarta.annotation.Resource;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 公共接口
 * @author Administrator
 */
@RestController
@RequestMapping("/admin/common")
public class CommonController {
    @Resource
    private CommonService commonService;
    @Resource
    private AliOssUtil aliOssUtil;

    private static final List<String> SMS_CODE_TYPE = List.of("editShopInfoOpePwd", "wxMiniPwd", "shopPwd");

    /**
     * 发送验证码
     * @param type 验证码类型 目前
     *             editShopInfoOpePwd 店铺操作密码
     *             wxMiniPwd 微信小程序用户登录密码
     *             shopPwd 店铺登录密码
     * @return 是否成功
     */
    @GetMapping("/sms")
    public CzgResult<Boolean> sendSms(@RequestParam String type) {
        if(StrUtil.isEmpty(type) || !SMS_CODE_TYPE.contains(type)) return CzgResult.failure("验证码类型错误");
        return CzgResult.success(commonService.sendSms(type));
    }

    /**
     * 文件上传
     * @param file 文件对象
     * @return 文件地址
     */
    @PostMapping("/upload")
    public CzgResult<String> upload(@RequestParam MultipartFile file) throws Exception {
        return CzgResult.success(aliOssUtil.uploadSuffix(file.getBytes(), FilenameUtils.getExtension(file.getOriginalFilename())));
    }
}
