const config = require(‘../conf/config‘); const userModel = require(‘../model/userModel‘); const wechatApi = require(‘../util/wechatApi‘); const API = wechatApi.API; exports.addUser = function(openId, callback){ callback = callback || function(){}; userModel.findOne({openId: openId}, function(err, user){ if(err) return callback(err) if(user){ return callback(null, user); }else { API.getUser(openId, function(err2, newUser){ if(err2) return callback(err2) user = new userModel({ openId: newUser.openid, nickname: newUser.nickname, sex: newUser.sex, province: newUser.province, city: newUser.city, country: newUser.country, headimgurl: newUser.headimgurl, privilege: newUser.privilege, createTime: new Date(), lastUpdateTime: new Date() }) user.save(function(err3){ if(err3) return callback(err3) callback(null, user) }) }) } }) }
,