ai-clone/frontend-ai/unpackage/dist/build/mp-weixin/utils/payment.js

2 lines
10 KiB
JavaScript
Raw Permalink Normal View History

"use strict";const e=require("../common/vendor.js"),n=require("../config/api.js");const t={CREATE_VOICE:"voice_clone",PHOTO_REVIVAL:"photo_revival",VOLCENGINE_VIDEO:"volcengine_video",SYNTHESIZE:"tts_synthesis",VIDEO_CALL:"video_call",AI_CALL:"conversation"},o={VOICE_CLONE:{type:"voice_clone",backendType:"CREATE_VOICE",name:"声音克隆",desc:"克隆您的声音让AI学会您的音色",price:0,freeTrialCount:0},PHOTO_REVIVAL:{type:"photo_revival",backendType:"PHOTO_REVIVAL",name:"照片复活",desc:"让照片中的人开口说话",price:0,freeTrialCount:0},VOLCENGINE_VIDEO:{type:"volcengine_video",backendType:"VOLCENGINE_VIDEO",name:"火山视频复活",desc:"火山视频复活",price:0,freeTrialCount:0},TTS_SYNTHESIS:{type:"tts_synthesis",backendType:"SYNTHESIZE",name:"语音合成",desc:"使用已有音色合成语音",price:0,freeTrialCount:0},VIDEO_CALL:{type:"video_call",backendType:"VIDEO_CALL",name:"AI视频通话",desc:"AI视频通话",price:0,freeTrialCount:0},CONVERSATION:{type:"conversation",backendType:"AI_CALL",name:"实时对话",desc:"与AI进行实时语音对话",price:0,freeTrialCount:0}};let a=null,r=0;async function s(){const s=Date.now();return a&&s-r<3e5?(console.log("[Payment] 使用缓存价格数据"),a):(console.log("[Payment] 开始获取服务价格API地址:",`${n.API_BASE}/api/pay/prices`),new Promise(((i,c)=>{e.index.request({url:`${n.API_BASE}/api/pay/prices`,method:"GET",success:e=>{if(console.log("[Payment] 价格API响应:",e),200===e.statusCode&&e.data){a=e.data,r=s,console.log("[Payment] 后端返回价格数据:",e.data);let n=0;e.data.forEach((e=>{console.log("[Payment] 处理价格项:",e);const a=t[e.serviceType];if(console.log("[Payment] 映射类型:",e.serviceType,"->",a),a){const t=Object.keys(o).find((e=>o[e].type===a));t&&o[t]&&(console.log("[Payment] 更新服务:",t,"价格:",e.price,"免费次数:",e.freeTrialCount),o[t].price=e.price,o[t].freeTrialCount=e.freeTrialCount||0,o[t].name=e.serviceName,o[t].desc=e.description||o[t].desc,n++)}else console.warn("[Payment] 未找到映射的前端类型:",e.serviceType)})),console.log("[Payment] 价格更新完成,共更新",n,"个服务"),console.log("[Payment] 最终SERVICE_TYPES:",JSON.stringify(o,null,2)),i(e.data)}else{if(console.error("[Payment] 获取价格失败,状态码:",e.statusCode),a)return void i(a);c(new Error("获取价格失败"))}},fail:e=>{console.error("[Payment] 获取价格请求失败:",e),a?i(a):c(e)}})})))}function i(){return`ORD${Date.now()}${Math.floor(1e4*Math.random()).toString().padStart(4,"0")}`}function c(){return{userId:e.index.getStorageSync("userId")||"",token:e.index.getStorageSync("token")||""}}async function l(t){try{const{userId:a,token:r}=c();if(!a)return!1;const s=Object.values(o).find((e=>e.type===t)),i=s?s.backendType:t;return await new Promise((t=>{e.index.request({url:`${n.API_BASE}/api/pay/check?userId=${a}&serviceType=${i}`,method:"GET",header:{"X-User-Id":a,Authorization:r?`Bearer ${r}`:""},success:e=>{if(200===e.statusCode&&e.data&&"boolean"==typeof e.data.paid){const n=e.data||{};if(console.log("[Payment] /api/pay/check 响应:",i,n),!1===n.paid)return void t(!1);const o="number"==typeof n.remainingTotal?n.remainingTotal:"number"==typeof n.remaining?n.remaining:"number"==typeof n.remainingCount?n.remainingCount:"number"==typeof n.remainingTimes?n.remainingTimes:null,a="number"==typeof n.freeTrialRemaining?n.freeTrialRemaining:"number"==typeof n.freeTrialLeft?n.freeTrialLeft:null;return"number"==typeof o&&o>0||"number"==typeof a&&a>0?void t(!0):void t(!1)}t(!1)},fail:()=>t(!1)})}))}catch(a){return console.error("[Payment] 后端资格校验异常:",a),!1}}async function d(t,a){try{if(await l(t))return console.log("[Payment] 后端校验通过,跳过支付:",t),{success:!0,message:"已具备使用资格,无需支付",skipPayment:!0}}catch(c){console.error("[Payment] 后端资格校验失败(忽略继续走支付):",c)}await s();const r=Object.values(o).find((e=>e.type===t));if(r&&0===r.price){console.log("[Payment] 服务价格为0跳过支付流程");const n={su