
import {Service} from './Service'
import {LoginDto,UserInfo} from '@/types/Dto'

import { token } from '@/util/userInfoUtils'
export class UserService extends Service {
    router = {
        sendCode:{ path:'/verificationCode/send/sms', dataType:'application/x-www-form-urlencoded'},
        login:{ path:'/user/login'},
        isRegisterd:{ path:'/user/isRegister', dataType:'application/x-www-form-urlencoded'},
        isPwdSet:{ path:'/user/isSetPassword', dataType:'application/x-www-form-urlencoded'},
        userInfo:{ path:'/user/detail',dataType:'application/x-www-form-urlencoded'},
        editUser:{path:'/user/update'},
        avatar:{ path:'/user/upload/avatar',dataType:'multipart/form-data'},
        getUserInfoByAddr:{ path:'/user/getUserByWallet', dataType:'application/x-www-form-urlencoded'}
    }
    constructor(){
        super()
    }

    private getAuth(){
        return  'Bearer '+ token.getToken()
      }
    /**
     * 获取验证码
     * @param phone 
     * @param codeType  短信模板, 1:登录短信 2:修改密码 3:修改手机号	
     */
    async getVerificationCode(telephone:string,codetype:string){
        return await this.service.post(this.router.sendCode.path,{},{
             headers:{"Content-Type": this.router.sendCode.dataType},
             params:{"telephone":telephone, "codetype":codetype}
         })
    }

    /**
     * 登录，没有账号则注册
     * @param data 
     * @returns 
     */
    async loginAndSign(data:LoginDto):Promise<{Authorization: string, user: object}>{
        return await this.service.post(this.router.login.path,data)
    }

    /**
     * 查看是否有该用户
     * @param account 手机号或者邮箱
     * @returns 
     */
    async isRegisterd(account:string){
        return await this.service.get(this.router.isRegisterd.path+'/'+account)
    }
    /**
     * @description 查看用户是否设置了密码
     * @param account 手机号或者邮箱
     * @returns 
     */
    async isPwdSet(account:string){
        return await this.service.get(`${this.router.isPwdSet.path}/${account}`)
    }

    /**
    * 获取用户的相关信息
     * @returns 
     */
    async getUserInfo(){
        return await this.service.get(this.router.userInfo.path,{
            headers:{
                Authorization: this.getAuth(),
                "Content-Type": this.router.userInfo.dataType
            }
        })
    }

    async avatarUpload(file:any){
        const fd = new FormData()
        fd.append('file', file)
        console.log(fd);
        return await this.service.post(this.router.avatar.path,fd,{
            headers:{
                Authorization: this.getAuth(),
                "Content-Type": this.router.avatar.dataType
            }
        })
    }

    async getUserInfoByAddr(addr:string):Promise<any>{
        return await this.service.get(this.router.getUserInfoByAddr.path,{
            headers:{
                Authorization: this.getAuth()
            },
            params:{"wallet":addr}
        })
    }

    /**
    * 编辑用户的相关信息
     * @returns 
     */
    async editUserInfo(userInfo:UserInfo):Promise<any>{
        return await this.service.post(this.router.editUser.path,userInfo,{
            headers:{
                Authorization: this.getAuth()
            }
        })
    }

}


