wordsinput.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. import common from '../../utils/util';
  2. import main from '../../utils/main';
  3. const app = getApp();
  4. Page({
  5. data: {
  6. Words:[],
  7. IsShowAlert:false,
  8. IsShowSetPanel:false,
  9. IsShowPanelHelp:false,
  10. },
  11. onLoad: function (options) {
  12. var that = this;
  13. let grade=wx.getStorageSync('Grade');
  14. if (!grade)
  15. grade=[{Name:"小学",CSS:"Selected"},{Name:"初中",CSS:""},{Name:"高中",CSS:""},{Name:"大学",CSS:""}];
  16. let ArticleStyle=wx.getStorageSync('ArticleStyle');
  17. if (!ArticleStyle)
  18. 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:""}];
  19. const hiddenhelp=wx.getStorageSync('HiddenWordInputHelp');
  20. that.setData({
  21. Containnerheight: main.getWindowHeight(),
  22. Grade:grade,
  23. ArticleStyle:ArticleStyle,
  24. IsShowPanelHelp:!hiddenhelp,
  25. KeyboardBtnName:"next",
  26. });
  27. },
  28. onShow:function(e){
  29. var that = this;
  30. app.globalData.SelectedWords=common.removeDuplicateAndTrimStrings(app.globalData.SelectedWords);
  31. that.data.Words=[];
  32. for(let i=0;i<10;i++){
  33. let obj={};
  34. obj.ID=i+1;
  35. if (app.globalData.SelectedWords[i]){
  36. obj.Word=app.globalData.SelectedWords[i];
  37. obj.CSS="txtWordFinished";
  38. }
  39. else{
  40. obj.Word="";
  41. obj.CSS="";
  42. }
  43. obj.IsError=false;
  44. that.data.Words.push(obj);
  45. }
  46. console.log(app.globalData.SelectedWords);
  47. that.setData({
  48. Words:that.data.Words,
  49. });
  50. that.isShowAlert();
  51. },
  52. bindKeyInput: function (e) {
  53. let that=this;
  54. let id=e.currentTarget.dataset.id;
  55. let word=e.detail.value;
  56. for(let i=0;i<10;i++){
  57. if (i+1==id){
  58. that.data.Words[i].Word=word;
  59. that.data.Words[i].CSS="txtWordFinished";
  60. if (word && !that.isValidInput(word)){
  61. that.data.Words[i].IsError=true;
  62. }
  63. else{
  64. that.data.Words[i].IsError=false;
  65. if (!word)
  66. that.data.Words[i].CSS="";
  67. }
  68. break;
  69. }
  70. }
  71. that.setData({
  72. Words:that.data.Words,
  73. });
  74. that.isShowAlert();
  75. },
  76. isShowAlert:function(){
  77. const that=this;
  78. let b=false;
  79. for(let i=0;i<10;i++){
  80. if (that.data.Words[i].IsError){
  81. b=true;
  82. break;
  83. }
  84. }
  85. that.setData({
  86. IsShowAlert:b,
  87. AlertContent:"不支持特殊符号、数字、句子、非英语单词内容"
  88. });
  89. },
  90. setArticleParam:function(e){
  91. const that=this;
  92. let count=0;
  93. for(let i=0;i<10;i++){
  94. if (that.data.Words[i].CSS=="txtWordFinished"){
  95. count++
  96. }
  97. }
  98. if (count<5){
  99. that.setData({
  100. IsShowAlert:true,
  101. AlertContent:"请输入至少5个英语单词"
  102. });
  103. }
  104. else{
  105. that.setData({
  106. IsShowAlert:false,
  107. IsShowSetPanel:true,
  108. });
  109. }
  110. },
  111. setMenu:function(){
  112. this.setData({
  113. IsShowSetPanel:!this.data.IsShowSetPanel,
  114. });
  115. },
  116. keyboardOK:function(e){
  117. const that=this;
  118. let id=e.currentTarget.dataset.id;
  119. id++;
  120. that.setFocus({currentTarget:{dataset:{id:id}}});
  121. },
  122. setFocus:function(e){
  123. const that=this;
  124. let id=e.currentTarget.dataset.id;
  125. for(let i=0;i<that.data.Words.length;i++){
  126. that.data.Words[i].Focus=false;
  127. if (that.data.Words[i].ID==id)
  128. that.data.Words[i].Focus=true;
  129. }
  130. that.setData({
  131. Words:that.data.Words,
  132. });
  133. },
  134. closeHelp:function(){
  135. this.setData({
  136. IsShowPanelHelp:false,
  137. });
  138. wx.setStorageSync('HiddenWordInputHelp', true);
  139. },
  140. selectBtn:function(e){
  141. const index=e.currentTarget.dataset.index;
  142. const id=e.currentTarget.dataset.id;
  143. let arr=this.data.Grade;
  144. if (id==1)
  145. arr=this.data.ArticleStyle;
  146. for(let i=0;i<arr.length;i++){
  147. arr[i].CSS="";
  148. if (i==index)
  149. arr[i].CSS="Selected";
  150. }
  151. if (id==1){
  152. this.setData({
  153. ArticleStyle:arr,
  154. });
  155. wx.setStorageSync('ArticleStyle', arr);
  156. }
  157. else{
  158. this.setData({
  159. Grade:arr,
  160. });
  161. wx.setStorageSync('Grade', arr);
  162. }
  163. },
  164. isValidInput:function(input) {
  165. // 正则表达式匹配:大小写字母、空格、单引号、减号
  166. const regex = /^[a-zA-Z\s'-]+$/;
  167. return regex.test(input);
  168. },
  169. goto: function (e) {
  170. let that=this;
  171. var url=e.currentTarget.dataset.url;
  172. app.globalData.SelectedWords=[];
  173. for(let i=0;i<that.data.Words.length;i++){
  174. if (that.data.Words[i].Word)
  175. app.globalData.SelectedWords.push(that.data.Words[i].Word);
  176. }
  177. app.globalData.SelectedWords=common.removeDuplicateAndTrimStrings(app.globalData.SelectedWords);
  178. if (url=="article"){
  179. let arr=this.data.Grade;
  180. for(let i=0;i<arr.length;i++){
  181. if (arr[i].CSS=="Selected"){
  182. url+="?Level="+i;
  183. break;
  184. }
  185. }
  186. arr=this.data.ArticleStyle;
  187. for(let i=0;i<arr.length;i++){
  188. if (arr[i].CSS=="Selected"){
  189. url+="&ArticleStyle="+arr[i].Name;
  190. break;
  191. }
  192. }
  193. }
  194. wx.navigateTo({
  195. url: url,
  196. });
  197. },
  198. onShareAppMessage: function () {
  199. return {
  200. title: app.globalData.ShareTitle,
  201. path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
  202. imageUrl: app.globalData.ShareImage,
  203. }
  204. },
  205. })