wordsinput.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. import common from '../../utils/util';
  2. import main from '../../utils/main';
  3. import animation from '../../utils/animation';
  4. const app = getApp();
  5. let isFocus=true;
  6. Page({
  7. data: {
  8. Words:[],
  9. IsShowAlert:false,
  10. IsShowSetPanel:false,
  11. IsShowFirstOpen:false,
  12. IsShowExample:false,
  13. IsShowRemind:false,
  14. IsShowGuideContainer:false,
  15. IsShowGuideRemind:false,
  16. },
  17. onLoad: function (options) {
  18. let that = this;
  19. const IsShowFirstOpen=wx.getStorageSync('IsShowFirstOpen');
  20. const IsShowGuideContainer=wx.getStorageSync('IsShowGuideContainer');
  21. if (!IsShowGuideContainer && !IsShowFirstOpen){
  22. that.setData({
  23. IsShowFirstOpen:1,
  24. });
  25. }
  26. else{
  27. const IsShowGuideRemind=wx.getStorageSync('IsShowGuideRemind');
  28. if (!IsShowGuideRemind){
  29. that.setData({
  30. IsShowGuideRemind:1,
  31. });
  32. }
  33. }
  34. that.setData({
  35. Containnerheight: main.getWindowHeight(),
  36. KeyboardBtnName:"next",
  37. });
  38. that.initMenu();
  39. main.checkGenerating();
  40. },
  41. onShow:function(e){
  42. let that = this;
  43. that.initWords();
  44. that.isShowAlert();
  45. that.initGuide(null);
  46. },
  47. onHide:function(e){
  48. this.getInputData();
  49. },
  50. initWords:function(){
  51. let that = this;
  52. app.globalData.SelectedWords=common.removeDuplicateAndTrimStrings(app.globalData.SelectedWords);
  53. that.data.Words=[];
  54. for(let i=0;i<10;i++){
  55. let obj={};
  56. obj.ID=i+1;
  57. if (app.globalData.SelectedWords[i]){
  58. obj.Word=app.globalData.SelectedWords[i];
  59. obj.CSS="txtWordFinished";
  60. if (obj.Word && !that.isValidInput(obj.Word)){
  61. obj.IsError=true;
  62. }
  63. else
  64. obj.IsError=false;
  65. }
  66. else{
  67. obj.Word="";
  68. obj.CSS="";
  69. obj.IsError=false;
  70. }
  71. that.data.Words.push(obj);
  72. }
  73. //console.log(app.globalData.SelectedWords);
  74. that.setData({
  75. Words:that.data.Words,
  76. });
  77. },
  78. initGuide:function(e){
  79. let that=this;
  80. if (e && e.currentTarget.dataset.isstart){
  81. wx.setStorageSync('IsShowGuideContainer', true);
  82. //wx.setStorageSync('IsShowGuideRemind', 2);
  83. wx.navigateBack({
  84. delta: 1,
  85. });
  86. }
  87. else if (wx.getStorageSync("IsShowGuideContainer")){
  88. that.setData({
  89. IsShowGuideContainer:true,
  90. IsShowGuideRemind:false,
  91. });
  92. app.globalData.SelectedWords=["cat","dog","meat","fight","","happy","love","study","cry","flower"];
  93. that.initWords();
  94. main.showGuideContainer(that,"#txtWord5",290,-457,"pic_ha02",-50,5,-5,-5);
  95. }
  96. },
  97. initMenu:function(){
  98. let that = this;
  99. let GradeArr=[{Name:"小学",CSS:"Selected"},{Name:"初中",CSS:""},{Name:"高中",CSS:""},{Name:"大学",CSS:""}];
  100. let grade=wx.getStorageSync('Grade');
  101. if (grade && grade>=0 && grade<=3){
  102. for(let i=0;i<GradeArr.length;i++){
  103. GradeArr[i].CSS="";
  104. if (grade==i)
  105. GradeArr[i].CSS="Selected";
  106. }
  107. }
  108. let ArticleStyleArr=[{Name:"童话",CSS:"Selected"},{Name:"奇幻",CSS:""},{Name:"动物",CSS:""},{Name:"校园生活",CSS:""},{Name:"家庭亲子",CSS:""},{Name:"成长",CSS:""},{Name:"科幻",CSS:""},{Name:"旅行",CSS:""},{Name:"大自然",CSS:""},{Name:"科普",CSS:""},{Name:"节日文化",CSS:""},{Name:"人生励志",CSS:""}];
  109. let ArticleStyle=wx.getStorageSync('ArticleStyle');
  110. if (ArticleStyle && ArticleStyle>=0 && ArticleStyle<=12){
  111. for(let i=0;i<ArticleStyleArr.length;i++){
  112. ArticleStyleArr[i].CSS="";
  113. if (ArticleStyle==i)
  114. ArticleStyleArr[i].CSS="Selected";
  115. }
  116. }
  117. let AIVersionArr=[{Version:"1.0",Content:"词句丰富,结构简明\n平均30秒生成",CSS:"Selected"},{Version:"1.5",Content:"深度表达,更多要素\n平均60秒生成",CSS:""}];
  118. let AIVersion=wx.getStorageSync('AIVersion');
  119. if (AIVersion && AIVersion>=0 && AIVersion<=2){
  120. for(let i=0;i<AIVersionArr.length;i++){
  121. AIVersionArr[i].CSS="";
  122. if (AIVersion==i)
  123. AIVersionArr[i].CSS="Selected";
  124. }
  125. }
  126. that.setData({
  127. GradeArr:GradeArr,
  128. ArticleStyleArr:ArticleStyleArr,
  129. AIVersionArr:AIVersionArr,
  130. });
  131. },
  132. bindKeyInput: function (e) {
  133. let that=this;
  134. let id=e.currentTarget.dataset.id;
  135. let word=e.detail.value;
  136. if (wx.getStorageSync("IsShowGuideContainer") && word.toString().toLowerCase()=="win"){
  137. that.data.Words[4].Focus=false;
  138. that.data.Words[4].Word="win";
  139. that.setData({
  140. Words:that.data.Words,
  141. });
  142. main.showGuideContainer(that,"#btnNext",80,-260,"pic_ha03",0,0,-3,-20);
  143. }
  144. else if (that.data.IsShowGuideContainer){
  145. return;
  146. }
  147. for(let i=0;i<10;i++){
  148. if (i+1==id){
  149. that.data.Words[i].Word=word;
  150. that.data.Words[i].CSS="txtWordFinished";
  151. if (word && !that.isValidInput(word)){
  152. that.data.Words[i].IsError=true;
  153. }
  154. else{
  155. that.data.Words[i].IsError=false;
  156. if (!word)
  157. that.data.Words[i].CSS="";
  158. }
  159. break;
  160. }
  161. }
  162. that.setData({
  163. Words:that.data.Words,
  164. });
  165. that.isShowAlert();
  166. },
  167. isShowAlert:function(){
  168. let that=this;
  169. let b=false;
  170. for(let i=0;i<10;i++){
  171. if (that.data.Words[i].IsError){
  172. b=true;
  173. break;
  174. }
  175. }
  176. that.setData({
  177. IsShowAlert:b,
  178. AlertContent:"请勿使用数字、符号、句子等非英语单词内容"
  179. });
  180. },
  181. setArticleParam:function(e){
  182. let that=this;
  183. let count=0;
  184. app.globalData.SelectedWords=[];
  185. for(let i=0;i<10;i++){
  186. if (that.data.Words[i].CSS=="txtWordFinished"){
  187. app.globalData.SelectedWords.push(that.data.Words[i].Word);
  188. count++
  189. }
  190. }
  191. if (count<5){
  192. that.setData({
  193. IsShowAlert:true,
  194. AlertContent:"请输入至少5个英语单词或词组"
  195. });
  196. }
  197. else{
  198. that.checkMsgSec(function(result){
  199. if (result){
  200. that.setData({
  201. IsShowAlert:false,
  202. IsShowSetPanel:true,
  203. });
  204. if (wx.getStorageSync("IsShowGuideContainer")){
  205. main.showGuideContainer(that,"#btnLevel1",165,-115,"pic_ha04",270,330,-2,0);
  206. }
  207. }
  208. });
  209. }
  210. },
  211. //敏感词判断
  212. checkMsgSec:function(callback){
  213. let that=this;
  214. var content = app.globalData.SelectedWords.join(",");
  215. main.postData("MsgSecCheck2", {
  216. Content: content,
  217. ProgramID:app.globalData.ProgramID,
  218. UserID:app.globalData.userInfo.UserID,
  219. },
  220. function (data) {
  221. if (data && data.errcode == 0) {
  222. callback(true);
  223. } else {
  224. if (data.errmsg) {
  225. wx.showToast({
  226. title: data.errmsg,
  227. duration: 2000,
  228. image: "../images/sysIcon_b16.png",
  229. });
  230. }
  231. callback(false);
  232. }
  233. });
  234. },
  235. setMenu:function(){
  236. this.setData({
  237. IsShowSetPanel:!this.data.IsShowSetPanel,
  238. });
  239. },
  240. showExample:function(){
  241. this.setData({
  242. IsShowExample:!this.data.IsShowExample,
  243. });
  244. },
  245. showGuideRemind:function(){
  246. this.setData({
  247. IsShowGuideRemind:!this.data.IsShowGuideRemind,
  248. });
  249. wx.setStorageSync('IsShowGuideRemind', 2);
  250. },
  251. closeGuideContainer:function(){
  252. this.setData({
  253. IsShowGuideContainer:false,
  254. });
  255. wx.setStorageSync('IsShowGuideContainer', false);
  256. },
  257. keyboardOK:function(e){
  258. if (wx.getStorageSync('IsShowGuideContainer')){
  259. return ;
  260. }
  261. let that=this;
  262. let id=e.currentTarget.dataset.id;
  263. id++;
  264. that.setFocus({currentTarget:{dataset:{id:id}}});
  265. },
  266. setFocus:function(e){
  267. let that=this;
  268. let id=e.currentTarget.dataset.id;
  269. for(let i=0;i<that.data.Words.length;i++){
  270. that.data.Words[i].Focus=false;
  271. if (that.data.Words[i].ID==id)
  272. that.data.Words[i].Focus=true;
  273. }
  274. that.setData({
  275. Words:that.data.Words,
  276. });
  277. },
  278. closeIsShowFirstOpen:function(){
  279. this.setData({
  280. IsShowFirstOpen:false,
  281. IsShowGuideRemind:true,
  282. });
  283. wx.setStorageSync('IsShowFirstOpen', 2);
  284. },
  285. selectBtn:function(e){
  286. let that=this;
  287. const index=e.currentTarget.dataset.index;
  288. const id=e.currentTarget.dataset.id;
  289. let arr=this.data.GradeArr;
  290. if (id==1)
  291. arr=this.data.ArticleStyleArr;
  292. else if (id==2)
  293. arr=this.data.AIVersionArr;
  294. for(let i=0;i<arr.length;i++){
  295. arr[i].CSS="";
  296. if (i==index)
  297. arr[i].CSS="Selected";
  298. }
  299. if (id==1){
  300. this.setData({
  301. ArticleStyleArr:arr,
  302. });
  303. wx.setStorageSync('ArticleStyle', index);
  304. if (wx.getStorageSync("IsShowGuideContainer")){
  305. main.showGuideContainer(that,"#btnBuild",38,-247,"pic_ha06",480,0,-5,-17);
  306. }
  307. }
  308. else if (id==2){
  309. this.setData({
  310. AIVersionArr:arr,
  311. });
  312. wx.setStorageSync('AIVersion', index);
  313. }
  314. else{
  315. this.setData({
  316. GradeArr:arr,
  317. });
  318. wx.setStorageSync('Grade', index);
  319. if (wx.getStorageSync("IsShowGuideContainer")){
  320. main.showGuideContainer(that,"#btnArticleStyle1",140,-32,"pic_ha05",240,290,-1,-3);
  321. }
  322. }
  323. },
  324. isValidInput:function(input) {
  325. // 正则表达式匹配:大小写字母、空格、单引号、减号
  326. const regex = /^[a-zA-Z\s'-]+$/;
  327. return regex.test(input);
  328. },
  329. goto: function (e) {
  330. let that=this;
  331. var url=e.currentTarget.dataset.url;
  332. that.getInputData();
  333. if (url=="article"){
  334. let arr=this.data.GradeArr;
  335. for(let i=0;i<arr.length;i++){
  336. if (arr[i].CSS=="Selected"){
  337. url+="?Level="+i;
  338. break;
  339. }
  340. }
  341. arr=this.data.ArticleStyleArr;
  342. for(let i=0;i<arr.length;i++){
  343. if (arr[i].CSS=="Selected"){
  344. url+="&ArticleStyle="+arr[i].Name;
  345. break;
  346. }
  347. }
  348. arr=this.data.AIVersionArr;
  349. for(let i=0;i<arr.length;i++){
  350. if (arr[i].CSS=="Selected"){
  351. url+="&AIVersion="+arr[i].Version;
  352. break;
  353. }
  354. }
  355. if (wx.getStorageSync("IsShowGuideContainer")){
  356. wx.navigateTo({
  357. url: "article?ID=258&Type=Guide",
  358. });
  359. return;
  360. }
  361. }
  362. if (url=="../main/ocr" || url=="../main/selectword"){
  363. if ( app.globalData.SelectedWords.length>=10){
  364. that.showRemind();
  365. return;
  366. }
  367. if (app.globalData.OCRWords.length>0){
  368. url="../main/selectword";
  369. }
  370. else{
  371. url="../main/ocr"
  372. }
  373. }
  374. wx.navigateTo({
  375. url: url,
  376. });
  377. },
  378. getInputData:function(){
  379. let that=this;
  380. app.globalData.SelectedWords=[];
  381. for(let i=0;i<that.data.Words.length;i++){
  382. if (that.data.Words[i].Word)
  383. app.globalData.SelectedWords.push(that.data.Words[i].Word);
  384. }
  385. app.globalData.SelectedWords=common.removeDuplicateAndTrimStrings(app.globalData.SelectedWords);
  386. },
  387. clearInput:function(e){
  388. let that=this;
  389. const id=e.currentTarget.dataset.id;
  390. if (id=="0"){
  391. app.globalData.SelectedWords=[];
  392. for(let i=0;i<that.data.Words.length;i++){
  393. let obj=that.data.Words[i];
  394. obj.Word="";
  395. obj.CSS="";
  396. obj.IsError=false;
  397. }
  398. that.setData({
  399. Words:that.data.Words,
  400. });
  401. }
  402. else{
  403. for(let i=0;i<10;i++){
  404. if (i+1==id){
  405. that.data.Words[i].Word="";
  406. that.data.Words[i].CSS="";
  407. that.data.Words[i].IsError=false;
  408. app.globalData.SelectedWords.splice(i,1);
  409. break;
  410. }
  411. }
  412. that.setData({
  413. Words:that.data.Words,
  414. });
  415. that.isShowAlert();
  416. }
  417. },
  418. showRemind:function(e){
  419. animation.toggleRemindWithAnimation(this);
  420. },
  421. // 阻止示例面板的触摸事件传递到底层
  422. catchTouchMove: function(e) {
  423. // 这个函数不需要做任何事情,只需要捕获事件防止冒泡
  424. return false;
  425. },
  426. onShareAppMessage: function () {
  427. return {
  428. title: app.globalData.ShareTitle,
  429. path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
  430. imageUrl: app.globalData.ShareImage,
  431. }
  432. },
  433. })