wordsinput.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. import common from '../../utils/util';
  2. import main from '../../utils/main';
  3. const app = getApp();
  4. let isFocus=true;
  5. Page({
  6. data: {
  7. Words:[],
  8. IsShowAlert:false,
  9. IsShowSetPanel:false,
  10. IsShowFirstOpen:false,
  11. IsShowExample:false,
  12. IsShowRemind:false,
  13. },
  14. onLoad: function (options) {
  15. let that = this;
  16. let grade=wx.getStorageSync('Grade');
  17. if (!grade)
  18. grade=[{Name:"小学",CSS:"Selected"},{Name:"初中",CSS:""},{Name:"高中",CSS:""},{Name:"大学",CSS:""}];
  19. let ArticleStyle=wx.getStorageSync('ArticleStyle');
  20. if (!ArticleStyle)
  21. ArticleStyle=[{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:""},{Name:"环保",CSS:""}];
  22. const hiddenhelp=wx.getStorageSync('HiddenWordInputFirstOpen');
  23. that.setData({
  24. Containnerheight: main.getWindowHeight(),
  25. Grade:grade,
  26. ArticleStyle:ArticleStyle,
  27. IsShowFirstOpen:!hiddenhelp,
  28. KeyboardBtnName:"next",
  29. });
  30. },
  31. onShow:function(e){
  32. let that = this;
  33. app.globalData.SelectedWords=common.removeDuplicateAndTrimStrings(app.globalData.SelectedWords);
  34. that.data.Words=[];
  35. for(let i=0;i<10;i++){
  36. let obj={};
  37. obj.ID=i+1;
  38. if (app.globalData.SelectedWords[i]){
  39. obj.Word=app.globalData.SelectedWords[i];
  40. obj.CSS="txtWordFinished";
  41. }
  42. else{
  43. obj.Word="";
  44. obj.CSS="";
  45. }
  46. obj.IsError=false;
  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. bindKeyInput: function (e) {
  59. let that=this;
  60. let id=e.currentTarget.dataset.id;
  61. let word=e.detail.value;
  62. for(let i=0;i<10;i++){
  63. if (i+1==id){
  64. that.data.Words[i].Word=word;
  65. that.data.Words[i].CSS="txtWordFinished";
  66. if (word && !that.isValidInput(word)){
  67. that.data.Words[i].IsError=true;
  68. }
  69. else{
  70. that.data.Words[i].IsError=false;
  71. if (!word)
  72. that.data.Words[i].CSS="";
  73. }
  74. break;
  75. }
  76. }
  77. that.setData({
  78. Words:that.data.Words,
  79. });
  80. that.isShowAlert();
  81. },
  82. isShowAlert:function(){
  83. let that=this;
  84. let b=false;
  85. for(let i=0;i<10;i++){
  86. if (that.data.Words[i].IsError){
  87. b=true;
  88. break;
  89. }
  90. }
  91. that.setData({
  92. IsShowAlert:b,
  93. AlertContent:"不支持特殊符号、数字、句子、非英语单词内容"
  94. });
  95. },
  96. setArticleParam:function(e){
  97. let that=this;
  98. let count=0;
  99. for(let i=0;i<10;i++){
  100. if (that.data.Words[i].CSS=="txtWordFinished"){
  101. count++
  102. }
  103. }
  104. if (count<5){
  105. that.setData({
  106. IsShowAlert:true,
  107. AlertContent:"请输入至少5个英语单词"
  108. });
  109. }
  110. else{
  111. that.setData({
  112. IsShowAlert:false,
  113. IsShowSetPanel:true,
  114. });
  115. }
  116. },
  117. setMenu:function(){
  118. this.setData({
  119. IsShowSetPanel:!this.data.IsShowSetPanel,
  120. });
  121. },
  122. showExample:function(){
  123. this.setData({
  124. IsShowExample:!this.data.IsShowExample,
  125. });
  126. },
  127. keyboardOK:function(e){
  128. let that=this;
  129. let id=e.currentTarget.dataset.id;
  130. id++;
  131. that.setFocus({currentTarget:{dataset:{id:id}}});
  132. },
  133. setFocus:function(e){
  134. let that=this;
  135. let id=e.currentTarget.dataset.id;
  136. for(let i=0;i<that.data.Words.length;i++){
  137. that.data.Words[i].Focus=false;
  138. if (that.data.Words[i].ID==id)
  139. that.data.Words[i].Focus=true;
  140. }
  141. that.setData({
  142. Words:that.data.Words,
  143. });
  144. },
  145. closeHelp:function(){
  146. this.setData({
  147. IsShowFirstOpen:false,
  148. });
  149. wx.setStorageSync('HiddenWordInputFirstOpen', true);
  150. },
  151. selectBtn:function(e){
  152. const index=e.currentTarget.dataset.index;
  153. const id=e.currentTarget.dataset.id;
  154. let arr=this.data.Grade;
  155. if (id==1)
  156. arr=this.data.ArticleStyle;
  157. for(let i=0;i<arr.length;i++){
  158. arr[i].CSS="";
  159. if (i==index)
  160. arr[i].CSS="Selected";
  161. }
  162. if (id==1){
  163. this.setData({
  164. ArticleStyle:arr,
  165. });
  166. wx.setStorageSync('ArticleStyle', arr);
  167. }
  168. else{
  169. this.setData({
  170. Grade:arr,
  171. });
  172. wx.setStorageSync('Grade', arr);
  173. }
  174. },
  175. isValidInput:function(input) {
  176. // 正则表达式匹配:大小写字母、空格、单引号、减号
  177. const regex = /^[a-zA-Z\s'-]+$/;
  178. return regex.test(input);
  179. },
  180. goto: function (e) {
  181. let that=this;
  182. var url=e.currentTarget.dataset.url;
  183. that.getInputData();
  184. if (url=="article"){
  185. let arr=this.data.Grade;
  186. for(let i=0;i<arr.length;i++){
  187. if (arr[i].CSS=="Selected"){
  188. url+="?Level="+i;
  189. break;
  190. }
  191. }
  192. arr=this.data.ArticleStyle;
  193. for(let i=0;i<arr.length;i++){
  194. if (arr[i].CSS=="Selected"){
  195. url+="&ArticleStyle="+arr[i].Name;
  196. break;
  197. }
  198. }
  199. }
  200. if (url=="../main/ocr" || url=="../main/selectword"){
  201. if ( app.globalData.SelectedWords.length>=10){
  202. that.showRemind();
  203. return;
  204. }
  205. if (app.globalData.OCRWords.length>0){
  206. url="../main/selectword";
  207. }
  208. else{
  209. url="../main/ocr"
  210. }
  211. }
  212. wx.navigateTo({
  213. url: url,
  214. });
  215. },
  216. getInputData:function(){
  217. let that=this;
  218. app.globalData.SelectedWords=[];
  219. for(let i=0;i<that.data.Words.length;i++){
  220. if (that.data.Words[i].Word)
  221. app.globalData.SelectedWords.push(that.data.Words[i].Word);
  222. }
  223. app.globalData.SelectedWords=common.removeDuplicateAndTrimStrings(app.globalData.SelectedWords);
  224. },
  225. clearInput:function(e){
  226. let that=this;
  227. const id=e.currentTarget.dataset.id;
  228. if (id=="0"){
  229. app.globalData.SelectedWords=[];
  230. this.onShow();
  231. }
  232. else{
  233. for(let i=0;i<10;i++){
  234. if (i+1==id){
  235. that.data.Words[i].Word="";
  236. that.data.Words[i].CSS="";
  237. that.data.Words[i].IsError=false;
  238. app.globalData.SelectedWords.splice(i,1);
  239. break;
  240. }
  241. }
  242. that.setData({
  243. Words:that.data.Words,
  244. });
  245. that.isShowAlert();
  246. }
  247. },
  248. showRemind:function(e){
  249. this.setData({
  250. IsShowRemind:!this.data.IsShowRemind,
  251. });
  252. },
  253. // 阻止示例面板的触摸事件传递到底层
  254. catchTouchMove: function(e) {
  255. // 这个函数不需要做任何事情,只需要捕获事件防止冒泡
  256. return false;
  257. },
  258. onShareAppMessage: function () {
  259. return {
  260. title: app.globalData.ShareTitle,
  261. path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
  262. imageUrl: app.globalData.ShareImage,
  263. }
  264. },
  265. })