wordsinput.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 articleCategory=wx.getStorageSync('ArticleCategory');
  17. if (!articleCategory)
  18. articleCategory=[{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. ArticleCategory:articleCategory,
  24. IsShowPanelHelp:!hiddenhelp,
  25. KeyboardBtnName:"next",
  26. });
  27. },
  28. onShow:function(e){
  29. var that = this;
  30. that.data.Words=[];
  31. for(let i=0;i<10;i++){
  32. let obj={};
  33. obj.ID=i+1;
  34. if (app.globalData.SelectedWords[i]){
  35. obj.Word=app.globalData.SelectedWords[i];
  36. obj.CSS="txtWordFinished";
  37. }
  38. else{
  39. obj.Word="";
  40. obj.CSS="";
  41. }
  42. obj.IsError=false;
  43. that.data.Words.push(obj);
  44. }
  45. console.log(app.globalData.SelectedWords);
  46. that.setData({
  47. Words:that.data.Words,
  48. });
  49. that.isShowAlert();
  50. },
  51. bindKeyInput: function (e) {
  52. let that=this;
  53. let id=e.currentTarget.dataset.id;
  54. let word=e.detail.value;
  55. for(let i=0;i<10;i++){
  56. if (i+1==id){
  57. that.data.Words[i].Word=word;
  58. that.data.Words[i].CSS="txtWordFinished";
  59. if (word && !that.isValidInput(word)){
  60. that.data.Words[i].IsError=true;
  61. }
  62. else{
  63. that.data.Words[i].IsError=false;
  64. if (!word)
  65. that.data.Words[i].CSS="";
  66. }
  67. break;
  68. }
  69. }
  70. that.setData({
  71. Words:that.data.Words,
  72. });
  73. that.isShowAlert();
  74. },
  75. isShowAlert:function(){
  76. const that=this;
  77. let b=false;
  78. for(let i=0;i<10;i++){
  79. if (that.data.Words[i].IsError){
  80. b=true;
  81. break;
  82. }
  83. }
  84. that.setData({
  85. IsShowAlert:b,
  86. AlertContent:"不支持特殊符号、数字、句子、非英语单词内容"
  87. });
  88. },
  89. setArticleParam:function(e){
  90. const that=this;
  91. let count=0;
  92. for(let i=0;i<10;i++){
  93. if (that.data.Words[i].CSS=="txtWordFinished"){
  94. count++
  95. }
  96. }
  97. if (count<5){
  98. that.setData({
  99. IsShowAlert:true,
  100. AlertContent:"请输入至少5个英语单词"
  101. });
  102. }
  103. else{
  104. that.setData({
  105. IsShowAlert:false,
  106. IsShowSetPanel:true,
  107. });
  108. }
  109. },
  110. setMenu:function(){
  111. this.setData({
  112. IsShowSetPanel:!this.data.IsShowSetPanel,
  113. });
  114. },
  115. keyboardOK:function(e){
  116. const that=this;
  117. let id=e.currentTarget.dataset.id;
  118. id++;
  119. that.setFocus({currentTarget:{dataset:{id:id}}});
  120. },
  121. setFocus:function(e){
  122. const that=this;
  123. let id=e.currentTarget.dataset.id;
  124. for(let i=0;i<that.data.Words.length;i++){
  125. that.data.Words[i].Focus=false;
  126. if (that.data.Words[i].ID==id)
  127. that.data.Words[i].Focus=true;
  128. }
  129. that.setData({
  130. Words:that.data.Words,
  131. });
  132. },
  133. closeHelp:function(){
  134. this.setData({
  135. IsShowPanelHelp:false,
  136. });
  137. wx.setStorageSync('HiddenWordInputHelp', true);
  138. },
  139. selectBtn:function(e){
  140. const index=e.currentTarget.dataset.index;
  141. const id=e.currentTarget.dataset.id;
  142. let arr=this.data.Grade;
  143. if (id==1)
  144. arr=this.data.ArticleCategory;
  145. for(let i=0;i<arr.length;i++){
  146. if (i==index)
  147. arr[i].CSS="Selected";
  148. else
  149. arr[i].CSS="";
  150. }
  151. if (id==1){
  152. this.setData({
  153. ArticleCategory:arr,
  154. });
  155. wx.setStorageSync('ArticleCategory', 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. app.globalData.SelectedWords=[];
  172. for(let i=0;i<that.data.Words.length;i++){
  173. if (that.data.Words[i].Word)
  174. app.globalData.SelectedWords.push(that.data.Words[i].Word);
  175. }
  176. //debugger;
  177. var url=e.currentTarget.dataset.url;
  178. wx.navigateTo({
  179. url: url,
  180. });
  181. },
  182. onShareAppMessage: function () {
  183. return {
  184. title: app.globalData.ShareTitle,
  185. path: app.globalData.SharePath + '?UserID=' + app.globalData.userInfo.UserID,
  186. imageUrl: app.globalData.ShareImage,
  187. }
  188. },
  189. })