| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173 |
- import moment from 'moment';
- import commonModel from '../../model/commonModel.js';
- import hanzi from '../../model/hanzi.js';
- import config from '../../config/index.js';
- import _ from 'lodash';
- import axios from 'axios';
- import { Encrypt, Decrypt } from '../../util/crypto/index.js';
- import { stringUtils } from '../../util/stringClass.js';
- import WXBizDataCrypt from '../../util/WXBizDataCrypt.js';
- import { globalCache } from '../../util/GlobalCache.js';
- import constantClass from '../../util/constant/index.js';
- import gm from 'gm';
- import fs from 'fs';
- import { promises as fsPromises } from 'fs';
- import COS from 'cos-nodejs-sdk-v5';
- const imageMagick = gm.subClass({ imageMagick: true });
- export async function HanziLogin(ctx) {
- let param = ctx.request.body;
- if (param.param) {
- const paramStr = Decrypt(param.param, config.urlSecrets.aes_key, config.urlSecrets.aes_iv);
- //console.log("paramStr:"+paramStr);
- param = JSON.parse(paramStr);
- }
- const code = param.Code;
- //console.log("code:"+code);
- const url = `https://api.weixin.qq.com/sns/jscode2session?appid=${config.wx.hanzi_appid}&secret=${config.wx.hanzi_appsecret}&js_code=${code}&grant_type=authorization_code`;
- let result = await axios.get(url)
- .then(res => {
- const json = res.data;
- //console.log("json:"+json);
- if (json && json.openid) {
- param.OpenID = json.openid;
- param.sessionKey = json.session_key;
- if (json.unionid)
- param.UnionID = json.unionid;
- return {errcode: 10000};
- }
- else {
- return json;
- }
- })
- .catch(err => {
- return {errcode: 101, errStr: err};
- });
- if (result.errcode == 10000) {
- delete param.Code;
- if (param.sessionKey && param.iv && param.encryptedData){
- //console.log("param.sessionKey:"+param.sessionKey);
- const pc = new WXBizDataCrypt(config.wx.hanzi_appid, param.sessionKey);
- const dataUnionID = pc.decryptData(param.encryptedData , param.iv);
- //console.log(dataUnionID);
- param.UnionID = dataUnionID.unionId;
- }
- delete param.sessionKey;
- delete param.iv;
- delete param.encryptedData;
- //todo
- //param.OpenID="o4UHq4gaNlHfdTWxgl3fTgC1mFsI";
- let userList = await hanzi.GetUsersInfo(param);
- if (userList.length > 0) {
- param.LastLoginTime = new Date();
- const time1 = moment(userList[0].ProductServiceTime).format('YYYY-MM-DD HH:mm:ss');
- const time3 = moment().format('YYYY-MM-DD HH:mm:ss');
- if (time1 < time3)
- param.IsMember = 0;
- delete param.Introducer;
- delete param.UserSource;
- delete param.SourceID;
- //console.log(param.NickName);
- if (param.NickName == "陌生用户") {
- delete param.NickName;
- delete param.AvatarUrl;
- delete param.Language;
- delete param.Gender;
- delete param.City;
- delete param.Province;
- delete param.Country;
- }
- await hanzi.UpdateUsers(param);
- userList = await hanzi.GetUsersInfo(param);
- }
- else {
- param.NickName = "陌生用户";
- param.AvatarUrl = "../images/userface_default.png";
- param.CreateTime = new Date();
- param.LastLoginTime = param.CreateTime;
- param.ProductServiceTime = param.CreateTime;
- const inseredID = await hanzi.AddUsers(param);
- userList = await hanzi.GetUsersInfo(param);
- }
- delete userList[0].OpenID;
- delete userList[0].UnionID;
- //产品支付是否显示
- if (param.ProgramVersion) {
- let param2 = {
- ProgramID: 106,
- Version: param.ProgramVersion,
- };
- let result3 = await commonModel.GetProductVersionList(param2);
- if (result3) {
- if ((param2.Version == result3[0].Version && result3[0].IsShowPay <= 0)
- || param2.Version > result3[0].Version) {
- userList[0].IsShow = result3[0].IsShowPay;
- }
- else {
- userList[0].IsShow = 1;
- }
- //针对iphone测试用户,永远是无支付状态
- if (userList[0].Brand == 'iPhone' && userList[0].WXLanguage == 'en-US'
- && userList[0].UserSource == '1001' && userList[0].IsPay == 0) {
- userList[0].IsShow = 0;
- }
- //针对微信测试用户,永远是无支付状态
- if ((userList[0].UserSource=='1001' && userList[0].System=="iOS 10.0.1")
- || (!userList[0].UserSource && (!userList[0].LastUserSource || userList[0].LastUserSource>10000))
- || userList[0].NickName.indexOf("dgztest")>=0){
- userList[0].IsShow=-1;
- }
- if (userList[0].IsMember===1)
- userList[0].IsShow=1;
- }
- }
- result = {errcode: 10000, result: userList[0]};
- }
- ctx.body = result;
- }
- //新增汉字记录
- export async function AddHanziRecord(ctx) {
- const param = ctx.request.body;
- const inseredID = await hanzi.AddHanziRecord(param);
- ctx.body = {errcode: 10000, result: inseredID};
- }
- //更新汉字记录
- export async function UpdateHanziRecord(ctx) {
- const param = ctx.request.body;
- await hanzi.UpdateHanziRecord(param);
- ctx.body = {errcode: 10000};
- }
- //新增或删除汉字完成结果
- export async function UpdateHanziFinished(ctx) {
- const param = ctx.request.body;
- if (param.Style == "undefined")
- param.Style = "";
- if (param.IsFinished) {
- delete param.IsFinished;
- if (param.UserID) {
- await hanzi.AddHanziFinished(param);
- }
- }
- else {
- delete param.IsFinished;
- if (param.UserID) {
- await hanzi.DeleteHanziFinished(param);
- }
- }
- ctx.body = {errcode: 10000};
- }
- //得到用户完成记录
- export async function GetHanziFinishedData(ctx) {
- const param = {
- UserID: ctx.query.UserID || 0,
- };
- if (param.UserID=="undefined")
- param.UserID=0;
- const result = await hanzi.GetHanziFinishedData(param);
- if (result && result.length > 0)
- ctx.body = {"errcode": 10000, result: result};
- else
- ctx.body = {"errcode": 10000};
- }
- //得到汉字数据(1.6.9以后识字小程序持续使用)
- export async function GetHanziAll0124(ctx) {
- const param = {
- UpdateTime: ctx.query.UpdateTime,
- HasEnglish: ctx.query.HasEnglish || false,
- HasHanziWrite: ctx.query.HasHanziWrite || false,
- };
- const updateTimeList = await commonModel.GetDataUpdateStatus();
- const updateTime = moment(updateTimeList[5].UpdateTime).format('YYYY.MM.DD HH:mm:ss');
-
- if (param.UpdateTime != updateTime) {
- let result = globalCache.get("GetHanziAll");
- if (result == 0) {
- result = {
- UpdateTime: updateTime,
- }
- let readList = [];
- const arrBooks = await hanzi.GetHanziBooks();
- const arrUnits = await hanzi.GetHanziUnits();
- const arrWords = await hanzi.GetHanziWords();
- const arr = [];
-
- for (let i = 0; i < arrBooks.length; i++) {
- arrBooks[i].Units = [];
- if (arrBooks[i].ID >= 61 && arrBooks[i].ID <= 72) {
- if (arrBooks[i].ID == 71)
- arrBooks[i].ID = 23;
- if (arrBooks[i].ID == 72)
- arrBooks[i].ID = 24;
- for (let j = 0; j < arrUnits.length; j++) {
- if (arrBooks[i].ID == arrUnits[j].HanziBookID) {
- arrUnits[j].Words = [];
- for (let n = 0; n < arrUnits[j].Example.length; n++) {
- const word = arrUnits[j].Example[n];
- for (let k = 0; k < arrWords.length; k++) {
- if (word == arrWords[k].Name) {
- delete arrWords[k].HanziUnitID;
- arrUnits[j].Words.push(arrWords[k]);
- break;
- }
- }
- }
- delete arrUnits[j].HanziBookID;
- arrUnits[j].Title = arrUnits[j].Example.substr(0,4);
- arrBooks[i].Units.push(arrUnits[j]);
- }
- }
- if (arrBooks[i].ID == 23)
- arrBooks[i].ID = 71;
- if (arrBooks[i].ID == 24)
- arrBooks[i].ID = 72;
- arr.push(arrBooks[i]);
- }
- }
- readList = arr;
- readList = JSON.stringify(readList);
- readList = Encrypt(readList, config.urlSecrets.aes_key, config.urlSecrets.aes_iv);
- result.List = readList;
- globalCache.set("GetHanziAll", result, config.BufferMemoryTimeHigh);
- console.log("缓存");
- }
- ctx.body = {"errcode": 10000, result: result};
- } else {
- const result = {
- String: globalCache.get("BaiduToken"),
- }
- ctx.body = {"errcode": 10000, result: result};
- }
- }
- //得到汉字数据(2.1.60)后使用
- export async function GetHanziAll3(ctx) {
- const param = {
- UpdateTime: ctx.query.UpdateTime,
- };
- const updateTimeList = await commonModel.GetDataUpdateStatus();
- const updateTime = moment(updateTimeList[5].UpdateTime).format('YYYY.MM.DD HH:mm:ss');
-
- if (param.UpdateTime != updateTime) {
- let result = globalCache.get("GetHanziAll3");
- if (result == 0) {
- result = {
- UpdateTime: updateTime,
- }
- let readList = [];
- const arrBooks = await hanzi.GetHanziBooks();
- const arrUnits = await hanzi.GetHanziUnits();
- const arrWords = await hanzi.GetHanziWords();
- const arrAncientList = await hanzi.GetAncientChineseProse({IsList:0});
- const arrAncientChineseProseSentenct = await hanzi.GetAncientChineseProseSentence({});
- const arr = [];
-
- for (let i = 0; i < arrBooks.length; i++) {
- arrBooks[i].Units = [];
- for (let j = 0; j < arrUnits.length; j++) {
- if (arrBooks[i].ID == arrUnits[j].HanziBookID) {
- arrUnits[j].Words = [];
- //早期识字
- if (arrUnits[j].HanziBookID <= 12) {
- }
- //写字
- else if (arrUnits[j].HanziBookID >= 13 && arrUnits[j].HanziBookID <= 24) {
- const words = arrUnits[j].Example;
- for(let m = 0; m < words.length; m++) {
- for (let k = 0; k < arrWords.length; k++) {
- if (words[m] == arrWords[k].Name) {
- arrUnits[j].Words.push(arrWords[k].Name);
- arrUnits[j].Words = _.union(arrUnits[j].Words);
- }
- }
- }
- }
- //词语
- else if (arrUnits[j].HanziBookID >= 43 && arrUnits[j].HanziBookID <= 60) {
- const words = arrUnits[j].Example.split(",");
- for(let m = 0; m < words.length; m++) {
- arrUnits[j].Words.push(words[m]);
- }
- }
- //新识字
- else if (arrUnits[j].HanziBookID >= 61 && arrUnits[j].HanziBookID <= 72) {
- const words = arrUnits[j].Example;
- for(let m = 0; m < words.length; m++) {
- for (let k = 0; k < arrWords.length; k++) {
- if (words[m] == arrWords[k].Name) {
- arrUnits[j].Words.push(arrWords[k].Name);
- arrUnits[j].Words = _.union(arrUnits[j].Words);
- }
- }
- }
- }
- //文言文
- else if (arrUnits[j].HanziBookID == 73) {
- for(let n = 0; n < arrAncientList.length; n++) {
- if (arrUnits[j].Name == arrAncientList[n].Word) {
- arrUnits[j].Words.push(arrAncientList[n].Definitions);
- }
- }
- }
- //小古文
- else if (arrUnits[j].HanziBookID == 86) {
- for(let n = 0; n < arrAncientChineseProseSentenct.length; n++) {
- if (arrUnits[j].Name == arrAncientChineseProseSentenct[n].Unit) {
- for(let m = 0; m < arrAncientChineseProseSentenct[n].Count; m++)
- arrUnits[j].Words.push("");
- }
- }
- }
- delete arrUnits[j].HanziBookID;
- delete arrUnits[j].Example;
- delete arrUnits[j].OrderID;
- delete arrUnits[j].IsLocked;
- delete arrUnits[j].UnitType;
- arrBooks[i].Units.push(arrUnits[j]);
- }
- }
- arr.push(arrBooks[i]);
- }
- readList = arr;
- readList = JSON.stringify(readList);
- readList = Encrypt(readList, config.urlSecrets.aes_key, config.urlSecrets.aes_iv);
- result.List = readList;
- globalCache.set("GetHanziAll3", result, config.BufferMemoryTimeHigh);
- console.log("缓存");
- }
- ctx.body = {"errcode": 10000, result: result};
- } else {
- const result = {
- String: globalCache.get("BaiduToken"),
- }
- ctx.body = {"errcode": 10000, result: result};
- }
- }
- //得到一册书的字词数据
- export async function GetHanziByBookID(ctx) {
- const param = ctx.request.body;
- let result = globalCache.get("GetHanziByBookID?BookID="+param.BookID);
- if (result == 0) {
- const arrUnits = await hanzi.GetHanziUnits(param);
- const arrWords = await hanzi.GetHanziWords();
- for (let j = 0; j < arrUnits.length; j++) {
- arrUnits[j].Words = [];
- //写字
- if (arrUnits[j].HanziBookID >= 13 && arrUnits[j].HanziBookID <= 24) {
- const words = arrUnits[j].Example;
- for(let m = 0; m < words.length; m++) {
- for (let k = 0; k < arrWords.length; k++) {
- if (words[m] == arrWords[k].Name) {
- arrUnits[j].Words.push(arrWords[k].Name);
- arrUnits[j].Words = _.union(arrUnits[j].Words);
- }
- }
- }
- }
- //词语
- else if (arrUnits[j].HanziBookID >= 43 && arrUnits[j].HanziBookID <= 60) {
- const words = arrUnits[j].Example.split(",");
- for(let m = 0; m < words.length; m++) {
- arrUnits[j].Words.push(words[m]);
- }
- }
- //新识字
- else if (arrUnits[j].HanziBookID >= 61 && arrUnits[j].HanziBookID <= 72) {
- const words = arrUnits[j].Example;
- for(let m = 0; m < words.length; m++) {
- for (let k = 0; k < arrWords.length; k++) {
- if (words[m] == arrWords[k].Name) {
- arrUnits[j].Words.push(arrWords[k].Name);
- arrUnits[j].Words = _.union(arrUnits[j].Words);
- }
- }
- }
- }
- delete arrUnits[j].HanziBookID;
- delete arrUnits[j].OrderID;
- delete arrUnits[j].Example;
- delete arrUnits[j].UnitType;
- delete arrUnits[j].IsLocked;
- }
- result = arrUnits;
- globalCache.set("GetHanziByBookID?BookID="+param.BookID, result, config.BufferMemoryTimeHigh);
- console.log("缓存");
- }
- ctx.body = {"errcode": 10000, result: result};
- }
- //得到英语数据(2.1.60)后使用
- export async function GetSoundErrorArr(ctx) {
- const param = {
- UpdateTime: ctx.query.UpdateTime,
- };
- const updateTimeList = await commonModel.GetDataUpdateStatus();
- const updateTime = moment(updateTimeList[7].UpdateTime).format('YYYY.MM.DD HH:mm:ss');
- if (param.UpdateTime != updateTime) {
- const result = {
- List: await constantClass.GetSoundErrorArr(),
- UpdateTime: updateTime
- };
- ctx.body = {"errcode": 10000, result: result};
- } else {
- ctx.body = {"errcode": 10000};
- }
- }
- //得到汉字详细数据
- export async function GetHanziDetail(ctx) {
- const param = {
- ID: ctx.query.ID || 0,
- Type: "all",
- };
- if (param.ID && param.ID > 0) {
- let result = globalCache.get("GetHanziDetail?ID=" + param.ID);
- if (result == 0) {
- const arrWords = await hanzi.GetHanziWords(param);
- result = JSON.parse(arrWords[0].Details);
- for (let i = 0; i < result.PinYin.length; i++) {
- let item = result.PinYin[i].explain;
- item = stringUtils.ReplaceAllString(item, "<p>", "");
- item = stringUtils.ReplaceAllString(item, "<span>", "");
- item = stringUtils.ReplaceAllString(item, "</span>", "");
- item = stringUtils.ReplaceAllString(item, "\r", "");
- item = stringUtils.ReplaceAllString(item, "\n", "");
- const arr = item.split("</p>");
- arr.pop();
- result.PinYin[i].explain = arr;
- }
- globalCache.set("GetHanziDetail?ID=" + param.ID, result, config.BufferMemoryTimeHigh);
- }
- ctx.body = {"errcode": 10000, result: result};
- } else {
- ctx.body = {"errcode": 10000};
- }
- }
- //得到单元汉字数据
- export async function GetHanziUnitWords(ctx) {
- const param = {
- UnitID: ctx.query.UnitID || 0,
- TestType: ctx.query.TestType || "read",
- };
- let result = globalCache.get("GetHanziUnitWords?UnitID=" + param.UnitID + "&TestType=" + param.TestType);
- if (result == 0) {
- const unitInfo = await hanzi.GetHanziUnitInfo(param);
- //词语部分
- if (unitInfo && unitInfo.length > 0 && unitInfo[0].UnitType == 2) {
- const words = await hanzi.GetHanziUnitByID(param);
- if (words[0] && words[0].Example) {
- let words1 = words[0].Example.split(",").join("','");
- param.Words = words1;
- result = await hanzi.GetMiaoguoLiteracyWords(param);
- let temp = "";
- let arrTemp = [];
- for (let i = 0; i < result.length; i++) {
- if (temp != result[i].Word && result[i].JSONString) {
- const json = JSON.parse(result[i].JSONString);
- const obj = {};
- obj.ID = i + 1;
- let qustion, answer, tags;
-
- if (param.TestType == "read") {
- qustion = result[i].Word;
- if (process.env.NODE_ENV == 'development')
- console.log(qustion);
- answer = "[读 src='" + result[i].Word + "']" + json.CHN.PinYin[0].pinyin + "[/读]\n\n";
- answer += json.CHN.PinYin[0].explain;
- tags = "怎么念";
- obj.FontSize = 108;
- } else {
- qustion = "[读 src='" + result[i].Word + "']" + json.CHN.PinYin[0].pinyin + "[/读]\n";
- answer = result[i].Word + "\n\n";
- if (json.CHN.KaitiArr) {
- for (let j = 0; j < json.CHN.KaitiArr.length; j++) {
- answer += "[图 w='650' h='650']" + json.CHN.KaitiArr[j] + "[/图]\n";
- }
- }
- answer += json.CHN.PinYin[0].explain;
- tags = "怎么写";
- }
- obj.Word = result[i].Word;
- obj.ReadString = result[i].Word;
- obj.Content = [];
- obj.Content.push({ContentType: 0, Content: tags});
- obj.Content.push({ContentType: 1, Content: qustion});
- obj.Content.push({ContentType: 2, Content: answer});
- obj.Content.push({ContentType: 3, Content: ""});
- arrTemp.push(obj);
- }
- temp = result[i].Word;
- }
- result = arrTemp;
- }
- }
- //文言文部分
- else if (unitInfo && unitInfo.length > 0 && unitInfo[0].UnitType == 4) {
- const words = await hanzi.GetHanziUnitByID(param);
- const arrAncientList = await hanzi.GetAncientChineseProse({IsList: 0});
- if (words[0] && words[0].Name) {
- result = [];
- for (let i = 0; i < arrAncientList.length; i++) {
- if (arrAncientList[i].Word == words[0].Name) {
- let qustion, answer, tags;
- qustion = arrAncientList[i].ExampleSentence;
- qustion = stringUtils.ReplaceAllString(qustion, arrAncientList[i].Word, "[光]" + arrAncientList[i].Word + "[/光]");
- const readString = "“" + arrAncientList[i].Word + "”字在这里的意思是" + arrAncientList[i].Definitions;
- answer = "[读 src='" + readString + "']" + arrAncientList[i].Definitions + "[/读]\n\n";
- if (arrAncientList[i].ExampleDefinitions.indexOf("译文") < 0)
- answer += "译文:";
- answer += arrAncientList[i].ExampleDefinitions;
- tags = "文言文字释义";
-
- const obj = {};
- obj.Word = "【" + arrAncientList[i].Word + "】" + arrAncientList[i].Definitions;
- obj.ReadString = readString;
- obj.Content = [];
- obj.Content.push({ContentType: 0, Content: tags});
- obj.Content.push({ContentType: 1, Content: qustion});
- obj.Content.push({ContentType: 2, Content: answer});
- obj.Content.push({ContentType: 3, Content: ""});
- result.push(obj);
- }
- }
- }
- }
- //小古文部分
- else if (unitInfo && unitInfo.length > 0 && unitInfo[0].UnitType == 3) {
- const words = await hanzi.GetHanziUnitByID(param);
- const resultList = await hanzi.GetAncientChineseProseSentence({Unit: words[0].Name});
- if (words[0] && words[0].Name) {
- result = [];
- for (let i = 0; i < resultList.length; i++) {
- let qustion, answer, tags;
- if (param.TestType == "read") {
- qustion = resultList[i].Question;
- answer = "[光]" + resultList[i].Sentence + "[/光]\n\n";
- answer += "含义\n• ";
- answer += resultList[i].Paraphrase + "\n\n";
- if (resultList[i].Explain) {
- answer += "详解\n• ";
- answer += resultList[i].Explain + "\n\n";
- }
- if (resultList[i].Idiom) {
- answer += "句中包含的成语\n• ";
- answer += resultList[i].Idiom + "\n\n";
- }
- if (resultList[i].SentenceFragment) {
- answer += "原文片段\n• ";
- answer += resultList[i].SentenceFragment + "\n\n";
- }
- if (resultList[i].Source) {
- answer += "出自\n• ";
- answer += "《" + resultList[i].Source + "》 ";
- if (resultList[i].Author)
- answer += resultList[i].Author + " ";
- if (resultList[i].Dynasty)
- answer += "(" + resultList[i].Dynasty + ")";
- answer += "\n\n";
- }
- tags = "接下文";
- } else {
- qustion = resultList[i].Paraphrase;
- answer = "[光]" + resultList[i].Sentence + "[/光]\n\n";
- if (resultList[i].Explain) {
- answer += "详解\n• ";
- answer += resultList[i].Explain + "\n\n";
- }
- if (resultList[i].Idiom) {
- answer += "句中包含的成语\n• ";
- answer += resultList[i].Idiom + "\n\n";
- }
- if (resultList[i].SentenceFragment) {
- answer += "原文片段\n• ";
- answer += resultList[i].SentenceFragment + "\n\n";
- }
- if (resultList[i].Source) {
- answer += "出自\n• ";
- answer += "《" + resultList[i].Source + "》 ";
- if (resultList[i].Author)
- answer += resultList[i].Author + " ";
- if (resultList[i].Dynasty)
- answer += "(" + resultList[i].Dynasty + ")";
- answer += "\n\n";
- }
- tags = "说古文";
- }
- const obj = {};
- obj.Word = resultList[i].Sentence;
- obj.Content = [];
- obj.Content.push({ContentType: 0, Content: tags});
- obj.Content.push({ContentType: 1, Content: qustion});
- obj.Content.push({ContentType: 2, Content: answer});
- obj.Content.push({ContentType: 3, Content: ""});
- result.push(obj);
- }
- }
- }
- globalCache.set("GetHanziUnitWords?UnitID=" + param.UnitID + "&TestType=" + param.TestType, result, config.BufferMemoryTimeHigh);
- }
- ctx.body = {"errcode": 10000, result: result};
- }
- //添加自定义汉字
- export async function AddHanziCustom(ctx) {
- const param = ctx.request.body;
- let userCustom = await hanzi.GetUsersUnitCustomList(param);
- if (userCustom && userCustom.length>0){
- await hanzi.UpdateHanziCustom(param);
- }
- else {
- await hanzi.AddHanziCustom(param);
- }
- ctx.body = {"errcode": 10000};
- }
- //得到自定义汉字列表
- export async function GetHanziCustomList(ctx) {
- const param = {
- UserID: ctx.query.UserID || 0,
- };
- if (param.UserID=="undefined")
- param.UserID=0;
- let result = await hanzi.GetHanziCustomList(param);
- if (result && result.length > 0) {
- for (let n = 0; n < result.length; n++) {
- result[n].List = result[n].UnitCustom;
- result[n].ID = result[n].UnitID;
- delete result[n].CreateTime;
- delete result[n].UnitCustom;
- delete result[n].UserID;
- delete result[n].UnitID;
- }
- ctx.body = {"errcode": 10000, result: result};
- }
- else
- ctx.body = {"errcode": 10000, result: []};
- }
- //得到测试汉字列表
- export async function GetHanziTestList(ctx) {
- const param = {
- Type: "random"
- };
- const result = await hanzi.GetHanziWords(param);
- if (result && result.length > 0) {
- let arr = [], arrChild = [];
- let temp = 0;
- for (let i = 0; i < result.length; i++) {
- if (temp > 0 && temp != result[i].HanziUnitID) {
- arr.push(arrChild);
- arrChild=[];
- }
- arrChild.push(result[i]);
- temp = result[i].HanziUnitID;
- }
- arr.push(arrChild);
- const arrResult = [];
- for (let i = 0; i < arr.length; i++) {
- //console.log(arr[i].length);
- const arrTemp = stringUtils.RandomArray(arr[i]);
- for (let j = 0; j < 3; j++) {
- //console.log(arrTemp[j].ID);
- if (arrTemp && arrTemp[j] && arrTemp[j].PinyinTest) {
- arrTemp[j].PinyinTest = JSON.parse(arrTemp[j].PinyinTest);
- const arr1 = [];
- if (arrTemp[j].PinyinTest.easy.length > 0) {
- arr1.push(arrTemp[j].PinyinTest.easy);
- arr1.push(arrTemp[j].PinyinTest.easy);
- }
- if (arrTemp[j].PinyinTest.normal.length > 0) {
- arr1.push(arrTemp[j].PinyinTest.normal);
- arr1.push(arrTemp[j].PinyinTest.normal);
- arr1.push(arrTemp[j].PinyinTest.normal);
- }
- const num = stringUtils.Random(0, arr1.length - 1);
- //if (arr1[num]==undefined) {
- // console.log(num);
- // console.log(arr1);
- //}
- const arr3 = arr1[num];
- const arr2 = [];
- if (arr3[0])
- arr2.push({ options: arr3[0] });
- if (arr3[1])
- arr2.push({ options: arr3[1] });
- if (arr3[2])
- arr2.push({ options: arr3[2] });
- arr2.push({ options: arrTemp[j].PinyinTest.answer, "IsAnswer": true });
- arrTemp[j].PinyinTest = stringUtils.RandomArray(arr2);
- arrResult.push(arrTemp[j]);
- }
- }
- }
- ctx.body = { errcode: 10000, result: arrResult };
- }
- else {
- ctx.body = { errcode: 10000, result: [] };
- }
- }
- //得到非课本汉字数据
- export async function SearchHanzi(ctx) {
- const param = {
- Word: ctx.query.Word,
- };
- const result = await hanzi.GetHanziWordsByWord(param);
- if (result && result.length > 0) {
- if (result[0].HasImage==0)
- buildHanziKaishu(param.Word);
- result[0].WordUrl="https://hanzi-1253256735.file.myqcloud.com/images/hanzi"+param.Word+".jpg";
- result[0].IsNew=false;
- ctx.body = {"errcode": 10000, result: result[0]};
- }
- else {
- const hanzi1 = encodeURI(param.Word);
- const url = "http://localhost:"+config.port+"/api/GetLiteracyData?Word=" + hanzi1;
- const result2 = await axios.get(url)
- .then(function (response) {
- const obj = response.data;
- const data = obj.result;
- const arrTemp = [];
- if (data.CombineWords && data.CombineWords.length>0)
- arrTemp.push(data.CombineWords[0]);
- let pinyin = "";
- for(let i=0; i<data.PinYin.length; i++){
- pinyin += "," + data.PinYin[i].pinyin;
- }
- if (pinyin)
- pinyin = pinyin.substring(1);
- const obj2 = {
- HanziUnitID: 0,
- Name: param.Word,
- Pinyin: pinyin,
- CombineWords: arrTemp.join(","),
- Details: JSON.stringify(data),
- };
- return obj2;
- });
- const insertID = await hanzi.AddHanziWord(result2);
- result2.ID = insertID.insertId;
- result2.IsNew = true;
- buildHanziKaishu(param.Word);
- result2.WordUrl = "https://hanzi-1253256735.file.myqcloud.com/images/hanzi"+param.Word+".jpg";
- ctx.body = {"errcode": 10000, result: result2};
- }
- // 生成汉字楷书图片并上传到腾讯云
- async function buildHanziKaishu(data) {
- try {
- const path = './public/images/';
- const imageUrl = path + 'hanziBg.png';
- const filepath = path + 'hanzi/hanzi.jpeg';
-
- // 确保目录存在
- await fsPromises.mkdir(path + 'hanzi', { recursive: true }).catch(() => {});
-
- // 生成汉字图片
- await new Promise((resolve, reject) => {
- imageMagick(imageUrl)
- .font(config.fontsPathKaiti, 366)
- .fill('#000000')
- .drawText(0, 290, data)
- .write(filepath, (err) => {
- if (err) reject(err);
- else resolve();
- });
- });
-
- // 上传图片到腾讯云
- await uploadHanziImage(data, filepath);
-
- } catch (error) {
- console.error('生成汉字图片失败:', error);
- }
- }
-
- // 上传汉字图片到腾讯云
- async function uploadHanziImage(data, filepath) {
- const timeStart = new Date();
- const cos = new COS(config.QCloud);
-
- try {
- // 检查文件是否存在
- await fsPromises.stat(filepath);
-
- const params = {
- Bucket: 'hanzi-1253256735',
- Region: 'ap-guangzhou',
- Key: 'images/hanzi' + data + '.jpg',
- Body: fs.createReadStream(filepath),
- ContentLength: fs.statSync(filepath).size
- };
-
- // 使用Promise包装COS上传
- await new Promise((resolve, reject) => {
- cos.putObject(params, (err, data) => {
- if (err) reject(err);
- else resolve(data);
- });
- });
-
- // 更新数据库中的汉字状态
- await hanzi.UpdateHanziWords({
- Name: data,
- HasImage: 1
- });
-
- const duration = (new Date().getTime() - timeStart.getTime());
- console.log(`汉字"${data}"图片上传完成,耗时: ${duration}ms`);
-
- } catch (error) {
- console.error(`上传汉字"${data}"图片失败:`, error);
- } finally {
- // 清理临时文件
- try {
- if (await fsPromises.stat(filepath).catch(() => false)) {
- await fsPromises.unlink(filepath);
- }
- } catch (e) {
- console.error('清理临时文件失败:', e);
- }
- }
- }
- }
- //得到用户记录
- export async function GetHanziRecordData(ctx) {
- const param = {
- UserID: ctx.query.UserID || 0,
- Version: ctx.query.Version || "1.0.0",
- };
- if (param.UserID=="undefined")
- param.UserID=0;
- const result = await hanzi.GetHanziRecordData(param);
- if (param.UserID>0 && result && result.length > 0) {
- const userList = await hanzi.GetUsersInfoByUserID(param);
- //是否是首日
- const timeCreateTime = moment(userList[0].CreateTime).format('YYYYMMDD');
- const timeToday = moment().format('YYYYMMDD');
- if (timeCreateTime==timeToday)
- result[0].IsFirstDay=true;
- else
- result[0].IsFirstDay=false;
- //得到分享新增用户数
- let productServiceTime = moment(userList[0].ProductServiceTime).format('YYYYMMDD');
- if (productServiceTime=="20991231"){
- result[0].NewUserNumber=999999;
- }
- else {
- const newUserNumber = await hanzi.GetNewUserByUserID(param);
- if (newUserNumber) {
- result[0].NewUserNumber = newUserNumber.length;
- //console.log("userList[0].ActivityTime:"+userList[0].ActivityTime);
- productServiceTime = moment(userList[0].ProductServiceTime).format('YYYYMMDD');
- const currentTime = moment().format('YYYYMMDD');
- //如果推荐用户数超过6个,就改为增加3个月
- if (newUserNumber.length>=6 && !userList[0].ActivityTime && productServiceTime>=currentTime){
- const obj={
- ActivityTime:moment().format('YYYY-MM-DD HH:mm:ss'),
- ProductServiceTime:moment(userList[0].ProductServiceTime).add(1, 'months').format('YYYY-MM-DD HH:mm:ss'),
- OpenID:userList[0].OpenID,
- }
- await hanzi.UpdateUsers(obj);
- result[0].NewUserNumber=999999;
- }
- }
- }
- result[0].IsMember=userList[0].IsMember;
- if (productServiceTime<timeToday)
- result[0].IsMember=0;
- //完成章节
- const result2 = await hanzi.GetHanziFinishedDataCount(param);
- if (result2)
- result[0].FinishedList=result2;
- //复习字
- const list2 = await hanzi.GetHanziReviewList(param);
- if (list2)
- result[0].ReviewList = list2;
- //产品支付是否显示
- const param2={
- ProgramID:106,
- Version:param.Version,
- };
- const result3 = await commonModel.GetProductVersionList(param2);
- if (result3) {
- if ((param.Version==result3[0].Version && result3[0].IsShowPay<=0)
- || param.Version>result3[0].Version){
- result[0].IsShow=result3[0].IsShowPay;
- }
- else{
- result[0].IsShow=1;
- }
- //针对iphone测试用户,永远是无支付状态
- if (userList[0].Brand=='iPhone' && userList[0].WXLanguage=='en-US'
- && userList[0].UserSource=='1001' && userList[0].IsPay==0){
- result[0].IsShow=0;
- }
- }
- if (!result[0].HanziNumber)
- result[0].HanziNumber=0;
- ctx.body = {"errcode": 10000, result: result[0]};
- }
- else
- ctx.body = {"errcode": 10000};
- }
- //新增识字复习记录
- export async function UpdateHanziReview(ctx) {
- const param = ctx.request.body;
- if (param.Status == "add") {
- delete param.Status;
- if (param.UserID && param.Word) {
- await hanzi.AddHanziReview(param);
- }
- }
- else if (param.Status == "deleteAll") {
- delete param.Status;
- if (param.UserID) {
- await hanzi.DeleteHanziReviewAll(param);
- }
- }
- else if (param.Status == "delete") {
- delete param.Status;
- if (param.UserID && param.Word) {
- await hanzi.DeleteHanziReview(param);
- }
- }
- const result = await hanzi.GetHanziReviewList(param);
- if (result && result.length > 0)
- ctx.body = {"errcode": 10000, result: result};
- else
- ctx.body = {"errcode": 10000, result: []};
- }
- //得到用户完成记录
- export async function GetHanziReviewList(ctx) {
- const param = {
- UserID: ctx.query.UserID || 0,
- };
- if (param.UserID=="undefined")
- param.UserID=0;
- const result = await hanzi.GetHanziReviewList(param);
- if (result && result.length > 0)
- ctx.body = {"errcode": 10000, result: result};
- else
- ctx.body = {"errcode": 10000, result: []};
- }
- //得到用户邀请新用户列表
- export async function GetHanziUserListByIntroducer(ctx) {
- const param = {
- UserID: ctx.query.UserID || 0,
- };
- if (param.UserID=="undefined")
- param.UserID=0;
- const result = await hanzi.GetNewUserByUserID(param);
- if (result && result.length > 0) {
- ctx.body = {"errcode": 10000, result: result};
- }
- else
- ctx.body = {"errcode": 10000, result: []};
- }
- //得到单元汉字列表
- export async function GetHanziWordListByUnitID(ctx) {
- const param = {
- UserID: ctx.query.UserID || 0,
- UnitID: ctx.query.UnitID || 0,
- Type: "all",
- };
- if (param.UserID == "undefined")
- param.UserID = 0;
- const result = await hanzi.GetHanziWords(param);
- const userCustom = await hanzi.GetUsersUnitCustomList(param);
- let list = [];
- if (userCustom && userCustom.length > 0 && userCustom[0].UnitCustom) {
- //console.log(userCustom[0].UnitCustom);
- list = JSON.parse(userCustom[0].UnitCustom);
- }
- if (result && result.length > 0) {
- for (let i = 0; i < result.length; i++) {
- const item = result[i];
- item.Details = JSON.parse(item.Details);
- const CombineWordsOther = item.Details.CombineWords;
- const other = [];
- const main = item.CombineWords.split(",");
- for (let j = 0; j < CombineWordsOther.length; j++) {
- let b = true;
- for (let k = 0; k < main.length; k++) {
- if (CombineWordsOther[j] == main[k]) {
- b = false;
- break;
- }
- }
- if (b)
- other.push(CombineWordsOther[j]);
- }
- item.CombineWords = [];
- if (list.length > 0) {
- //console.log(list);
- for (let n = 0; n < main.length; n++) {
- let obj = {
- css: "",
- name: main[n],
- }
- for (let k = 0; k < list.length; k++) {
- if (list[k].Name == item.Name) {
- list[k].CombineWords = "," + list[k].CombineWords;
- if (list[k].CombineWords.indexOf("," + main[n]) >= 0) {
- //console.log(list[k].CombineWords);
- //console.log(main[n]);
- obj.css = "selected";
- break;
- }
- }
- }
- item.CombineWords.push(obj);
- }
- for (let n = 0; n < other.length; n++) {
- let obj = {
- css: "",
- name: other[n],
- }
- for (let k = 0; k < list.length; k++) {
- if (list[k].Name == item.Name) {
- list[k].CombineWords = "," + list[k].CombineWords;
- if (list[k].CombineWords.indexOf("," + other[n]) >= 0) {
- obj.css = "selected";
- break;
- }
- }
- }
- item.CombineWords.push(obj);
- }
- }
- else {
- for (let n = 0; n < main.length; n++) {
- let obj = {
- css: "selected",
- name: main[n],
- }
- item.CombineWords.push(obj);
- }
- for (let n = 0; n < other.length; n++) {
- let obj = {
- css: "",
- name: other[n],
- }
- item.CombineWords.push(obj);
- }
- }
- //console.log(item.Pinyin);
- //console.log(item.CombineWords);
- delete item.Details;
- delete item.HanziUnitID;
- delete item.HasImage;
- }
- ctx.body = { "errcode": 10000, result: result };
- }
- else
- ctx.body = { "errcode": 10000, result: [] };
- }
|