Просмотр исходного кода

feat: 更新关注和取消关注接口,调整返回格式,优化事务处理逻辑

caipin 1 месяц назад
Родитель
Сommit
6d482f315f
2 измененных файлов с 41 добавлено и 50 удалено
  1. 16 16
      app/controller/weapp_attention_controller.js
  2. 25 34
      app/service/weapp_attention.js

+ 16 - 16
app/controller/weapp_attention_controller.js

@@ -8,17 +8,17 @@ module.exports = app => {
             try {
                 const { tenderIds } = ctx.request.body;
                 if (!tenderIds || !Array.isArray(tenderIds) || tenderIds.length === 0) {
-                    ctx.body = { err: 1, msg: '参数错误:请提供标段ID数组', data: null };
+                    ctx.body = { code: 0, msg: '参数错误:请提供标段ID数组', data: null };
                     return;
                 }
                 const result = await ctx.service.weappAttention.followSections(tenderIds, ctx.projectAccount.id);
                 if (result) {
-                    ctx.body = { err: 0, msg: '关注成功', data: null };
+                    ctx.body = { code: 0, msg: '关注成功', data: null };
                 } else {
-                    ctx.body = { err: 1, msg: '关注失败', data: null };
+                    ctx.body = { code: 0, msg: '关注失败', data: null };
                 }
             } catch (error) {
-                ctx.body = this.ajaxErrorBody(error, '关注操作失败');
+                ctx.body = { code: -1, msg: error.toString(), data: null };
             }
         }
 
@@ -26,19 +26,19 @@ module.exports = app => {
         async unfollow() {
             const { ctx } = this;
             try {
-                const { tenderIds } = ctx.request.body;
-                if (!tenderIds || !Array.isArray(tenderIds) || tenderIds.length === 0) {
-                    ctx.body = { err: 1, msg: '参数错误:请提供标段ID数组', data: null };
+                const { attentionIds } = ctx.request.body;
+                if (!attentionIds || !Array.isArray(attentionIds) || attentionIds.length === 0) {
+                    ctx.body = { code: 0, msg: '参数错误:请提供关注ID', data: null };
                     return;
                 }
-                const result = await ctx.service.weappAttention.unfollowSections(tenderIds, ctx.projectAccount.id);
+                const result = await ctx.service.weappAttention.unfollowSections(attentionIds, ctx.projectAccount.id);
                 if (result) {
-                    ctx.body = { err: 0, msg: '取消关注成功', data: null };
+                    ctx.body = { code: 0, msg: '取消关注成功', data: null };
                 } else {
-                    ctx.body = { err: 1, msg: '取消关注失败', data: null };
+                    ctx.body = { code: -1, msg: '取消关注失败', data: null };
                 }
             } catch (error) {
-                ctx.body = this.ajaxErrorBody(error, '取消关注操作失败');
+                ctx.body = { code: -1, msg: error.toString(), data: null };
             }
         }
 
@@ -47,9 +47,9 @@ module.exports = app => {
             const { ctx } = this;
             try {
                 const result = await ctx.service.weappAttention.getFollowedSections(ctx.projectAccount.id);
-                ctx.body = { err: 0, msg: '', data: result };
+                ctx.body = { code: 0, msg: '', data: result };
             } catch (error) {
-                ctx.body = this.ajaxErrorBody(error, '获取关注列表失败');
+                ctx.body = { code: -1, msg: error.toString(), data: null };
             }
         }
 
@@ -59,13 +59,13 @@ module.exports = app => {
             try {
                 const { tenderId } = ctx.query;
                 if (!tenderId) {
-                    ctx.body = { err: 1, msg: '参数错误:请提供标段ID', data: null };
+                    ctx.body = { code: 0, msg: '参数错误:请提供标段ID', data: null };
                     return;
                 }
                 const isFollowing = await ctx.service.weappAttention.isFollowingSection(ctx.projectAccount.id, tenderId);
-                ctx.body = { err: 0, msg: '', data: { following: isFollowing } };
+                ctx.body = { code: 0, msg: '', data: { following: isFollowing } };
             } catch (error) {
-                ctx.body = this.ajaxErrorBody(error, '检查关注状态失败');
+                ctx.body = { code: -1, msg: error.toString(), data: null };
             }
         }
 

+ 25 - 34
app/service/weapp_attention.js

@@ -7,16 +7,9 @@ module.exports = app => {
             this.tableName = 'weapp_attention';
         }
 
-        /**
-         * 关注标段
-         * @param {Array} tenderIds - 标段ID数组
-         * @param {Number} userId - 用户ID
-         * @return {Boolean} - 是否成功
-         */
         async followSections(tenderIds, userId) {
-            const conn = this.db;
+            const conn = await this.db.beginTransaction();
             try {
-                await conn.beginTransaction();
                 for (const tenderId of tenderIds) {
                     const existingRecord = await this.getDataByCondition({
                         project_account_id: userId,
@@ -26,9 +19,12 @@ module.exports = app => {
                     if (existingRecord) {
                         if (existingRecord.status === 0) {
                             await conn.update(this.tableName, {
-                                id: existingRecord.id,
                                 status: 1,
                                 create_time: this.app.formatDate(new Date())
+                            }, {
+                                where: {
+                                    id: existingRecord.id
+                                }
                             });
                         }
                     } else {
@@ -46,26 +42,30 @@ module.exports = app => {
                 await conn.rollback();
                 this.ctx.throw(500, error.message);
                 return false;
+            } finally {
+                conn.release && conn.release();
             }
         }
 
-        /**
-         * 取消关注标段
-         * @param {Array} tenderIds - 标段ID数组
-         * @param {Number} userId - 用户ID
-         * @return {Boolean} - 是否成功
-         */
-        async unfollowSections(tenderIds, userId) {
-            const conn = this.db;
+        async unfollowSections(attentionIds, userId) {
+            const conn = await this.db.beginTransaction();
             try {
-                await conn.beginTransaction();
-
-                for (const tenderId of tenderIds) {
-                    await conn.update(this.tableName, {
+                for (const attentionId of attentionIds) {
+                    const existingRecord = await this.getDataByCondition({
                         project_account_id: userId,
-                        tender_id: tenderId,
-                    }, {
+                        id: attentionId,
+                    });
+                    if (!existingRecord) {
+                        await conn.rollback();
+                        return false;
+                    }
+                    await conn.update(this.tableName, {
                         status: 0
+                    }, {
+                        where: {
+                            project_account_id: userId,
+                            id: attentionId,
+                        }
                     });
                 }
                 await conn.commit();
@@ -74,14 +74,11 @@ module.exports = app => {
                 await conn.rollback();
                 this.ctx.throw(500, error.message);
                 return false;
+            } finally {
+                conn.release && conn.release();
             }
         }
 
-        /**
-         * 获取用户关注的标段列表
-         * @param {Number} userId - 用户ID
-         * @return {Array} - 关注的标段列表
-         */
         async getFollowedSections(userId) {
             const attentionRecords = await this.getAllDataByCondition({
                 where: {
@@ -121,12 +118,6 @@ module.exports = app => {
             return result;
         }
 
-        /**
-         * 检查用户是否关注了某个标段
-         * @param {Number} userId - 用户ID
-         * @param {Number} tenderId - 标段ID
-         * @return {Boolean} - 是否关注
-         */
         async isFollowingSection(userId, tenderId) {
             const record = await this.getDataByCondition({
                 project_account_id: userId,