Explorar o código

增加表与修改表结构

liuyu %!s(int64=4) %!d(string=hai) anos
pai
achega
cfff2dbb3d

+ 16 - 1
app/controller/.setting.js

@@ -1,6 +1,11 @@
 module.exports = {
   create: {
     requestBody: [
+      'planyearid',
+      'planid',
+      'termid',
+      'user_email',
+      'auth_code',
       'am_start',
       'am_end',
       'pm_start',
@@ -16,6 +21,11 @@ module.exports = {
   update: {
     params: ['!id'],
     requestBody: [
+      'planyearid',
+      'planid',
+      'termid',
+      'user_email',
+      'auth_code',
       'am_start',
       'am_end',
       'pm_start',
@@ -33,7 +43,12 @@ module.exports = {
   index: {
     parameters: {
       query: {
-        am_start :'am_start'
+        planyearid: 'planyearid',
+        planid: 'planid',
+        termid: 'termid',
+        user_email: 'user_email',
+        auth_code: 'auth_code',
+        am_start: 'am_start'
       }
     },
     service: 'query',

+ 2 - 0
app/controller/.user.js

@@ -5,6 +5,7 @@ module.exports = {
       '!mobile',
       '!passwd',
       'openid',
+      'unionid',
       'remark',
       'type',
       'uid'
@@ -21,6 +22,7 @@ module.exports = {
       'mobile',
       'passwd',
       'openid',
+      'unionid',
       'remark',
       'type',
       'uid'

+ 5 - 0
app/controller/setting.js

@@ -13,6 +13,11 @@ class SettingController extends Controller {
     this.service = this.ctx.service.setting;
   }
 
+  async findone() {
+    const data = await this.service.findone();
+    this.ctx.ok({ data });
+  }
+
 }
 
 module.exports = CrudController(SettingController, meta);

+ 2 - 1
app/controller/weixin.js

@@ -103,8 +103,9 @@ class WeixinController extends Controller {
 
         } else {
           console.log('rrr0000--->' + redirect_uri);
+          const resunionid = await weixin.fetchUnionID(openid);
           const touri = `${this.app.config.baseUrl}/student/bind`;
-          const to_uri = urljoin(touri, `?openid=${openid}`);
+          const to_uri = urljoin(touri, `?openid=${openid}&unionid=${resunionid.unionid}`);
           // TODO: 重定性页面
           this.ctx.redirect(to_uri);
         }

+ 5 - 0
app/model/setting.js

@@ -4,6 +4,11 @@ const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
 
 // 设置表
 const SettingSchema = {
+  planyearid: { type: String, required: true, maxLength: 100 }, // 默认大批次id
+  planid: { type: String, required: true, maxLength: 100 }, // 默认年度计划id
+  termid: { type: String, required: true, maxLength: 100 }, // 默认期id
+  user_email: { type: String, required: true, maxLength: 100 }, // 服务器邮箱
+  auth_code: { type: String, required: true, maxLength: 100 }, // 服务器邮箱授权码
   am_start: { type: String, required: true, maxLength: 20 }, // 上午考勤开始时间
   am_end: { type: String, required: true, maxLength: 20 }, // 上午考勤结束时间
   pm_start: { type: String, required: true, maxLength: 20 }, // 下午开始时间

+ 20 - 0
app/model/trainplanyear.js

@@ -0,0 +1,20 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
+
+// 培训计划表
+const TrainplanyearSchema = {
+  year: { type: String, required: true, maxLength: 200 }, // 年份
+  title: { type: String, required: true, maxLength: 500 }, // 标题
+  remark: { type: String, required: false }, // 备注
+};
+
+
+const schema = new Schema(TrainplanyearSchema, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.plugin(metaPlugin);
+
+module.exports = app => {
+  const { mongoose } = app;
+  return mongoose.model('Trainplanyear', schema, 'trainplan_year');
+};

+ 1 - 0
app/model/user.js

@@ -9,6 +9,7 @@ const UserSchema = {
   mobile: { type: String, required: true, maxLength: 64 }, // 手机
   passwd: { type: Secret, select: false }, // 注册密码
   openid: { type: String, required: false }, // 微信openid
+  unionid: { type: String, required: false }, // 微信unionid
   remark: { type: String, required: false }, // 备注
   type: { type: String, required: false, maxLength: 200 }, // 身份,0、中心管理元1、班主任用户2、学校用户3、教师4、学生
   uid: { type: String, required: false, maxLength: 200 }, // 关联用户信息id

+ 4 - 0
app/router.js

@@ -8,6 +8,10 @@ module.exports = app => {
   router.get('/', controller.home.index);
   // 共通查询单条记录方法
   router.get('/api/train/common/findone/:modelname', controller.common.findone);
+  // 基础设置表路由
+  router.get('/api/train/setting/findone', controller.setting.findone);
+  router.resources('setting', '/api/train/setting', controller.setting); // index、create、show、destroy
+  router.post('setting', '/api/train/setting/update/:id', controller.setting.update);
 
   // 科目表设置路由
   router.resources('subject', '/api/train/subject', controller.subject); // index、create、show、destroy

+ 3 - 3
app/service/attendance.js

@@ -21,10 +21,10 @@ class AttendanceService extends CrudService {
   }
 
   async attendancecreate(data) {
-    const { openid, type, ibeacon } = data;
-    assert(openid && type && ibeacon, '缺少信息项');
+    const { unionid, type, ibeacon } = data;
+    assert(unionid && type && ibeacon, '缺少信息项');
     // 通过openid取得学生信息
-    const user = await this.ctx.service.user.findByOpenid(openid);
+    const user = await this.ctx.service.user.findByunionid(unionid);
     if (!user) {
       throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '用户不存在');
     }

+ 47 - 0
app/service/setting.js

@@ -13,6 +13,53 @@ class SettingService extends CrudService {
     this.model = this.ctx.model.Setting;
   }
 
+  async findone() {
+    return await this.model.findOne();
+  }
+
+  async update({ id }, { planyearid, planid, termid, user_email, auth_code, am_start, am_end, pm_start, pm_end, bd_start, bd_end }) {
+    assert(id, '缺少部分信息项');
+    const setting = await this.model.findById(id);
+    if (!setting) {
+      throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '设置信息不存在');
+    }
+    if (planyearid) {
+      setting.planyearid = planyearid;
+    }
+    if (planid) {
+      setting.planid = planid;
+    }
+    if (termid) {
+      setting.termid = termid;
+    }
+    if (user_email) {
+      setting.user_email = user_email;
+    }
+    if (auth_code) {
+      setting.auth_code = auth_code;
+    }
+    if (am_start) {
+      setting.am_start = am_start;
+    }
+    if (am_end) {
+      setting.am_end = am_end;
+    }
+    if (pm_start) {
+      setting.pm_start = pm_start;
+    }
+    if (pm_end) {
+      setting.pm_end = pm_end;
+    }
+    if (bd_start) {
+      setting.bd_start = bd_start;
+    }
+    if (bd_end) {
+      setting.bd_end = bd_end;
+    }
+    await setting.save();
+    return setting;
+  }
+
 }
 
 module.exports = SettingService;

+ 10 - 2
app/service/user.js

@@ -78,7 +78,7 @@ class UserService extends CrudService {
 
   // 学生绑定
   async bind(data) {
-    const { openid, id_number, mobile, passwd } = data;
+    const { openid, id_number, mobile, passwd, unionid } = data;
     assert(openid && id_number && mobile, '缺少部分信息项');
     let user = await this.model.findOne({ mobile });
     if (user) {
@@ -86,6 +86,7 @@ class UserService extends CrudService {
         throw new BusinessError(ErrorCode.BAD_PASSWORD);
       }
       user.openid = openid;
+      user.unionid = unionid;
       await user.save();
     } else {
       const stud = await this.stuModel.findOne({ id_number });
@@ -94,7 +95,7 @@ class UserService extends CrudService {
       }
       stud.openid = openid;
       await stud.save();
-      const newdata = { name: stud.name, mobile: stud.phone, type: '4', uid: stud.id, openid };
+      const newdata = { name: stud.name, mobile: stud.phone, type: '4', uid: stud.id, openid, unionid };
       newdata.passwd = { secret: '12345678' };
       user = await this.model.create(newdata);
     }
@@ -138,6 +139,13 @@ class UserService extends CrudService {
     return user;
   }
 
+  // 通过unionid查询用户信息
+  async findByunionid(unionid) {
+    // 通过unionid查询用户信息
+    const user = await this.model.findOne({ unionid });
+    return user;
+  }
+
   // 学校用户生成
   async schoolregister() {
     const schools = await this.schModel.find();

+ 39 - 0
app/service/weixin.js

@@ -34,6 +34,45 @@ class WeixinAuthService extends AxiosService {
     return res;
   }
 
+  // 通过openid获得用户信息
+  async fetchUnionID(openid) {
+    // TODO:参数检查和默认参数处理
+    assert(openid);
+    const appid = 'wxdf3ed83c095be97a';
+    const grant_type = 'client_credential';
+    const secret = '748df7c2a75077a79ae0c971b1638244';
+    // TODO: 获得用户信息
+    const url = 'http://wx.cc-lotus.info/api.weixin.qq.com/cgi-bin/token?appid=' + appid + '&grant_type=' + grant_type + '&secret=' + secret;
+    const res = await this.ctx.curl(url, {
+      method: 'get',
+      headers: {
+        'content-type': 'application/json',
+      },
+      dataType: 'json',
+    });
+    // console.debug('res: ', res);
+    if (res.errcode && res.errcode !== 0) {
+      this.ctx.logger.error(`[WeixinAuthService] fetch userinfo by openid fail, errcode: ${res.errcode}, errmsg: ${res.errmsg}`);
+      throw new BusinessError(ErrorCode.SERVICE_FAULT, '获得微信用户信息失败');
+    }
+    const token = res.access_token;
+    console.log(token);
+    const urlun = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token=' + token + '&openid=' + openid + '&lang=zh_CN';
+    const result = await this.ctx.curl(urlun, {
+      method: 'get',
+      headers: {
+        'content-type': 'application/json',
+      },
+      dataType: 'json',
+    });
+    // console.debug('res: ', res);
+    if (result.errcode && result.errcode !== 0) {
+      this.ctx.logger.error(`[WeixinAuthService] fetch userinfo by openid fail, errcode: ${res.errcode}, errmsg: ${res.errmsg}`);
+      throw new BusinessError(ErrorCode.SERVICE_FAULT, '获得微信用户信息失败');
+    }
+    return result;
+  }
+
   async createJwt({ openid, nickname, subscribe }) {
     const { secret, expiresIn = '1d', issuer = 'weixin' } = this.config.jwt;
     const subject = openid;