wordsinput.js 5.6 KB

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