Commit 2a1a6964 authored by 任国军's avatar 任国军

add getUserPhone

parent e2739a15
Pipeline #17998 passed with stage
in 9 seconds
......@@ -5,137 +5,135 @@ const uuidv4 = require('uuid/v4');
class UserController extends Controller {
async auth() {
const { ctx, app } = this;
const code = ctx.request.body.code;
if (!code) {
ctx.failed('error code');
}
//请求微信授权 获取openid
const wx_auth_ret = await ctx.service.course.user.requestWxAuth(code);
const openid = wx_auth_ret.openid;//获取openid
//检查是否已授权过 是否已入用户表
let user = await ctx.classModel.CourseUser.one({ where: { openid, is_deleted: 0 } });
if (!user || !user.uuid) {
const uuid = uuidv4();
user = await ctx.classModel.CourseUser.create({ uuid, openid });
// user.uuid = uuid;
}
console.info(user);
//存储缓存标识
const user_uuid = user.uuid;
const key = 'course_user_session_' + user_uuid;
await app.memcache.set(key, { user_uuid, openid }, 86400 * 180);
const auth_token = ctx.helper.md5(openid + user_uuid + 'jbwl');
ctx.set('uuid', key);
ctx.set('auth_token', auth_token);
const result = { uuid: user_uuid, auth_token };
ctx.success({ result });
async auth() {
const { ctx, app } = this;
const code = ctx.request.body.code;
if (!code) {
ctx.failed('error code');
}
async registerUserInfo() {
// 请求微信授权 获取openid
const wx_auth_ret = await ctx.service.course.user.requestWxAuth(code);
const openid = wx_auth_ret.openid;// 获取openid
const { ctx } = this;
const uuid = ctx.userUuid;
const input_params = ctx.request.body;
const { avatar, nickname, province, country, sex, city } = input_params;
//查找用户是否存在并更新
const user = ctx.classModel.CourseUser.one({ where: { uuid } });
await ctx.classModel.CourseUser.edit({ params: { avatar, nickname, sex }, where: { uuid } });
let bindphone = 0;
if (user.phone) {
bindphone = 1;
}
const result = { bindphone };
ctx.success({ result });
// 检查是否已授权过 是否已入用户表
let user = await ctx.classModel.CourseUser.one({ where: { openid, is_deleted: 0 } });
if (!user || !user.uuid) {
const uuid = uuidv4();
user = await ctx.classModel.CourseUser.create({ uuid, openid });
// user.uuid = uuid;
}
console.info(user);
// 存储缓存标识
const user_uuid = user.uuid;
const key = 'course_user_session_' + user_uuid;
await app.memcache.set(key, { user_uuid, openid }, 86400 * 180);
const auth_token = ctx.helper.md5(openid + user_uuid + 'jbwl');
ctx.set('uuid', key);
ctx.set('auth_token', auth_token);
const result = { uuid: user_uuid, auth_token };
ctx.success({ result });
}
async registerUserInfo() {
const { ctx } = this;
const uuid = ctx.userUuid;
const input_params = ctx.request.body;
const { avatar, nickname, province, country, sex, city } = input_params;
// 查找用户是否存在并更新
const user = ctx.classModel.CourseUser.one({ where: { uuid } });
await ctx.classModel.CourseUser.edit({ params: { avatar, nickname, sex }, where: { uuid } });
let bindphone = 0;
if (user.phone) {
bindphone = 1;
}
const result = { bindphone };
/**
ctx.success({ result });
}
/**
* 获取baby信息
*/
async getBabyInfo() {
async getBabyInfo() {
const { ctx } = this;
const result = await ctx.service.course.user.getBabyInfo();
const { ctx } = this;
const result = await ctx.service.course.user.getBabyInfo();
ctx.success({ result });
}
ctx.success({ result });
}
/**
/**
* 保存baby信息
*/
async saveBabyInfo() {
async saveBabyInfo() {
const { ctx } = this;
const input_params = ctx.request.body;
const result = await ctx.service.course.user.saveBabyInfo(input_params);
const { ctx } = this;
const input_params = ctx.request.body;
const result = await ctx.service.course.user.saveBabyInfo(input_params);
ctx.success({ result });
}
ctx.success({ result });
}
/**
/**
* 删除baby信息
*/
async delBabyInfo() {
async delBabyInfo() {
const { ctx } = this;
const result = await ctx.service.course.user.delBabyInfo();
const { ctx } = this;
const result = await ctx.service.course.user.delBabyInfo();
ctx.success({ result });
}
ctx.success({ result });
}
/**
/**
* 用户收藏机构列表
*/
async getCollectInstitutions() {
async getCollectInstitutions() {
const { ctx } = this;
let input_params = ctx.request.body;
input_params = Object.assign(input_params, ctx.request.query);
const result = await ctx.service.course.user.getCollectInstitutions(input_params);
const { ctx } = this;
let input_params = ctx.request.body;
input_params = Object.assign(input_params, ctx.request.query);
const result = await ctx.service.course.user.getCollectInstitutions(input_params);
ctx.success({ result });
}
ctx.success({ result });
}
/**
/**
* 收藏机构
*/
async collectInstitution() {
const { ctx } = this;
const institution_id = ctx.request.body.institution_id;
if (!institution_id) {
ctx.failed('error institution_id');
}
const result = await ctx.service.course.user.collectInstitution(institution_id);
async collectInstitution() {
ctx.success({ result });
const { ctx } = this;
const institution_id = ctx.request.body.institution_id;
if (!institution_id) {
ctx.failed('error institution_id');
}
const result = await ctx.service.course.user.collectInstitution(institution_id);
/**
ctx.success({ result });
}
/**
* 取消收藏机构
*/
async delCollectInstitution() {
const { ctx } = this;
const institution_id = ctx.request.body.institution_id;
if (!institution_id) {
ctx.failed('error institution_id');
}
const result = await ctx.service.course.user.delCollectInstitution(institution_id);
async delCollectInstitution() {
ctx.success({ result });
const { ctx } = this;
const institution_id = ctx.request.body.institution_id;
if (!institution_id) {
ctx.failed('error institution_id');
}
const result = await ctx.service.course.user.delCollectInstitution(institution_id);
ctx.success({ result });
}
}
......
......@@ -5,139 +5,137 @@ const uuidv4 = require('uuid/v4');
class UserController extends Controller {
async auth() {
const { ctx, app } = this;
const code = ctx.request.body.code;
if (!code) {
ctx.failed('error code');
}
//请求微信授权 获取openid
const wx_auth_ret = await ctx.service.course.v2.user.requestWxAuth(code);
const openid = wx_auth_ret.openid;//获取openid
//检查是否已授权过 是否已入用户表
let user = await ctx.classModel.V2.CourseUser.one({ where: { openid, is_deleted: 0 } });
if (!user || !user.uuid) {
const uuid = uuidv4();
user = await ctx.classModel.V2.CourseUser.create({ uuid, openid });
// user.uuid = uuid;
}
console.info(user);
//存储缓存标识
const user_uuid = user.uuid;
const key = 'course_v2_user_session_' + user_uuid;
await app.memcache.set(key, { user_uuid, openid }, 86400 * 7);
const auth_token = ctx.helper.md5(openid + user_uuid + 'jbwl');
ctx.set('uuid', key);
ctx.set('auth_token', auth_token);
const result = { uuid: user_uuid, auth_token };
ctx.success({ result });
async auth() {
const { ctx, app } = this;
const code = ctx.request.body.code;
if (!code) {
ctx.failed('error code');
}
async registerUserInfo() {
const { ctx } = this;
const uuid = ctx.userUuid;
const input_params = ctx.request.body;
const { avatar, nickname, province, country, sex, city } = input_params;
//查找用户是否存在并更新
const user = ctx.classModel.V2.CourseUser.one({ where: { uuid } });
await ctx.classModel.V2.CourseUser.edit({ params: { avatar, nickname, sex }, where: { uuid } });
let bindphone = 0;
if (user.phone) {
bindphone = 1;
}
const result = { bindphone };
ctx.success({ result });
// 请求微信授权 获取openid
const wx_auth_ret = await ctx.service.course.v2.user.requestWxAuth(code);
const openid = wx_auth_ret.openid;// 获取openid
const session_key = wx_auth_ret.session_key;
// 检查是否已授权过 是否已入用户表
let user = await ctx.classModel.V2.CourseUser.one({ where: { openid, is_deleted: 0 } });
if (!user || !user.uuid) {
const uuid = uuidv4();
user = await ctx.classModel.V2.CourseUser.create({ uuid, openid });
// user.uuid = uuid;
}
/**
console.info(user);
// 存储缓存标识
const user_uuid = user.uuid;
const key = 'course_v2_user_session_' + user_uuid;
await app.memcache.set(key, { user_uuid, openid, session_key }, 86400 * 7);
const auth_token = ctx.helper.md5(openid + user_uuid + 'jbwl');
ctx.set('uuid', key);
ctx.set('auth_token', auth_token);
const result = { uuid: user_uuid, auth_token, session_key };
ctx.success({ result });
}
async registerUserInfo() {
const { ctx, service } = this;
const input_params = ctx.request.body;
const { avatar, nickname, province, country, sex, city } = input_params;
const encryptedData = input_params.encryptedData || '';
const iv = input_params.iv || '';
const params = { avatar, nickname, province, country, sex, city, encryptedData, iv };
const ret = await service.course.v2.registerUserInfo(params);
ctx.success(ret);
}
/**
* 获取baby信息
*/
async getBabyInfo() {
async getBabyInfo() {
const { ctx } = this;
const result = await ctx.service.course.v2.user.getBabyInfo();
const { ctx } = this;
const result = await ctx.service.course.v2.user.getBabyInfo();
ctx.success({ result });
}
ctx.success({ result });
}
/**
/**
* 保存baby信息
*/
async saveBabyInfo() {
async saveBabyInfo() {
const { ctx } = this;
const input_params = ctx.request.body;
const result = await ctx.service.course.v2.user.saveBabyInfo(input_params);
const { ctx } = this;
const input_params = ctx.request.body;
const result = await ctx.service.course.v2.user.saveBabyInfo(input_params);
ctx.success({ result });
}
ctx.success({ result });
}
/**
/**
* 删除baby信息
*/
async delBabyInfo() {
async delBabyInfo() {
const { ctx } = this;
const result = await ctx.service.course.v2.user.delBabyInfo();
const { ctx } = this;
const result = await ctx.service.course.v2.user.delBabyInfo();
ctx.success({ result });
}
ctx.success({ result });
}
/**
/**
* 用户收藏机构列表
*/
async getCollectInstitutions() {
async getCollectInstitutions() {
const { ctx } = this;
let input_params = ctx.request.body;
input_params = Object.assign(input_params, ctx.request.query);
const result = await ctx.service.course.v2.user.getCollectInstitutions(input_params);
const { ctx } = this;
let input_params = ctx.request.body;
input_params = Object.assign(input_params, ctx.request.query);
const result = await ctx.service.course.v2.user.getCollectInstitutions(input_params);
ctx.success({ result });
}
ctx.success({ result });
}
/**
/**
* 收藏机构
*/
async collectInstitution() {
const { ctx } = this;
const institution_id = ctx.request.body.institution_id;
if (!institution_id) {
ctx.failed('error institution_id');
}
const result = await ctx.service.course.v2.user.collectInstitution(institution_id);
async collectInstitution() {
ctx.success({ result });
const { ctx } = this;
const institution_id = ctx.request.body.institution_id;
if (!institution_id) {
ctx.failed('error institution_id');
}
const result = await ctx.service.course.v2.user.collectInstitution(institution_id);
/**
ctx.success({ result });
}
/**
* 取消收藏机构
*/
async delCollectInstitution() {
const { ctx } = this;
const institution_id = ctx.request.body.institution_id;
if (!institution_id) {
ctx.failed('error institution_id');
}
const result = await ctx.service.course.v2.user.delCollectInstitution(institution_id);
async delCollectInstitution() {
ctx.success({ result });
const { ctx } = this;
const institution_id = ctx.request.body.institution_id;
if (!institution_id) {
ctx.failed('error institution_id');
}
const result = await ctx.service.course.v2.user.delCollectInstitution(institution_id);
ctx.success({ result });
}
// 获取用户信息
async getUserInfo() {
const { ctx, service } = this;
const ret = await service.course.v2.user.getUserInfo();
ctx.success(ret);
}
}
module.exports = UserController;
......@@ -19,6 +19,7 @@ module.exports = app => {
router.post('third', '/user/auth', 'course.v2.user.auth');// 微信授权登录
router.post('third', '/user/register_user', miniAuth, 'course.v2.user.registerUserInfo');// 授权后注册用户
router.get('third', '/user/info', miniAuth, 'course.v2.user.getUserInfo');// 获取用户信息
router.get('third', '/user/baby', miniAuth, 'course.v2.user.getBabyInfo');// 获取baby信息
router.post('third', '/user/baby', miniAuth, 'course.v2.user.saveBabyInfo');// 保存baby信息
......@@ -34,9 +35,9 @@ module.exports = app => {
router.get('third', '/comments', miniAuth, 'course.v2.institution.getComments');// 评论列表
router.get('third', '/search/hot', miniAuth, 'course.v2.institution.getHotSearch');// 热搜
router.get('third', '/search/suggest', miniAuth, 'course.v2.institution.getSuggestSearch');//搜索联想
router.get('third', '/search/suggest', miniAuth, 'course.v2.institution.getSuggestSearch');// 搜索联想
router.get('third', '/search', miniAuth, 'course.v2.institution.search');// 搜索
router.get('third', '/user/search', miniAuth, 'course.v2.institution.getUserSearch');// 用户搜索历史
router.delete('third', '/user/search', miniAuth, 'course.v2.institution.deleteUserSearch');//清空用户搜索记录
router.delete('third', '/user/search', miniAuth, 'course.v2.institution.deleteUserSearch');// 清空用户搜索记录
};
......@@ -118,7 +118,58 @@ class UserService extends Service {
}
// 获取用户信息
async getUserInfo() {
const { ctx } = this;
const userUuid = ctx.userUuid;
const userInfo = await ctx.classModel.V2.CourseUser.findOne({ where: { uuid: userUuid, is_deleted: 0 } });
if (ctx.isEmpty(userInfo)) {
ctx.failed('用户不存在');
}
const ret = {
bind_phone: ctx.isEmpty(userInfo.phone) ? 0 : 1,
};
return ret;
}
// 保存用户信息
async registerUserInfo(input) {
const { ctx } = this;
const userUuid = ctx.userUuid;
const { avatar, nickname, province, country, sex, city, encryptedData, iv } = input;
// 查找用户是否存在并更新
const user = ctx.classModel.V2.CourseUser.findOne({ where: { uuid: userUuid, is_deleted: 0 } });
if (ctx.isEmpty(user)) {
ctx.failed('用户不存在');
}
const data = {};
if (!ctx.isEmpty(avatar)) {
data.avatar = avatar;
}
if (!ctx.isEmpty(nickname)) {
data.nickname = nickname;
}
if (!ctx.isEmpty(encryptedData) && !ctx.isEmpty(iv)) {
const decoded = await ctx.service.course.v2.wechat.decodeData(encryptedData, iv);
if (!ctx.isEmpty(decoded) && !ctx.isEmpty(decoded.phone)) {
console.log(decoded);
data.phone = decoded.phone;
}
}
await ctx.classModel.V2.CourseUser.update(data, { where: { id: user.id } });
const ret = {
result: {
bind_phone: 0,
},
};
return ret;
}
}
module.exports = UserService;
......@@ -4,6 +4,7 @@ const APPID = 'wx07a5f0ed5bdf4751';
const SECRET = 'a1b2d32b018988176181497bd74a0b7d';
const fs = require('fs');
const request = require('request');
const crypto = require('crypto');
class WechatService extends Service {
async getAccessToken() {
......@@ -136,6 +137,32 @@ class WechatService extends Service {
return ret;
}
// 解密
async decodeData(encryptedData, iv) {
const { ctx } = this;
const userUuid = ctx.userUuid;
const userInfo = await ctx.app.memcache.get('course_v2_user_session_' + userUuid);
if (ctx.isEmpty(userInfo) || ctx.isEmpty(userInfo.session_key)) {
ctx.failed('sessionKey不存在');
}
const sessionKey = new Buffer(userInfo.session_key, 'base64');
// base64 decode
encryptedData = new Buffer(encryptedData, 'base64');
iv = new Buffer(iv, 'base64');
// 解密
const decipher = crypto.createDecipheriv('aes-128-cbc', sessionKey, iv);
// 设置自动 padding 为 true,删除填充补位
decipher.setAutoPadding(true);
let decoded = decipher.update(encryptedData, 'binary', 'utf8');
decoded += decipher.final('utf8');
decoded = JSON.parse(decoded);
return decoded;
}
}
module.exports = WechatService;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment