// pages/rechargeDetail/index.js import { getRechargeDetail } from "../../service/recharge"; import { alertError, alertInfo, loadingFunc } from "../../utils/index"; import { getRechargeOperateType, getRechargeOperateWay } from "../../utils/data"; import request from "../../utils/request" const { OK } = request Page({ /** * 页面的初始数据 */ data: { detail: {} }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { const { id } = options; loadingFunc(async () => { await this.init(id) }) }, async init(id) { const { code, message, data } = await getRechargeDetail(id) if (code !== OK) { alertInfo(message) return; } this.setData({ detail: { ...data, way: getRechargeOperateWay(data.way), type: getRechargeOperateType(data.type) } }) }, record() { const handleDownload = () => { return new Promise((resolve, rej) => { const { detail } = this.data; const result = wx.getAccountInfoSync(); const { envVersion } = result.miniProgram; let api = "" switch (envVersion) { // 开发版 case 'develop': // api = `http://localhost:9001`; api = `https://zgd.hbhcbn.com/recharge-print-api-test`; // api = `https://zgd.hbhcbn.com/recharge-print-api-test`; break; // 体验版 case 'trial': api = `https://zgd.hbhcbn.com/recharge-print-api-test`; break; // 正式版 case 'release': api = `https://zgd.hbhcbn.com/recharge-print-api`; break; } console.log("api", `${api}/${detail.id}`) wx.downloadFile({ url: `${api}/${detail.id}`, success: (res) => { console.log('res', res) if (res.statusCode === 200) { if (!res.tempFilePath) { alertError("获取文件失败") rej() return; } wx.openDocument({ filePath: res.tempFilePath, fileType: [ "pdf"], // 3. 这个必须写合法类型,不然下载不了 !!! success: function (res) { resolve() }, fail: function (e) { alertError("打开失败") console.log('打开失败错误为', e) rej() } }) } } }) }) } loadingFunc(async () => { await handleDownload(); }) }, back() { wx.navigateBack(); }, })