diff --git a/usr/client/mp/app.js b/usr/client/mp/app.js index ec3e23ae..990349bf 100644 --- a/usr/client/mp/app.js +++ b/usr/client/mp/app.js @@ -1,32 +1,73 @@ App({ + log: function(type, args) { + console[type](args) + }, toast: function(text) { wx.showToast() }, + place: function(cb) { + var app = this + wx.authorize({scope: "scope.userLocation"}) + + wx.chooseLocation({success: function(res) { + app.log("info", res) + typeof cb == "function" && cb(res) + }}) + }, + navigate: function(page, args) { + if (!page) { + wx.navigateBack() + return + } + var list = [] + for (var k in args) { + list.push(k+"="+args[k]) + } + + wx.navigateTo({url:"/pages/"+page+"/"+page + (list.length>0? "?"+list.join("&"): "")}) + }, + request: function(data, done, fail) { var app = this data = data || {} data.sessid = app.sessid || "" - wx.request({method: "POST", url: "https://shylinux.com/chat/mp", data: data, + var what = {method: "POST", url: "https://shylinux.com/chat/mp", data: data, success: function(res) { + what.res = res + app.log("info", what) typeof done == "function" && done(res.data) }, fail: function(res) { + what.res = res + app.log("info", what) typeof done == "function" && done(res.data) }, - }) + } + + wx.request(what) }, + + sessid: "", + userInfo: {}, login: function(cb) { var app = this - wx.login({success: res => { + if (app.sessid) { + typeof cb == "function" && cb(app.userInfo) + return + } + + wx.login({success: function(res) { app.request({code: res.code}, function(sessid) { app.sessid = sessid - wx.getSetting({success: res => { + wx.getSetting({success: function(res) { if (res.authSetting['scope.userInfo']) { - wx.getUserInfo({success: res => { + wx.getUserInfo({success: function(res) { app.userInfo = res.userInfo - app.request(res, cb) + app.request(res, function() { + typeof cb == "function" && cb(app.userInfo) + }) }}) } }}) @@ -34,11 +75,82 @@ App({ }}) }, - onLaunch: function () { + command: function(args, cb) { var app = this - this.login(function() { - app.request({"cmd": ["note", "model"]}) - + app.login(function(userInfo) { + app.request({cmd: ["context", "ssh", "sh", "node", "note", "context", "mdb"].concat(args["cmd"])}, function(res) { + app.toast("ok") + typeof cb == "function" && cb(res) + }) }) }, + + model: {}, + data: {model: {}, list: []}, + load: function(type, cb) { + var app = this + switch (type) { + case "model": + if (app.data.length > 0 && app.data.model.length > 0) { + typeof cb == "function" && cb(app.data.model) + return + } + + var cmd = {"cmd": ["note", type]} + if (type == "note") { + cmd.cmd.push("note") + } + + app.command(cmd, function(res) { + var ncol = res.append.length + var nrow = res[res.append[0]].length + for (var i = 0; i < nrow; i++) { + app.data.model[res["name"][i]] = { + name: res["name"][i], + data: JSON.parse(res["data"][i] || "[]"), + view: JSON.parse(res["view"][i] || "{}"), + } + } + typeof cb == "function" && cb(app.data.model) + }) + break + case "list": + if (app.data.length > 0 && app.data.list.length > 0) { + typeof cb == "function" && cb(app.data.list) + return + } + + var cmd = {"cmd": ["note", "search", "username"]} + + app.command(cmd, function(res) { + if (!res || !res.append) { + return + } + + var ncol = res.append.length + var nrow = res[res.append[0]].length + for (var i = 0; i < nrow; i++) { + var args = {} + var value = JSON.parse(res["value"][i] || "[]") + for (var j = 0; j < value.length; j++) { + args[value[j].name] = value[j].value + } + + app.data.list.push({ + create_date: res["create_time"][i].split(" ")[0].replace("-", "/").replace("-", "/"), + create_time: res["create_time"][i], + model: res["model"][i], + name: res["name"][i], + value: value, + args: args, + view: JSON.parse(res["view"][i] || "{}"), + }) + } + typeof cb == "function" && cb(app.data.list) + }) + break + } + }, + + onLaunch: function () {}, }) diff --git a/usr/client/mp/app.json b/usr/client/mp/app.json index 0c119d89..084acc1e 100644 --- a/usr/client/mp/app.json +++ b/usr/client/mp/app.json @@ -1,7 +1,7 @@ { "pages": [ - "pages/index/index", "pages/list/list", + "pages/index/index", "pages/note/note" ], "window": { diff --git a/usr/client/mp/pages/index/index.js b/usr/client/mp/pages/index/index.js index c0dd795d..5340b703 100644 --- a/usr/client/mp/pages/index/index.js +++ b/usr/client/mp/pages/index/index.js @@ -2,6 +2,7 @@ const app = getApp() Page({ data: { + focus: false, cmd: "", table: [], append: [], @@ -9,7 +10,8 @@ Page({ }, onCommand: function(e) { var page = this - app.request({"cmd": e.detail.value}, function(res) { + var cmd = e.detail.value + app.command({"cmd": cmd}, function(res) { if (res.append) { var table = [] for (var i = 0; i < res[res.append[0]].length; i++) { @@ -24,7 +26,29 @@ Page({ page.setData({append: [], table: []}) } page.setData({result: res.result? res.result.join("") :res}) + if (page.data.cmd) { + return + } + app.command({"cmd": ["note", cmd, "flow", cmd]}, function(res) {}) + }) + }, + onLoad: function (options) { + app.log("info", {page: "pages/index/index", options: options}) + + var page = this + app.load("model", function(model) { + app.log("info", app.data.list[options.index]) + var cmd = app.data.list[options.index]? app.data.list[options.index].args["cmd"]: "" + page.setData({ + model: model[options.model], + value: app.data.list[options.index], + view: model[options.model].view, + cmd: cmd, + focus: cmd? false: true, + }) + if (cmd) { + page.onCommand({detail:{value:cmd}}) + } }) }, - onLoad: function () {}, }) diff --git a/usr/client/mp/pages/index/index.wxml b/usr/client/mp/pages/index/index.wxml index 725a29bd..f765823e 100644 --- a/usr/client/mp/pages/index/index.wxml +++ b/usr/client/mp/pages/index/index.wxml @@ -1,6 +1,6 @@ - + {{item}} diff --git a/usr/client/mp/project.config.json b/usr/client/mp/project.config.json index 75faa3df..a855c4c7 100644 --- a/usr/client/mp/project.config.json +++ b/usr/client/mp/project.config.json @@ -37,12 +37,20 @@ "list": [] }, "miniprogram": { - "current": 0, + "current": 1, "list": [ { - "id": -1, + "id": 0, "name": "note", "pathName": "pages/note/note", + "query": "model=note", + "scene": null + }, + { + "id": -1, + "name": "list", + "pathName": "pages/list/list", + "query": "", "scene": null } ]