From 41b45db4d406985eff6efcfd96fe18781e4b82a1 Mon Sep 17 00:00:00 2001 From: shaoying Date: Tue, 17 Mar 2020 13:21:54 +0800 Subject: [PATCH] opt mp --- client/mp/app.js | 16 +++++++++------- client/mp/pages/storm/storm.js | 3 ++- client/mp/utils/kit.js | 30 ++++++++++++++++++++++++++++++ client/mp/utils/util.js | 9 ++++----- 4 files changed, 45 insertions(+), 13 deletions(-) create mode 100644 client/mp/utils/kit.js diff --git a/client/mp/app.js b/client/mp/app.js index dcbdbaba..99e534f2 100644 --- a/client/mp/app.js +++ b/client/mp/app.js @@ -1,4 +1,4 @@ -const utils = require("utils/util.js") +const kit = require("utils/kit.js") App({ data: {}, conf: {serve: "https://shylinux.com/chat"}, @@ -61,25 +61,27 @@ App({ modal: function(title, cb) {wx.showModal({title: title, success: cb})}, toast: function(title) {wx.showToast({title: title})}, - jumps: function(url, args, cb) { - wx.navigateTo({url: "/pages/"+utils.Args(url, args), success: cb}) + jumps: function(url, args, cb) {var next = "/pages/"+kit.Args(url, args) + console.log("jump", next), wx.navigateTo({url: next, success: cb}) }, scans: function(cb) {var app = this - wx.scanCode({success(res) { + wx.scanCode({success(res) {console.log("scan", res) try { var value = JSON.parse(res.result) switch (value.type) { case "active": app.userinfo(function(userInfo) { app.modal("授权登录", function(res) { - res.confirm && app.request("mp/login/auth", {auth: value.name}) + res.confirm && app.request("mp/login/auth", {auth: value.name}, function(msg) { + app.toast("授权成功") + }) }) }) default: - typeof cb == "function" && cb(res) + typeof cb == "function" && cb(value) } } catch(e) { - typeof cb == "function" && cb(res) + typeof cb == "function" && cb({type: "", text: res.result}) } }}) }, diff --git a/client/mp/pages/storm/storm.js b/client/mp/pages/storm/storm.js index 8c0733ca..adb625f3 100644 --- a/client/mp/pages/storm/storm.js +++ b/client/mp/pages/storm/storm.js @@ -1,4 +1,4 @@ -const utils = require("../../utils/util.js") +const kit = require("../../utils/kit.js") const app = getApp() Page({ @@ -10,6 +10,7 @@ Page({ action: { "扫码": function(event, page, data, name) { app.scans(function(res) { + app.toast() }) }, "刷新": function(event, page, data, name) { diff --git a/client/mp/utils/kit.js b/client/mp/utils/kit.js new file mode 100644 index 00000000..813e3853 --- /dev/null +++ b/client/mp/utils/kit.js @@ -0,0 +1,30 @@ +module.exports = { + Number: function(d, n) {var res = []; + while (d > 0) {res.push(d % 10); d = parseInt(d / 10); n--} + while (n > 0) {res.push("0"); n--} + return res.reverse(), res.join(""); + }, + Time: function(t, fmt) {var now = t? new Date(t): new Date(); + fmt = fmt || "%y-%m-%d %H:%M:%S"; + fmt = fmt.replace("%y", now.getFullYear()) + fmt = fmt.replace("%m", Number(now.getMonth()+1, 2)) + fmt = fmt.replace("%d", Number(now.getDate(), 2)) + fmt = fmt.replace("%H", Number(now.getHours(), 2)) + fmt = fmt.replace("%M", Number(now.getMinutes(), 2)) + fmt = fmt.replace("%S", Number(now.getSeconds(), 2)) + return fmt + }, + Args: function(url, args) {var list = [] + for (var k in args) { + list.push(encodeURIComponent(k)+"="+encodeURIComponent(args[k])) + } + return url+"?"+list.join("&") + }, + List: function(list, cb, cbs) {var res = [], val; + for (var i = 0; i < list.length; i++) { + typeof cb == "function"? (val = cb(list[i], i, list)) != undefined && res.push(val): res.push(list[i]) + } + return typeof cbs == "function" && cbs(res), res + }, +} + diff --git a/client/mp/utils/util.js b/client/mp/utils/util.js index 95089069..4149ac48 100644 --- a/client/mp/utils/util.js +++ b/client/mp/utils/util.js @@ -13,11 +13,10 @@ const formatNumber = n => { n = n.toString() return n[1] ? n : '0' + n } -function Number(d, n) {var result = []; - while (d>0) {result.push(d % 10); d = parseInt(d / 10); n--} - while (n > 0) {result.push("0"); n--} - result.reverse(); - return result.join(""); +function Number(d, n) {var res = []; + while (d > 0) {res.push(d % 10); d = parseInt(d / 10); n--} + while (n > 0) {res.push("0"); n--} + return res.reverse(), res.join(""); } function Time(t, fmt) {var now = t? new Date(t): new Date(); fmt = fmt || "%y-%m-%d %H:%M:%S";