Commit 4b3afa89 authored by 李尚科's avatar 李尚科

fix

parent e7dc2c31
Pipeline #14048 passed with stage
in 9 seconds
'use strict'; 'use strict';
const Controller = require('egg').Controller; const Controller = require('egg').Controller;
const uuidv4 = require('uuid/v4');
class UserController extends Controller { class UserController extends Controller {
async auth() {
const { ctx, app } = this;
const code = ctx.request.body.code = '123123sfdsf';
if (!code) {
ctx.failed('error code');
}
const wx_auth_ret = await ctx.service.course.user.requestWxAuth(code);
const openid = wx_auth_ret.openid;
// const session_key = wx_auth_ret.session_key;
let user = ctx.classModel.CourseUser.one({ where: { openid, is_deleted: 0 } });
if (!user || !user.uuid) {
const uuid = uuidv4();
user = await ctx.classModel.CourseUser.add({ uuid, openid });
}
const user_uuid = user.uuid;
const key = 'course_user_session_' + user_uuid;
await app.memcache.set(key, { user_uuid, openid }, 86400);
const token = ctx.helper.md5(openid + user_uuid + 'jbwl');
ctx.set('Session_Id', key);
ctx.set('token', token);
const result = { user_uuid };
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 });
}
async verify_auth() {
const { ctx } = this;
const headers = ctx.request.headers;
ctx.success({ headers });
}
/** /**
* *
*/ */
......
...@@ -12,6 +12,7 @@ const APPTYPEID = Symbol('Context#appTypeId'); ...@@ -12,6 +12,7 @@ const APPTYPEID = Symbol('Context#appTypeId');
const OLDUSERID = Symbol('Context#oldUserId'); const OLDUSERID = Symbol('Context#oldUserId');
const USERID = Symbol('Context#userId'); const USERID = Symbol('Context#userId');
const OLDCOOKIE = Symbol('Context#oldCookie'); const OLDCOOKIE = Symbol('Context#oldCookie');
const USERUUID = Symbol('Context#userUuid');
module.exports = { module.exports = {
failed(message) { failed(message) {
...@@ -172,6 +173,10 @@ module.exports = { ...@@ -172,6 +173,10 @@ module.exports = {
return this[OLDCOOKIE]; return this[OLDCOOKIE];
}, },
get userUuid() {
return this[USERUUID];
},
setAppUserId(app_user_id) { setAppUserId(app_user_id) {
this[APPUSERID] = app_user_id; this[APPUSERID] = app_user_id;
}, },
...@@ -203,4 +208,8 @@ module.exports = { ...@@ -203,4 +208,8 @@ module.exports = {
setOldCookie(cookie) { setOldCookie(cookie) {
this[OLDCOOKIE] = cookie; this[OLDCOOKIE] = cookie;
}, },
setUserUuid(user_uuid) {
this[USERUUID] = user_uuid;
},
}; };
'use strict';
module.exports = (options, app) => {
return async function (ctx, next) {
const session_id = ctx.headers.Session_Id;
const key = 'course_user_session_' + session_id;
const auth_info = await app.memcache.get(key);
const openid = auth_info.openid;
const user_uuid = auth_info.user_uuid;
const token = ctx.headers.token;
if (ctx.helper.md5(openid + user_uuid + 'jbwl') != token) {
ctx.failed('login auth error');
}
ctx.setUserUuid(user_uuid);
await next();
};
};
'use strict';
'use strict';
const moment = require('moment');
module.exports = app => {
const { STRING, INTEGER, DATE, DECIMAL, TEXT, ENUM } = app.Sequelize;
const CourseLogUserGps = app.classModel.define('course_log_user_gps', {
id: {
type: INTEGER,
primaryKey: true,
autoIncrement: true
},
user_uuid: STRING,
address: STRING,
lat: DECIMAL,
lng: DECIMAL,
is_deleted: INTEGER,
created_at: {
type: DATE,
allowNull: true,
get() {
const date = this.getDataValue('created_at');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
updated_at: {
type: DATE,
allowNull: true,
get() {
const date = this.getDataValue('updated_at');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
}, {
timestamps: false,
tableName: 'course_log_user_gps',
});
CourseLogUserGps.one = async (data) => {
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
return await CourseLogUserGps.findOne({
attributes: attributes,
where: where,
});
}
CourseLogUserGps.all = async (data) => {
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
const order = data.order ? data.order : [];
return await CourseLogUserGps.findAll({
attributes: attributes,
where: where,
order,
});
}
CourseLogUserGps.list = async (data = {}) => {
const limit = data.limit ? Number(data.limit) : 10;
const page = data.page ? data.page : 1;
const order = data.order ? data.order : [];
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
const condition = {
offset: (page - 1) * limit,
limit,
where: where,
order: order,
attributes: attributes,
};
const { count, rows } = await CourseLogUserGps.findAndCountAll(condition);
return { page, count, rows };
}
CourseLogUserGps.add = async (data) => {
try {
//返回promise对象实力 instance
const res = await CourseLogUserGps.create(data);
//从promise 实例中中获得需要的id号,id 必须是自增长,而且必须主键,否则返回null
return res.id;
} catch (error) {
throw (error);
}
}
CourseLogUserGps.edit = async (data) => {
const where = data.where;
const params = data.params;
try {
const res = await CourseLogUserGps.update(params, { where: where })
return res;
} catch (error) {
throw (error);
}
}
return CourseLogUserGps;
};
\ No newline at end of file
'use strict';
'use strict';
const moment = require('moment');
module.exports = app => {
const { STRING, INTEGER, DATE, DECIMAL, TEXT, ENUM } = app.Sequelize;
const CourseUser = app.classModel.define('course_user', {
id: {
type: INTEGER,
primaryKey: true,
autoIncrement: true
},
uuid: STRING,
app_id: STRING,
app_user_id: STRING,
app_type_id: STRING,
user_id: STRING,
phone: STRING,
nickname: STRING,
avatar: STRING,
sex: STRING,
openid: STRING,
is_deleted: INTEGER,
created_at: {
type: DATE,
allowNull: true,
get() {
const date = this.getDataValue('created_at');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
updated_at: {
type: DATE,
allowNull: true,
get() {
const date = this.getDataValue('updated_at');
return date ? moment(date).format('YYYY-MM-DD HH:mm:ss') : undefined;
},
},
}, {
timestamps: false,
tableName: 'course_user',
});
CourseUser.one = async (data) => {
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
return await CourseUser.findOne({
attributes: attributes,
where: where,
});
}
CourseUser.all = async (data) => {
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
const order = data.order ? data.order : [];
return await CourseUser.findAll({
attributes: attributes,
where: where,
order,
});
}
CourseUser.list = async (data = {}) => {
const limit = data.limit ? Number(data.limit) : 10;
const page = data.page ? data.page : 1;
const order = data.order ? data.order : [];
const attributes = data.attributes ? data.attributes : {};
const where = data.where ? data.where : {};
const condition = {
offset: (page - 1) * limit,
limit,
where: where,
order: order,
attributes: attributes,
};
const { count, rows } = await CourseUser.findAndCountAll(condition);
return { page, count, rows };
}
CourseUser.add = async (data) => {
try {
//返回promise对象实力 instance
const res = await CourseUser.create(data);
//从promise 实例中中获得需要的id号,id 必须是自增长,而且必须主键,否则返回null
return res.id;
} catch (error) {
throw (error);
}
}
CourseUser.edit = async (data) => {
const where = data.where;
const params = data.params;
try {
const res = await CourseUser.update(params, { where: where })
return res;
} catch (error) {
throw (error);
}
}
return CourseUser;
};
\ No newline at end of file
...@@ -15,6 +15,8 @@ module.exports = app => { ...@@ -15,6 +15,8 @@ module.exports = app => {
router.get('/teachers', 'course.institution.teacherList'); router.get('/teachers', 'course.institution.teacherList');
router.get('/teacher/:teacher_id', 'course.institution.teacherInfo'); router.get('/teacher/:teacher_id', 'course.institution.teacherInfo');
router.get('/user/auth', 'course.user.auth');
router.get('/user/verify_auth', 'course.user.verify_auth');
router.get('/user/baby', 'course.user.getBabyInfo'); router.get('/user/baby', 'course.user.getBabyInfo');
router.post('/user/baby', 'course.user.saveBabyInfo'); router.post('/user/baby', 'course.user.saveBabyInfo');
router.delete('/user/baby', 'course.user.delBabyInfo'); router.delete('/user/baby', 'course.user.delBabyInfo');
......
...@@ -11,7 +11,7 @@ class InstitutionService extends Service { ...@@ -11,7 +11,7 @@ class InstitutionService extends Service {
async getInstitutions(input) { async getInstitutions(input) {
const { ctx } = this; const { ctx } = this;
const { cat, age, institution } = input; const { cat, age, institution, lat, lng } = input;
let where = { status: 1, is_deleted: 0 }; let where = { status: 1, is_deleted: 0 };
if (cat) { if (cat) {
const cat_ret = await ctx.classModel.CourseCat.one({ where: { id: cat } }); const cat_ret = await ctx.classModel.CourseCat.one({ where: { id: cat } });
...@@ -38,7 +38,7 @@ class InstitutionService extends Service { ...@@ -38,7 +38,7 @@ class InstitutionService extends Service {
const institutions = await ctx.classModel.CourseInstitution.findAll({ attributes, include, where }); const institutions = await ctx.classModel.CourseInstitution.findAll({ attributes, include, where });
const institution_area_list = await this.getInstitutionAreaList(institutions); const institution_area_list = await this.getInstitutionAreaList(institutions);
const area_lbs = await this.computeDistance(institution_area_list); const area_lbs = await this.computeDistance(institution_area_list, { lat, lng });
const institution_areas = await this.findShortestDistanceAreas(institution_area_list, area_lbs); const institution_areas = await this.findShortestDistanceAreas(institution_area_list, area_lbs);
const ret = await this.formatInstitutions(institution_areas); const ret = await this.formatInstitutions(institution_areas);
...@@ -61,6 +61,7 @@ class InstitutionService extends Service { ...@@ -61,6 +61,7 @@ class InstitutionService extends Service {
institution_detail = institution_detail[0]; institution_detail = institution_detail[0];
institution_detail.address = current_area.address; institution_detail.address = current_area.address;
institution_detail.phone = current_area.phone; institution_detail.phone = current_area.phone;
institution_detail.description = institution.description;
//处理图片 //处理图片
const photo_album = []; const photo_album = [];
for (let i in institution_images) { for (let i in institution_images) {
...@@ -143,7 +144,7 @@ class InstitutionService extends Service { ...@@ -143,7 +144,7 @@ class InstitutionService extends Service {
async getInstitutionAreas(input) { async getInstitutionAreas(input) {
const { ctx } = this; const { ctx } = this;
const attributes = ['id', 'institution_id', 'name', 'address', 'phone']; const attributes = ['id', 'institution_id', 'name', 'address', 'phone', 'lat', 'lng'];
const { institution_id, page, limit } = input; const { institution_id, page, limit } = input;
const where = { institution_id }; const where = { institution_id };
const areas = await ctx.classModel.CourseArea.list({ attributes, page, limit, where }); const areas = await ctx.classModel.CourseArea.list({ attributes, page, limit, where });
...@@ -190,10 +191,10 @@ class InstitutionService extends Service { ...@@ -190,10 +191,10 @@ class InstitutionService extends Service {
} }
//distance=3km //distance=3km
async computeDistance(lbs_array, distance = 3) { async computeDistance(lbs_array, from_gps, distance = 3) {
const { ctx } = this; const { ctx } = this;
const from = { lat: '30.291210', lng: '120.068642' }; const from = from_gps;
const driving_results = await ctx.service.course.lbs.getLBSDistance('driving', from, lbs_array); const driving_results = await ctx.service.course.lbs.getLBSDistance('driving', from, lbs_array);
const walking_results = await ctx.service.course.lbs.getLBSDistance('walking', from, lbs_array); const walking_results = await ctx.service.course.lbs.getLBSDistance('walking', from, lbs_array);
......
...@@ -100,6 +100,30 @@ class UserService extends Service { ...@@ -100,6 +100,30 @@ class UserService extends Service {
return { page: collect_institution_rows.page, count: collect_institution_rows.count, rows: ret }; return { page: collect_institution_rows.page, count: collect_institution_rows.count, rows: ret };
} }
async requestWxAuth(code) {
const { ctx } = this;
const APPID = 'wx4769ebba9b91f8ec';
const SECRET = '1231312321312';
const url = `https://api.weixin.qq.com/sns/jscode2session?appid=${APPID}&secret=${SECRET}&js_code=${code}&grant_type=authorization_code`;
const result = await ctx.helper.send_request(url, {}, { method: 'GET' });
ctx.logger.info(JSON.stringify({ course_mini_auth_ret: result }));
if (result.status === 200) {
ctx.failed('授权失败');
}
const ret = result.data;
if (ret.errcode !== 0) {
ctx.failed(ret.errmsg);
}
const openid = ret.openid;
const session_key = ret.session_key;
return { openid, session_key };
}
} }
......
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