main.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. import common from '../utils/util';
  2. import constant from '../utils/constant';
  3. var app = getApp();
  4. var dataSendTimeout = 0;
  5. function getData(url, callback) {
  6. if (!app.globalData.IsProduction)
  7. console.log("加密前的结果为===", url);
  8. var url = common.Encrypt(url);
  9. //console.log("加密后的结果为===",url);
  10. wx.request({
  11. url: app.globalData.serverUrl + url,
  12. timeout:120000,
  13. success: function (res) {
  14. if (res.statusCode)
  15. common.checkError(res.statusCode);
  16. var data = res.data.result;
  17. callback(data);
  18. },
  19. fail: function () {
  20. wx.showToast({
  21. title: '系统忙请稍候',
  22. // image: "../images/universalpic_warning_white_126x120.png",
  23. duration: 3000
  24. });
  25. var err={};
  26. err.errCode=100;
  27. err.errMsg="网络错误";
  28. callback(null,err);
  29. },
  30. });
  31. }
  32. function postData(url, postData, callback) {
  33. console.log("加密前的结果为===", url);
  34. var url = common.Encrypt(url);
  35. //console.log("加密后的结果为===",url);
  36. wx.request({
  37. url: app.globalData.serverUrl + url,
  38. method: "POST",
  39. timeout:120000,
  40. data: postData,
  41. success: function (res) {
  42. if (res.statusCode)
  43. common.checkError(res.statusCode);
  44. var data = res.data.result;
  45. callback(data);
  46. },
  47. fail: function () {
  48. wx.showToast({
  49. title: '系统忙请稍候',
  50. duration: 3000
  51. });
  52. },
  53. });
  54. }
  55. function getLocalHost(callback) {
  56. if (!app.globalData.IsProduction) {
  57. var url = common.Encrypt("Ping");
  58. wx.request({
  59. url: app.globalData.serverUrlLocalhost + url,
  60. success: function (res) {
  61. app.globalData.serverUrl = app.globalData.serverUrlLocalhost;
  62. callback();
  63. },
  64. fail: function () {
  65. app.globalData.serverUrl = app.globalData.serverUrlServer;
  66. callback();
  67. },
  68. });
  69. } else {
  70. app.globalData.serverUrl = app.globalData.serverUrlServer;
  71. callback();
  72. }
  73. }
  74. function getPageInfo(){
  75. let result={};
  76. const pages = getCurrentPages(); // 获取页面栈
  77. result.NavigationDepth = pages.length;
  78. console.log("导航深度:", result.NavigationDepth);
  79. const prevPage = pages[pages.length - 2];
  80. result.FromRoute = prevPage ? prevPage.route : null;
  81. console.log("来源页面:", result.FromRoute);
  82. const currentPage = pages[pages.length - 1]; // 当前页面对象
  83. result.CurrentRoute = currentPage.route; // 当前页面路径
  84. console.log("当前页面:", result.CurrentRoute);
  85. if (pages.length > 1) {
  86. result.BackFromPage = pages[pages.length - 1].route;
  87. console.log("返回来源页面:", result.BackFromPage);
  88. }
  89. return result;
  90. }
  91. function getWindowHeight() {
  92. var height = app.globalData.systemInfo.windowHeight;
  93. //console.log("app.globalData.systemInfo.windowHeight:" + app.globalData.systemInfo.windowHeight * 2);
  94. if (app.globalData.systemInfo.model) {
  95. if (height == 504 && (
  96. app.globalData.systemInfo.model.indexOf("iPhone 6<") >= 0 ||
  97. app.globalData.systemInfo.model.indexOf("iPhone 7<") >= 0 ||
  98. app.globalData.systemInfo.model.indexOf("iPhone 6s<") >= 0 ||
  99. app.globalData.systemInfo.model.indexOf("iPhone 5") >= 0 ||
  100. app.globalData.systemInfo.model.indexOf("iPhone SE") >= 0
  101. )) {
  102. height = 596;
  103. } else if (app.globalData.systemInfo.model.indexOf("iPad") >= 0) {
  104. height = 470;
  105. }
  106. }
  107. height = height * 2;
  108. if (app.globalData.systemInfo.system && app.globalData.systemInfo.system.indexOf("Android") >= 0) {
  109. height = height + 168;
  110. } else {
  111. height = height + 50;
  112. }
  113. //console.log("height:" + height);
  114. //var height = app.globalData.systemInfo.screenHeight * 2;
  115. return height;
  116. }
  117. function onSelect(obj,event,callback){
  118. var result=false;
  119. var str=event.currentTarget.dataset.object;
  120. var index=event.currentTarget.dataset.index;
  121. var clickType=event.currentTarget.dataset.clicktype;
  122. if (clickType=="checkbox"){
  123. if (obj.data[str][index].CSS)
  124. obj.data[str][index].CSS="";
  125. else{
  126. obj.data[str][index].CSS="Selected";
  127. result=true;
  128. }
  129. }
  130. else{
  131. for(var i=0;i<obj.data[str].length;i++){
  132. if (i==index){
  133. obj.data[str][i].CSS="Selected";
  134. }
  135. else{
  136. obj.data[str][i].CSS="";
  137. }
  138. }
  139. }
  140. obj.setData(obj.data);
  141. callback(obj,event,result);
  142. }
  143. function checkGenerating(){
  144. let that=this;
  145. if (app.globalData.Generating){
  146. let timeStart=new Date().getTime();
  147. let intervalGenerate=setInterval(function(){
  148. let time=(new Date().getTime()-timeStart)/1000;
  149. console.log(time);
  150. if (!app.globalData.Generating){
  151. clearInterval(intervalGenerate);
  152. let page=that.getPageInfo();
  153. if (!(page.NavigationDepth==3
  154. && page.CurrentRoute=="pages/main/article"
  155. && page.BackFromPage=="pages/main/article")){
  156. wx.showModal({
  157. title: '通知',
  158. content: '文章生成好了。',
  159. cancelText:"不去",
  160. confirmText:"去看",
  161. complete: (res) => {
  162. if (res.confirm) {
  163. let next='article?ID=MAX';
  164. wx.reLaunch({
  165. url: 'index',
  166. complete:function(){
  167. wx.navigateTo({
  168. url: next,
  169. })
  170. }
  171. })
  172. }
  173. }
  174. });
  175. }
  176. }
  177. if (time > 120)
  178. clearInterval(intervalGenerate);
  179. },1000);
  180. }
  181. }
  182. function showGuideContainer(obj,myClass,imageLeft,imageTop,imageName,closeLeft,closeTop,offsetLeft,offsetTop){
  183. setTimeout(function(){
  184. wx.createSelectorQuery().select(myClass).boundingClientRect(function (rect) {
  185. console.log("rect.left:"+rect.left);
  186. console.log("rect.top:"+rect.top);
  187. console.log("rect.width:"+rect.width);
  188. console.log("rect.height:"+rect.height);
  189. rect.top-=10;
  190. if (offsetTop)
  191. rect.top+=offsetTop;
  192. rect.height+=5;
  193. if (offsetLeft)
  194. rect.left+=offsetLeft;
  195. rect.left2=(rect.left+rect.width)-5;
  196. obj.setData({
  197. panelGuideContainerTop:rect.top*2,
  198. panelGuideContainerWidth1:rect.left*2,
  199. panelGuideContainerHeight1:rect.height*2,
  200. panelGuideContainerLeft2:rect.left2*2,
  201. panelGuideContainerCloseLeft:closeLeft,
  202. panelGuideContainerCloseTop:closeTop,
  203. panelGuideContainerImage:imageName,
  204. });
  205. obj.setData({
  206. panelGuideContainerImageLeft:obj.data.panelGuideContainerWidth1+imageLeft,
  207. panelGuideContainerImageTop:obj.data.panelGuideContainerTop+imageTop,
  208. });
  209. }).exec();
  210. },200);
  211. }
  212. // 阻止示例面板的触摸事件传递到底层
  213. function catchTouchMove(e) {
  214. // 这个函数不需要做任何事情,只需要捕获事件防止冒泡
  215. return false;
  216. }
  217. module.exports = {
  218. getData: getData,
  219. postData: postData,
  220. getLocalHost: getLocalHost,
  221. getWindowHeight: getWindowHeight,
  222. onSelect:onSelect,
  223. checkGenerating:checkGenerating,
  224. getPageInfo:getPageInfo,
  225. showGuideContainer:showGuideContainer,
  226. catchTouchMove: catchTouchMove,
  227. }