目录
前言
HarmonyOS ArkUI应用开发之AI作诗,根据输入的藏头诗关键字或诗的第一句,通过调用AI接口自动生成诗,然后将返回的诗按照句号切割为数组,使用Marquee标签滚动诗句,由于一句诗句长度不够,不能滚动,在后面使用空格补上长度,使诗句可以滚动。
效果

开发环境
开发工具:DevEco Studio 3.0 Beta4
SDK:HarmonyOS SDK API Version 8
开发过程
- 由于调用AI接口生成诗,通过网络获取数据。
需要权限: ohos.permission.INTERNET
之前我们都是直接在config.json代码模式添加权限的,最新开发工具可以在图形化下添加:

新版本开发工具的预览器下,也支持网络请求数据了,从上面效果图就可以看出,是在预览器下录制的效果。
- 界面相对比较简单,就把关键标签加上注释,HML代码如下:
- CSS代码如下:
- JS代码如下:
import fetch from '@system.fetch';
import prompt from '@system.prompt';
export default {
data: {
flagOne: false,
flagTwo: false,
inputOne: '我爱祖国',
inputTwo: '沁园春雪',
poetryOne: [],
poetryTwo: []
},
textChangeOne(e) {
this.inputOne = e.text;
},
textChangeTwo(e) {
this.inputTwo = e.text;
},
onClickOne() {
this.inputOne = this.inputOne.replace(/[\s]+/g, "").replace(/\n/g, "").replace(/\r/g, "");
if (this.inputOne === "") {
prompt.showDialog({
message: "不能为空,请输入汉字",
duration: 3000,
bottom: '80%'
})
return;
}
var url = "https://py.myie9.com/cangtoutest/"+this.inputOne;
var that = this;
fetch.fetch({
url: url,
method: 'GET',
responseType: 'text',
success: function(res) {
console.log('xx' + JSON.stringify(res));
if(res.code == 500) {
prompt.showToast({
message: "请输入至少4个不同的汉字",
duration: 3000,
bottom: '80%'
})
return;
}
prompt.showToast({
message: "藏头诗生成成功",
duration: 3000,
bottom: '80%'
})
var descOne = res.data;
var descOneArray = descOne.split("。");
console.log('xx' + descOne)
that.poetryOne = []
var rowLen = 50;
for (var index = 0; index < descOneArray.length; index++) {
if(descOneArray[index] === '') {
continue;
}
var str = descOneArray[index] + "。";
var currentLen = str.length;
if(currentLen < rowLen) {
for(var i=0; i< rowLen - currentLen; i++) {
str += " ";
}
}
that.poetryOne.push(str)
}
},
fail: function(err) {
prompt.showToast({
message: "生成失败,请重新生成",
duration: 3000,
bottom: '80%'
})
}
})
},
onClickTwo() {
this.inputTwo = this.inputTwo.replace(/[\s]+/g, "").replace(/\n/g, "").replace(/\r/g, "");
if (this.inputTwo === "") {
prompt.showToast({
message: "不能为空,请输入一句诗",
duration: 3000,
bottom: '50%'
})
return;
}
var url = "https://py.myie9.com/xuxietest/"+this.inputTwo;
var that = this;
fetch.fetch({
url: url,
method: 'GET',
responseType: 'text',
success: function(res) {
console.log('xx' + JSON.stringify(res));
if(res.code == 500) {
prompt.showToast({
message: "整首诗生成失败",
duration: 3000,
bottom: '50%'
})
return;
}
prompt.showToast({
message: "整首诗生成成功",
duration: 3000,
bottom: '50%'
})
var descTwo = res.data;
var descTwoArray = descTwo.split("。");
console.log('xx' + descTwo)
that.poetryTwo = []
var rowLen = 50;
for (var index = 0; index < descTwoArray.length; index++) {
if(descTwoArray[index] === '') {
continue;
}
var str = descTwoArray[index] + "。";
var currentLen = str.length;
if(currentLen < rowLen) {
for(var i=0; i< rowLen - currentLen; i++) {
str += " ";
}
}
that.poetryTwo.push(str)
}
},
fail: function(err) {
prompt.showToast({
message: "生成失败,请重新生成",
duration: 3000,
bottom: '50%'
})
}
})
},
onMarqueeOne() {
for (var i = 0; i < this.poetryOne.length; i++) {
if(!this.flagOne){
this.$element('oneMarquee'+i).stop();
}else{
this.$element('oneMarquee'+i).start();
}
}
this.flagOne = !this.flagOne
},
onMarqueeTwo() {
for (var i = 0; i < this.poetryTwo.length; i++) {
if(!this.flagTwo){
this.$element('twoMarquee'+i).stop();
}else{
this.$element('twoMarquee'+i).start();
}
}
this.flagTwo = !this.flagTwo;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
- 177.
- 178.
- 179.
- 180.
- 181.
- 182.
- 183.
- 184.
- 185.
- 186.
- 187.
- 188.
- 189.
- 190.
- 191.
- 192.
- 193.
- 194.
- 195.
- 196.
- 197.
- 198.
- 199.
- 200.
- 201.
- 202.
- 203.
- 204.
- 205.
- 206.
- 207.
- 208.
- 209.
- 210.
- 211.
- 212.
- 213.
- 214.
- 215.
- 216.
- 217.
- 218.
- 219.
- 220.
- 221.
总结
项目虽简单,但可以让我们学习到HarmonyOS JS开发,原来和其它小程序,html5开发一样简单,有了基础知识了,结合官方API文档,就可以做出更多有趣的项目。
没发现社区竟然潜伏了这么多大诗人。
都是同一个老师教出来的^_^
哈哈哈,搞搞情诗,造福一下社区的开发者😝😝😝