Skip to content

Commit

Permalink
send file
Browse files Browse the repository at this point in the history
  • Loading branch information
trazyn committed Aug 11, 2017
1 parent bc2b4bc commit 86a5d57
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/js/pages/Footer/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ export default class Input extends Component {
async process(file) {
if (!file) return;

var mediaId = await this.props.upload(file);
var { mediaId, type } = await this.props.upload(file);

this.props.sendMessage(this.props.user, {
type: 49 + 6,
type,
file: {
name: file.name,
size: file.size,
Expand Down
4 changes: 2 additions & 2 deletions src/js/pages/Home/ChatContent/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ export default class ChatContent extends Component {
return message.Content;
case 3:
// Image
let images = message.images;
return `<img class="open-image" data-id="${message.MsgId}" src="${images.src}" />`;
let image = message.image;
return `<img class="open-image" data-id="${message.MsgId}" src="${image.src}" />`;
case 34:
/* eslint-disable */
// Voice
Expand Down
44 changes: 30 additions & 14 deletions src/js/stores/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ async function resolveMessage(message) {
break;
case 3:
// Image
let images = helper.parseKV(content);
images.src = `${axios.defaults.baseURL}cgi-bin/mmwebwx-bin/webwxgetmsgimg?&msgid=${message.MsgId}&skey=${auth.skey}`;
message.images = images;
let image = helper.parseKV(content);
image.src = `${axios.defaults.baseURL}cgi-bin/mmwebwx-bin/webwxgetmsgimg?&msgid=${message.MsgId}&skey=${auth.skey}`;
message.image = image;
break;

case 34:
Expand Down Expand Up @@ -378,11 +378,7 @@ class Chat {
}

@action async sendImageMessage(auth, message, isForward) {
var response = await axios.post((
isForward
? '/cgi-bin/mmwebwx-bin/webwxsendmsgimg?fun=async&f=json'
: '/cgi-bin/mmwebwx-bin/webwxsendmsgimg&fun=async'
), {
var response = await axios.post('/cgi-bin/mmwebwx-bin/webwxsendmsgimg?fun=async&f=json', {
BaseRequest: {
Sid: auth.wxsid,
Uin: auth.wxuin,
Expand All @@ -394,6 +390,7 @@ class Chat {
ToUserName: message.to,
ClientMsgId: message.ClientMsgId,
LocalID: message.LocalID,
MediaId: isForward ? '' : message.file.mediaId,
Type: 3,
},
Scene: isForward ? 2 : 0,
Expand All @@ -405,8 +402,12 @@ class Chat {
isme: true,
CreateTime: +new Date() / 1000,
MsgId: response.data.MsgID,
MsgType: 3,
HeadImgUrl: session.user.User.HeadImgUrl,

MsgType: 3,
image: {
src: `${axios.defaults.baseURL}cgi-bin/mmwebwx-bin/webwxgetmsgimg?&msgid=${response.data.MsgID}&skey=${auth.skey}`
}
}),
};

Expand Down Expand Up @@ -494,13 +495,20 @@ class Chat {
LocalID: id,
}), isForward);
} else if (message.type === 3) {
res = await self.sendImageMessage(auth, Object.assign({}, message, {
content: helper.decodeHTML(message.content),
let data = Object.assign({}, message, {
content: '',
from,
to,
ClientMsgId: message.MsgId,
LocalID: id,
}), isForward);
});

if (isForward === true) {
Object.assign(data, {
content: helper.decodeHTML(message.content),
});
}
res = await self.sendImageMessage(auth, data, isForward);
} else if (message.type === 49 + 6) {
res = await self.sendFileMessage(auth, Object.assign({}, message, {
from,
Expand Down Expand Up @@ -546,6 +554,7 @@ class Chat {
var ticket = await helper.getCookie('webwx_data_ticket');
var formdata = new window.FormData();
var server = axios.defaults.baseURL.replace(/https:\/\//, 'https://file.') + 'cgi-bin/mmwebwx-bin/webwxuploadmedia?f=json';
var mediaType = helper.getMediaType(file.name.split('.').slice(-1).pop());

// Increase the counter
self.upload.count = self.upload.count ? 0 : self.upload.count + 1;
Expand All @@ -555,7 +564,7 @@ class Chat {
formdata.append('type', file.type);
formdata.append('lastModifieDate', new Date(file.lastModifieDate).toString());
formdata.append('size', file.size);
formdata.append('mediatype', helper.getMediaType(file.name.split('.').slice(-1).pop()));
formdata.append('mediatype', mediaType);
formdata.append('uploadmediarequest', JSON.stringify({
BaseRequest: {
Sid: auth.wxsid,
Expand All @@ -577,7 +586,14 @@ class Chat {
var response = await axios.post(server, formdata);

if (response.data.BaseResponse.Ret === 0) {
return response.data.MediaId;
return {
mediaId: response.data.MediaId,
type: {
'pic': 3,
'video': 43,
'doc': 49 + 6,
}[mediaType],
};
}

return false;
Expand Down
6 changes: 5 additions & 1 deletion src/js/utils/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,16 @@ const helper = {
return text.replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&amp;/g, '&');
},

isImage: (ext) => {
return ['bmp', 'gif', 'jpeg', 'jpg', 'png'].includes(ext);
},

// 3 types supported: pic, video, doc
getMediaType(ext = '') {
ext = ext.toLowerCase();

switch (true) {
case ['bmp', 'gif', 'jpeg', 'jpg', 'png'].includes(ext):
case helper.isImage(ext):
return 'pic';

case ['mp4'].includes(ext):
Expand Down

0 comments on commit 86a5d57

Please sign in to comment.