uni-app 139发布朋友圈api开发(一)

142 篇文章 4 订阅
128 篇文章 1 订阅

/app/controller/moment.js

'use strict';

const Controller = require('egg').Controller;

class MomentController extends Controller {
    // 发布朋友圈
    async create() {
        const { ctx, app } = this;
        let current_user_id = ctx.authUser.id;
        // 参数验证
        ctx.validate({
            content: {
                type: 'string',
                required: false,
                desc: '内容'
            },
            image: {
                type: 'string',
                required: false,
                desc: '图片'
            },
            video: {
                type: 'string',
                required: false,
                desc: '视频'
            },
            type: {
                type: 'string',
                required: true,
                range: {
                    in: ['content', 'image', 'video']
                },
                desc: '朋友圈类型'
            },
            location: {
                type: 'string',
                required: false,
                desc: '位置'
            },
            remind: {
                type: 'string',
                required: false,
                defValue: "",
                desc: '提醒谁看'
            },
            see: {
                type: 'string',
                required: false,
                defValue: "all",
                desc: '谁可以看'
            }
        });

        let { content, image, video, type, location, remind, see } = ctx.request.body;

        if (!ctx.request.body[type]) {
            return ctx.apiFail(`${type} 不能为空`);
        }

        let moment = await app.model.Moment.create({
            content, image, video, location, remind, see,
            user_id: current_user_id
        });

        if (!moment) {
            return ctx.apiFail('发布失败');
        }

        // 推送到好友的时间轴
        this.toTimeline(moment);

        ctx.apiSuccess('ok');
    }

    // 推送到好友的时间轴
    async toTimeline(moment) {
        const { ctx, app } = this;
        let current_user_id = ctx.authUser.id;
        // 获取当前用户所有好友
        let friends = await app.model.Friend.findAll({
            where: {
                user_id: current_user_id,
                isblack: 0
            },
            attributes: ['friend_id']
        });
        // 谁可以看
        /**
         all                全部人可看
         only:1,2,3         指定人可见
         except:1,2,3       谁不可看
         none               仅自己可见
         */
        let sees = moment.see.split(':');
        let o = {
            only: [],
            except: []
        }
        let oType = sees[0];
        if ((sees[0] === 'only' || sees[0] === 'except') && sees[1]) {
            o[sees[0]] = (sees[1].split(',')).map(v => parseInt(v));
        }

        
    }

  

}

module.exports = MomentController;

感谢大家观看,我们下次见

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

2019ab

你的鼓励就是我的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值