diff --git a/miniprogram/pages/index/index.ts b/miniprogram/pages/index/index.ts index 3973b11..ed1bcc1 100644 --- a/miniprogram/pages/index/index.ts +++ b/miniprogram/pages/index/index.ts @@ -21,74 +21,77 @@ Page({ tip_hidden: false, active_Page: "startPage", currentDate: '', - remarkTemp:'', - resultTitle:"查询结果", + remarkTemp: '', + resultTitle: "查询结果", - pageStack:[], + 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", + 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, - } - ], + // "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 @@ -102,46 +105,6 @@ Page({ // "riskBgColor": "springgreen", // "riskStr": "低风险", // }, - // { - // "date": "2023.5.6", - // "res":{scores: 99, arr: []} , - // "imgUrl": "./static/icons/history.svg", - // "remark": "一二三四五六七", - // "riskBgColor": "sandybrown", - // "riskStr": "中风险" - // }, - // { - // "date": "2023.5.6", - // "res":{scores: 99, arr: []} , - // "imgUrl": "./static/icons/history.svg", - // "remark": "一二三四五六七", - // "riskBgColor": "tomato", - // "riskStr": "高风险" - // }, - // { - // "date": "2023.15.16", - // "res":{scores: 99, arr: []} , - // "imgUrl": "./static/icons/history.svg", - // "remark": "方师傅康便面", - // "riskBgColor": "springgreen", - // "riskStr": "低风险" - // }, - // { - // "date": "2023.5.6", - // "res":{scores: 1, arr: []} , - // "imgUrl": "./static/icons/history.svg", - // "remark": "一二三四五六七", - // "riskBgColor": "tomato", - // "riskStr": "高风险" - // }, - // { - // "date": "2023.5.6", - // "res":{scores: 99, arr: []} , - // "imgUrl": "./static/icons/history.svg", - // "remark": "一二三四五六七", - // "riskBgColor": "tomato", - // "riskStr": "高风险" - // }, ], // 存储所有成分的信息, hidden: false, "riskBgColor": "springgreen", "riskStr": "低风险"由前端实现 @@ -196,13 +159,13 @@ Page({ ], // 知识库搜索用 - hidden_lib:{}, + hidden_lib: {}, // 查询结果搜索用 - hidden_result:{}, + hidden_result: {}, - data_lib_map:{}, + data_lib_map: {}, //查询展示用 data_resultPage: { @@ -210,11 +173,11 @@ Page({ circleClip: "", circleColor: "", arr: [], - tr:null,//top right + tr: null,//top right }, //收藏/取消收藏用 - resultId:-1, + resultId: -1, // 收到的查询结果信息 data_rcv: { @@ -222,8 +185,6 @@ Page({ arr: ["山梨酸钾"], }, - - sessionKey: "", userInfo: {}, hasUserInfo: false, @@ -240,7 +201,7 @@ Page({ // this.updateSession() // },60*1000*5) // console.log(this.data.sessionKey) - + this.readStorageData() }, //开始界面的按钮相关动画 @@ -394,7 +355,7 @@ Page({ this.startPage_Bottons_in() this.animate('#page_lib', [ { opacity: 1, translateY: "-100%" }, { opacity: 0, translateY: "0", ease: 'ease-in-out' }, - ], 1000, ()=>{ + ], 1000, () => { // for(var n of this.data.data_lib){ // n.hidden=false // } @@ -459,7 +420,7 @@ Page({ ], 600) }, - + exit_result() { this.hide_curtain() this.animate('#page_result', [ @@ -529,131 +490,151 @@ Page({ this.hide_curtain2() }, - + // 根据传入的包含scores和arr的object更新this.data.data_resultPage并popup_result - show_result(res: { scores: any,arr:Array } ,tr:string) { + show_result(res: { scores: any, arr: Array }, tr: string) { // console.log(res) - var data_resultPage={ + var data_resultPage = { scores: res.scores, arr: [], - tr:tr, + tr: tr, } console.log(tr) - for(var name of res.arr){ + 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)){ + 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 + "data_resultPage": data_resultPage }) // console.log(data_resultPage) this.popup_result() }, - show_result_wxml(e:any){ - var res=e.currentTarget.dataset.index; + show_result_wxml(e: any) { + var res = e.currentTarget.dataset.index; console.log(res) - this.data.resultId=res.id + this.data.resultId = res.id // console.log(this.data.resultId) var tr = "" - switch(this.data.pageStack[this.data.pageStack.length-1]){ - case "collect":{ + // 不太好吧 + switch (this.data.pageStack[this.data.pageStack.length - 1]) { + case "collect": { tr = "delete" break; } - case "history":{ - if(res.hasCollect) + case "history": { + if (res.hasCollect) tr = "delete" - else + else tr = "heart" break; } } - res=res.res - this.show_result(res,tr) + 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") + } + } + } }, - getInputRemark(e:any){ + getInputRemark(e: any) { this.data.remarkTemp = e.detail.value }, // 收藏当前页面 - add_collection(){ + add_collection() { var r = this.id2resultAndDate(this.data.resultId) - if(r == null)return + if (r == null) return var result = r.result var date = r.date var remark = this.data.remarkTemp - console.log(result,date,remark) + 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, + "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" + 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} + { + data_collect: this.data.data_collect, + data_history: this.data.data_history, + data_resultPage: this.data.data_resultPage + } ) + this.save2storage() this.exit_setRemark() }, - - editRemark(){ + + editRemark() { var r = this.id2resultAndDate(this.data.resultId) - if(r == null)return + 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 + 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} + { + data_collect: this.data.data_collect, + data_history: this.data.data_history, + data_resultPage: this.data.data_resultPage + } ) + this.save2storage() this.exit_editRemark() }, //取消收藏。。。。 - remove_collection(){ + remove_collection() { var flag = true var r = this.id2resultAndDate(this.data.resultId) - if(r == null) flag = false - if(flag){ + if (r == null) flag = false + if (flag) { var result = r.result - result.hasCollect=false + 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) + 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.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} + { + data_collect: this.data.data_collect, + data_history: this.data.data_history, + data_resultPage: this.data.data_resultPage + } ) + this.save2storage() this.exit_removeCollectionVerify() }, @@ -681,95 +662,98 @@ Page({ return res }, - //收到搜索结果处理 - receive_result(){ + //收到搜索结果处理(并没有写) + receive_result() { }, - //收到成分数据更新处理 - receive_lib(){ + //收到成分数据更新处理(并没有写) + receive_lib() { }, //更新成分数据 - updateLib(){ + updateLib() { wx.request({ - url: 'http://'+serverAddr+'/getLib', //仅为示例,并非真实的接口地址 + url: 'http://' + serverAddr + '/getLib', //仅为示例,并非真实的接口地址 data: { }, - method:"GET", + method: "GET", header: { 'content-type': 'application/json' // 默认值 }, - success:res=> { + success: res => { // console.log(res) var data = res.data console.log(data) // 能跑 - for(var n of data){ + 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} + { "data_lib": data } ) + this.save2storage() }, - fail(res){ + fail(res) { console.log(res) } }) }, //更新历史记录 - updateHistory(){ + updateHistory() { wx.request({ - url: 'http://'+serverAddr+'/getHistory', //仅为示例,并非真实的接口地址 + url: 'http://' + serverAddr + '/getHistory', //仅为示例,并非真实的接口地址 data: { - userCode:this.data.sessionKey + userCode: this.data.sessionKey }, - method:"POST", + method: "POST", header: { 'content-type': 'application/json' // 默认值 }, - success:res=> { + success: res => { console.log(res) var data = res.data this.setData( - {"data_history" : data} + { "data_history": data } ) + this.save2storage() }, - fail(res){ + fail(res) { console.log(res) } }) }, // 更新session - updateSession(){ - return new Promise((resolve, reject)=>{ + updateSession() { + return new Promise((resolve, reject) => { this.getCode().then(code => { wx.request({ - url: 'http://'+serverAddr+'/updateSession', //仅为示例,并非真实的接口地址 + url: 'http://' + serverAddr + '/updateSession', //仅为示例,并非真实的接口地址 data: { - userCode:code + userCode: code }, - method:"POST", + method: "POST", header: { 'content-type': 'application/json' // 默认值 }, - success:res=> { + success: res => { console.log(res) var data = res.data this.setData( - {"sessionKey" : data} + { "sessionKey": data } ) resolve() // setTimeout(this.updateSession,1000*60*5) }, - fail:res=>{ + fail: res => { console.log(res) // setTimeout(this.updateSession,1000) reject() @@ -779,6 +763,57 @@ Page({ }) }, + // 上传照片并 + uploadImg(tempImagePath: any) { + wx.getFileSystemManager().readFile({ + filePath: tempImagePath, + encoding: 'base64', + success: res => { + // console.log('data:image/png;base64,' + res.data) + const imgBase64 = res.data + wx.request({ + url: 'http://' + serverAddr + '/rcvImg', //仅为示例,并非真实的接口地址 + data: { + imgBase64: imgBase64 + }, + method: "POST", + header: { + 'content-type': 'application/json' // 默认值 + }, + success: res => { + var data = res.data + console.log(res.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,//目前全是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) + } + }) + } + }) + }, + //拍照 takePhoto() { const ctx = wx.createCameraContext() @@ -790,30 +825,7 @@ Page({ const tempImagePath = res.tempImagePath console.log(tempImagePath) console.log(res) - wx.getFileSystemManager().readFile({ - filePath: tempImagePath, //选择图片返回的相对路径 - encoding: 'base64', //编码格式 - success: res => { //成功的回调 - console.log('data:image/png;base64,' + res.data) - const imgBase64 = res.data - wx.request({ - url: 'http://'+serverAddr+'/rcvImg', //仅为示例,并非真实的接口地址 - data: { - imgBase64:imgBase64 - }, - method:"POST", - header: { - 'content-type': 'application/json' // 默认值 - }, - success (res) { - console.log(res) - }, - fail(res){ - console.log(res) - } - }) - } - }) + this.uploadImg(tempImagePath) } }) }, @@ -825,35 +837,11 @@ Page({ sourceType: ['album'], sizeType: ["compressed"], count: 1, - success(res) { + success: res => { const tempImagePath = res.tempFiles[0].tempFilePath console.log(tempImagePath) console.log(res) - wx.getFileSystemManager().readFile({ - filePath: tempImagePath, //选择图片返回的相对路径 - encoding: 'base64', //编码格式 - success: res => { //成功的回调 - console.log('data:image/png;base64,' + res.data) - const imgBase64 = res.data - // 发送图片 - wx.request({ - url: 'http://'+serverAddr+'/rcvImg', //仅为示例,并非真实的接口地址 - data: { - imgBase64:imgBase64 - }, - method:"POST", - header: { - 'content-type': 'application/json' // 默认值 - }, - success (res) { - console.log(res) - }, - fail(res){ - console.log(res) - } - }) - } - }) + this.uploadImg(tempImagePath) } }) }, @@ -947,17 +935,44 @@ Page({ 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} - } + 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'] + for (var k of list) { + wx.setStorage({ + key: k, + data: this.data[k], + }) + } + }, + + // 从本地读取各种数据 + readStorageData(){ + var list = ['data_history', 'data_collect', 'data_lib'] + for (var k of list) { + this.data[k]=wx.getStorageSync(k) + console.log(wx.getStorageSync(k)) + } + this.setData( + { + data_history:this.data["data_history"], + data_collect:this.data["data_collect"], + data_lib:this.data["data_lib"], + } + ) + + }, // 事件处理函数 bindViewTap() { @@ -998,6 +1013,60 @@ Page({ }); }, + // 根据日期排序历史记录 + 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 + }, + // 获取用户code,以给服务端换取openId getCode: async function () { var code @@ -1030,7 +1099,7 @@ Page({ const tempFilePaths = res.tempFilePaths console.log(tempFilePaths) wx.uploadFile({ - url: 'http://'+serverAddr+'/rcvImg', //仅为示例,非真实的接口地址 + url: 'http://' + serverAddr + '/rcvImg', //仅为示例,非真实的接口地址 filePath: tempFilePaths[0], name: 'Img', formData: {