Skip to content

Instantly share code, notes, and snippets.

View ZZR-china's full-sized avatar
🏠
Working from home

magic-wind ZZR-china

🏠
Working from home
View GitHub Profile
@ZZR-china
ZZR-china / falskapp-docker-compose.yml
Created January 23, 2017 13:58
simple flask nginx mongo redis docker-compose.yml
nginx:
restart: always
build: ./nginx
ports:
- "80:80"
volumes:
- /www/static
volumes_from:
- web
links:
@ZZR-china
ZZR-china / firstco.js
Created December 27, 2016 15:12
co is awesome!!!!!
insertOrderItem(req, reply) {
const order_id = req.payload.order_id;
const product_id = req.payload.product_id;
const msg = new message();
co(function*() {
const product = yield Product.get({ _id: product_id });
const menu = yield Menu.get({ _id: product.menu_id });
const order_item_doc = yield new Orderitem({
is_add: true,
order_id: order_id,
@ZZR-china
ZZR-china / gulp.babel.js
Created December 27, 2016 12:44
gulp task js
import gulp from 'gulp';
import gulpLoadPlugins from 'gulp-load-plugins';
import path from 'path';
import del from 'del';
import runSequence from 'run-sequence';
const plugins = gulpLoadPlugins();
const paths = {
js: ['./**/*.js', '!dist/**', '!node_modules/**'],
@ZZR-china
ZZR-china / hapi_ext.js
Created December 2, 2016 11:05
hapi ext func
/**
* with hapi, you should catch request in his way
* I am going to build a web Permission System
* use hapi's ext func , I can catch req before it get in
* route
*/
server.ext('onRequest', function(request, reply) {
const JWT = require('jsonwebtoken');
const token = request.headers.authorization;
const path = request.path;
@ZZR-china
ZZR-china / mongo_helper.js
Last active December 2, 2016 11:02
a mongoose helper func, you can new it with a model. (the args should all be object in es6, like {status, name}, there status, name can be string or number.)
/*
* Author: Magic <magic@foowala.com>
* Module description: mongo func helper
*/
'use strict';
var mongo_funcs = function (mongo_name) {
this.findOne = (data) => {
return new Promise((resolve, reject) => {
@ZZR-china
ZZR-china / datetime.js
Last active December 1, 2016 05:31
a simple moment.js demo
const moment = require('moment'),
localTime = require('moment-timezone');
const timeHelp = {
//Returns the current time in Shanghai time zone
timeShanghai: function () {
return localTime.tz(moment(), "Asia/Shanghai").format("YYYY-MM-DD HH:mm");
},
//Passes in a UNIX time value, converts to local Shanghai Time, and returns the value
unixToShanghaiTime: function (unixTime) {
@ZZR-china
ZZR-china / forever_wxtoken.js
Last active December 1, 2016 05:29
a function to get wx access_token which will nerver expires
const router = require('express').Router(),
Promise = require("bluebird"),
readFileAsync = Promise.promisify(require("fs").readFile),
writeFileAsync = Promise.promisify(require("fs").writeFile),
request_get = Promise.promisify(require('request').get);
router.route('/accesstoken')
.get((req, res) => {
readFileAsync("accesstoken.txt","utf8")
.then(data =>{
@ZZR-china
ZZR-china / websocket_broadcast.js
Last active December 1, 2016 05:26
websocket broadcast func , npm package use websocket (author theturtle32) and use timestamp as client uid
const PORT = parseInt(process.env.LEANCLOUD_APP_PORT || 3000);
app = express();
const server = require('http').createServer(app);
//WebSocket;
let clients = []; // list of currently connected clients (users)
const WebSocketServer = require('websocket').server;
const wsServer = new WebSocketServer({
httpServer: server,
const getOrderById = (_id)=> {
return new Promise((resolve, reject)=>{
order_item_mongo.aggregate([{
$match: {order_id: id}
}, {
$group : {
// _id : "$menu_id",
_id: { menu_id:"$menu_id", menu_name:"$menu_name"},
products: { $push: { product_name: "$product_name"} }
}
@ZZR-china
ZZR-china / jq.modal.js
Last active November 30, 2016 10:22
jquery modal
(function() {
$.fn.extend({
modalShow:function () {
var btnVal = this.attr('data-val');
var $wantModal = $('[val='+btnVal+']');
this.bind('click', function(event) {
/* Act on the event */
$wantModal.removeClass('hide');
});