Home>Article>WeChat Applet> Let’s take a look at how to create the “Intelligent Spring Festival Couplet” applet for the Spring Festival in this article!
2022 has officially arrived. There are only ten days left before the Chinese Lunar New Year. Spring Festival couplets are indispensable for the Spring Festival. The following article will look at how to implement the "Smart Spring Festival Couplets" applet for the Spring Festival. I hope it will be helpful to everyone. Helps!
The New Year is approaching, let’s make a smart Spring Festival couplet applet to cheer everyone up! I wish all digging friends a happy new year in 2022 in advance! New Year is coming soon!
Random Spring Couplets
##Specified last name
Experience addressScan the QR code below on WeChat or WeChat search treasure program
##Source code addressMaster the languageGitee: https://gitee.com/nanfangzhe/wechat_demo
2. Preparation work
:WeChat Mini Program LanguageTechnical Points
: ①WeChat Mini Program Cloud Development②Baidu AI Intelligent Creation Platform - Intelligent Writing of Spring Festival Couplets3. Game Process and Rules
: You can clickrandom Spring Festival CoupletsandSpecify the last name
to get the desired Spring Festival couplets~Explanation of terms
: ①Random Spring Festival couplets, randomly display a pair of Spring Festival couplets. ②Specify surname: Enter your surname to generate a couplet.4. Deployment steps
3️⃣What needs to be modified: APPID (the APPID entered when opening it with the WeChat developer tool), Baidu token (in the couplet folder index.js file), cloud development environment env (in app.js file)
5. Logic explanation and core code
1 Logic explanation of random Spring Festival coupletsSince the smart Spring Festival couplets API interface provided by Baidu requires parameters to be passed, we can prepare a small data collection in advance, pass the value when clicked, and then return the required couplets. Therefore, we have collected some words and phrases used to welcome the Spring Festival and celebrate the New Year.
var RANDOM_TEXT_LIST = ["虎", "虎年", "迎春", "春节", "过年", "年兽", "过春节", "初一", "年初", "红红火火", "红火", "开心", "开开心心", "健康", "健健康康", "长寿", "平安", "平平安安", "家庭", "家庭和睦", "和睦", "子子孙孙", "勤劳", "福气", "福", "致富", "富裕", "富", "合家欢喜", "合家", "欢喜", "喜庆", "喜", "囍", "生意兴隆", "恭喜发财", "大富大贵", "富贵", "富裕", "丰年", "子孙满堂", "心欢喜", "人间喜", "灯火", "灯笼", "烟花", "爆竹"]
2 Explanation of the logic of specifying the surnameThis is similar to the logic of random Spring Festival couplets, only However, the user is required to enter the last name, but the API interface used is a bit awkward. If only one character of the last name is entered, the output content is a bit unreasonable. So the solution here is to splice and add the word "家". For example, if you enter the surname: Liu, the passed value is the Liu family; if the input is Zhang, it is the Zhang family...
ok: function () { var text = this.data.textV if (!text || text.length > 4) { wx.showToast({ title: '姓氏暂不支持超过4个字哦!', icon: 'none' }) return; } this.getCoupletByTxt(text + "家") // 智能写对联 this.setData({ showModal: false }) },
3 Core CodeAfter the previous logical explanation, you may know that the core code is a publicly called method. (Bingo~ You guessed it)
Method called by random Spring Festival couplets
// 随机春联的调用方法 bindGetRandomCouplet() { let that = this var num = parseInt(Math.random() * (MAX_NUM - MIN_NUM + 1) + MIN_NUM, 10); // 生成[n,m]的随机整数 that.getCoupletByTxt(RANDOM_TEXT_LIST[num]) // 智能写对联 },Method called by specified last name
// 随机春联的调用方法 ok: function () { var text = this.data.textV if (!text || text.length > 4) { wx.showToast({ title: '姓氏暂不支持超过4个字哦!', icon: 'none' }) return; } this.getCoupletByTxt(text + "家") // 智能写对联 this.setData({ showModal: false }) },Public methods for calling smart Spring Festival couplets
// 智能写对联(API接口来源,参考百度-语言处理技术-智能创作平台-智能写对联:https://ai.baidu.com/ai-doc/NLP/Ok53wb6dh) getCoupletByTxt(text) { let that = this console.log(text) // 字符串(限5字符数以内)即作诗的主题 if (!text || text.length > 5) { wx.showToast({ title: '主题限制5个字以内哦!', icon: 'none' }) return; } wx.cloud.callFunction({ name: 'couplet', data: { action: 'getCoupletByTxt', text } }).then(res => { console.log(res) if (res.result.error_code) { if ("17".indexOf(res.result.error_code) != -1) { wx.showToast({ title: '调用次数用完啦,点击右下角小电话,联系开发者充次钱充次数啦!', icon: 'none', duration: 3000, }) } else { wx.showToast({ title: '当前对联不太行,请重试!', icon: 'none' }) } return; } that.setData({ couplets: res.result.couplets }) }) },Public methods for calling cloud functions
// 注:先看readme.md文件 // 对联生成请求 const cloud = require('wx-server-sdk') var rp = require('request-promise') cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }) const DB = cloud.database() // 天行数据的KEY var TIAN_XING_KEY = '' // 天行数据的接口API var TIAN_XING_API = 'http://api.tianapi.com/duilian/index' // 百度Token var BAI_DU_ACCESS_TOKEN = '' // 这里需要自行去申请咯~ // 百度接口api var BAI_DU_API = [ "https://aip.baidubce.com/rpc/2.0/creation/v1/poem", // 智能写诗 "https://aip.baidubce.com/rpc/2.0/creation/v1/couplets" // 智能写对联 ] // 云函数入口函数 exports.main = async (event, context) => { var { action, text } = event var data = {} switch (action) { case 'getPoemByTxt': { data.text = text if (text == "") return { message: '缺少参数text' } // 智能写诗(API接口来源,参考百度-语言处理技术-智能创作平台-智能写诗:https://ai.baidu.com/ai-doc/NLP/ak53wc3o3) return new Promise((resolve, reject) => { try { rp({ method: 'POST', headers: { "content-type": "application/json", }, body: JSON.stringify(data), url: BAI_DU_API[0] + '?access_token=' + BAI_DU_ACCESS_TOKEN, // text必要参数,写诗的主题内容 }, function (error, response, body) { if (error) { return reject(error); } return resolve(JSON.parse(body)); }) } catch (e) { return reject(e) } }); } case 'getCoupletByTxt': { data.text = text if (text == "") return { message: '缺少参数text' } // 智能写对联(API接口来源,参考百度-语言处理技术-智能创作平台-智能写对联:https://ai.baidu.com/ai-doc/NLP/Ok53wb6dh) return new Promise((resolve, reject) => { try { rp({ method: 'POST', headers: { "content-type": "application/json", }, body: JSON.stringify(data), url: BAI_DU_API[1] + '?access_token=' + BAI_DU_ACCESS_TOKEN, // text必要参数,对联的主题内容 }, function (error, response, body) { if (error) { return reject(error); } return resolve(JSON.parse(body)); }) } catch (e) { return reject(e) } }); } case 'getRandomCouplet': { // 随机一对对联(无横批)(API接口来源,天行数据:https://www.tianapi.com/console/) return new Promise((resolve, reject) => { rp({ url: TIAN_XING_API + '?key=' + TIAN_XING_KEY, method: "POST", json: true, }, function (error, response, body) { console.log("响应" + body) resolve(body) if (!error && response.statusCode == 200) { try { } catch (e) { reject() } } }) }) } default: { return { message: 'action错误!' } } } }(There are some redundant codes in the middle, but they are not redundant. It’s just not sorted out, and this small program is still being developed...) Full code address: https://gitee.com/nanfangzhe/wechat_demo
【 Related learning recommendations:小program development tutorial
The above is the detailed content of Let’s take a look at how to create the “Intelligent Spring Festival Couplet” applet for the Spring Festival in this article!. For more information, please follow other related articles on the PHP Chinese website!