wordsinput.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. },
  15. onLoad: function (options) {
  16. let that = this;
  17. const hiddenhelp=wx.getStorageSync('HiddenWordInputFirstOpen');
  18. that.setData({
  19. Containnerheight: main.getWindowHeight(),
  20. IsShowFirstOpen:!hiddenhelp,
  21. KeyboardBtnName:"next",
  22. });
  23. that.initMenu();
  24. main.checkGenerating();
  25. },
  26. onShow:function(e){
  27. let that = this;
  28. app.globalData.SelectedWords=common.removeDuplicateAndTrimStrings(app.globalData.SelectedWords);
  29. that.data.Words=[];
  30. for(let i=0;i<10;i++){
  31. let obj={};
  32. obj.ID=i+1;
  33. if (app.globalData.SelectedWords[i]){
  34. obj.Word=app.globalData.SelectedWords[i];
  35. obj.CSS="txtWordFinished";
  36. if (obj.Word && !that.isValidInput(obj.Word)){
  37. obj.IsError=true;
  38. }
  39. else
  40. obj.IsError=false;
  41. }
  42. else{
  43. obj.Word="";
  44. obj.CSS="";
  45. obj.IsError=false;
  46. }
  47. that.data.Words.push(obj);
  48. }
  49. //console.log(app.globalData.SelectedWords);
  50. that.setData({
  51. Words:that.data.Words,
  52. });
  53. that.isShowAlert();
  54. },
  55. onHide:function(e){
  56. this.getInputData();
  57. },
  58. initMenu:function(){
  59. let that = this;
  60. let GradeArr=[{Name:"小学",CSS:"Selected"},{Name:"初中",CSS:""},{Name:"高中",CSS:""},{Name:"大学",CSS:""}];
  61. let grade=wx.getStorageSync('Grade');
  62. if (grade && grade>=0 && grade<=3){
  63. for(let i=0;i<GradeArr.length;i++){
  64. GradeArr[i].CSS="";
  65. if (grade==i)
  66. GradeArr[i].CSS="Selected";
  67. }
  68. }
  69. 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:""}];
  70. let ArticleStyle=wx.getStorageSync('ArticleStyle');
  71. if (ArticleStyle && ArticleStyle>=0 && ArticleStyle<=12){
  72. for(let i=0;i<ArticleStyleArr.length;i++){
  73. ArticleStyleArr[i].CSS="";
  74. if (ArticleStyle==i)
  75. ArticleStyleArr[i].CSS="Selected";
  76. }
  77. }
  78. let AIVersionArr=[{Version:"1.0",Content:"词句丰富,结构简明\n平均30秒生成",CSS:"Selected"},{Version:"1.5",Content:"深度表达,更多要素\n平均60秒生成",CSS:""}];
  79. let AIVersion=wx.getStorageSync('AIVersion');
  80. if (AIVersion && AIVersion>=0 && AIVersion<=2){
  81. for(let i=0;i<AIVersionArr.length;i++){
  82. AIVersionArr[i].CSS="";
  83. if (AIVersion==i)
  84. AIVersionArr[i].CSS="Selected";
  85. }
  86. }
  87. that.setData({
  88. GradeArr:GradeArr,
  89. ArticleStyleArr:ArticleStyleArr,
  90. AIVersionArr:AIVersionArr,
  91. });
  92. },
  93. bindKeyInput: function (e) {
  94. let that=this;
  95. let id=e.currentTarget.dataset.id;
  96. let word=e.detail.value;
  97. for(let i=0;i<10;i++){
  98. if (i+1==id){
  99. that.data.Words[i].Word=word;
  100. that.data.Words[i].CSS="txtWordFinished";
  101. if (word && !that.isValidInput(word)){
  102. that.data.Words[i].IsError=true;
  103. }
  104. else{
  105. that.data.Words[i].IsError=false;
  106. if (!word)
  107. that.data.Words[i].CSS="";
  108. }
  109. break;
  110. }
  111. }
  112. that.setData({
  113. Words:that.data.Words,
  114. });
  115. that.isShowAlert();
  116. },
  117. isShowAlert:function(){
  118. let that=this;
  119. let b=false;
  120. for(let i=0;i<10;i++){
  121. if (that.data.Words[i].IsError){
  122. b=true;
  123. break;
  124. }
  125. }
  126. that.setData({
  127. IsShowAlert:b,
  128. AlertContent:"请勿使用数字、符号、句子等非英语单词内容"
  129. });
  130. },
  131. setArticleParam:function(e){
  132. let that=this;
  133. let count=0;
  134. app.globalData.SelectedWords=[];
  135. for(let i=0;i<10;i++){
  136. if (that.data.Words[i].CSS=="txtWordFinished"){
  137. app.globalData.SelectedWords.push(that.data.Words[i].Word);
  138. count++
  139. }
  140. }
  141. if (count<5){
  142. that.setData({
  143. IsShowAlert:true,
  144. AlertContent:"请输入至少5个英语单词或词组"
  145. });
  146. }
  147. else{
  148. that.checkMsgSec(function(result){
  149. if (result){
  150. that.setData({
  151. IsShowAlert:false,
  152. IsShowSetPanel:true,
  153. });
  154. }
  155. });
  156. }
  157. },
  158. //敏感词判断
  159. checkMsgSec:function(callback){
  160. let that=this;
  161. var content = app.globalData.SelectedWords.join(",");
  162. main.postData("MsgSecCheck2", {
  163. Content: content,
  164. ProgramID:app.globalData.ProgramID,
  165. UserID:app.globalData.userInfo.UserID,
  166. },
  167. function (data) {
  168. if (data && data.errcode == 0) {
  169. callback(true);
  170. } else {
  171. if (data.errmsg) {
  172. wx.showToast({
  173. title: data.errmsg,
  174. duration: 2000,
  175. image: "../images/sysIcon_b16.png",
  176. });
  177. }
  178. callback(false);
  179. }
  180. });
  181. },
  182. setMenu:function(){
  183. this.setData({
  184. IsShowSetPanel:!this.data.IsShowSetPanel,
  185. });
  186. },
  187. showExample:function(){
  188. this.setData({
  189. IsShowExample:!this.data.IsShowExample,
  190. });
  191. },
  192. keyboardOK:function(e){
  193. let that=this;
  194. let id=e.currentTarget.dataset.id;
  195. id++;
  196. that.setFocus({currentTarget:{dataset:{id:id}}});
  197. },
  198. setFocus:function(e){
  199. let that=this;
  200. let id=e.currentTarget.dataset.id;
  201. for(let i=0;i<that.data.Words.length;i++){
  202. that.data.Words[i].Focus=false;
  203. if (that.data.Words[i].ID==id)
  204. that.data.Words[i].Focus=true;
  205. }
  206. that.setData({
  207. Words:that.data.Words,
  208. });
  209. },
  210. closeHelp:function(){
  211. this.setData({
  212. IsShowFirstOpen:false,
  213. });
  214. wx.setStorageSync('HiddenWordInputFirstOpen', true);
  215. },
  216. selectBtn:function(e){
  217. const index=e.currentTarget.dataset.index;
  218. const id=e.currentTarget.dataset.id;
  219. let arr=this.data.GradeArr;
  220. if (id==1)
  221. arr=this.data.ArticleStyleArr;
  222. else if (id==2)
  223. arr=this.data.AIVersionArr;
  224. for(let i=0;i<arr.length;i++){
  225. arr[i].CSS="";
  226. if (i==index)
  227. arr[i].CSS="Selected";
  228. }
  229. if (id==1){
  230. this.setData({
  231. ArticleStyleArr:arr,
  232. });
  233. wx.setStorageSync('ArticleStyle', index);
  234. }
  235. else if (id==2){
  236. this.setData({
  237. AIVersionArr:arr,
  238. });
  239. wx.setStorageSync('AIVersion', index);
  240. }
  241. else{
  242. this.setData({
  243. GradeArr:arr,
  244. });
  245. wx.setStorageSync('Grade', index);
  246. }
  247. },
  248. isValidInput:function(input) {
  249. // 正则表达式匹配:大小写字母、空格、单引号、减号
  250. const regex = /^[a-zA-Z\s'-]+$/;
  251. return regex.test(input);
  252. },
  253. goto: function (e) {
  254. let that=this;
  255. var url=e.currentTarget.dataset.url;
  256. that.getInputData();
  257. if (url=="article"){
  258. let arr=this.data.GradeArr;
  259. for(let i=0;i<arr.length;i++){
  260. if (arr[i].CSS=="Selected"){
  261. url+="?Level="+i;
  262. break;
  263. }
  264. }
  265. arr=this.data.ArticleStyleArr;
  266. for(let i=0;i<arr.length;i++){
  267. if (arr[i].CSS=="Selected"){
  268. url+="&ArticleStyle="+arr[i].Name;
  269. break;
  270. }
  271. }
  272. arr=this.data.AIVersionArr;
  273. for(let i=0;i<arr.length;i++){
  274. if (arr[i].CSS=="Selected"){
  275. url+="&AIVersion="+arr[i].Version;
  276. break;
  277. }
  278. }
  279. }
  280. if (url=="../main/ocr" || url=="../main/selectword"){
  281. if ( app.globalData.SelectedWords.length>=10){
  282. that.showRemind();
  283. return;
  284. }
  285. if (app.globalData.OCRWords.length>0){
  286. url="../main/selectword";
  287. }
  288. else{
  289. url="../main/ocr"
  290. }
  291. }
  292. wx.navigateTo({
  293. url: url,
  294. });
  295. },
  296. getInputData:function(){
  297. let that=this;
  298. app.globalData.SelectedWords=[];
  299. for(let i=0;i<that.data.Words.length;i++){
  300. if (that.data.Words[i].Word)
  301. app.globalData.SelectedWords.push(that.data.Words[i].Word);
  302. }
  303. app.globalData.SelectedWords=common.removeDuplicateAndTrimStrings(app.globalData.SelectedWords);
  304. },
  305. clearInput:function(e){
  306. let that=this;
  307. const id=e.currentTarget.dataset.id;
  308. if (id=="0"){
  309. app.globalData.SelectedWords=[];
  310. for(let i=0;i<that.data.Words.length;i++){
  311. let obj=that.data.Words[i];
  312. obj.Word="";
  313. obj.CSS="";
  314. obj.IsError=false;
  315. }
  316. that.setData({
  317. Words:that.data.Words,
  318. });
  319. }
  320. else{
  321. for(let i=0;i<10;i++){
  322. if (i+1==id){
  323. that.data.Words[i].Word="";
  324. that.data.Words[i].CSS="";
  325. that.data.Words[i].IsError=false;
  326. app.globalData.SelectedWords.splice(i,1);
  327. break;
  328. }
  329. }
  330. that.setData({
  331. Words:that.data.Words,
  332. });
  333. that.isShowAlert();
  334. }
  335. },
  336. showRemind:function(e){
  337. animation.toggleRemindWithAnimation(this);
  338. },
  339. // 阻止示例面板的触摸事件传递到底层
  340. catchTouchMove: function(e) {
  341. // 这个函数不需要做任何事情,只需要捕获事件防止冒泡
  342. return false;
  343. },
  344. onShareAppMessage: function () {
  345. return {
  346. title: app.globalData.ShareTitle,
  347. path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
  348. imageUrl: app.globalData.ShareImage,
  349. }
  350. },
  351. })