wordsinput.js 8.0 KB

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