import { ApiCall } from "tsrpc";
import { ReqGift, ResGift } from "../../shared/protocols/gmapi/PtlGift";
import { PlayerFun } from "../../public/player";
import axios from 'axios'
import util from 'util'

export default async function (call: ApiCall<ReqGift, ResGift>) {
    let baseUrl = util.format('https://%s', G.config.baseUrl)
    // let params:any =  {g:'admin', m:'data', a: "card_active",game: '12zWQ5fj8ikhjW03' } // 默认固定参数
    let params:any =  {g:'admin', m:'data', a: "card_active",game: '12zWQ5fj8ikhjW03' } // 默认固定参数
    params.uid = call.uid; 
    params.uname = call.conn.gud.name; // 用户昵称name
    params.cnum = call.req.cnum; //兑换码
    params.owner = ''; // 渠道
    let res = await axios.get(baseUrl + '/gm/index.php', {params});
    if(res.data.result == 0) {
        await PlayerFun.addItem(call, res.data.code.prize); // 发送奖励
        call.succ({result: res.data.result, prize: res.data.code.prize});
    } else {
        call.error(lng.duihuanma_tips_4); // 兑换码已使用或不存在。
    }
}