写文章

作者: 六年的承诺 | 来源:发表于2019-04-17 22:29 被阅读0次

验证码过期
点击验证码60码的按钮的禁用问题?
下一个按钮
注册成功后,页面跳转问题
新用户跳转

文章的三种内容
没有图,一到两张张图,大于三章图

1、pom.xml依赖,增加fastjson依赖

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.56</version>
</dependency>

2、数据库表和entity已完成
3、mapper
ArticleMapper,新增文章方法,增加@Options注解,返回自增主键

@Insert("INSERT INTO t_article (u_id,title,content,create_time) VALUES (#{uId},#{title},#{content},#{createTime}) ")
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
void insertArticle(Article article);

ImgMapper,新增图片接口

@Insert("INSERT INTO t_img(a_id,img_url) VALUES (#{aId},#{imgUrl})")
void insertImg(Img img);

4、service
ArticleService接口增加方法

void insertArticle(Article article);

ImgServie接口增加方法

void insertImg(Img img);

5、controller
ArticleController

@PostMapping("/add")
public ResponseResult postArticle(@RequestParam("uId") int uId,
                                  @RequestParam("title") String title,
                                  @RequestParam("content") String content) {
    Article article = new Article();
    article.setUId(uId);
    article.setTitle(title);
    article.setContent(content);
    article.setCreateTime(new Date());
    articleService.insertArticle(article);
    //新增文章后,将获取到的自增主键返回给客户端,用于图片地址的写入
    return ResponseResult.success(article.getId());
}
新建ImgController,注入ImgService,编写如下方法

@PostMapping("/add")
public ResponseResult addImg(@RequestParam("aId") int aId,
                             @RequestParam("imgs") String imgs) {
    //调用FastJson的序列化工具,将前端传过来的图片数组字符串反序列化为Java的List对象
    List<String> imgList = JSONArray.parseArray(imgs, String.class);
    //遍历图片List,创建Img对象写入数据库
    for (String imgUrl:imgList) {
        Img img = new Img();
        img.setAId(aId);
        img.setImgUrl(imgUrl);
        imgService.insertImg(img);
    }
    return ResponseResult.success();
}

6、swagger测试自行完成
7、前端
注意:在首页点击“写文章”按钮,要判定登录状态,如果没登录,跳转到登录页面;如果已经登录,跳转到写文章页面
write.vue文件的HTML部分
<template>
<view class="container">

<input type="text" v-model="title" placeholder="请输入标题" />

<button class="add-btn" @tap="chooseImg">+图片</button>

<textarea placeholder="输入内容" v-model="content" class="content" />
<text>预览</text>

<view class="grace-text">、
<rich-text :nodes="content" bindtap="tap"></rich-text>
</view>
<button class="green-btn" @tap="postArticle">发布文章</button>
</view>
</template>

  • js部分
    data区
      return {
            title: '',
            content: '',
            userId: uni.getStorageSync('login_key').userId,
            imgs: []
        };

methods区
选择图片上传方法

    chooseImg: function() {
            var _this = this;
            uni.chooseImage({
                count: 1,
                sizeType: ['original', 'compressed'],
                sourceType: ['album'],
                success: function(res) {
                    console.log(JSON.stringify(res.tempFilePaths));
                    uni.uploadFile({
                        url: _this.apiServer + '/upload',
                        filePath: res.tempFilePaths[0],
                        name: 'file',
                        success: uploadFileRes => {
                            //图片上传成功,回显图片地址
                            console.log(uploadFileRes.data);
                            //将图片地址加入imgs数组
                            _this.imgs.push(uploadFileRes.data);
                            //将图片地址拼接HTML标签,加入文章内容
                            _this.content += '<img src="' + uploadFileRes.data + '" width = "100%"/>';
                        }
                    });
                }
            });
        }

选择图片上传方法

chooseImg: function() {
        var _this = this;
        uni.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'],
            sourceType: ['album'],
            success: function(res) {
                console.log(JSON.stringify(res.tempFilePaths));
                uni.uploadFile({
                    url: _this.apiServer + '/upload',
                    filePath: res.tempFilePaths[0],
                    name: 'file',
                    success: uploadFileRes => {
                        //图片上传成功,回显图片地址
                        console.log(uploadFileRes.data);
                        //将图片地址加入imgs数组
                        _this.imgs.push(uploadFileRes.data);
                        //将图片地址拼接HTML标签,加入文章内容
                        _this.content += '<img src="' + uploadFileRes.data + '" width = "100%"/>';
                    }
                });
            }
        });
    }

发布文章方法

postArticle: function() {
            var _this = this;
            uni.request({
                url: this.apiServer + '/article/add',
                method: 'POST',
                header: { 'content-type': 'application/x-www-form-urlencoded' },
                data: {
                    uId: this.userId,
                    title: this.title,
                    content: '<div>' + this.content + '</div>'
                },
                success: res => {
                    if (res.data.code === 0) {
                        //获得发布文章成功返回的文章id
                        var aId = res.data.data;
                        console.log(aId);
                        uni.showToast({
                            title: '发布成功'
                        });
                        //将文章id和文章对应的图片地址数组传到后台,存入数据库
                        uni.request({
                            url: this.apiServer + '/img/add',
                            method: 'POST',
                            header: { 'content-type': 'application/x-www-form-urlencoded' },
                            data: {
                                aId: aId,
                                imgs: JSON.stringify(_this.imgs)  //序列化imgs数组
                            },
                            success: res => {
                                if (res.data.code === 0) {
                                    console.log('文章图片地址已写入数据库');
                                }
                            }
                        });
                        uni.switchTab({
                            url: '../index/index'
                        });
                    }
                }
            });
        }

省略css部分

相关文章

  • 测试简书写文章

    测试简书写文章测试简书写文章 测试简书写文章 v测试简书写文章 测试简书写文章 测试简书写文章 测试简书写文章 测...

  • 2022-09-09

    左左很会写文章 左左很会写文章 左左很会写文章 左左很会写文章 左左很会写文章 左左很会写文章 左左很会写文章 左...

  • 为了写文章而写文章

    年底本就是最忙的时候,偏偏宝宝感冒了,老婆也感冒了,等下晚上还要去医院,实在没办法了,只能写这么几句作为00...

  • 写文章vs没写文章

    在简书开会员写文章跟没写文章收益还是有很大的对比性。 第一,有写文章但不阅读文章点赞的权重不高的用户收益不可能很高...

  • 写文章,

    当务之急,先发两篇文章再说。

  • 写文章

    愿生活是有诗意的

  • 写文章

    建立个人品牌。 击败百分八十的作者。 题材观点独特,标题党。 流行观点相反,是肯定某些理论。 写争议性的话题。。 ...

  • 写文章

    写丢了。

  • 写文章

    职场中,作为管理者会遇到各种各样的员工,有的员工工作能力强,是自我驱动型的人,领导不需要太花费时间和精力去盯着他做...

  • 写文章

    一篇非常棒的文章。排版也非常重要,也就是版式。现在人是碎片是生活,不爱看大段的文字。我们排版就要符合阅读习惯,要小...

网友评论

      本文标题:写文章

      本文链接:https://www.haomeiwen.com/subject/snpyiqtx.html