Files
wxProject-w4w/miniprogram/pages/index/index.ts
T

1564 lines
61 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// index.ts
// 获取应用实例
const app = getApp<IAppOption>()
const serverAddr = "https://www.chenziyang.top:5000"
Page({
data: {
miao_style:`
--main-color:rgb(234, 234, 234);
--secondary-color:rgb(247, 247, 247);
--tertiary-color:rgb(255, 255, 255);
--text-color:rgb(0, 0, 0);
`,
active_style:"skyline",
// 也不会有人看到
style_lib:{
wechat:{
name:"Unknow Mother-Goose",
intro:"",
style:{
main:'rgb(234, 234, 234)',
secondary:'rgb(247, 247, 247)',
tertiary:'rgb(255, 255, 255)',
text:'rgb(0, 0, 0)',
}
},
wechat_dark:{
name:"栖身之地",
intro:"",
style:{
main:'#4D4D4D',
secondary:'#F1F1F1',
tertiary:'#AAAAAA',
text:'rgb(0, 0, 0)',
}
},
skyline:{
name:"陪你的猫睡午觉",
intro:"",
style:{
main:'rgb(210, 233, 233)',
secondary:'rgb(248, 246, 244)',
tertiary:'rgb(227, 244, 244)',
text:'rgb(0, 0, 0)',
}
},
white:{
name:"他们说我是没有用的年轻人",
intro:"",
style:{
main:'rgb(205, 201, 195)',
secondary:'#EBE3D5',
tertiary:'rgb(251, 247, 240)',
text:'rgb(0, 0, 0)',
}
},
purple:{
name:"或许我们都忘了最初说过的话",
intro:"",
style:{
main:'rgb(208, 162, 247)',
secondary:'rgb(241, 234, 255)',
tertiary:'rgb(229, 212, 255)',
text:'rgb(0, 0, 0)',
}
},
shine:{
name:"至少我想感同你的身受",
intro:"",
style:{
main:'rgb(242, 190, 209)',
secondary:'rgb(248, 232, 238)',
tertiary:'rgb(253, 206, 223)',
text:'rgb(0, 0, 0)',
}
},
warm:{
name:"被爱灌溉成长的人",
intro:"",
style:{
main:'rgb(206, 90, 103)',
secondary:'rgb(252, 245, 237)',
tertiary:'rgb(244, 191, 150)',
text:'rgb(0, 0, 0)',
}
},
watermelon:{
name:"没有人在乎你在乎的事",
intro:"",
style:{
main:'rgb(255, 170, 167)',
secondary:'rgb(213, 236, 194)',
tertiary:'rgb(255, 211, 180)',
text:'rgb(0, 0, 0)',
}
},
bin:{
name:"在游荡的路上学会宽容",
intro:"",
style:{
main:'rgb(57, 62, 70)',
secondary:'rgb(238, 238, 238)',
tertiary:'rgb(66, 125, 157)',
text:'rgb(0, 0, 0)',
}
},
Ukraine:{
name:"我还年轻我还年轻",
intro:"",
style:{
main:'rgb(12, 53, 106)',
secondary:'rgb(255, 196, 54)',
tertiary:'rgb(1, 116, 190)',
text:'rgb(0, 0, 0)',
}
},
tree:{
name:"我好想成为你的鹿",
intro:"",
style:{
main:'rgb(60, 98, 85)',
secondary:'rgb(234, 231, 177)',
tertiary:'rgb(97, 135, 110)',
text:'rgb(0, 0, 0)',
}
},
cookie:{
name:"我爱你,却不能拯救你",
intro:"",
style:{
main:'rgb(92, 84, 112)',
secondary:'rgb(250, 240, 230)',
tertiary:'rgb(185, 180, 199)',
text:'rgb(0, 0, 0)',
}
},
},
// motto: 'Hello World',
text_startPage1: "知识库",
imgUrl_startPage1: "./static/icons/lib.svg",
function_startPage1: "popup_lib",
imgUrl_startPage2: "./static/icons/history.svg",
function_startPage2: "popup_history",
imgUrl_startPage3: `./static/icons/heart.svg`,
function_startPage3: "popup_collect",
text_startPage4: "个性化",
imgUrl_startPage4: "./static/icons/self.svg",
function_startPage4: "popup_style",
tip_content: "不吃饭会饿死",
tip_hidden: false,
active_Page: "startPage",
currentDate: '',
remarkTemp: '',
resultTitle: "查询结果",
showLib : false,
// 没用到
pageStack: [],
curtain_hidden: true,
curtain2_hidden: true,
imgUrl_test: "https://tse4-mm.cn.bing.net/th/id/OIP-C.2MyVJAPY4CWrMUIKfruz9AHaHa?w=199&h=199&c=7&r=0&o=5&dpr=1.3&pid=1.7",
// 有点误导性
// 瞬间开启摄像头会导致卡顿,所以还是注释掉吧
// camera_hidden:true,
// 历史记录data
data_history: {
// "2023.2.30": [
// {
// // 时间复杂度需求哪有那么高。。。
// id: 1,
// imgUrl: "https://tse3-mm.cn.bing.net/th/id/OIP-C.qM9D7c-Ow-C-xLARJdSwQwHaIO?pid=ImgDet&rs=1", res: { scores: 1, arr: ["山梨酸钾", "山梨酸钾", "山梨酸钾", "山梨酸钾", "山梨酸钾", "山梨酸钾", "山梨酸钾", "山梨酸钾", "山梨酸钾", "山梨酸钾", "山梨酸钾", "山梨酸钾", "山梨酸钾", "山梨酸钾", "山梨酸钾", "山梨酸钾",] },
// hasCollect: false,//目前全是false。。。
// }, {
// id: 2,
// imgUrl: "https://tse3-mm.cn.bing.net/th/id/OIP-C.qM9D7c-Ow-C-xLARJdSwQwHaIO?pid=ImgDet&rs=1", res: { scores: 75, arr: [] },
// hasCollect: false,
// }, {
// id: 3,
// imgUrl: "https://tse3-mm.cn.bing.net/th/id/OIP-C.qM9D7c-Ow-C-xLARJdSwQwHaIO?pid=ImgDet&rs=1", res: { scores: 1, arr: [] },
// hasCollect: false,
// }, {
// id: 4,
// imgUrl: "https://tse3-mm.cn.bing.net/th/id/OIP-C.qM9D7c-Ow-C-xLARJdSwQwHaIO?pid=ImgDet&rs=1", res: { scores: 1, arr: [] },
// hasCollect: false,
// }, {
// id: 5,
// imgUrl: "https://tse3-mm.cn.bing.net/th/id/OIP-C.qM9D7c-Ow-C-xLARJdSwQwHaIO?pid=ImgDet&rs=1", res: { scores: 1, arr: [] },
// hasCollect: false,
// }, {
// id: 6,
// imgUrl: "https://tse3-mm.cn.bing.net/th/id/OIP-C.qM9D7c-Ow-C-xLARJdSwQwHaIO?pid=ImgDet&rs=1", res: { scores: 1, arr: [] },
// hasCollect: false,
// },
// ], "2023.2.10": [
// {
// id: 7,
// imgUrl: "https://tse3-mm.cn.bing.net/th/id/OIP-C.qM9D7c-Ow-C-xLARJdSwQwHaIO?pid=ImgDet&rs=1", res: { scores: 1, arr: [] },
// hasCollect: false,
// }
// ], "2023.2.11": [
// {
// id: 8,
// imgUrl: "https://tse3-mm.cn.bing.net/th/id/OIP-C.qM9D7c-Ow-C-xLARJdSwQwHaIO?pid=ImgDet&rs=1", res: { scores: 1, arr: [] },
// hasCollect: false,
// }
// ], "2023.2.12": [
// {
// id: 9,
// imgUrl: "https://tse3-mm.cn.bing.net/th/id/OIP-C.qM9D7c-Ow-C-xLARJdSwQwHaIO?pid=ImgDet&rs=1", res: { scores: 1, arr: [] },
// hasCollect: false,
// }
// ], "2023.2.13": [
// {
// id: 10,
// imgUrl: "https://tse3-mm.cn.bing.net/th/id/OIP-C.qM9D7c-Ow-C-xLARJdSwQwHaIO?pid=ImgDet&rs=1", res: { scores: 1, arr: [] },
// hasCollect: false,
// }
// ],
},
//收藏夹data
data_collect: [
// {
// "id":1,
// "date": "2023.15.16",
// "res":{scores: 99, arr: []} ,
// "imgUrl": "./static/icons/history.svg",
// "remark": "方师傅康便面",
// "riskBgColor": "springgreen",
// "riskStr": "低风险",
// },
],
// 存储所有成分的信息, hidden: false, "riskBgColor": "springgreen", "riskStr": "低风险"由前端实现
data_lib: [
{
name: {
zh: "山梨酸钾",
en: "Potassium sorbate"
},
scores: 73,
info: "山梨酸钾,又名2,4-己二烯酸钾,是山梨酸的钾盐,分子式为C6H7O2K,无色或白色鳞片状结晶或结晶性粉末。 [1] 无臭或微有臭味,长期暴露在空气中易吸潮、被氧化分解而变色。易溶于水(67.6g/100mL 20℃),溶于丙二醇(5.8g/100mL)和乙醇(0.3g/10 mL)。山梨酸钾和山梨酸是常用的有机防腐剂,广泛用于食品、化状品、饲料的防腐。易于被人体吸收代谢,在体内无残留,无毒性。",
extra: [],
ADI: "",
// 搜索用,前端赋值
hidden: false,
//前端赋值
"riskBgColor": "springgreen",
"riskStr": "低风险"
}, {
name: {
zh: "李宁",
en: "L.N.G.N.M.S.L."
},
scores: 45,
info: "HAHA",
extra: [
{ type: "safe", name: "LPL" },
{ type: "unsafe", name: "飞舞" },
{ type: "unsafe", name: "精忠报国" },
],
ADI: "",
hidden: false,
riskBgColor: "sandybrown",
riskStr: "中风险"
}, {
name: {
zh: "丁基羟基茴香醚",
en: "Butyl hydroxyanisole(BHA)"
},
scores: 21,
info: "丁基羟基茴香醚(BHA),又名叔丁基-4-羟基茴香醚,化学式为C11H16O2,有两种异构体:3-BHA和2-BHA,一般指二者的混合物。2017年10月27日,世界卫生组织国际癌症研究机构公布的致癌物清单初步整理参考,叔丁基对羟基茴香醚(BHA)在2B类致癌物清单中。",
extra: [
{ type: "unsafe", name: "2B类致癌物" }
],
ADI: "0~0.5mg/kg bw",
hidden: false,
riskBgColor: "tomato",
riskStr: "高风险"
}, {
name: {
zh: "生牛乳",
en: "Hatsune Miku"
},
scores: 93,
info: "HAHA",
extra: [
{ type: "safe", name: "好喝" },
],
ADI: "",
// 搜索用,前端赋值
hidden: false,
//前端赋值
"riskBgColor": "springgreen",
"riskStr": "低风险"
}
],
// 知识库搜索用
hidden_lib: {},
// 查询结果搜索用
hidden_result: {},
data_lib_map: {},
//查询展示用
data_resultPage: {
scores: 65,
circleClip: "",
circleColor: "",
arr: [],
tr: null,//top right
},
//收藏/取消收藏用
resultId: -1,
// 收到的查询结果信息
data_rcv: {
scores: 1,
arr: ["山梨酸钾"],
},
session_key: "",
userInfo: {},
hasUserInfo: false,
canIUse: wx.canIUse('button.open-type.getUserInfo'),
canIUseGetUserProfile: false,
canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName') // 如需尝试获取用户信息可改为false
},
// 启动时获取session
async onLoad() {
// this.data.session_key = wx.getStorageSync("session_key")
// await this.updateSession()
this.readStorageData()
this.data.session_key = '12345'
this.set_miaoStyle(this.data.active_style)
// this.edit_svg_color()
this.updateLib()
// this.updateCollection()
// this.updateHistory()
},
// 好像微信小程序没有atob处理base64,此函数放弃
edit_svg_color(rgb="#000000"){
return;
rgb
var lib=`<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1697811750837" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="38161" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><path d="M454.5083 1023.996806H340.163697a57.4917 57.4917 0 0 1-57.4917-57.4917V59.413839a57.4917 57.4917 0 0 1 57.4917-57.4917H454.5083a57.4917 57.4917 0 0 1 57.4917 57.4917v907.091267a57.4917 57.4917 0 0 1-57.4917 57.4917z m-107.956637-63.879667H448.120333V65.801806H346.551663zM198.350837 1023.996806H84.006233a57.4917 57.4917 0 0 1-57.4917-57.4917V59.413839a57.4917 57.4917 0 0 1 57.4917-57.4917h114.344604A57.4917 57.4917 0 0 1 256.481333 59.413839v907.091267a57.4917 57.4917 0 0 1-58.130496 57.4917zM90.3942 960.117139H192.601667V65.801806H90.3942z" fill="#000000" p-id="38162"></path><path d="M223.902703 234.444126H58.454367a31.939833 31.939833 0 0 1 0-63.879667h165.448336a31.939833 31.939833 0 0 1 0 63.879667zM480.060167 234.444126H314.61183a31.939833 31.939833 0 0 1 0-63.879667H480.060167a31.939833 31.939833 0 0 1 0 63.879667zM831.398333 1023.996806a56.852903 56.852903 0 0 1-55.57531-42.799377L538.82946 104.768403A58.130497 58.130497 0 0 1 579.712447 33.861973l110.511823-31.939834a57.4917 57.4917 0 0 1 70.267633 40.882987l235.077174 875.79023a58.130497 58.130497 0 0 1-40.882987 70.90643L844.174267 1023.996806zM602.07033 93.908859L831.398333 960.117139l98.374687-26.190663L703.639 65.801806z m96.458297-58.769293z" fill="#000000" p-id="38163"></path><path d="M767.518667 866.214029a31.939833 31.939833 0 0 1-30.66224-23.635476 31.301037 31.301037 0 0 1 22.357883-38.966597l160.337963-42.799377a31.301037 31.301037 0 0 1 38.966597 22.357884 31.939833 31.939833 0 0 1-22.99668 38.966596l-159.699167 42.799377a19.1639 19.1639 0 0 1-8.304356 1.277593z" fill="#000000" p-id="38164"></path></svg>`;
var history=`<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1697812141935" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="52846" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><path d="M411.733333 885.333333H192c-6.4 0-10.666667-4.266667-10.666667-10.666666V149.333333c0-6.4 4.266667-10.666667 10.666667-10.666666h576c6.4 0 10.666667 4.266667 10.666667 10.666666v219.733334c0 17.066667 14.933333 32 32 32s32-14.933333 32-32V149.333333c0-40.533333-34.133333-74.666667-74.666667-74.666666H192C151.466667 74.666667 117.333333 108.8 117.333333 149.333333v725.333334c0 40.533333 34.133333 74.666667 74.666667 74.666666h219.733333c17.066667 0 32-14.933333 32-32s-14.933333-32-32-32z" fill="#000000" p-id="52847"></path><path d="M704 458.666667c-134.4 0-245.333333 110.933333-245.333333 245.333333S569.6 949.333333 704 949.333333 949.333333 838.4 949.333333 704 838.4 458.666667 704 458.666667z m0 426.666666c-100.266667 0-181.333333-81.066667-181.333333-181.333333s81.066667-181.333333 181.333333-181.333333 181.333333 81.066667 181.333333 181.333333-81.066667 181.333333-181.333333 181.333333z" fill="#000000" p-id="52848"></path><path d="M802.133333 716.8l-66.133333-29.866667V597.333333c0-17.066667-14.933333-32-32-32s-32 14.933333-32 32v110.933334c0 12.8 8.533333 23.466667 19.2 29.866666l85.333333 38.4c4.266667 2.133333 8.533333 2.133333 12.8 2.133334 12.8 0 23.466667-6.4 29.866667-19.2 6.4-17.066667 0-34.133333-17.066667-42.666667zM693.333333 298.666667c0-17.066667-14.933333-32-32-32H298.666667c-17.066667 0-32 14.933333-32 32s14.933333 32 32 32h362.666666c17.066667 0 32-14.933333 32-32zM298.666667 437.333333c-17.066667 0-32 14.933333-32 32s14.933333 32 32 32h106.666666c17.066667 0 32-14.933333 32-32s-14.933333-32-32-32h-106.666666z" fill="#000000" p-id="52849"></path></svg>`;
var heart=`<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1697812442065" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="57278" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><path d="M702.131071 124.121212c-93.893818 0-161.150707 84.605414-190.140768 129.353697C482.974384 208.726626 415.747232 124.121212 321.858586 124.121212 187.126303 124.121212 77.550545 245.53503 77.550545 394.757172c0 69.772929 47.327677 169.808162 104.782869 222.157576 79.475071 102.110384 302.898424 285.923556 330.484364 285.923556 28.070788 0 246.702545-180.236929 327.611475-284.696566 58.46497-53.272566 106.020202-153.439677 106.020202-223.384566C946.449455 245.53503 836.858182 124.121212 702.131071 124.121212M888.84299 394.757172c0 55.802828-40.468687 139.081697-88.362667 181.838869-1.313616 1.197253-2.505697 2.504404-3.697778 3.944727C725.586101 673.303273 582.242909 788.363636 512.970343 838.758141 461.474263 801.065374 306.369293 683.62602 226.742949 580.176162c-1.071838-1.408-2.323394-2.721616-3.63701-3.943434-47.706505-42.702869-87.964444-125.763232-87.964444-181.475556 0-117.474263 83.752081-213.060525 186.717091-213.060525 66.368646 0 130.267798 71.83903 164.141253 143.066505 4.490343 9.438384 14.449778 15.317333 25.995636 15.317333 11.544566 0 21.500121-5.878949 26.020202-15.317333 33.842424-71.227475 97.711838-143.066505 164.115394-143.066505C805.090909 181.696646 888.84299 277.282909 888.84299 394.757172" fill="#000000" p-id="57279"></path></svg>`;
var self=`<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1697812304038" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="55172" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><path d="M957.8 625.2c-5-15.4-17.4-27.4-33-32l-116.6-34.4-68.8-100.2c-9.2-13.4-24.4-21.4-40.6-21.4-16.2 0-31.4 8-40.6 21.4l-68.8 100.2-116.6 34.4c-15.6 4.6-27.8 16.6-33 32-5 15.4-2.2 32.4 7.8 45.2l74.2 96.4-3.4 121.6c-0.4 16.2 7.2 31.6 20.2 41.2 8.6 6.2 18.6 9.4 29 9.4 5.6 0 11.2-1 16.6-2.8l114.6-40.8 114.6 40.8c15.4 5.4 32.4 3 45.4-6.6 13.2-9.6 20.8-25 20.2-41.2l-3.4-121.6 74.2-96.4c10-12.8 13-29.8 8-45.2z m-131.8 108c-6.8 9-10.6 20.2-10.2 31.4l3 109.6-103.4-36.8c-10.6-3.8-22.4-3.8-33 0l-103.4 36.8 3-109.6c0.4-11.2-3.4-22.4-10.2-31.4l-66.8-87 105.2-31c10.8-3.2 20.4-10 26.8-19.4l62-90.4 62 90.4c6.4 9.4 15.8 16.2 26.8 19.4l105.2 31-67 87z" fill="#000000" p-id="55173"></path><path d="M641 660c-16.6 0-30 13.4-30 30v27.4c0 16.6 13.4 30 30 30s30-13.4 30-30V690c0-16.6-13.6-30-30-30zM756.6 660c-16.6 0-30 13.4-30 30v27.4c0 16.6 13.4 30 30 30s30-13.4 30-30V690c0-16.6-13.4-30-30-30zM406.4 737.2H179.6c-30.8 0-55.8-25-55.8-55.8V200.8c0-30.8 25-55.8 55.8-55.8h543.4c30.8 0 55.8 25 55.8 55.8v230.2c0 16.6 13.4 30 30 30s30-13.4 30-30V200.8c0-63.8-52-115.8-115.8-115.8H179.6c-63.8 0-115.8 52-115.8 115.8v480.4c0 63.8 52 115.8 115.8 115.8h226.8c16.6 0 30-13.4 30-30s-13.4-29.8-30-29.8z" fill="#000000" p-id="55174"></path><path d="M629 343.2h58.2c16.6 0 30-13.4 30-30s-13.4-30-30-30h-58.2c-12.8-41.6-51.6-72-97.4-72-45.8 0-84.6 30.4-97.4 72H218.8c-16.6 0-30 13.4-30 30s13.4 30 30 30H434c12.8 41.6 51.6 72 97.4 72 45.8 0 84.6-30.2 97.6-72z m-139.6-30c0-23.2 18.8-42 42-42s42 18.8 42 42-18.8 42-42 42-42-18.8-42-42zM388.2 503.2c-12.8-41.6-51.6-72-97.4-72-56.2 0-102 45.8-102 102s45.8 102 102 102c45.8 0 84.6-30.4 97.4-72h63c16.6 0 30-13.4 30-30s-13.4-30-30-30h-63z m-97.4 72c-23.2 0-42-18.8-42-42s18.8-42 42-42 42 18.8 42 42-18.8 42-42 42z" fill="#000000" p-id="55175"></path></svg>`
var camera=`<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg t="1697812002437" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="50178" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><path d="M273.066667 102.4h494.933333c94.2592 0 170.666667 76.407467 170.666667 170.666667v494.933333c0 94.2592-76.407467 170.666667-170.666667 170.666667H273.066667c-94.2592 0-170.666667-76.407467-170.666667-170.666667V273.066667c0-94.2592 76.407467-170.666667 170.666667-170.666667z m0 68.266667a102.4 102.4 0 0 0-102.4 102.4v494.933333a102.4 102.4 0 0 0 102.4 102.4h494.933333a102.4 102.4 0 0 0 102.4-102.4V273.066667a102.4 102.4 0 0 0-102.4-102.4H273.066667z" fill="#000000" p-id="50179"></path><path d="M517.973333 699.733333c94.2592 0 170.666667-76.407467 170.666667-170.666666s-76.407467-170.666667-170.666667-170.666667-170.666667 76.407467-170.666666 170.666667 76.407467 170.666667 170.666666 170.666666z m0-68.266666a102.4 102.4 0 1 1 0-204.8 102.4 102.4 0 0 1 0 204.8z" fill="#000000" p-id="50180"></path><path d="M750.933333 335.121067a34.133333 34.133333 0 1 0 0-68.266667 34.133333 34.133333 0 0 0 0 68.266667z" fill="#000000" p-id="50181"></path></svg>`
lib=(lib)
this.setData({
imgUrl_startPage1: lib,
imgUrl_startPage2: history,
imgUrl_startPage3: heart,
imgUrl_startPage4: self,
})
},
// 编辑wxss那几个css变量实现风格转换
set_miaoStyle(e){
var name = ''
if(typeof e == "string")name = e
else{
name = e.currentTarget.dataset.index
}
// console.log(name)
var style = this.data.style_lib[name].style
var str = `
--main-color:${style.main};
--secondary-color:${style.secondary};
--tertiary-color:${style.tertiary};
--text-color:${style.text};
`
this.setData({
miao_style:str,
active_style:name
})
console.log("设置主题:"+this.data.style_lib[name].name)
this.save2storage()
},
//开始界面的按钮相关动画
startPage_Bottons_leave() {
// var fd = wx.createSelectorQuery()
var time = 700;
var time1 = () => {
return Math.random() * 200;
}
this.animate('#gotoSearch', [
{ opacity: 1, translateY: 0, ease: 'ease-in' }, { opacity: 0, translateY: -300, ease: 'ease-in' },
], time)
this.animate('#tips_startPage', [
{ opacity: 1, ease: 'ease-in' }, { opacity: 0, ease: 'ease-in' },
], time, () => {
// this.setData({ tip_hidden: true })
})
setTimeout(() => {
this.animate('#buttom_startPage1', [
{ opacity: 1, translateX: 0, ease: 'ease-in' }, { opacity: 0, translateX: -300, ease: 'ease-in' },
], time)
}, time1())
setTimeout(() => {
this.animate('#buttom_startPage3', [
{ opacity: 1, translateX: 0, ease: 'ease-in' }, { opacity: 0, translateX: -300, ease: 'ease-in' },
], time)
}, time1())
setTimeout(() => {
this.animate('#buttom_startPage4', [
{ opacity: 1, translateX: 0, ease: 'ease-in' }, { opacity: 0, translateX: 300, ease: 'ease-in' },
], time)
}, time1())
setTimeout(() => {
this.animate('#buttom_startPage2', [
{ opacity: 1, translateX: 0, ease: 'ease-in' }, { opacity: 0, translateX: 300, ease: 'ease-in' },
], time)
}, time1())
},
startPage_Bottons_in() {
// var fd = wx.createSelectorQuery()
var time = 700;
var time1 = () => {
return Math.random() * 200;
}
this.animate('#gotoSearch', [
{ opacity: 0, translateY: -300, ease: 'ease-in' }, { opacity: 1, translateY: 0, ease: 'ease-out' },
], time)
// this.setData({ tip_hidden: false })
this.animate('#tips_startPage', [
{ opacity: 0, ease: 'ease-in' }, { opacity: 1, ease: 'ease-out' },
], time)
setTimeout(() => {
this.animate('#buttom_startPage1', [
{ opacity: 0, translateX: -300, ease: 'ease-in' }, { opacity: 1, translateX: 0, ease: 'ease-out' },
], time)
}, time1())
setTimeout(() => {
this.animate('#buttom_startPage3', [
{ opacity: 0, translateX: -300, ease: 'ease-in' }, { opacity: 1, translateX: 0, ease: 'ease-out' },
], time)
}, time1())
setTimeout(() => {
this.animate('#buttom_startPage4', [
{ opacity: 0, translateX: 300, ease: 'ease-in' }, { opacity: 1, translateX: 0, ease: 'ease-out' },
], time)
}, time1())
setTimeout(() => {
this.animate('#buttom_startPage2', [
{ opacity: 0, translateX: 300, ease: 'ease-in' }, { opacity: 1, translateX: 0, ease: 'ease-out' },
], time)
}, time1())
},
popup_camera() {
// this.setData({
// camera_hidden:false,
// })
this.data.pageStack.push("camera")
this.startPage_Bottons_leave()
this.animate('#page_camera', [
{ opacity: 0, translateY: 0 }, { opacity: 1, translateY: "-100%", ease: 'ease-in-out', },
], 1000, () => {
this.selectComponent('#page_camera')
})
},
exit_camera() {
this.data.pageStack.pop()
this.startPage_Bottons_in()
this.animate('#page_camera', [
{ opacity: 1, translateY: "-100%" }, { opacity: 0, translateY: "0", ease: 'ease-in-out' },
], 1000, () => {
// this.setData({
// camera_hidden:false,
// })
})
},
popup_history() {
this.data.pageStack.push("history")
this.startPage_Bottons_leave()
this.animate('#page_history', [
{ opacity: 0, translateY: 0 }, { opacity: 1, translateY: "-100%", ease: 'ease-in-out', },
], 1000, () => {
this.selectComponent('#page_history')
})
},
exit_history() {
this.data.pageStack.pop()
this.startPage_Bottons_in()
this.animate('#page_history', [
{ opacity: 1, translateY: "-100%" }, { opacity: 0, translateY: "0", ease: 'ease-in-out' },
], 1000)
},
popup_collect() {
this.data.pageStack.push("collect")
this.startPage_Bottons_leave()
this.animate('#page_collect', [
{ opacity: 0, translateY: 0 }, { opacity: 1, translateY: "-100%", ease: 'ease-in-out', },
], 1000, () => {
this.selectComponent('#page_collect')
})
},
exit_collect() {
this.data.pageStack.pop()
this.startPage_Bottons_in()
this.animate('#page_collect', [
{ opacity: 1, translateY: "-100%" }, { opacity: 0, translateY: "0", ease: 'ease-in-out' },
], 1000)
},
popup_lib() {
this.data.pageStack.push("lib")
this.setData({showLib : true})
this.startPage_Bottons_leave()
this.animate('#page_lib', [
{ opacity: 0, translateY: 0 }, { opacity: 1, translateY: "-100%", ease: 'ease-in-out', },
], 1000, () => {
this.selectComponent('#page_collect')
})
},
exit_lib() {
this.data.pageStack.pop()
this.startPage_Bottons_in()
this.animate('#page_lib', [
{ opacity: 1, translateY: "-100%" }, { opacity: 0, translateY: "0", ease: 'ease-in-out' },
], 1000, () => {
this.setData({showLib : false})
})
},
popup_style() {
this.data.pageStack.push("style")
this.startPage_Bottons_leave()
this.animate('#page_style', [
{ opacity: 0, translateY: 0 }, { opacity: 1, translateY: "-100%", ease: 'ease-in-out', },
], 1000, () => {
this.selectComponent('#page_style')
})
},
exit_style() {
this.data.pageStack.pop()
this.startPage_Bottons_in()
this.animate('#page_style', [
{ opacity: 1, translateY: "-100%" }, { opacity: 0, translateY: "0", ease: 'ease-in-out' },
], 1000)
},
//遮罩动画
show_curtain() {
this.setData({
curtain_hidden: false
})
this.animate('#page_curtain', [
{ opacity: 0 }, { opacity: 1, ease: 'ease-in-out' },
], 1000)
},
hide_curtain() {
this.animate('#page_curtain', [
{ opacity: 1 }, { opacity: 0, ease: 'ease-in-out' },
], 1000, () => {
this.setData({
curtain_hidden: true
})
console.log('suc')
})
this.setData({
curtain_hidden: true
})
},
//查询结果页面相关
popup_result() {
this.data.data_resultPage.circleClip = this.calculate_clip_path(this.data.data_resultPage.scores * 3.6)
this.data.data_resultPage.circleColor = this.scores2map(this.data.data_resultPage.scores).color
this.setData({
data_resultPage: this.data.data_resultPage
})
this.show_curtain()
this.animate('#page_result', [
{ opacity: 0, translateY: 0 }, { opacity: 1, translateY: "-100%", ease: 'ease-in-out', },
], 600)
},
exit_result() {
this.hide_curtain()
this.animate('#page_result', [
{ opacity: 1, translateY: "-100%" }, { opacity: 0, translateY: "0", ease: 'ease-in-out' },
], 600)
},
//遮罩动画2
show_curtain2() {
this.setData({
curtain2_hidden: false
})
this.animate('#page_curtain2', [
{ opacity: 0 }, { opacity: 1, ease: 'ease-in-out' },
], 1000)
},
hide_curtain2() {
this.animate('#page_curtain2', [
{ opacity: 1 }, { opacity: 0, ease: 'ease-in-out' },
], 1000)
this.setData({
curtain2_hidden: true
})
},
// 设置备注
popup_setRemark() {
this.show_curtain2()
this.animate('#star_setRemark', [
{ opacity: 0, translateY: 0 }, { opacity: 1, translateY: "-1300rpx", ease: 'ease-in-out', },
], 600)
},
exit_setRemark() {
this.animate('#star_setRemark', [
{ opacity: 1, translateY: "-1300rpx" }, { opacity: 0, translateY: "0", ease: 'ease-in-out' },
], 600)
this.hide_curtain2()
},
popup_editRemark() {
this.show_curtain2()
this.animate('#editRemark', [
{ opacity: 0, translateY: 0 }, { opacity: 1, translateY: "-1300rpx", ease: 'ease-in-out', },
], 600)
},
exit_editRemark() {
this.animate('#editRemark', [
{ opacity: 1, translateY: "-1300rpx" }, { opacity: 0, translateY: "0", ease: 'ease-in-out' },
], 600)
this.hide_curtain2()
},
popup_removeCollectionVerify() {
this.show_curtain2()
this.animate('#removeCollectionVerify', [
{ opacity: 0, translateY: 0 }, { opacity: 1, translateY: "-1300rpx", ease: 'ease-in-out', },
], 600)
},
exit_removeCollectionVerify() {
this.animate('#removeCollectionVerify', [
{ opacity: 1, translateY: "-1300rpx" }, { opacity: 0, translateY: "0", ease: 'ease-in-out' },
], 600)
this.hide_curtain2()
},
// 根据传入的包含scores和arr的object更新this.data.data_resultPage并popup_result
show_result(res: { scores: any, arr: Array<any> }, tr: string) {
// console.log(res)
var data_resultPage = {
scores: res.scores,
arr: [],
tr: tr,
}
console.log(tr)
for (var name of res.arr) {
// 有时间去写个时间复杂度更低的,不过感觉也够用了
for (var n of this.data.data_lib) {
if (n.name.zh.includes(name) || n.name.en.includes(name)) {
data_resultPage.arr.push(n)
break
}
}
}
this.setData({
"data_resultPage": data_resultPage
})
// console.log(data_resultPage)
this.popup_result()
},
show_result_wxml(e: any) {
var res = e.currentTarget.dataset.index;
console.log(res)
this.data.resultId = res.id
// console.log(this.data.resultId)
var tr = ""
// 不太好吧
switch (this.data.pageStack[this.data.pageStack.length - 1]) {
case "collect": {
tr = "delete"
break;
}
case "history": {
if (res.hasCollect)
tr = "delete"
else
tr = "heart"
break;
}
}
res = res.res
this.show_result(res, tr)
},
show_result_byId(id: any) {
for (var date in this.data.data_history) {
for (var n of this.data.data_history[date]) {
if (n.id == id) {
this.data.resultId = id
this.show_result(n.res, "heart")
break;
}
}
}
},
getInputRemark(e: any) {
this.data.remarkTemp = e.detail.value
},
// 收藏当前页面
add_collection() {
var r = this.id2resultAndDate(this.data.resultId)
if (r == null) return
var result = r.result
var date = r.date
var remark = this.data.remarkTemp
console.log(result, date, remark)
this.data.data_collect.push({
"id": this.data.resultId,
"date": date,
"res": result.res,
"imgUrl": result.imgUrl,
"remark": this.data.remarkTemp,
"riskBgColor": this.scores2map(result.res.scores).color,
"riskStr": this.scores2map(result.res.scores).content,
})
result.hasCollect = true
this.data.data_resultPage.tr = "delete"
this.setData(
{
data_collect: this.data.data_collect,
data_history: this.data.data_history,
data_resultPage: this.data.data_resultPage
}
)
this.save2storage()
this.uploadHistoryAndCollection()
this.exit_setRemark()
},
editRemark() {
var r = this.id2resultAndDate(this.data.resultId)
if (r == null) return
var result = r.result
var date = r.date
var remark = this.data.remarkTemp
for (var i in this.data.data_collect) {
if (this.data.data_collect[i].id == this.data.resultId) {
this.data.data_collect[i].remark = remark
}
}
this.setData(
{
data_collect: this.data.data_collect,
data_history: this.data.data_history,
data_resultPage: this.data.data_resultPage
}
)
this.save2storage()
this.uploadHistoryAndCollection()
this.exit_editRemark()
},
//取消收藏。。。。
remove_collection() {
var flag = true
var r = this.id2resultAndDate(this.data.resultId)
if (r == null) flag = false
if (flag) {
var result = r.result
result.hasCollect = false
}
for (var i in this.data.data_collect) {
if (this.data.data_collect[i].id == this.data.resultId) {
this.data.data_collect.splice(i, 1)
}
}
this.data.data_resultPage.tr = "heart"
this.setData(
{
data_collect: this.data.data_collect,
data_history: this.data.data_history,
data_resultPage: this.data.data_resultPage
}
)
this.save2storage()
this.uploadHistoryAndCollection()
this.exit_removeCollectionVerify()
},
// 根据角度计算剪切路径,错了不怪我。。
calculate_clip_path(deg: number) {
var res = "polygon(50% 50%,0 0"
deg = deg % 360
if (deg < 90) {
var x = -Math.cos(deg * Math.PI / 90) * 50 + 50;
res = res + "," + x + "% 0"
} else if (deg < 180) {
deg -= 90
var y = -Math.cos(deg * Math.PI / 90) * 50 + 50;
res = res + ",100% 0" + ",100% " + y + "%"
} else if (deg < 270) {
deg = 270 - deg
var x = -Math.cos(deg * Math.PI / 90) * 50 + 50;
res = res + ",100% 0" + ",100% 100%" + "," + x + "% 100%"
} else {
deg = 360 - deg
var y = -Math.cos(deg * Math.PI / 90) * 50 + 50;
res = res + ",100% 0" + ",100% 100%" + ",0 100%" + ",0 " + y + "%"
}
res = res + ");"
return res
},
//收到搜索结果处理(并没有写)
receive_result() {
},
//收到成分数据更新处理(并没有写)
receive_lib() {
},
//更新成分数据
updateLib() {
wx.request({
url: serverAddr + '/api/ing',
data: {
session_key:this.data.session_key
},
method: "GET",
header: {
'content-type': 'application/json' // 默认值
},
success: res => {
// console.log(res)
var data = res.data
console.log(data)
// 能跑
// name: {
// zh: "山梨酸钾",
// en: "Potassium sorbate"
// },
// scores: 73,
// info: "山梨酸钾,又名2,4-己二烯酸钾,是山梨酸的钾盐,分子式为C6H7O2K,无色或白色鳞片状结晶或结晶性粉末。 [1] 无臭或微有臭味,长期暴露在空气中易吸潮、被氧化分解而变色。易溶于水(67.6g/100mL 20℃),溶于丙二醇(5.8g/100mL)和乙醇(0.3g/10 mL)。山梨酸钾和山梨酸是常用的有机防腐剂,广泛用于食品、化状品、饲料的防腐。易于被人体吸收代谢,在体内无残留,无毒性。",
// extra: [],
// ADI: "",
// 适配后端返回数据
for(var n of data){
n.name = {}
n.name.zh = n.chinese_name
n.name.en = n.english_name
n.scores = parseInt(n.rating)
n.extra = []
// for(var i of n.effects){
n.extra.push({ 'type': "safe", 'name': n.effects },)
// }
// for(var i of n.potential_risk_people){
n.extra.push({ 'type': "unsafe", 'name': "风险人群:" + n.potential_risk_people })
// }
n.ADI = n.daily_intake_recommendation
}
for (var n of data) {
var scores = n.scores;
var map = this.scores2map(scores)
n.hidden = false
n.riskBgColor = map.color
n.riskStr = map.content
}
this.data.data_lib = data
this.setData(
{ "data_lib": data }
)
this.save2storage()
},
fail(res) {
console.log(res)
}
})
},
//更新历史记录
updateHistory() {
wx.request({
url: '' + serverAddr + '/getHistory', //仅为示例,并非真实的接口地址
data: {
session_key:this.data.session_key
},
method: "POST",
header: {
'content-type': 'application/json' // 默认值
},
success: res => {
console.log(res)
var data = res.data
this.setData(
{ "data_history": data }
)
this.sortHistory()
this.save2storage()
},
fail(res) {
console.log(res)
}
})
},
//更新收藏
updateCollection() {
wx.request({
url: '' + serverAddr + '/getCollection', //仅为示例,并非真实的接口地址
data: {
session_key:this.data.session_key
},
method: "POST",
header: {
'content-type': 'application/json' // 默认值
},
success: res => {
console.log(res)
var data = res.data
this.setData(
{ "data_collect": data }
)
this.save2storage()
},
fail(res) {
console.log(res)
}
})
},
// 更新session
updateSession() {
return new Promise((resolve, reject) => {
// 仅测试
if(this.data.session_key == '' || true){
this.getCode().then(code => {
wx.request({
url: '' + serverAddr + '/updateSession',
data: {
userCode: code
},
method: "POST",
header: {
'content-type': 'application/json'
},
success: res => {
console.log(res)
var data = res.data
this.setData(
{ "session_key": data }
)
resolve()
// setTimeout(this.updateSession,1000*60*5)
},
fail: res => {
console.log(res)
// setTimeout(this.updateSession,1000)
reject()
}
})
})
}else{
wx.checkSession({
success:resolve,
fail:()=>{
this.getCode().then(code => {
wx.request({
url: '' + serverAddr + '/updateSession',
data: {
userCode: code
},
method: "POST",
header: {
'content-type': 'application/json'
},
success: res => {
console.log(res)
var data = res.data
this.setData(
{ "session_key": data }
)
resolve()
// setTimeout(this.updateSession,1000*60*5)
},
fail: res => {
console.log(res)
// setTimeout(this.updateSession,1000)
reject()
}
})
})
}
})
}
})
},
// 上传照片并展示收到的结果
uploadImg(tempImagePath: any) {
wx.showLoading({ // 显示加载中loading效果
title: "查询中喵",
mask: true //开启蒙版遮罩
});
// return setTimeout(()=>{
// wx.hideLoading();
// var fake_data = {
// id:70582140,
// imgUrl:"./static/icons/history.svg",
// res:{
// scores: 93,
// arr: ["生牛乳"],
// }
// }
// var data = fake_data
// var nowdate = new Date();
// var nowyear = nowdate.getFullYear();
// var nowmonth = nowdate.getMonth() + 1;
// var nowday = nowdate.getDate();
// var dateStr = nowyear + '.' + nowmonth + '.' + nowday
// if (!this.data.data_history[dateStr]) {
// this.data.data_history[dateStr] = []
// }
// var historyTemp =
// {
// id: data.id,
// imgUrl: data.imgUrl,
// res: data.res,
// hasCollect: false,
// }
// this.data.data_history[dateStr].push(historyTemp)
// this.setData({
// data_history: this.data.data_history
// })
// this.sortHistory()
// this.show_result_byId(data.id)
// this.save2storage()
// },3000)
wx.getFileSystemManager().readFile({
filePath: tempImagePath,
encoding: 'base64',
success: res => {
// console.log('data:image/png;base64,' + res.data)
const imgBase64 = res.data
console.log(res.data)
wx.request({
url: '' + serverAddr + '/api/ocr', //仅为示例,并非真实的接口地址
data: {
imgBase64: imgBase64,
// session_key:this.data.session_key
},
method: "POST",
header: {
'content-type': 'application/json' // 默认值
},
success: res => {
var data = res.data
console.log(res.data)
wx.hideLoading();
var nowdate = new Date();
var nowyear = nowdate.getFullYear();
var nowmonth = nowdate.getMonth() + 1;
var nowday = nowdate.getDate();
var dateStr = nowyear + '.' + nowmonth + '.' + nowday
if (!this.data.data_history[dateStr]) {
this.data.data_history[dateStr] = []
}
var historyTemp =
{
id: data.id,
imgUrl: data.imgUrl,
res: data.res,
hasCollect: false,//目前全是false。。。
}
this.data.data_history[dateStr].push(historyTemp)
this.setData({
data_history: this.data.data_history
})
this.sortHistory()
this.show_result_byId(data.id)
this.save2storage()
},
fail: res => {
console.log(res)
}
})
}
})
// wx.uploadFile({
// url: '' + serverAddr + '/api/ocr', //仅为示例,并非真实的接口地址
// filePath: tempImagePath,
// name: 'file',
// // formData: {
// // session_key:this.data.session_key
// // },
// success:res=>{
// const data = res.data
// wx.hideLoading();
// var nowdate = new Date();
// var nowyear = nowdate.getFullYear();
// var nowmonth = nowdate.getMonth() + 1;
// var nowday = nowdate.getDate();
// var dateStr = nowyear + '.' + nowmonth + '.' + nowday
// if (!this.data.data_history[dateStr]) {
// this.data.data_history[dateStr] = []
// }
// var historyTemp =
// {
// id: data.id,
// imgUrl: data.imgUrl,
// res: data.res,
// hasCollect: false,//目前全是false。。。
// }
// this.data.data_history[dateStr].push(historyTemp)
// this.setData({
// data_history: this.data.data_history
// })
// this.sortHistory()
// this.show_result_byId(data.id)
// this.save2storage()
// }
// })
},
// 向云发送历史记录和收藏记录数据
uploadHistoryAndCollection(){
wx.request({
url: '' + serverAddr + '/uploadHistoryAndCollection', //仅为示例,并非真实的接口地址
data: {
history : this.data.data_history,
collection:this.data.data_collect,
session_key:this.data.session_key
},
method: "POST",
header: {
'content-type': 'application/json' // 默认值
},
success: res => {
console.log(res)
},
fail: res => {
console.log(res)
}
})
},
//拍照
takePhoto() {
const ctx = wx.createCameraContext()
console.log('takePhoto..')
ctx.takePhoto({
// quality: 'normal',
quality: 'normal',
//找时间把这里和choosePhoto的success合并一下(算了)
success: res => {
const tempImagePath = res.tempImagePath
console.log(tempImagePath)
console.log(res)
this.uploadImg(tempImagePath)
}
})
},
//选择照片
choosePhoto() {
wx.chooseMedia({
mediaType: ['image'],
sourceType: ['album'],
sizeType: ["compressed"],
count: 1,
success: res => {
const tempImagePath = res.tempFiles[0].tempFilePath
console.log(tempImagePath)
console.log(res)
this.uploadImg(tempImagePath)
}
})
},
scores2risk(scores: Number) {
if (scores < 33) {
return "high";
} else if (scores < 66) {
return "middle";
} else {
return "low";
}
},
readInput_lib(e: any) {
//其他搜索内容
var exf = (o: any, keyw: string) => {
//搜索额外信息
for (var i of o.extra) {
if (i.name.includes(keyw)) {
return true
}
}
//搜索风险
if (o.riskStr.includes(keyw)) {
return true
}
return false
}
var key = e.detail.value
if(key==null)return
for (var n of this.data.data_lib) {
// console.log(n)
if (n.name.zh.includes(key)|| exf(n, key)) {
// n.hidden = false;
this.data.hidden_lib[n.name.zh] = false
// console.log(n.name.zh)
} else {
// n.hidden = true;
this.data.hidden_lib[n.name.zh] = true
}
}
this.setData({ hidden_lib: this.data.hidden_lib })
// console.log(this.data.data_lib)
},
readInput_result(e: any) {
//其他搜索内容
var exf = (o: any, keyw: string) => {
//搜索额外信息
for (var i of o.extra) {
if (i.name.includes(keyw)) {
return true
}
}
//搜索风险
if (o.riskStr.includes(keyw)) {
return true
}
return false
}
var key = e.detail.value
for (var n of this.data.data_lib) {
// console.log(n)
if (n.name.zh.includes(key) || n.name.en.toLowerCase().includes(key.toLowerCase()) || exf(n, key)) {
// n.hidden = false;
this.data.hidden_result[n.name.zh] = false
// console.log(n.name.zh)
} else {
// n.hidden = true;
this.data.hidden_result[n.name.zh] = true
}
}
this.setData({ hidden_result: this.data.hidden_result })
// console.log(this.data.data_lib)
},
//将risk转为存储color和content的map
scores2map(scores: Number) {
var risk = this.scores2risk(scores)
switch (risk) {
case "low": {
return { "color": "springgreen", "content": "低风险" };
}
case "middle": {
return { "color": "sandybrown", "content": "中风险" };
}
case "high": {
return { "color": "tomato", "content": "高风险" };
}
}
return { "color": "tomato", "content": "高风险" };
},
//..
id2resultAndDate(id: number) {
for (var i in this.data.data_history) {
for (var n of this.data.data_history[i])
if (n.id == id) {
return { result: n, date: i }
}
}
return null;
},
// 将各种数据存入本地
save2storage() {
var list = ['data_history', 'data_collect', 'data_lib','active_style']
for (var k of list) {
wx.setStorageSync(k,this.data[k])
// console.log(k,this.data[k])
}
},
// 从本地读取各种数据
readStorageData(){
var list = ['data_history', 'data_collect', 'data_lib','active_style']
var m = {}
for (var k of list) {
m[k] = wx.getStorageSync(k)
// console.log(k,this.data[k])
// this.data[k]=wx.getStorageSync(k)
// console.log(wx.getStorageSync(k))
}
if(m['data_history'] == ''){
m['data_history'] = {}
}
if(m['data_collect'] == ''){
m['data_collect'] = []
}
if(m['data_lib'] == ''){
m['data_lib'] = []
}
this.setData(m)
// this.setData(
// {
// data_history:this.data["data_history"],
// data_collect:this.data["data_collect"],
// data_lib:this.data["data_lib"],
// active_style:this.data['']
// }
// )
},
// 清空收藏,可以放在个性化里面,最好加个确认的选项
clearCollection(){
this.data.data_collect=[]
this.setData({
data_collect:this.data.data_collect,
})
this.save2storage()
},
//清空历史记录,可以放在个性化里面,最好加个确认的选项
clearHistory(){
this.data.data_history=[]
this.setData({
data_history:this.data.data_history,
})
this.save2storage()
},
// 事件处理函数
bindViewTap() {
wx.navigateTo({
url: '../logs/logs',
})
},
getUserProfile() {
// 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认,开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
wx.getUserProfile({
desc: '存储历史记录', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
success: (res) => {
console.log(res, 666)
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
}
})
},
getUserInfo(e: any) {
// 不推荐使用getUserInfo获取用户信息,预计自2021年4月13日起,getUserInfo将不再弹出弹窗,并直接返回匿名的用户个人信息
console.log(e)
this.setData({
userInfo: e.detail.userInfo,
hasUserInfo: true
})
},
getDate: function () {
var nowdate = new Date();
var nowyear = nowdate.getFullYear();
var nowmonth = nowdate.getMonth() + 1;
var nowday = nowdate.getDate();
var nowdata1 = nowyear + '年' + nowmonth + '月' + nowday + '日'
this.setData({
currentDate: nowdata1
});
},
// 根据日期排序历史记录
sortHistory: function () {
let keysSorted = Object.keys(this.data.data_history).sort((a, b) => { return this.cmpDate(a, b) })
// console.log(keysSorted)
var data_history_new = {}
for (var i of keysSorted) {
data_history_new[i] = this.data.data_history[i]
}
// console.log(data_history_new)
// console.log(arrayObj)
// var data_history_new = new Map(arrayObj.map(i => [i[0], i[1]]));
this.setData({
data_history: data_history_new
})
// console.log(this.data.data_history)
// this.data.data_history.sort()
},
cmpDate: function (date0: string, date1: string) {
// console.log(date0,date1)
date0 = date0.split('.')
date1 = date1.split('.')
var y0 = parseInt(date0[0]),
y1 = parseInt(date1[0]),
m0 = parseInt(date0[1]),
m1 = parseInt(date1[1]),
d0 = parseInt(date0[2]),
d1 = parseInt(date1[2])
// console.log(date0, date1, y0, y1, m0, m1, d0, d1)
var res = 0
if (y1 != y0) {
if (y1 > y0) {
res = -1
} else if (y1 < y0) {
res = 1
}
} else if (m1 != m0) {
if (m1 > m0) {
res = -1
} else if (m1 < m0) {
res = 1
}
} else if (d1 != d0) {
if (d1 > d0) {
res = -1
} else if (d1 < d0) {
res = 1
}
}
res = -res
// console.log(res)
return res
},
// 文字转语音
text2speech:function(e:any){
var str = e.target.dataset.name
// console.log(e.target.dataset.name)
// 个人小程序做不了
var plugin = requirePlugin("WechatSI")
// plugin.textToSpeech({
// lang: "zh_CN",
// tts: true,
// content: str,
// success: function(res:any) {
// console.log("succ tts", res.filename)
// const innerAudioContext = wx.createInnerAudioContext()
// innerAudioContext.autoplay = true
// innerAudioContext.src = res.filename
// innerAudioContext.onPlay(() => {
// console.log('开始播放')
// })
// innerAudioContext.onError((res) => {
// console.log(res.errMsg)
// console.log(res.errCode)
// })
// },
// fail: function(res:any) {
// console.log("fail tts", res)
// }
// })
},
// 获取用户code,以给服务端换取openId
getCode: async function () {
var code
await new Promise(function (res, rej) {
wx.login({
success: rcv => {
res(rcv.code)
}, fail: rcv => {
rej(rcv)
}
})
}).then(function (input: unknown) {
code = input
}).catch(function () {
code = ""
})
// console.log(code)
return code
},
testfunction: async function () {
await this.getCode().then(code => {
// code == "" => failed
if (code == "") {
this.testfunction()
} else {
console.log(code)
wx.chooseImage({
success(res) {
const tempFilePaths = res.tempFilePaths
console.log(tempFilePaths)
wx.uploadFile({
url: '' + serverAddr + '/rcvImg', //仅为示例,非真实的接口地址
filePath: tempFilePaths[0],
name: 'Img',
formData: {
'user': code
},
success(res) {
const data = res.data
console.log(data)
}
})
}
})
}
})
}
})