赞
踩
- 1、云函数下安装好依赖,开发云函数功能
- 2、部署云端云函数成功后,
- 3、体验版或者正式版需要连接云端云函数,连接本地无法使用
云函数逻辑:
- // 云函数入口文件
- const cloud = require('wx-server-sdk')
- cloud.init()
- //操作excel用的类库
- const xlsx = require('node-xlsx');
-
- // 云函数入口函数
- exports.main = async(event, context) => {
- try {
- let { userdata } = event
- console.log(userdata)
- //1,定义excel表格名
- let dataCVS = new Date().getTime() + '.xlsx'
- //2,定义存储数据的
- let alldata = [];
- // '维修金额',
- let row = ['vin','车辆类型', '出险时间','理赔项类型','理赔名称','理赔金额']; //表属性
- alldata.push(row);
-
- function singleTypeFilter(val) {
- let str = "";
- switch (val) {
- case "1":
- str = "更换";
- break;
- case "2":
- str = " 维修";
- break;
- case "3":
- str = "材料";
- break;
- }
- return str;
- }
- function regFenToYuan(a) {
- var num = Number(a);
- if (!num) {
- //等于0
- return num + ".00";
- } else {
- //不等于0
- num = Math.round(num * 100) / 10000;
- num = num.toFixed(2);
- num += ""; //转成字符串
- var reg =
- num.indexOf(".") > -1
- ? /(\d{1,3})(?=(?:\d{3})+\.)/g
- : /(\d{1,3})(?=(?:\d{3})+$)/g; //千分符的正则
- console.log(num.indexOf(".") > -1);
- return num.replace(reg, "$1,"); //千分位格式化
- }
- }
-
-
- let records =userdata? userdata.records || []:[]
- records.forEach((item,index)=>{
- item.resultInfo.forEach((j,i)=>{
- let arr = [];
- arr.push(item.vin);
- arr.push(item.vehicleType);
- arr.push(item.dangerDate);
- arr.push(singleTypeFilter(j.dangerSingleType));
- arr.push(j.dangerSingleName);
- arr.push(regFenToYuan(j.dangerSingleMoney));
- alldata.push(arr)
- })
- })
-
- //3,把数据保存到excel里
- var buffer = await xlsx.build([{
- name: "(请保存)出险记录明细",
- data: alldata
- }]);
- //4,把excel文件保存到云存储里
- return await cloud.uploadFile({
- cloudPath: dataCVS,
- fileContent: buffer, //excel二进制文件
- })
-
- } catch (e) {
- console.error(e)
- return e
- }
- }
小程序端逻辑
serviceInfo数据结构
- serviceInfo: {
- serviceSumCount: "5",
- serviceSumMoney: "1838148",
- records: [
- {
- dangerDate: "2016-11",
- resultInfo: [
- {
- dangerSingleType: "2",
- dangerSingleName: "前保险杠(全喷)",
- dangerSingleMoney: "40000",
- },
- {
- dangerSingleType: "2",
- dangerSingleName: "右前叶子板(全喷)",
- dangerSingleMoney: "40000",
- },
- ],
- vin: "LG29384",
- vehicleType: "长城CC7150FM01轿车",
- serviceMoney: "80000",
- },
- ],
- },
- //获取云存储文件下载地址,这个地址有效期一天
- getFileUrl(fileID) {
- let that = this;
- uniCloud.getTempFileURL({
- fileList: [fileID],
- success: (res) => {
- // get temp file URL
- console.log("文件下载链接", res.fileList[0].tempFileURL);
- uni.downloadFile({
- url: res.fileList[0].tempFileURL,
- fail: function (err) {
- console.log(err);
- },
- success: function (res) {
- console.log(res);
- var filePath = res.tempFilePath;
- uni.openDocument({
- filePath: filePath,
- showMenu: true,
- success: function (res) {
- uni.hideLoading();
- console.log("打开文档成功");
- },
- });
- // 云函数删除云存储文件
- uniCloud
- .deleteFile({
- fileList: [fileID],
- })
- .then((res) => {
- console.log(res, "yyy");
- })
- .catch((err) => {
- console.log(err, "yyy2");
- });
- },
- });
- },
- fail: (err) => {
- // handle error
- },
- });
- },
- export() {
- uni.showLoading({
- title: "加载中...",
- });
-
- // details_demo
- uniCloud
- .callFunction({
- name: "getExcelInfo",
- data: { userdata: this.serviceInfo },
- })
- .then((res) => {
- console.log(res.result);
- this.getFileUrl(res.result.fileID);
- });
- // this.$message.info("查询成功,请注意查收结果");
- console.log(this.serviceInfo);
- },
效果:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。