pinyinController.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. import moment from 'moment';
  2. import commonModel from '../../model/commonModel.js';
  3. import pinyin from '../../model/pinyin.js';
  4. import config from '../../config/index.js';
  5. import _ from 'lodash';
  6. import axios from 'axios';
  7. import { Encrypt, Decrypt } from '../../util/crypto/index.js';
  8. import { stringUtils } from '../../util/stringClass.js';
  9. import WXBizDataCrypt from '../../util/WXBizDataCrypt.js';
  10. import { globalCache } from '../../util/GlobalCache.js';
  11. import constantClass from '../../util/constant/index.js';
  12. export async function PinyinLogin(ctx) {
  13. let param = ctx.request.body;
  14. if (param.param) {
  15. const paramStr = Decrypt(param.param, config.urlSecrets.aes_key, config.urlSecrets.aes_iv);
  16. //console.log("paramStr:"+paramStr);
  17. param = JSON.parse(paramStr);
  18. }
  19. const code = param.Code;
  20. //console.log("code:"+code);
  21. 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`;
  22. let result = await axios.get(url)
  23. .then(res => {
  24. const json = res.data;
  25. //console.log("json:"+json);
  26. if (json && json.openid) {
  27. param.OpenID = json.openid;
  28. param.sessionKey = json.session_key;
  29. if (json.unionid)
  30. param.UnionID = json.unionid;
  31. return {errcode: 10000};
  32. }
  33. else {
  34. return json;
  35. }
  36. })
  37. .catch(err => {
  38. return {errcode: 101, errStr: err};
  39. });
  40. if (result.errcode == 10000) {
  41. delete param.Code;
  42. if (param.sessionKey && param.iv && param.encryptedData){
  43. //console.log("param.sessionKey:"+param.sessionKey);
  44. const pc = new WXBizDataCrypt(config.wx.pinyin_appid, param.sessionKey);
  45. const dataUnionID = pc.decryptData(param.encryptedData , param.iv);
  46. //console.log(dataUnionID);
  47. param.UnionID = dataUnionID.unionId;
  48. }
  49. delete param.sessionKey;
  50. delete param.iv;
  51. delete param.encryptedData;
  52. //todo
  53. //param.OpenID="o4UHq4gaNlHfdTWxgl3fTgC1mFsI";
  54. let userList = await pinyin.GetUsersInfo(param);
  55. if (userList.length > 0) {
  56. param.LastLoginTime = new Date();
  57. const time1 = moment(userList[0].ProductServiceTime).format('YYYY-MM-DD HH:mm:ss');
  58. const time3 = moment().format('YYYY-MM-DD HH:mm:ss');
  59. if (time1 < time3)
  60. param.IsMember = 0;
  61. delete param.Introducer;
  62. delete param.UserSource;
  63. delete param.SourceID;
  64. //console.log(param.NickName);
  65. if (param.NickName == "陌生用户") {
  66. delete param.NickName;
  67. delete param.AvatarUrl;
  68. delete param.Language;
  69. delete param.Gender;
  70. delete param.City;
  71. delete param.Province;
  72. delete param.Country;
  73. }
  74. await pinyin.UpdateUsers(param);
  75. userList = await pinyin.GetUsersInfo(param);
  76. }
  77. else {
  78. param.NickName = "陌生用户";
  79. param.AvatarUrl = "../images/userface_default.png";
  80. param.CreateTime = new Date();
  81. param.LastLoginTime = param.CreateTime;
  82. param.ProductServiceTime = param.CreateTime;
  83. const inseredID = await pinyin.AddUsers(param);
  84. userList = await pinyin.GetUsersInfo(param);
  85. }
  86. delete userList[0].OpenID;
  87. delete userList[0].UnionID;
  88. //产品支付是否显示
  89. if (param.ProgramVersion) {
  90. let param2 = {
  91. ProgramID: 98,
  92. Version: param.ProgramVersion,
  93. };
  94. let result3 = await commonModel.GetProductVersionList(param2);
  95. if (result3) {
  96. if ((param2.Version == result3[0].Version && result3[0].IsShowPay <= 0)
  97. || param2.Version > result3[0].Version) {
  98. userList[0].IsShow = result3[0].IsShowPay;
  99. }
  100. else {
  101. userList[0].IsShow = 1;
  102. }
  103. //针对iphone测试用户,永远是无支付状态
  104. if (userList[0].Brand == 'iPhone' && userList[0].WXLanguage == 'en-US'
  105. && userList[0].UserSource == '1001' && userList[0].IsPay == 0) {
  106. userList[0].IsShow = 0;
  107. }
  108. //针对微信测试用户,永远是无支付状态
  109. if ((userList[0].UserSource=='1001' && userList[0].System=="iOS 10.0.1")
  110. || (!userList[0].UserSource && (!userList[0].LastUserSource || userList[0].LastUserSource>10000))
  111. || userList[0].NickName.indexOf("dgztest")>=0){
  112. userList[0].IsShow=-1;
  113. }
  114. if (userList[0].IsMember===1)
  115. userList[0].IsShow=1;
  116. }
  117. }
  118. result = {errcode: 10000, result: userList[0]};
  119. }
  120. ctx.body = result;
  121. }
  122. //新增拼音记录
  123. export async function AddPinyinRecord(ctx) {
  124. const param = ctx.request.body;
  125. const inseredID = await pinyin.AddPinyinRecord(param);
  126. ctx.body = {errcode: 10000, result: inseredID};
  127. }
  128. //更新拼音记录
  129. export async function UpdatePinyinRecord(ctx) {
  130. const param = ctx.request.body;
  131. await pinyin.UpdatePinyinRecord(param);
  132. ctx.body = {errcode: 10000};
  133. }
  134. //得到用户记录
  135. export async function GetPinyinRecordData(ctx) {
  136. const param = {
  137. UserID: ctx.query.UserID || 0,
  138. Version: ctx.query.Version || "1.0.0",
  139. };
  140. const result = await pinyin.GetPinyinRecordData(param);
  141. if (param.UserID > 0 && result && result.length > 0) {
  142. const userList = await pinyin.GetUsersInfoByUserID(param);
  143. //是否是首日
  144. const timeCreateTime = moment(userList[0].CreateTime).format('YYYYMMDD');
  145. const timeToday = moment().format('YYYYMMDD');
  146. if (timeCreateTime === timeToday)
  147. result[0].IsFirstDay = true;
  148. else
  149. result[0].IsFirstDay = false;
  150. const a = moment(userList[0].CreateTime);
  151. const b = moment();
  152. result[0].DayNumber = b.diff(a, 'days') + 1;
  153. //得到分享新增用户数
  154. let productServiceTime = moment(userList[0].ProductServiceTime).format('YYYYMMDD');
  155. if (productServiceTime === "20991231") {
  156. result[0].NewUserNumber = 999999;
  157. }
  158. else {
  159. const newUserNumber = await pinyin.GetNewUserByUserID(param);
  160. if (newUserNumber) {
  161. result[0].NewUserNumber = newUserNumber.length;
  162. //console.log("userList[0].ActivityTime:"+userList[0].ActivityTime);
  163. productServiceTime = moment(userList[0].ProductServiceTime).format('YYYYMMDD');
  164. const currentTime = moment().format('YYYYMMDD');
  165. //如果推荐用户数超过6个,就改为增加3个月
  166. if (newUserNumber.length >= 6 && !userList[0].ActivityTime && productServiceTime >= currentTime) {
  167. const obj = {
  168. ActivityTime: moment().format('YYYY-MM-DD HH:mm:ss'),
  169. ProductServiceTime: moment(userList[0].ProductServiceTime).add(1, 'months').format('YYYY-MM-DD HH:mm:ss'),
  170. OpenID: userList[0].OpenID,
  171. }
  172. await pinyin.UpdateUsers(obj);
  173. result[0].NewUserNumber = 999999;
  174. }
  175. }
  176. }
  177. result[0].IsMember = userList[0].IsMember;
  178. if (productServiceTime < timeToday)
  179. result[0].IsMember = 0;
  180. const result2 = await pinyin.GetPinyinFinishedDataCount(param);
  181. if (result2)
  182. result[0].FinishedCount = result2[0].count;
  183. const param2 = {
  184. ProgramID: 98,
  185. Version: param.Version,
  186. };
  187. const result3 = await commonModel.GetProductVersionList(param2);
  188. if (result3) {
  189. if ((param.Version === result3[0].Version && result3[0].IsShowPay <= 0)
  190. || param.Version > result3[0].Version) {
  191. result[0].IsShow = result3[0].IsShowPay;
  192. }
  193. else {
  194. result[0].IsShow = 1;
  195. }
  196. //针对iphone测试用户,永远是无支付状态
  197. if (userList[0].Brand === 'iPhone' && userList[0].WXLanguage === 'en-US'
  198. && userList[0].UserSource === '1001' && userList[0].IsPay === 0) {
  199. result[0].IsShow = 0;
  200. }
  201. }
  202. ctx.body = {"errcode": 10000, result: result[0]};
  203. }
  204. else
  205. ctx.body = {"errcode": 10000};
  206. }
  207. //新增或删除拼音完成结果
  208. export async function UpdatePinyinFinished(ctx) {
  209. const param = ctx.request.body;
  210. if (param.IsFinished) {
  211. delete param.IsFinished;
  212. if (param.UserID) {
  213. await pinyin.AddPinyinFinished(param);
  214. }
  215. }
  216. else {
  217. delete param.IsFinished;
  218. if (param.UserID) {
  219. await pinyin.DeletePinyinFinished(param);
  220. }
  221. }
  222. ctx.body = {errcode: 10000};
  223. }
  224. //得到用户完成记录
  225. export async function GetPinyinFinishedData(ctx) {
  226. const param = {
  227. UserID: ctx.query.UserID || 0,
  228. Category: ctx.query.Category || 0,
  229. };
  230. const result = await pinyin.GetPinyinFinishedData(param);
  231. if (result && result.length > 0)
  232. ctx.body = {"errcode": 10000, result: result};
  233. else
  234. ctx.body = {"errcode": 10000};
  235. }
  236. //帮助用户使用兑换卡
  237. export async function SetPinyinUserExchange(ctx) {
  238. const param = {
  239. ExchangeUserID: ctx.query.UserID || 0,
  240. ID: ctx.query.ProductPayInfoID || 0,
  241. ExchangeTime: new Date(),
  242. };
  243. const ProductPayInfo = await pinyin.GetPinyinUserExchange(param);
  244. if (ProductPayInfo && ProductPayInfo.length) {
  245. if (ProductPayInfo[0].ExchangeUserID === 0) {
  246. await pinyin.SetPinyinUserExchange(param);
  247. const param2 = {};
  248. param2.UserID = param.ExchangeUserID;
  249. const userList = await pinyin.GetUsersInfoByUserID(param2);
  250. if (userList && userList.length) {
  251. const time1 = moment().format('YYYYMMDD');
  252. const time2 = moment(userList[0].ProductServiceTime).format('YYYYMMDD');
  253. if (time1 < time2) {
  254. param2.ProductServiceTime = moment(userList[0].ProductServiceTime).add(6, 'months').format('YYYY-MM-DD HH:mm:ss');
  255. }
  256. else {
  257. param2.ProductServiceTime = moment().add(6, 'months').format('YYYY-MM-DD HH:mm:ss');
  258. }
  259. param2.IsMember = 1;
  260. await pinyin.UpdateUsersByUserID(param2);
  261. }
  262. ctx.body = {"errcode": 10000, result: true};
  263. }
  264. else
  265. ctx.body = {"errcode": 10000, result: false};
  266. }
  267. else
  268. ctx.body = {"errcode": 10000, result: false};
  269. }
  270. //得到拼音单元数据
  271. export async function GetPinyinUnitWords(ctx) {
  272. const param = {
  273. BookID: ctx.query.BookID || 44,
  274. UnitID: ctx.query.UnitID || 431,
  275. Word: ctx.query.Word || "b",
  276. TestType: ctx.query.TestType || "read",
  277. };
  278. let result = globalCache.get("GetPinyinUnitWords?TestType=" + param.TestType + "&BookID=" + param.BookID + "&UnitID=" + param.UnitID + "&Word=" + param.Word);
  279. if (result === 0) {
  280. result = [];
  281. if (param.BookID === 44) {
  282. let arrExclude = [];
  283. const arrPinyinBasic = constantClass.getPinyinBasic();
  284. for (let k = 0; k < arrPinyinBasic.length; k++) {
  285. for (let i = 0; i < arrPinyinBasic[k].List.length; i++) {
  286. if (arrPinyinBasic[k].List[i].Name === param.Word) {
  287. arrExclude = arrPinyinBasic[k].List[i].Exclude;
  288. break;
  289. }
  290. }
  291. if (arrExclude.length > 0)
  292. break;
  293. }
  294. const pinyinArr = constantClass.getPinyinArray();
  295. const arr = [];
  296. for (let i = 0; i < pinyinArr.length; i++) {
  297. let word = param.Word;
  298. const item = pinyinArr[i];
  299. if (word === "un") {
  300. if (item[0].substr(0, 1) === "j"
  301. || item[0].substr(0, 1) === "q"
  302. || item[0].substr(0, 1) === "x"
  303. || item[0].substr(0, 1) === "y"
  304. ) {
  305. continue;
  306. }
  307. }
  308. if (word.indexOf("ü") >= 0) {
  309. if (item[0].substr(0, 1) === "j"
  310. || item[0].substr(0, 1) === "q"
  311. || item[0].substr(0, 1) === "x"
  312. || item[0].substr(0, 1) === "y"
  313. ) {
  314. word = word.replace("ü", "u");
  315. }
  316. else {
  317. word = word.replace("ü", "v");
  318. }
  319. }
  320. if (item[0].length > word.length &&
  321. ((item[0].indexOf(word) === 0 && item[0].indexOf("5") < 0)
  322. || item[0].lastIndexOf(word + "1") === (item[0].length - word.length - 1)
  323. || item[0].lastIndexOf(word + "2") === (item[0].length - word.length - 1)
  324. || item[0].lastIndexOf(word + "3") === (item[0].length - word.length - 1)
  325. || item[0].lastIndexOf(word + "4") === (item[0].length - word.length - 1)
  326. )
  327. ) {
  328. let b = true;
  329. for (let j = 0; j < arrExclude.length; j++) {
  330. if (item[0].indexOf(arrExclude[j]) >= 0) {
  331. b = false;
  332. break;
  333. }
  334. }
  335. if (b) {
  336. if (item[0].indexOf(word) >= 0 && !(item[2].length === 1 && item[2] === "")) {
  337. arr.push(item)
  338. }
  339. }
  340. }
  341. }
  342. //console.log(arr);
  343. for (let i = 0; i < arr.length; i++) {
  344. const obj = {};
  345. obj.ID = i + 1;
  346. let qustion, answer, tags;
  347. const ReadString = "https://pinyin-1253256735.file.myqcloud.com/sounds/" + arr[i][0] + ".m4a";
  348. qustion = arr[i][1];
  349. //console.log(JSON.stringify(json.CHN));
  350. answer = "[读 src='" + ReadString + "']" + qustion + "[/读]\n\n";
  351. answer += arr[i][2].join(",");
  352. tags = "拼音怎么念";
  353. obj.Word = qustion;
  354. obj.ReadString = ReadString;
  355. obj.Content = [];
  356. obj.Content.push({ContentType: 0, Content: tags});
  357. obj.Content.push({ContentType: 1, Content: qustion});
  358. obj.Content.push({ContentType: 2, Content: answer});
  359. obj.Content.push({ContentType: 3, Content: ""});
  360. result.push(obj);
  361. }
  362. }
  363. else {
  364. let arr = "";
  365. switch (Number(param.UnitID)) {
  366. case 431:
  367. arr = "b,p,m,f,d,t,n,l";
  368. break;
  369. case 432:
  370. arr = "g,k,h,j,q,x";
  371. break;
  372. case 433:
  373. arr = "zh,ch,sh,r,z,c,s,y,w";
  374. break;
  375. case 434:
  376. arr = "a,o,e,i,u,ü";
  377. break;
  378. case 435:
  379. arr = "ai,ei,ui,ao,ou,iu,ie,üe,er";
  380. break;
  381. case 436:
  382. arr = "an,en,in,un,ün,ang,eng,ing,ong";
  383. break;
  384. case 437:
  385. arr = "zhi,chi,shi,ri,zi,ci,si,yi,wu";
  386. break;
  387. case 438:
  388. arr = "yu,ye,yue,yin,yun,yuan,ying";
  389. break;
  390. }
  391. arr = arr.split(",");
  392. for (let i = 0; i < arr.length; i++) {
  393. const obj = {};
  394. obj.ID = i + 1;
  395. let qustion, answer, tags;
  396. let sound = arr[i];
  397. sound = sound.replace("ü", "v");
  398. const ReadString = "https://pinyin-1253256735.file.myqcloud.com/basic/" + sound + ".m4a";
  399. if (param.TestType === "read") {
  400. qustion = arr[i];
  401. //console.log(JSON.stringify(json.CHN));
  402. answer = "[读 src='" + ReadString + "']" + qustion + "[/读]\n\n";
  403. tags = "拼音怎么念";
  404. }
  405. else {
  406. //console.log(result[i].Word);
  407. qustion = "[读 src='" + ReadString + "']拼音[/读]\n";
  408. answer = arr[i] + "\n\n";
  409. for (let j = 0; j < arr[i].length; j++) {
  410. let str = arr[i][j];
  411. if (str === "ü")
  412. str = "v1"
  413. answer += "[图 w='650' h='650']https://miaguo-1253256735.file.myqcloud.com/letter/s" + str + ".gif[/图]\n";
  414. }
  415. tags = "拼音怎么写";
  416. }
  417. obj.Word = arr[i];
  418. obj.ReadString = ReadString;
  419. obj.Content = [];
  420. obj.Content.push({ContentType: 0, Content: tags});
  421. obj.Content.push({ContentType: 1, Content: qustion});
  422. obj.Content.push({ContentType: 2, Content: answer});
  423. obj.Content.push({ContentType: 3, Content: ""});
  424. result.push(obj);
  425. }
  426. }
  427. globalCache.set("GetPinyinUnitWords?TestType=" + param.TestType + "&BookID=" + param.BookID + "&UnitID=" + param.UnitID + "&Word=" + param.Word, result, config.BufferMemoryTimeHigh);
  428. }
  429. ctx.body = {"errcode": 10000, result: result};
  430. }