您好,登錄后才能下訂單哦!
用戶可以上傳了和用戶的face更新到數(shù)據(jù)庫,接下來我們需要對圖片進(jìn)行展示,tomcat本身就提供了虛擬目錄的概念,直接把某個路徑的圖片映射到web服務(wù)器作為資源路徑。其實(shí)在springboot可以通過代碼的方式來進(jìn)行映射。源碼:https://github.com/limingios/wxProgram.git 中No.15
api 中新建類
package?com.idig8; import?org.springframework.beans.factory.annotation.Value; import?org.springframework.context.annotation.Configuration; import?org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import?org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration public?class?WebMvcConfig?extends?WebMvcConfigurerAdapter?{ ????@Value("${server.face.path}") ????private?String?fileSpace; ????@Override ????public?void?addResourceHandlers(ResourceHandlerRegistry?registry)?{ ????????//資源的路徑.swagger2的資源.所在的目錄, ????????registry.addResourceHandler("/**") ????????.addResourceLocations("classpath:/META-INF/resources/") ????????.addResourceLocations("file:"+fileSpace); ????} }
里面調(diào)用了wx api的插件,所以直接用this.setData就會報(bào)錯。VM708:1 thirdScriptError
Cannot read property ‘setData’ of null;at pages/mine/mine onShow function;at api uploadFile success callback function
TypeError: Cannot read property ‘setData’ of null 需要先將this復(fù)制給一個變量,然后通過變量.setData
//?pages/mine/mine.js const?app?=?getApp() Page({ ??/** ???*?頁面的初始數(shù)據(jù) ???*/ ??data:?{ ????faceUrl:?"../../resource/images/noneface.png", ????nickname:?"昵稱", ????fansCounts:?0, ????followCounts:?0, ????receiveLikeCounts:?0, ??}, ??/** ???*?用戶注銷 ???*/ ??logout:function(e){ ????var?user?=?app.userInfo; ????wx.showLoading({ ??????title:?'正在注銷中。。。' ????}); ????wx.request({ ??????url:?app.serverUrl?+?"/logout?userId="+user.id, ??????method:?"POST", ??????header:?{ ????????'content-type':?'application/json'?//?默認(rèn)值 ??????}, ??????success:?function?(res)?{ ????????console.log(res.data); ????????var?status?=?res.data.status; ????????wx.hideLoading(); ????????if?(status?==?200)?{ ??????????wx.showToast({ ????????????title:?"用戶注銷成功~!", ????????????icon:?'none', ????????????duration:?3000 ??????????}) ??????????app.userInfo?=?null; ??????????wx.redirectTo({ ????????????url:?'../userRegister/userRegister', ??????????}) ????????}?else?if?(status?==?500)?{ ??????????wx.showToast({ ????????????title:?res.data.msg, ????????????icon:?'none', ????????????duration:?3000 ??????????}) ????????} ??????} ????}) ??}, ??/** ???*?頭像上傳 ???*/ ??uploadFace:function(e){ ????var?user?=?app.userInfo; ????var?me?=?this; ????wx.chooseImage({ ??????count:?1,?//?默認(rèn)9 ??????sizeType:?['compressed'],?//?可以指定是原圖還是壓縮圖,默認(rèn)二者都有 ??????sourceType:?['album'],?//?可以指定來源是相冊還是相機(jī),默認(rèn)二者都有 ??????success:?function?(res)?{ ????????//?返回選定照片的本地文件路徑列表,tempFilePath可以作為img標(biāo)簽的src屬性顯示圖片 ????????var?tempFilePaths?=?res.tempFilePaths ????????if?(tempFilePaths.length>0){ ??????????console.log(tempFilePaths[0]); ??????????wx.showLoading({ ????????????title:?'正在加載中。。。' ??????????}); ??????????wx.chooseImage({ ????????????success:?function?(res)?{ ??????????????var?tempFilePaths?=?res.tempFilePaths ??????????????wx.uploadFile({ ????????????????url:?app.serverUrl?+?"/user/uploadFace?userId="?+?user.id,?//僅為示例,非真實(shí)的接口地址 ????????????????filePath:?tempFilePaths[0], ????????????????name:?'file', ????????????????success:?function?(res)?{ ??????????????????var?data?=?JSON.parse(res.data); ??????????????????console.log(data); ???????????????????wx.hideLoading(); ??????????????????if?(data.status?==?200)?{ ????????????????????wx.showToast({ ??????????????????????title:?"用戶上傳成功~!", ??????????????????????icon:?'none', ??????????????????????duration:?3000 ????????????????????}) ????????????????????me.setData({ ??????????????????????faceUrl:?app.serverUrl+data.data ????????????????????}) ??????????????????}?else?if?(data.status?==?500)?{ ????????????????????wx.showToast({ ??????????????????????title:?data.msg, ??????????????????????icon:?'none', ??????????????????????duration:?3000 ????????????????????}) ??????????????????} ????????????????} ??????????????}) ????????????} ??????????}) ????????} ??????} ????}) ??}, ??/** ???*?生命周期函數(shù)--監(jiān)聽頁面加載 ???*/ ??onLoad:?function?(options)?{ ??}, ??/** ???*?生命周期函數(shù)--監(jiān)聽頁面初次渲染完成 ???*/ ??onReady:?function?()?{ ??}, ??/** ???*?生命周期函數(shù)--監(jiān)聽頁面顯示 ???*/ ??onShow:?function?()?{ ??}, ??/** ???*?生命周期函數(shù)--監(jiān)聽頁面隱藏 ???*/ ??onHide:?function?()?{ ??}, ??/** ???*?生命周期函數(shù)--監(jiān)聽頁面卸載 ???*/ ??onUnload:?function?()?{ ??}, ??/** ???*?頁面相關(guān)事件處理函數(shù)--監(jiān)聽用戶下拉動作 ???*/ ??onPullDownRefresh:?function?()?{ ??}, ??/** ???*?頁面上拉觸底事件的處理函數(shù) ???*/ ??onReachBottom:?function?()?{ ??}, ??/** ???*?用戶點(diǎn)擊右上角分享 ???*/ ??onShareAppMessage:?function?()?{ ??} })
點(diǎn)擊手機(jī)預(yù)覽
手機(jī)掃描,進(jìn)行登錄
>但是始終無法登錄
在手機(jī)上如何像工具一樣正常登錄呢?
>1. 手機(jī)app 和 后臺 在同一個網(wǎng)段,也就是同一個wifi
>2. 打開調(diào)試模式,重啟登錄小程序
>3. 還有個不在同一個wifi的話,可以通過內(nèi)網(wǎng)穿透的方式,之前說過,但是app.js里面設(shè)置下內(nèi)網(wǎng)穿透的ip
PS:這次試用itools的方式在手機(jī)也演示了如何進(jìn)行圖片的選擇和上傳。wx的插件做的很棒,直接引用不會存在各種問題。穩(wěn)~!
>>原創(chuàng)文章,歡迎轉(zhuǎn)載。轉(zhuǎn)載請注明:轉(zhuǎn)載自IT人故事會,謝謝!
>>原文鏈接地址:「小程序JAVA實(shí)戰(zhàn)」小程序頭像圖片上傳(中)(44)
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。