STEP 06 微信小程序代碼問題分析
源碼地址:https://github.com/CFETeam/weapp-demo-album
這兒修改成自己的host,全局常量嘛
官方demo 是開發(fā)工具0.9版本出的篡悟,那時候有Promise 方法芹助,現(xiàn)已移除
直接運行會報如下錯誤:
Promise is not a constructor
解決方案:
在request.js文件頭加上一個引用:
var Promise=require('./bluebird.min.js');
module.exports = (options) => {
return new Promise((resolve, reject) => {
options = Object.assign(options, {
success(result) {
if (result.statusCode === 200) {
resolve(result.data);
} else {
reject(result);
}
},
fail: reject,
});
console.log(options);
wx.request(options);
});
};
"bluebird.min.js"文件可以到百度官方下載(77.4kb)
這里涉及的ES5與ES6 可以參考其他文章學(xué)習(xí)
在api.js中:
var config = require('../config.js');
module.exports = {
getUrl(route) {
return `https://${config.host}${config.basePath}${route}`;
},
};
即可看到訪問的api方法
STEP 07 微信相冊程序JS分析調(diào)試
/page/album/album.js就是我們的核心啦
onload方法
onLoad() {
this.renderAlbumList(); 這一步進行渲染相冊列表
this.getAlbumList().then((resp) => { request 訪問我們服務(wù)器再加載數(shù)據(jù)到頁面的"albumlist" list集合
console.log(resp.data);
this.setData({ 'albumList': this.data.albumList.concat(resp.data) });
this.renderAlbumList(); 再次渲染
});
},
getAlbumList方法
// 獲取相冊列表
getAlbumList() {
this.showLoading('加載列表中…');
setTimeout(() => this.hideLoading(), 1000);
return request({ method: 'GET', url: api.getUrl('/list') });
},
打印出來的訪問:
{method: "GET", url: "https://www.wxapptest.cc/applet/album/list"}
renderAlbumList方法(這個不是重點)
// 渲染相冊列表 renderAlbumList() { let layoutColumnSize = this.data.layoutColumnSize; let layoutList = []; console.log(this.data.albumList.length); if (this.data.albumList.length) { console.log("失敗2"); layoutList = listToMatrix([0].concat(this.data.albumList), layoutColumnSize); console.log(layoutList); let lastRow = layoutList[layoutList.length - 1]; if (lastRow.length < layoutColumnSize) { let supplement = Array(layoutColumnSize - lastRow.length).fill(0); lastRow.push(...supplement); } } console.log("失敗3"); this.setData({ layoutList }); },
chooseImage方法(前面這么多廢話斟赚,就只是為了它 镐侯!上傳)
// 從相冊選擇照片或拍攝照片
chooseImage() {
wx.chooseImage({
count: 9,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success: (res) => {
this.showLoading('正在上傳圖片…');
console.log(api.getUrl('/upload'));
wx.uploadFile({
url: api.getUrl('/upload'),
filePath: res.tempFilePaths[0],
name: 'image',
success: (res) => {
let response = JSON.parse(res.data);
if (response.code === 0) {
console.log(response);
let albumList = this.data.albumList;
albumList.unshift(response.data.imgUrl);
this.setData({ albumList });
this.renderAlbumList();
this.showToast('圖片上傳成功');
} else {
console.log(response);
}
},
fail: (res) => {
console.log('fail', res);
},
complete: () => {
this.hideLoading();
},
});
},
});
},```
分析重點
wx.uploadFile({
url: api.getUrl('/upload'), 訪問的是這個: https://www.wxapptest.cc/applet/album/upload
filePath: res.tempFilePaths[0],
name: 'image',
success: (res) => {
let response = JSON.parse(res.data);
if (response.code === 0) {
console.log(response);
let albumList = this.data.albumList;
albumList.unshift(response.data.imgUrl); 獲取上傳后的img的url
this.setData({ albumList });
this.renderAlbumList(); 重新刷新渲染頁面
this.showToast('圖片上傳成功');
} else {
console.log(response);
}
},
fail: (res) => {
console.log('fail', res);
},
complete: () => {
this.hideLoading();
},
});
大致效果如下:
![504.gif](http://upload-images.jianshu.io/upload_images/3362699-f4076980b517dc68.gif?imageMogr2/auto-orient/strip)
![服務(wù)端問題搀继,前文提過](http://upload-images.jianshu.io/upload_images/3362699-8176533d78578c8c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
出現(xiàn)這個問題莫慌。分析問題出在哪兒,是APP端問題還是SERVER響應(yīng)問題
先從APP端debug開始:
#####1.分析APP請求
![請求是發(fā)送成功的了](http://upload-images.jianshu.io/upload_images/3362699-b5b7374221827137.gif?imageMogr2/auto-orient/strip)
只是服務(wù)器返回的執(zhí)行的是fail 方法懈万,說明APP沒問題
![詳細](http://upload-images.jianshu.io/upload_images/3362699-470e7973d30f312c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
uploadfile方法其實很簡單女仰,但是我們要學(xué)會分析問題的方法
我們繼續(xù)追蹤到這個請求酱讶,看服務(wù)器有沒有接收到
>從APP到nginx到nodejs再到對象服務(wù)器(COS)
#####2.接著我們開始查nginx 的log日志
nginx日志在**/etc/nginx/logs**文件夾下
```cd /etc/nginx/logs
tail www.qcloud.la.error.log //查看```
![查看日志](http://upload-images.jianshu.io/upload_images/3362699-a7a9ecb4892f3464.gif?imageMogr2/auto-orient/strip)
貼上日志:
2016/12/05 01:16:15 [error] 3731#3731: *10 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 101.244.40.73, server: www.wxapptest.cc, request: "POST /applet/album/upload HTTP/1.1", upstream: "http://127.0.0.1:9993/applet/album/upload", host: "www.wxapptest.cc"
2016/12/05 01:17:10 [error] 3731#3731: *14 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 101.244.40.73, server: www.wxapptest.cc, request: "POST /applet/alb
![Uploading 77554422121jdf2322w_667991.gif . . .]um/upload HTTP/1.1", upstream: "http://127.0.0.1:9993/applet/album/upload", host: "www.wxapptest.cc"
2016/12/05 01:23:29 [error] 3731#3731: *16 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 101.244.40.73, server: www.wxapptest.cc, request: "POST /applet/album/upload HTTP/1.1", upstream: "http://127.0.0.1:9993/applet/album/upload", host: "www.wxapptest.cc"
2016/12/05 01:23:53 [error] 3731#3731: *20 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 101.244.40.73, server: www.wxapptest.cc, request: "POST /applet/album/upload HTTP/1.1", upstream: "http://127.0.0.1:9993/applet/album/upload", host: "www.wxapptest.cc"
2016/12/05 01:25:00 [error] 3731#3731: *23 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 101.244.40.73, server: www.wxapptest.cc, request: "POST /applet/album/upload HTTP/1.1", upstream: "http://127.0.0.1:9993/applet/album/upload", host: "www.wxapptest.cc"
2016/12/05 01:26:54 [error] 3731#3731: *28 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 101.244.40.73, server: www.wxapptest.cc, request: "POST /applet/album/upload HTTP/1.1", upstream: "http://127.0.0.1:9993/applet/album/upload", host: "www.wxapptest.cc"
發(fā)現(xiàn)nginx服務(wù)器能接受此stream請求交掏,說明nginx并沒有問題
#####3.接著我們開始查nodejs的log日志
前文中說過
pm2 show 0 指令標(biāo)明了nodejs進程運行的日志路徑
打開到根路徑:
cd /root/.pm2/logs
tail album-out.log
操作如下:
![接收到請求](http://upload-images.jianshu.io/upload_images/3362699-c9ab9e3da4159a15.gif?imageMogr2/auto-orient/strip)
tail album-out.log
POST /applet/album/upload - - - - ms
GET /applet/album/list 200 36 - 18.726 ms
GET /applet/album/list 200 36 - 17.683 ms
POST /applet/album/upload - - - - ms
POST /applet/album/upload - - - - ms
POST /applet/album/upload - - - - ms
GET /applet/album/list 200 36 - 16.605 ms
GET /applet/album/list 200 36 - 19.785 ms
GET /applet/album/list 200 36 - 19.398 ms
POST /applet/album/upload - - - - ms
日志已經(jīng)接收到了POST方法沈条,沒毛病
所以酣难,對于此項目我折騰很久也沒運行起來(主要我并不會nodejs而且COS新版SDK沒時間去看)
總結(jié)出是nodejs項目與COS直接的讀取存儲接口發(fā)生了問題
#####downloadImage方法
// 下載圖片
downloadImage() {
this.showLoading('正在保存圖片…');
console.log('download_image_url', this.data.imageInAction);
wx.downloadFile({
url: this.data.imageInAction,
type: 'image',
success: (resp) => {
wx.saveFile({
tempFilePath: resp.tempFilePath,
success: (resp) => {
this.showToast('圖片保存成功');
},
fail: (resp) => {
console.log('fail', resp);
},
complete: (resp) => {
console.log('complete', resp);
this.hideLoading();
},
});
},
fail: (resp) => {
console.log('fail', resp);
},
});
this.setData({ showActionsSheet: false, imageInAction: '' });
},```
其實前面講了那么多了让虐,這個wx.downloadFile()方法顯得特別容易理解了
那些重復(fù)的話我就不寫了