import moment from 'moment'; import commonModel from '../../model/commonModel.js'; import pinyin from '../../model/pinyin.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'; export async function PinyinLogin(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.pinyin_appid}&secret=${config.wx.pinyin_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.pinyin_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 pinyin.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 pinyin.UpdateUsers(param); userList = await pinyin.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 pinyin.AddUsers(param); userList = await pinyin.GetUsersInfo(param); } delete userList[0].OpenID; delete userList[0].UnionID; //产品支付是否显示 if (param.ProgramVersion) { let param2 = { ProgramID: 98, 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 AddPinyinRecord(ctx) { const param = ctx.request.body; const inseredID = await pinyin.AddPinyinRecord(param); ctx.body = {errcode: 10000, result: inseredID}; } //更新拼音记录 export async function UpdatePinyinRecord(ctx) { const param = ctx.request.body; await pinyin.UpdatePinyinRecord(param); ctx.body = {errcode: 10000}; } //得到用户记录 export async function GetPinyinRecordData(ctx) { const param = { UserID: ctx.query.UserID || 0, Version: ctx.query.Version || "1.0.0", }; const result = await pinyin.GetPinyinRecordData(param); if (param.UserID > 0 && result && result.length > 0) { const userList = await pinyin.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; const a = moment(userList[0].CreateTime); const b = moment(); result[0].DayNumber = b.diff(a, 'days') + 1; //得到分享新增用户数 let productServiceTime = moment(userList[0].ProductServiceTime).format('YYYYMMDD'); if (productServiceTime === "20991231") { result[0].NewUserNumber = 999999; } else { const newUserNumber = await pinyin.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 pinyin.UpdateUsers(obj); result[0].NewUserNumber = 999999; } } } result[0].IsMember = userList[0].IsMember; if (productServiceTime < timeToday) result[0].IsMember = 0; const result2 = await pinyin.GetPinyinFinishedDataCount(param); if (result2) result[0].FinishedCount = result2[0].count; const param2 = { ProgramID: 98, 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; } } ctx.body = {"errcode": 10000, result: result[0]}; } else ctx.body = {"errcode": 10000}; } //新增或删除拼音完成结果 export async function UpdatePinyinFinished(ctx) { const param = ctx.request.body; if (param.IsFinished) { delete param.IsFinished; if (param.UserID) { await pinyin.AddPinyinFinished(param); } } else { delete param.IsFinished; if (param.UserID) { await pinyin.DeletePinyinFinished(param); } } ctx.body = {errcode: 10000}; } //得到用户完成记录 export async function GetPinyinFinishedData(ctx) { const param = { UserID: ctx.query.UserID || 0, Category: ctx.query.Category || 0, }; const result = await pinyin.GetPinyinFinishedData(param); if (result && result.length > 0) ctx.body = {"errcode": 10000, result: result}; else ctx.body = {"errcode": 10000}; } //帮助用户使用兑换卡 export async function SetPinyinUserExchange(ctx) { const param = { ExchangeUserID: ctx.query.UserID || 0, ID: ctx.query.ProductPayInfoID || 0, ExchangeTime: new Date(), }; const ProductPayInfo = await pinyin.GetPinyinUserExchange(param); if (ProductPayInfo && ProductPayInfo.length) { if (ProductPayInfo[0].ExchangeUserID === 0) { await pinyin.SetPinyinUserExchange(param); const param2 = {}; param2.UserID = param.ExchangeUserID; const userList = await pinyin.GetUsersInfoByUserID(param2); if (userList && userList.length) { const time1 = moment().format('YYYYMMDD'); const time2 = moment(userList[0].ProductServiceTime).format('YYYYMMDD'); if (time1 < time2) { param2.ProductServiceTime = moment(userList[0].ProductServiceTime).add(6, 'months').format('YYYY-MM-DD HH:mm:ss'); } else { param2.ProductServiceTime = moment().add(6, 'months').format('YYYY-MM-DD HH:mm:ss'); } param2.IsMember = 1; await pinyin.UpdateUsersByUserID(param2); } ctx.body = {"errcode": 10000, result: true}; } else ctx.body = {"errcode": 10000, result: false}; } else ctx.body = {"errcode": 10000, result: false}; } //得到拼音单元数据 export async function GetPinyinUnitWords(ctx) { const param = { BookID: ctx.query.BookID || 44, UnitID: ctx.query.UnitID || 431, Word: ctx.query.Word || "b", TestType: ctx.query.TestType || "read", }; let result = globalCache.get("GetPinyinUnitWords?TestType=" + param.TestType + "&BookID=" + param.BookID + "&UnitID=" + param.UnitID + "&Word=" + param.Word); if (result === 0) { result = []; if (param.BookID === 44) { let arrExclude = []; const arrPinyinBasic = constantClass.getPinyinBasic(); for (let k = 0; k < arrPinyinBasic.length; k++) { for (let i = 0; i < arrPinyinBasic[k].List.length; i++) { if (arrPinyinBasic[k].List[i].Name === param.Word) { arrExclude = arrPinyinBasic[k].List[i].Exclude; break; } } if (arrExclude.length > 0) break; } const pinyinArr = constantClass.getPinyinArray(); const arr = []; for (let i = 0; i < pinyinArr.length; i++) { let word = param.Word; const item = pinyinArr[i]; if (word === "un") { if (item[0].substr(0, 1) === "j" || item[0].substr(0, 1) === "q" || item[0].substr(0, 1) === "x" || item[0].substr(0, 1) === "y" ) { continue; } } if (word.indexOf("ü") >= 0) { if (item[0].substr(0, 1) === "j" || item[0].substr(0, 1) === "q" || item[0].substr(0, 1) === "x" || item[0].substr(0, 1) === "y" ) { word = word.replace("ü", "u"); } else { word = word.replace("ü", "v"); } } if (item[0].length > word.length && ((item[0].indexOf(word) === 0 && item[0].indexOf("5") < 0) || item[0].lastIndexOf(word + "1") === (item[0].length - word.length - 1) || item[0].lastIndexOf(word + "2") === (item[0].length - word.length - 1) || item[0].lastIndexOf(word + "3") === (item[0].length - word.length - 1) || item[0].lastIndexOf(word + "4") === (item[0].length - word.length - 1) ) ) { let b = true; for (let j = 0; j < arrExclude.length; j++) { if (item[0].indexOf(arrExclude[j]) >= 0) { b = false; break; } } if (b) { if (item[0].indexOf(word) >= 0 && !(item[2].length === 1 && item[2] === "")) { arr.push(item) } } } } //console.log(arr); for (let i = 0; i < arr.length; i++) { const obj = {}; obj.ID = i + 1; let qustion, answer, tags; const ReadString = "https://pinyin-1253256735.file.myqcloud.com/sounds/" + arr[i][0] + ".m4a"; qustion = arr[i][1]; //console.log(JSON.stringify(json.CHN)); answer = "[读 src='" + ReadString + "']" + qustion + "[/读]\n\n"; answer += arr[i][2].join(","); tags = "拼音怎么念"; obj.Word = qustion; 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 { let arr = ""; switch (Number(param.UnitID)) { case 431: arr = "b,p,m,f,d,t,n,l"; break; case 432: arr = "g,k,h,j,q,x"; break; case 433: arr = "zh,ch,sh,r,z,c,s,y,w"; break; case 434: arr = "a,o,e,i,u,ü"; break; case 435: arr = "ai,ei,ui,ao,ou,iu,ie,üe,er"; break; case 436: arr = "an,en,in,un,ün,ang,eng,ing,ong"; break; case 437: arr = "zhi,chi,shi,ri,zi,ci,si,yi,wu"; break; case 438: arr = "yu,ye,yue,yin,yun,yuan,ying"; break; } arr = arr.split(","); for (let i = 0; i < arr.length; i++) { const obj = {}; obj.ID = i + 1; let qustion, answer, tags; let sound = arr[i]; sound = sound.replace("ü", "v"); const ReadString = "https://pinyin-1253256735.file.myqcloud.com/basic/" + sound + ".m4a"; if (param.TestType === "read") { qustion = arr[i]; //console.log(JSON.stringify(json.CHN)); answer = "[读 src='" + ReadString + "']" + qustion + "[/读]\n\n"; tags = "拼音怎么念"; } else { //console.log(result[i].Word); qustion = "[读 src='" + ReadString + "']拼音[/读]\n"; answer = arr[i] + "\n\n"; for (let j = 0; j < arr[i].length; j++) { let str = arr[i][j]; if (str === "ü") str = "v1" answer += "[图 w='650' h='650']https://miaguo-1253256735.file.myqcloud.com/letter/s" + str + ".gif[/图]\n"; } tags = "拼音怎么写"; } obj.Word = arr[i]; 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); } } globalCache.set("GetPinyinUnitWords?TestType=" + param.TestType + "&BookID=" + param.BookID + "&UnitID=" + param.UnitID + "&Word=" + param.Word, result, config.BufferMemoryTimeHigh); } ctx.body = {"errcode": 10000, result: result}; }