浏览代码

1.新增部分模块单元测试
2.新增登录记录时间

olym 7 年之前
父节点
当前提交
d9136c0fbe

+ 31 - 0
app/base/base_service.js

@@ -84,6 +84,37 @@ class BaseService extends Service {
     }
 
     /**
+     * 更新数据
+     *
+     * @param {Object} data - 需要更新的数据
+     * @param {Object} condition - 更新的条件筛选
+     * @return {Boolean} - 返回更新结果
+     */
+    async update(data, condition) {
+        if (Object.keys(data).length <= 0 || Object.keys(condition).length <= 0) {
+            return false;
+        }
+        const sqlParam = [];
+        const param = [];
+        for (const key in data) {
+            param.push(key + ' = ?');
+            sqlParam.push(data[key]);
+        }
+        const paramString = param.join(',');
+
+        const where = [];
+        for (const key in condition) {
+            where.push(key + ' = ?');
+            sqlParam.push(condition[key]);
+        }
+        const whereString = where.join(' AND ');
+
+        const sql = 'UPDATE ' + this.tableName + ' SET ' + paramString + ' WHERE ' + whereString;
+        const result = await this.db.query(sql, sqlParam);
+        return result.affectedRows > 0;
+    }
+
+    /**
      * 获取分页数据(SqlBuilder版本)
      *
      * @return {Array} - 返回分页数据

+ 8 - 4
app/lib/sso.js

@@ -43,16 +43,20 @@ class SSO {
             };
 
             const responseData = await this.ctx.helper.sendRequest(this.authUrl, postData);
-            if (responseData.length <= 0) {
-                throw '接口返回错误:' + responseData.err;
+            if (responseData.length <= 0 || typeof responseData === 'number') {
+                throw '接口返回错误:' + responseData;
             }
+
             // 如果验证成功,则新增SSO数据到数据库
             const addResult = await this.ctx.service.customer.addSSOUser(responseData[0]);
             if (!addResult) {
                 console.log('sso user add error');
             }
-
-            result = true;
+            // 更新用户登录时间
+            const updateData = {
+                last_login: new Date().getTime() / 1000,
+            };
+            result = await this.ctx.service.customer.update(updateData, { email: responseData[0].useremail });
         } catch (error) {
             console.log('sso:' + error);
             result = false;

+ 7 - 0
app/service/project_account.js

@@ -90,6 +90,13 @@ module.exports = app => {
                     result = encryptPassword === accountData.password;
                 }
 
+                // 如果成功则更新登录时间
+                if (result) {
+                    const updateData = {
+                        last_login: new Date().getTime() / 1000,
+                    };
+                    await this.update(updateData, { id: accountData.id });
+                }
             } catch (error) {
                 console.log(error);
                 result = false;

+ 15 - 0
test/app/service/customer.test.js

@@ -0,0 +1,15 @@
+/**
+ * sso账号数据模型单元测试
+ *
+ * @author CaiAoLin
+ * @date 2017/11/20
+ * @version
+ */
+
+'use strict';
+
+const { app, assert } = require('egg-mock/bootstrap');
+
+describe('test/app/service/customer.test.js', () => {
+
+});

+ 1 - 1
test/app/service/project.test.js

@@ -10,7 +10,7 @@
 
 const { app, assert } = require('egg-mock/bootstrap');
 
-describe('test/app/lib/project.test.js', () => {
+describe('test/app/service/project.test.js', () => {
 
     it('not exist code', function* () {
         const ctx = app.mockContext();

+ 50 - 0
test/app/service/project_account.test.js

@@ -0,0 +1,50 @@
+/**
+ * 项目账号数据模型单元测试
+ *
+ * @author CaiAoLin
+ * @date 2017/11/20
+ * @version
+ */
+
+'use strict';
+
+const { app, assert } = require('egg-mock/bootstrap');
+
+describe('test/app/service/project_account.test.js', () => {
+
+    it('test login success (sso)', function* () {
+        const ctx = app.mockContext();
+        const postData = {
+            account: 'laiku123@qq.com',
+            project: 'J201711163164',
+            project_password: '19930523',
+        };
+
+        const result = yield ctx.service.projectAccount.accountLogin(postData);
+        assert(result);
+    });
+
+    it('test login success (local)', function* () {
+        const ctx = app.mockContext();
+        const postData = {
+            account: 'test',
+            project: 'Y201711167257',
+            project_password: '123123',
+        };
+
+        const result = yield ctx.service.projectAccount.accountLogin(postData);
+        assert(result);
+    });
+
+    it('test login fail', function* () {
+        const ctx = app.mockContext();
+        const postData = {
+            account: 'laiku123@qq.com',
+            project: 'J201711163164',
+            project_password: '1111',
+        };
+
+        const result = yield ctx.service.projectAccount.accountLogin(postData);
+        assert(!result);
+    });
+});