Authored by 李奇

开关逻辑优化

@@ -7,6 +7,7 @@ import mta from './vendors/mta_analysis'; @@ -7,6 +7,7 @@ import mta from './vendors/mta_analysis';
7 import {Event} from '/login/utils/index.js'; 7 import {Event} from '/login/utils/index.js';
8 import {wxLogin} from '/login/utils/login/login'; 8 import {wxLogin} from '/login/utils/login/login';
9 import appReport from './libs/appReport'; 9 import appReport from './libs/appReport';
  10 +import iconfSwitch from './libs/switch';
10 import {isStringEmpty, getYHStorageSync} from './utils/util'; 11 import {isStringEmpty, getYHStorageSync} from './utils/util';
11 import regeneratorRuntime from '/login/libs/regenerator-runtime/index'; 12 import regeneratorRuntime from '/login/libs/regenerator-runtime/index';
12 13
@@ -115,6 +116,13 @@ App({ @@ -115,6 +116,13 @@ App({
115 116
116 //当应用程序进入前台显示状态时触发 117 //当应用程序进入前台显示状态时触发
117 onShow(options) { 118 onShow(options) {
  119 + iconfSwitch(this).then(data => {
  120 + console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> app onshow switch');
  121 + this.globalData.hasSetSwitch = true;
  122 + if (data['disableJavaApi'] && md5('disableJavaApi') === data['disableJavaApi']) { // TODO 键值确认
  123 + this.globalData.switch_javaApiEnable = false;
  124 + }
  125 + });
118 let that = this 126 let that = this
119 let params = {}; 127 let params = {};
120 yasReport(YB_ENTER_FOREGROUND, params, that) 128 yasReport(YB_ENTER_FOREGROUND, params, that)
@@ -313,6 +321,8 @@ App({ @@ -313,6 +321,8 @@ App({
313 }, 321 },
314 322
315 globalData: { 323 globalData: {
  324 + hasSetSwitch: false,
  325 + switch_javaApiEnable: true,
316 selectedChannel: 'boy', 326 selectedChannel: 'boy',
317 userInfo: {}, 327 userInfo: {},
318 systemInfo: null, 328 systemInfo: null,
@@ -9,65 +9,88 @@ import md5 from '../../../vendors/md5'; @@ -9,65 +9,88 @@ import md5 from '../../../vendors/md5';
9 const g_sourceApiArry = [ '/list', '/content', '/list/recommend', '/code/recent', '/list/mine', '/code/gain', '/code/mine' ]; 9 const g_sourceApiArry = [ '/list', '/content', '/list/recommend', '/code/recent', '/list/mine', '/code/gain', '/code/mine' ];
10 const g_toJavaApiArry = [ 'app.yoluck.activityList', 'app.yoluck.getContent', 'app.yoluck.recommendList', 'app.yoluck.recent', 'app.yoluck.participationList', 'app.yoluck.getCode', 'app.yoluck.userCode' ]; 10 const g_toJavaApiArry = [ 'app.yoluck.activityList', 'app.yoluck.getContent', 'app.yoluck.recommendList', 'app.yoluck.recent', 'app.yoluck.participationList', 'app.yoluck.getCode', 'app.yoluck.userCode' ];
11 11
  12 +
  13 +const app = getApp();
  14 +
12 class Service { 15 class Service {
13 constructor(url = '') { 16 constructor(url = '') {
14 this.url = url; 17 this.url = url;
15 - this.g_javaApiEnable = true;  
16 - this.hasSetSwitch = false;  
17 -  
18 - if (!this.hasSetSwitch) {  
19 - iconfSwitch().then(data => {  
20 - this.hasSetSwitch = true;  
21 - if (data['disableJavaApi'] && md5('disableJavaApi') === data['disableJavaApi']) { // TODO 键值确认  
22 - this.g_javaApiEnable = false;  
23 - }  
24 - });  
25 - }  
26 } 18 }
27 19
28 _get(path, data) { 20 _get(path, data) {
29 let method = path ? this.url + path : API_HOST; 21 let method = path ? this.url + path : API_HOST;
30 let pathIndex = g_sourceApiArry.indexOf(path); 22 let pathIndex = g_sourceApiArry.indexOf(path);
31 - //适配服务器更换Java接口,开关控制是否切换  
32 - if (this.g_javaApiEnable && pathIndex >= 0 && pathIndex < g_toJavaApiArry.length) {  
33 - method = API_HOST;  
34 - data.method = g_toJavaApiArry[pathIndex];  
35 - }  
36 -  
37 - return GET(method, data).then(result => {  
38 23
  24 + let executeReq = () => {
39 //适配服务器更换Java接口,开关控制是否切换 25 //适配服务器更换Java接口,开关控制是否切换
40 - if (this.g_javaApiEnable && pathIndex >= 0) {  
41 - if (result.data.list) {  
42 - let newList = Humps.decamelizeKeys(Object.values(result.data.list));  
43 - result.data.list = newList;  
44 - } else {  
45 - let newData = Humps.decamelizeKeys(result.data);  
46 - result.data = newData;  
47 - } 26 + if (app.globalData.switch_javaApiEnable && pathIndex >= 0 && pathIndex < g_toJavaApiArry.length) {
  27 + method = API_HOST;
  28 + data.method = g_toJavaApiArry[pathIndex];
48 } 29 }
49 - return result  
50 - }); 30 +
  31 + return GET(method, data).then(result => {
  32 +
  33 + //适配服务器更换Java接口,开关控制是否切换
  34 + if (app.globalData.switch_javaApiEnable && pathIndex >= 0) {
  35 + if (result.data.list) {
  36 + let newList = Humps.decamelizeKeys(Object.values(result.data.list));
  37 + result.data.list = newList;
  38 + } else {
  39 + let newData = Humps.decamelizeKeys(result.data);
  40 + result.data = newData;
  41 + }
  42 + }
  43 + return result
  44 + });
  45 + };
  46 +
  47 + if (!app.globalData.hasSetSwitch) {
  48 + iconfSwitch().then(data => {
  49 + console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> _get switch');
  50 + app.globalData.hasSetSwitch = true;
  51 + if (data['disableJavaApi'] && md5('disableJavaApi') === data['disableJavaApi']) { // TODO 键值确认
  52 + app.globalData.switch_javaApiEnable = false;
  53 + }
  54 + return executeReq();
  55 + });
  56 + } else {
  57 + return executeReq();
  58 + }
51 } 59 }
52 60
53 _post(path, data) { 61 _post(path, data) {
54 let method = path ? this.url + path : API_HOST; 62 let method = path ? this.url + path : API_HOST;
55 let pathIndex = g_sourceApiArry.indexOf(path); 63 let pathIndex = g_sourceApiArry.indexOf(path);
56 -  
57 - //适配服务器更换Java接口,开关控制是否切换  
58 - if (this.g_javaApiEnable && pathIndex >= 0 && pathIndex < g_toJavaApiArry.length) {  
59 - method = API_HOST;  
60 - data.method = g_toJavaApiArry[pathIndex];  
61 - }  
62 - return POST(method, data).then(result => {  
63 - 64 +
  65 + let executeReq = () => {
64 //适配服务器更换Java接口,开关控制是否切换 66 //适配服务器更换Java接口,开关控制是否切换
65 - if (this.g_javaApiEnable && pathIndex >= 0) {  
66 - let newData = Humps.decamelizeKeys(result.data);  
67 - result.data = newData; 67 + if (app.globalData.switch_javaApiEnable && pathIndex >= 0 && pathIndex < g_toJavaApiArry.length) {
  68 + method = API_HOST;
  69 + data.method = g_toJavaApiArry[pathIndex];
68 } 70 }
69 - return result;  
70 - }); 71 + return POST(method, data).then(result => {
  72 +
  73 + //适配服务器更换Java接口,开关控制是否切换
  74 + if (app.globalData.switch_javaApiEnable && pathIndex >= 0) {
  75 + let newData = Humps.decamelizeKeys(result.data);
  76 + result.data = newData;
  77 + }
  78 + return result;
  79 + });
  80 + }
  81 +
  82 + if (!app.globalData.hasSetSwitch) {
  83 + iconfSwitch().then(data => {
  84 + console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> _post switch');
  85 + app.globalData.hasSetSwitch = true;
  86 + if (data['disableJavaApi'] && md5('disableJavaApi') === data['disableJavaApi']) { // TODO 键值确认
  87 + app.globalData.switch_javaApiEnable = false;
  88 + }
  89 + return executeReq();
  90 + });
  91 + } else {
  92 + return executeReq();
  93 + }
71 } 94 }
72 } 95 }
73 96