├── .gitignore ├── test.png ├── .gitattributes ├── .vscode ├── settings.json ├── extensions.json └── launch.json ├── cache └── qrcode │ ├── 1820180913113944.png │ ├── 1820180913114008.png │ ├── 1820180913114210.png │ ├── 1820180913114434.png │ ├── 1820180913115834.png │ ├── 1820180913115852.png │ ├── 1820180913115917.png │ ├── 1820180913120119.png │ ├── 1820180913120143.png │ ├── 1820180913120344.png │ ├── 1820180913120409.png │ ├── 1820180913120610.png │ └── 1820180913120635.png ├── .vscodeignore ├── src ├── lineType │ ├── log.js │ ├── img.js │ ├── factory.js │ └── chat.js ├── View.js ├── template │ ├── main.css │ └── main.html ├── WechatDisplayer.js └── wechat.js ├── CHANGELOG.md ├── jsconfig.json ├── .eslintrc.json ├── test ├── extension.test.js └── index.js ├── package.json ├── vsc-extension-quickstart.md ├── README.md └── extension.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .vscode-test/ 3 | *.vsix 4 | -------------------------------------------------------------------------------- /test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/superhos/wechat-on-vscode/master/test.png -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Set default behavior to automatically normalize line endings. 2 | * text=auto 3 | 4 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | // Place your settings in this file to overwrite default and user settings. 2 | { 3 | } -------------------------------------------------------------------------------- /cache/qrcode/1820180913113944.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/superhos/wechat-on-vscode/master/cache/qrcode/1820180913113944.png -------------------------------------------------------------------------------- /cache/qrcode/1820180913114008.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/superhos/wechat-on-vscode/master/cache/qrcode/1820180913114008.png -------------------------------------------------------------------------------- /cache/qrcode/1820180913114210.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/superhos/wechat-on-vscode/master/cache/qrcode/1820180913114210.png -------------------------------------------------------------------------------- /cache/qrcode/1820180913114434.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/superhos/wechat-on-vscode/master/cache/qrcode/1820180913114434.png -------------------------------------------------------------------------------- /cache/qrcode/1820180913115834.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/superhos/wechat-on-vscode/master/cache/qrcode/1820180913115834.png -------------------------------------------------------------------------------- /cache/qrcode/1820180913115852.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/superhos/wechat-on-vscode/master/cache/qrcode/1820180913115852.png -------------------------------------------------------------------------------- /cache/qrcode/1820180913115917.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/superhos/wechat-on-vscode/master/cache/qrcode/1820180913115917.png -------------------------------------------------------------------------------- /cache/qrcode/1820180913120119.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/superhos/wechat-on-vscode/master/cache/qrcode/1820180913120119.png -------------------------------------------------------------------------------- /cache/qrcode/1820180913120143.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/superhos/wechat-on-vscode/master/cache/qrcode/1820180913120143.png -------------------------------------------------------------------------------- /cache/qrcode/1820180913120344.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/superhos/wechat-on-vscode/master/cache/qrcode/1820180913120344.png -------------------------------------------------------------------------------- /cache/qrcode/1820180913120409.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/superhos/wechat-on-vscode/master/cache/qrcode/1820180913120409.png -------------------------------------------------------------------------------- /cache/qrcode/1820180913120610.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/superhos/wechat-on-vscode/master/cache/qrcode/1820180913120610.png -------------------------------------------------------------------------------- /cache/qrcode/1820180913120635.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/superhos/wechat-on-vscode/master/cache/qrcode/1820180913120635.png -------------------------------------------------------------------------------- /.vscodeignore: -------------------------------------------------------------------------------- 1 | .vscode/** 2 | .vscode-test/** 3 | test/** 4 | .gitignore 5 | jsconfig.json 6 | vsc-extension-quickstart.md 7 | .eslintrc.json 8 | -------------------------------------------------------------------------------- /src/lineType/log.js: -------------------------------------------------------------------------------- 1 | class log { 2 | render(e){ 3 | return `${e.date} LOG [${e.content}]
`; 4 | } 5 | } 6 | 7 | module.exports = log; -------------------------------------------------------------------------------- /src/lineType/img.js: -------------------------------------------------------------------------------- 1 | class img { 2 | render(e){ 3 | return `${e.date} IMG
`; 4 | } 5 | } 6 | 7 | module.exports = img; -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | // See http://go.microsoft.com/fwlink/?LinkId=827846 3 | // for the documentation about the extensions.json format 4 | "recommendations": [ 5 | "dbaeumer.vscode-eslint" 6 | ] 7 | } -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | All notable changes to the "wechat-on-vscode" extension will be documented in this file. 3 | 4 | Check [Keep a Changelog](http://keepachangelog.com/) for recommendations on how to structure this file. 5 | 6 | ## [Unreleased] 7 | - Initial release -------------------------------------------------------------------------------- /jsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "module": "commonjs", 4 | "target": "es6", 5 | "checkJs": true, /* Typecheck .js files. */ 6 | "lib": [ 7 | "es6" 8 | ] 9 | }, 10 | "exclude": [ 11 | "node_modules" 12 | ] 13 | } -------------------------------------------------------------------------------- /src/lineType/factory.js: -------------------------------------------------------------------------------- 1 | class factory{ 2 | static getType(type){ 3 | const lineType = require(`./${type}`); 4 | if (lineType){ 5 | return new lineType(); 6 | }else{ 7 | console.log(lineType); 8 | } 9 | } 10 | } 11 | 12 | module.exports = factory; -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "browser": false, 4 | "commonjs": true, 5 | "es6": true, 6 | "node": true 7 | }, 8 | "parserOptions": { 9 | "ecmaFeatures": { 10 | "jsx": true 11 | }, 12 | "sourceType": "module", 13 | "ecmaVersion": 2017 14 | }, 15 | "rules": { 16 | "no-const-assign": "warn", 17 | "no-this-before-super": "warn", 18 | "no-undef": "warn", 19 | "no-unreachable": "warn", 20 | "no-unused-vars": "warn", 21 | "constructor-super": "warn", 22 | "valid-typeof": "warn" 23 | } 24 | } -------------------------------------------------------------------------------- /src/lineType/chat.js: -------------------------------------------------------------------------------- 1 | class chat { 2 | render(e){ 3 | // return ` 【${e.date}】 ${e.content}
`; 4 | return `${e.date} INFO [ ${e.from.name()} ---> ${e.to.name()} ] ${e.content}
`; 5 | } 6 | } 7 | 8 | module.exports = chat; -------------------------------------------------------------------------------- /src/View.js: -------------------------------------------------------------------------------- 1 | const fs = require('fs'); 2 | const _ = require('lodash'); 3 | 4 | class View { 5 | 6 | constructor(){ 7 | this.original = fs.readFileSync(`${__dirname}/template/main.html`).toString(); 8 | this.styleSheet = fs.readFileSync(`${__dirname}/template/main.css`).toString(); 9 | } 10 | 11 | render(data){ 12 | let template = _.cloneDeep(this.original); 13 | for (const key in data){ 14 | template = template.replace('\$\{'+key+'\}',data[key]); 15 | } 16 | 17 | template = template.replace('\$\{styles\}',this.styleSheet); 18 | // console.log(template); 19 | return template; 20 | } 21 | 22 | } 23 | 24 | module.exports = View; -------------------------------------------------------------------------------- /test/extension.test.js: -------------------------------------------------------------------------------- 1 | /* global suite, test */ 2 | 3 | // 4 | // Note: This example test is leveraging the Mocha test framework. 5 | // Please refer to their documentation on https://mochajs.org/ for help. 6 | // 7 | 8 | // The module 'assert' provides assertion methods from node 9 | const assert = require('assert'); 10 | 11 | // You can import and use all API from the 'vscode' module 12 | // as well as import your extension to test it 13 | // const vscode = require('vscode'); 14 | // const myExtension = require('../extension'); 15 | 16 | // Defines a Mocha test suite to group tests of similar kind together 17 | suite("Extension Tests", function() { 18 | 19 | // Defines a Mocha unit test 20 | test("Something 1", function() { 21 | assert.equal(-1, [1, 2, 3].indexOf(5)); 22 | assert.equal(-1, [1, 2, 3].indexOf(0)); 23 | }); 24 | }); -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | // A launch configuration that launches the extension inside a new window 2 | // Use IntelliSense to learn about possible attributes. 3 | // Hover to view descriptions of existing attributes. 4 | // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 5 | { 6 | "version": "0.2.0", 7 | "configurations": [ 8 | { 9 | "name": "Extension", 10 | "type": "extensionHost", 11 | "request": "launch", 12 | "runtimeExecutable": "${execPath}", 13 | "args": [ 14 | "--extensionDevelopmentPath=${workspaceFolder}" 15 | ] 16 | }, 17 | { 18 | "name": "Extension Tests", 19 | "type": "extensionHost", 20 | "request": "launch", 21 | "runtimeExecutable": "${execPath}", 22 | "args": [ 23 | "--extensionDevelopmentPath=${workspaceFolder}", 24 | "--extensionTestsPath=${workspaceFolder}/test" 25 | ] 26 | } 27 | ] 28 | } -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | // 2 | // PLEASE DO NOT MODIFY / DELETE UNLESS YOU KNOW WHAT YOU ARE DOING 3 | // 4 | // This file is providing the test runner to use when running extension tests. 5 | // By default the test runner in use is Mocha based. 6 | // 7 | // You can provide your own test runner if you want to override it by exporting 8 | // a function run(testRoot: string, clb: (error:Error) => void) that the extension 9 | // host can call to run the tests. The test runner is expected to use console.log 10 | // to report the results back to the caller. When the tests are finished, return 11 | // a possible error to the callback or null if none. 12 | 13 | const testRunner = require('vscode/lib/testrunner'); 14 | 15 | // You can directly control Mocha options by uncommenting the following lines 16 | // See https://github.com/mochajs/mocha/wiki/Using-mocha-programmatically#set-options for more info 17 | testRunner.configure({ 18 | ui: 'tdd', // the TDD UI is being used in extension.test.js (suite, test, etc.) 19 | useColors: true // colored output from test results 20 | }); 21 | 22 | module.exports = testRunner; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "wechat-on-vscode", 3 | "displayName": "wechat-on-vscode", 4 | "description": "", 5 | "version": "0.0.1", 6 | "publisher": "SevensChan", 7 | "engines": { 8 | "vscode": "^1.27.0" 9 | }, 10 | "categories": [ 11 | "Other" 12 | ], 13 | "activationEvents": [ 14 | "onCommand:extension.start" 15 | ], 16 | "main": "./extension", 17 | "contributes": { 18 | "commands": [ 19 | { 20 | "command": "extension.start", 21 | "title": "wechat:start" 22 | } 23 | ] 24 | }, 25 | "scripts": { 26 | "postinstall": "node ./node_modules/vscode/bin/install", 27 | "test": "node ./node_modules/vscode/bin/test" 28 | }, 29 | "devDependencies": { 30 | "typescript": "^2.6.1", 31 | "vscode": "^1.1.21", 32 | "eslint": "^4.11.0", 33 | "@types/node": "^8.10.25", 34 | "@types/mocha": "^2.2.42" 35 | }, 36 | "dependencies": { 37 | "lodash": "^4.17.11", 38 | "moment": "^2.22.2", 39 | "request": "^2.88.0", 40 | "wechaty": "^0.22.4", 41 | "wechaty-puppet-padchat": "^0.16.2" 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/template/main.css: -------------------------------------------------------------------------------- 1 | body{ 2 | padding:0; 3 | margin:0; 4 | overflow-x:hidden; 5 | } 6 | 7 | .line { 8 | padding:10px 3% 30px; 9 | } 10 | 11 | .command{ 12 | background:#202020; 13 | height:22px; 14 | border-top:1px solid #7f7f7f; 15 | width:100%; 16 | position:fixed; 17 | bottom:0; 18 | left:0; 19 | } 20 | 21 | .command span{ 22 | color:#d8d8d8; 23 | font-weight:bold; 24 | padding:0 5px; 25 | width:5%; 26 | } 27 | 28 | .command a{ 29 | font-size:13px; 30 | color:#f1f1f1; 31 | padding-right:5px; 32 | cursor:pointer; 33 | width:15%; 34 | overflow:hidden; 35 | white-space:nowrap; 36 | text-overflow:ellipsis 37 | } 38 | 39 | .command input{ 40 | font-size:13px; 41 | width:80%; 42 | height:22px; 43 | color:#d8d8d8; 44 | outline:none; 45 | border:0; 46 | background:transparent; 47 | } 48 | 49 | .command .chat_list{ 50 | position:absolute; 51 | background:#202020; 52 | border:1px solid #f1f1f1; 53 | border-radius: 3px; 54 | min-height:100px; 55 | width:100px; 56 | bottom:23px; 57 | left:2.5%; 58 | z-index: 999; 59 | } 60 | 61 | .mask{ 62 | width:100%; 63 | height:100%; 64 | left:0; 65 | top:0; 66 | position: fixed; 67 | z-index: 1; 68 | } -------------------------------------------------------------------------------- /vsc-extension-quickstart.md: -------------------------------------------------------------------------------- 1 | # Welcome to your VS Code Extension 2 | 3 | ## What's in the folder 4 | * This folder contains all of the files necessary for your extension. 5 | * `package.json` - this is the manifest file in which you declare your extension and command. 6 | The sample plugin registers a command and defines its title and command name. With this information 7 | VS Code can show the command in the command palette. It doesn’t yet need to load the plugin. 8 | * `extension.js` - this is the main file where you will provide the implementation of your command. 9 | The file exports one function, `activate`, which is called the very first time your extension is 10 | activated (in this case by executing the command). Inside the `activate` function we call `registerCommand`. 11 | We pass the function containing the implementation of the command as the second parameter to 12 | `registerCommand`. 13 | 14 | ## Get up and running straight away 15 | * Press `F5` to open a new window with your extension loaded. 16 | * Run your command from the command palette by pressing (`Ctrl+Shift+P` or `Cmd+Shift+P` on Mac) and typing `Hello World`. 17 | * Set breakpoints in your code inside `extension.js` to debug your extension. 18 | * Find output from your extension in the debug console. 19 | 20 | ## Make changes 21 | * You can relaunch the extension from the debug toolbar after changing code in `extension.js`. 22 | * You can also reload (`Ctrl+R` or `Cmd+R` on Mac) the VS Code window with your extension to load your changes. 23 | 24 | ## Explore the API 25 | * You can open the full set of our API when you open the file `node_modules/vscode/vscode.d.ts`. 26 | 27 | ## Run tests 28 | * Open the debug viewlet (`Ctrl+Shift+D` or `Cmd+Shift+D` on Mac) and from the launch configuration dropdown pick `Launch Tests`. 29 | * Press `F5` to run the tests in a new window with your extension loaded. 30 | * See the output of the test result in the debug console. 31 | * Make changes to `test/extension.test.js` or create new test files inside the `test` folder. 32 | * By convention, the test runner will only consider files matching the name pattern `**.test.js`. 33 | * You can create folders inside the `test` folder to structure your tests any way you want. 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # wechat-on-vscode README 2 | 3 | This is the README for your extension "wechat-on-vscode". After writing up a brief description, we recommend including the following sections. 4 | 5 | ## Features 6 | 7 | Describe specific features of your extension including screenshots of your extension in action. Image paths are relative to this README file. 8 | 9 | For example if there is an image subfolder under your extension project workspace: 10 | 11 | \!\[feature X\]\(images/feature-x.png\) 12 | 13 | > Tip: Many popular extensions utilize animations. This is an excellent way to show off your extension! We recommend short, focused animations that are easy to follow. 14 | 15 | ## Requirements 16 | 17 | If you have any requirements or dependencies, add a section describing those and how to install and configure them. 18 | 19 | ## Extension Settings 20 | 21 | Include if your extension adds any VS Code settings through the `contributes.configuration` extension point. 22 | 23 | For example: 24 | 25 | This extension contributes the following settings: 26 | 27 | * `myExtension.enable`: enable/disable this extension 28 | * `myExtension.thing`: set to `blah` to do something 29 | 30 | ## Known Issues 31 | 32 | Calling out known issues can help limit users opening duplicate issues against your extension. 33 | 34 | ## Release Notes 35 | 36 | Users appreciate release notes as you update your extension. 37 | 38 | ### 1.0.0 39 | 40 | Initial release of ... 41 | 42 | ### 1.0.1 43 | 44 | Fixed issue #. 45 | 46 | ### 1.1.0 47 | 48 | Added features X, Y, and Z. 49 | 50 | ----------------------------------------------------------------------------------------------------------- 51 | 52 | ## Working with Markdown 53 | 54 | **Note:** You can author your README using Visual Studio Code. Here are some useful editor keyboard shortcuts: 55 | 56 | * Split the editor (`Cmd+\` on macOS or `Ctrl+\` on Windows and Linux) 57 | * Toggle preview (`Shift+CMD+V` on macOS or `Shift+Ctrl+V` on Windows and Linux) 58 | * Press `Ctrl+Space` (Windows, Linux) or `Cmd+Space` (macOS) to see a list of Markdown snippets 59 | 60 | ### For more information 61 | 62 | * [Visual Studio Code's Markdown Support](http://code.visualstudio.com/docs/languages/markdown) 63 | * [Markdown Syntax Reference](https://help.github.com/articles/markdown-basics/) 64 | 65 | **Enjoy!** 66 | -------------------------------------------------------------------------------- /src/template/main.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 10 | 11 | 12 | 13 | 14 |
15 |
16 |
17 | 打開chrome調試 18 |
19 |
20 | > 21 | 22 | {{toUser}} 23 | 24 | 25 | 26 |
27 |
28 | 67 | 68 | 69 | -------------------------------------------------------------------------------- /extension.js: -------------------------------------------------------------------------------- 1 | const vscode = require('vscode'); 2 | 3 | function activate(context) { 4 | 5 | // Use the console to output diagnostic information (console.log) and errors (console.error) 6 | // This line of code will only be executed once when your extension is activated 7 | console.log('Congratulations, your extension "wechat-on-vscode" is now active!'); 8 | // The command has been defined in the package.json file 9 | // Now provide the implementation of the command with registerCommand 10 | // The commandId parameter must match the command field in package.json 11 | const Wechat = require('./src/wechat'); 12 | const wechat = new Wechat(vscode); 13 | 14 | // set up new custom provider 15 | 16 | let startDis = vscode.commands.registerCommand('extension.start', function () { 17 | 18 | wechat.start(); 19 | vscode.window.onDidChangeActiveTextEditor(() => { 20 | console.log('change active'); 21 | }) 22 | return vscode.commands.executeCommand('vscode.previewHtml', vscode.Uri.parse(wechat.customUri), vscode.ViewColumn.Two, 'Wechat on Vscode').then((success) => { 23 | }, (reason) => { 24 | vscode.window.showErrorMessage(reason); 25 | }); 26 | 27 | // wechat.displayer.update(customUri, ''); 28 | }); 29 | 30 | context.subscriptions.push(startDis,wechat.registration); 31 | 32 | let openDis = vscode.commands.registerCommand('extension.open', function () { 33 | vscode.window 34 | .showInformationMessage('hello', 'test', 'taco', 'cheeseburger') 35 | .then(selection => { 36 | console.log(selection); 37 | }); 38 | 39 | }); 40 | 41 | context.subscriptions.push(openDis,wechat.registration); 42 | 43 | let sendDis = vscode.commands.registerCommand('extension.send', function (ele) { 44 | wechat.store({text:''}); 45 | wechat.send(ele); 46 | // vscode.window.showInformationMessage('發送成功'); 47 | }); 48 | 49 | context.subscriptions.push(sendDis,wechat.registration); 50 | 51 | let storeDis = vscode.commands.registerCommand('extension.store', function (ele) { 52 | wechat.store(ele); 53 | // vscode.window.showInformationMessage('發送成功'); 54 | }); 55 | 56 | context.subscriptions.push(storeDis,wechat.registration); 57 | 58 | let chatDis = vscode.commands.registerCommand('extension.chatto', function (ele) { 59 | wechat.chatTo(ele); 60 | // vscode.window.showInformationMessage('發送成功'); 61 | }); 62 | 63 | context.subscriptions.push(chatDis,wechat.registration); 64 | 65 | 66 | } 67 | exports.activate = activate; 68 | 69 | // this method is called when your extension is deactivated 70 | function deactivate() { 71 | } 72 | exports.deactivate = deactivate; -------------------------------------------------------------------------------- /src/WechatDisplayer.js: -------------------------------------------------------------------------------- 1 | const moment = require('moment'); 2 | const factory = require('./lineType/factory'); 3 | const View = require('./View'); 4 | 5 | class WechatDisplayer { 6 | constructor(_vscode,uri){ 7 | this.vscode = _vscode; 8 | this.view = new View(); 9 | this.uri = uri; 10 | this.chatList = {}; 11 | this.init(); 12 | } 13 | 14 | init() { 15 | //初始化 16 | let vscode = this.vscode; 17 | 18 | this.onDidChangeEvent = new vscode.EventEmitter(); 19 | this.commandCache = ''; 20 | // this.content = ''; 21 | this.vContent = [{ 22 | index: 0, 23 | type: 'log', 24 | date: moment().format('YYYY-MM-DD HH:mm:ss'), 25 | content: `Welcome to use the Wechat-on-VScode...`, 26 | }]; 27 | //跟注册事件相配合的数组,事件的注册,也是需要释放的 28 | var disposable = []; 29 | //保存需要释放的资源 30 | this.disposable = vscode.Disposable.from(disposable); 31 | } 32 | 33 | setToUser (user){ 34 | this.toUser = user; 35 | this.update(); 36 | } 37 | 38 | render(){ 39 | if (this.vContent.length === 0)return ''; 40 | let result = []; 41 | this.vContent.forEach(e => { 42 | result.push(factory.getType(e.type).render(e)); 43 | }); 44 | 45 | return result.join(''); 46 | } 47 | 48 | provideTextDocumentContent(uri) { 49 | 50 | let href = encodeURI('command:extension.open1111'); 51 | const data = { 52 | line: this.render(), 53 | href, 54 | msg: this.commandCache, 55 | // chatList: JSON.stringify(this.chatList), // chatList.join(''), 56 | toUser: this.toUser?this.toUser.name():'non-target', 57 | } 58 | 59 | let body = this.view.render(data); 60 | 61 | return body; 62 | } 63 | 64 | get onDidChange() { 65 | return this.onDidChangeEvent.event; 66 | } 67 | 68 | showImg(url) { 69 | // this.update(``); 70 | this.vContent.push({ 71 | index: this.vContent.length, 72 | type: 'img', 73 | date: moment().format('YYYY-MM-DD HH:mm:ss'), 74 | content: url, 75 | }) 76 | this.update(); 77 | } 78 | 79 | log(content){ 80 | this.vContent.push({ 81 | index: this.vContent.length, 82 | type: 'log', 83 | date: moment().format('YYYY-MM-DD HH:mm:ss'), 84 | content: content, 85 | }) 86 | this.update(); 87 | } 88 | 89 | chat(payload){ 90 | this.vContent.push({ 91 | index: this.vContent.length, 92 | type: 'chat', 93 | date: moment().format('YYYY-MM-DD HH:mm:ss'), 94 | content: payload.content, 95 | from: payload.from, 96 | to: payload.to, 97 | }) 98 | 99 | this.update(); 100 | } 101 | 102 | clean(update = true){ 103 | this.vContent = []; 104 | if(update)this.update(); 105 | } 106 | 107 | update() { 108 | // if (message) this.content += `${message} `; 109 | this.onDidChangeEvent.fire(this.uri); 110 | } 111 | 112 | dispose() { //实现dispose方法 113 | this.disposable.dispose(); 114 | } 115 | } 116 | 117 | module.exports = WechatDisplayer; -------------------------------------------------------------------------------- /src/wechat.js: -------------------------------------------------------------------------------- 1 | const { Wechaty } = require('wechaty'); 2 | const WechatDisplayer = require('./WechatDisplayer'); 3 | 4 | class Wechat { 5 | constructor(_vscode){ 6 | this.vscode = _vscode; 7 | var scheme = 'wechatjs'; 8 | this.customUri = this.vscode.Uri.parse(`${scheme}://authority/wechatjs`); 9 | this.displayer = new WechatDisplayer(this.vscode,this.customUri); 10 | this.registration = this.vscode.workspace 11 | .registerTextDocumentContentProvider(scheme, this.displayer); 12 | this.init(); 13 | } 14 | 15 | init() { //初始化 16 | let vscode = this.vscode; 17 | 18 | this.chatList = this.displayer.chatList; 19 | this.friendList = []; 20 | //跟注册事件相配合的数组,事件的注册,也是需要释放的 21 | var disposable = []; 22 | //保存需要释放的资源 23 | this.disposable = vscode.Disposable.from(disposable); 24 | } 25 | 26 | start(){ 27 | Wechaty.instance({profile:'wechat-on-vscode'}) // Global Instance 28 | .on('scan', (qrcode, status) => this.scan(qrcode,status)) 29 | .on('login', user => this.login(user)) 30 | .on('message', message => this.messageHandler(message)) 31 | .start(); 32 | } 33 | 34 | scan(qrcode, status){ 35 | console.log(`Scan QR Code to login: ${status}\nhttps://api.qrserver.com/v1/create-qr-code/?data=${encodeURIComponent(qrcode)}`) 36 | this.displayer.log('Please scan the QRcode to log in.'); 37 | this.displayer.showImg(`https://api.qrserver.com/v1/create-qr-code/?data=${encodeURIComponent(qrcode)}`); 38 | } 39 | 40 | async login(user){ 41 | this.currentUser = user; 42 | this.toUser = null; 43 | // this.displayer.setToUser(this.toUser); 44 | this.displayer.clean(false); 45 | this.displayer.log(`User ${user.name()} logined`); 46 | 47 | // Cache all the contact 48 | const contactList = await Wechaty.instance().Contact.findAll() 49 | this.friendList = contactList.filter(contact => !!contact.friend()) 50 | console.log('contact List ================== '); 51 | console.log(JSON.stringify(this.friendList)); 52 | console.log(this.friendList); 53 | } 54 | 55 | chatTo ( payload ){ 56 | const id = payload.id; 57 | const toUser = this.chatList[id]?this.chatList[id].contact:this.friendList.find(e => e.payload.id === id); 58 | if (toUser){ 59 | this.toUser = toUser; 60 | console.log(this.toUser); 61 | this.displayer.setToUser(this.toUser); 62 | } 63 | } 64 | 65 | send ( payload ){ 66 | if (payload.text.indexOf(':') === 0){ 67 | // command mode 68 | const data = payload.text.split(' '); 69 | this.command(data[0].substring(1),data); 70 | return; 71 | } 72 | this.toUser.say(payload.text); 73 | if (!this.chatList[this.toUser.id]){ 74 | this.chatList[this.toUser.id] = { 75 | contact:this.toUser, 76 | history: [], 77 | } 78 | } 79 | 80 | this.chatList[this.toUser.id].history.push({ 81 | to: this.toUser, 82 | from: this.currentUser, 83 | timestamp: Math.round(new Date().getTime()/1000), 84 | text: payload.text, 85 | }); 86 | } 87 | 88 | command(cmd,data) { 89 | console.log('command hererererererere') 90 | console.log(cmd); 91 | 92 | this[`${cmd}Cmd`](data[1]); 93 | } 94 | 95 | async filterCmd(data){ 96 | console.log('call successssss data:' + data); 97 | // const contact = await Wechaty.instance().Contact.find({name: data}); 98 | const contact = this.friendList.filter(e => e.name().indexOf(data) > -1); 99 | console.log(JSON.stringify(contact)); 100 | } 101 | 102 | async chattoCmd(data){ 103 | console.log('call successssss data:' + data); 104 | // const contact = await Wechaty.instance().Contact.find({name: data}); 105 | const contact = this.friendList.filter(e => e.name().indexOf(data) > -1); 106 | console.log(JSON.stringify(contact)); 107 | if (contact.length > 0){ 108 | this.chatTo({id: contact[0].payload.id}); 109 | } 110 | } 111 | 112 | 113 | // 緩存input內容和光標位置 114 | store( payload ){ 115 | this.displayer.commandCache = payload.text; 116 | } 117 | 118 | messageHandler(message){ 119 | console.log(message); 120 | const room = message.room() 121 | // 暂时过滤群 122 | if (room)return; 123 | // 查看是否正在聊天 124 | this.toUser = message.from().id === this.currentUser.id?message.to():message.from(); 125 | if (!this.chatList[this.toUser.id]){ 126 | this.chatList[this.toUser.id] = { 127 | contact:this.toUser, 128 | history: [], 129 | } 130 | } 131 | 132 | this.chatList[this.toUser.id].history.push({ 133 | to: message.to(), 134 | from: message.from(), 135 | timestamp: message.payload.timestamp, 136 | text: message.text(), 137 | }); 138 | 139 | // this.displayer.setToUser(this.toUser); 140 | 141 | if (message.from().id !== this.currentUser.id){ 142 | // 自动回复 143 | // message.say('hi'); 144 | } 145 | 146 | this.displayer.chat({ 147 | content: `${message.text()}`, 148 | from: message.from(), 149 | to: message.to() || message.room(), 150 | }); 151 | } 152 | 153 | dispose() { //实现dispose方法 154 | this.disposable.dispose(); 155 | } 156 | } 157 | 158 | module.exports = Wechat; --------------------------------------------------------------------------------