wordsinput.js 8.7 KB

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