wordsinput.js 8.0 KB

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