Explorar o código

modify recordSource

zhashaonan %!s(int64=6) %!d(string=hai) anos
pai
achega
ab9da44095
Modificáronse 5 ficheiros con 224 adicións e 65 borrados
  1. 17 4
      pages/brand/brand.js
  2. 6 12
      pages/details/details.js
  3. 15 12
      pages/index/index.js
  4. 179 0
      utils/base64.js
  5. 7 37
      utils/recordSource.js

+ 17 - 4
pages/brand/brand.js

@@ -1,4 +1,6 @@
 const getReq = require('./../../config.js').getReq;
+import recordSource from '../../utils/recordSource'
+let app = getApp();
 Page({
 
   /**
@@ -12,7 +14,9 @@ Page({
     brand_id : '',
     hot_id:'',
     keyword:'',
-    isScroll: false
+    isScroll: false,
+    fromSource: '',
+    firstLoad: true
   },
 
   /**
@@ -35,19 +39,26 @@ Page({
       keyword
     })
     let self = this;
+    let fromSource = recordSource(app, `act=search&op=index&brand_id=${brand_id}&hot_id=${hot_id}&keyword=${keyword}&curpage=${curpage}&client_type=mini`)
+    this.setData({
+      fromSource: app.globalData.fromSource
+    })
+    
     getReq({
       act: 'search',
       op: 'index',
       brand_id,
       hot_id,
       keyword,
-      curpage
+      curpage,
+      from: fromSource
     }, function (res) {
       if (res.code == 200) {
         self.setData({
           special_datas: res.datas,
           summery: res.datas.summary,
-          hasmore: res.datas.mobile_page.hasmore
+          hasmore: res.datas.mobile_page.hasmore,
+          firstLoad: false
         });
         wx.hideLoading();
       }
@@ -87,7 +98,9 @@ Page({
    * 生命周期函数--监听页面显示
    */
   onShow: function () {
-
+    if(!this.data.firstLoad) {
+      app.setFromSource(this.data.fromSource)
+    }
   },
 
   /**

+ 6 - 12
pages/details/details.js

@@ -2,7 +2,7 @@
 
 const getReq = require('./../../config.js').getReq;
 const app = getApp();
-import recordSource from '../../utils/recordSource'
+import recordSource from '../../utils/recordSource';
 var WxParse = require('../../wxParse/wxParse.js');
 Page({
 
@@ -40,7 +40,8 @@ Page({
     bundleIndex: -1,
     swiperHeight: '',
     show: false,
-    firstLoad: true
+    firstLoad: true,
+    fromSource: ''
   },
 
   /**
@@ -63,13 +64,7 @@ Page({
 
   },
   getDatas(goods_id) {
-
     let fromSource = recordSource(app, `act=goods_common&op=index&goods_id=${goods_id}&client_type=mini`)
-
-    // let fromSource = app.globalData.fromSource
-    // let currentSource = new RecordSource(`act=goods_common&op=index&goods_id=${goods_id}&client_type=mini`).encode()
-    // app.setFromSource(currentSource)
-
     wx.showLoading({
       title: '加载中',
     })
@@ -119,7 +114,8 @@ Page({
           promotionDays,
           goodsIdMap,
           bundleList,
-          firstLoad: false
+          firstLoad: false,
+          fromSource: app.globalData.fromSource
         })
       }
       else {
@@ -461,9 +457,6 @@ Page({
    * 生命周期函数--监听页面显示
    */
   onShow: function () {
-    console.log('detail source:', app.globalData.fromSource);
-
-
     if (app.globalData.fcodeErr) {
       wx.showToast({
         icon: 'none',
@@ -481,6 +474,7 @@ Page({
     }
     if (app.globalData.backLogin && !this.data.firstLoad) {
       console.log('backLogin:', this.data.getOneSummary.goods_id);
+      app.setFromSource(this.data.fromSource)
       this.getDatas(this.data.getOneSummary.goods_id);
     }
   },

+ 15 - 12
pages/index/index.js

@@ -1,5 +1,5 @@
 let app = getApp();
-import recordSource from '../../utils/recordSource'
+import recordSource from '../../utils/recordSource';
 const getReq = require('./../../config.js').getReq;
 
 Page({
@@ -11,27 +11,29 @@ Page({
     summery: [],
     isScroll: false,
     isSendProp: false,
-    firstLoad: true
+    firstLoad: true,
+    fromSource: ''
   },
 
   onShow: function () {
-    if (app.globalData.backLogin && !this.data.firstLoad) {
-      app.globalData.backLogin = false;
-      this.getDatas(0)
+    if (!this.data.firstLoad) {
+      app.setFromSource(this.data.fromSource)
+      if (app.globalData.backLogin) {
+        app.globalData.backLogin = false;
+        this.getDatas(0)
+      }
     }
+    // if (app.globalData.backLogin && !this.data.firstLoad) {
+    //   app.globalData.backLogin = false;
+    //   this.getDatas(0)
+    // }
   },
 
   onLoad: function () {
     this.getDatas(0)
   },
   getDatas(special_id) {
-
     let fromSource = recordSource(app, 'act=special&op=index&special_id=0&page=10&curpage=1&client_type=mini')
-
-    // let fromSource = app.globalData.fromSource
-    // let currentSource = new RecordSource('act=special&op=index&special_id=0&page=10&curpage=1&client_type=mini').encode()
-    // app.setFromSource(currentSource)
-
     wx.showLoading({
       title: '加载中'
     });
@@ -52,7 +54,8 @@ Page({
           prop_special: sent_down_special,
           special_datas: res.datas,
           summery: res.datas.summary,
-          firstLoad: false
+          firstLoad: false,
+          fromSource: app.globalData.fromSource
         });
       }
       setTimeout(function () {

+ 179 - 0
utils/base64.js

@@ -0,0 +1,179 @@
+
+  var _PADCHAR = "=",
+    _ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
+    _VERSION = "1.1";//Mr. Ruan fix to 1.1 to support asian char(utf8)
+
+
+  function _getbyte64(s, i) {
+    // This is oddly fast, except on Chrome/V8.
+    // Minimal or no improvement in performance by using a
+    // object with properties mapping chars to value (eg. 'A': 0)
+
+    var idx = _ALPHA.indexOf(s.charAt(i));
+
+    if (idx === -1) {
+      throw "Cannot decode base64";
+    }
+
+    return idx;
+  }
+
+  function _decode_chars(y, x) {
+    while (y.length > 0) {
+      var ch = y[0];
+      if (ch < 0x80) {
+        y.shift();
+        x.push(String.fromCharCode(ch));
+      } else if ((ch & 0x80) == 0xc0) {
+        if (y.length < 2) break;
+        ch = y.shift();
+        var ch1 = y.shift();
+        x.push(String.fromCharCode(((ch & 0x1f) << 6) + (ch1 & 0x3f)));
+      } else {
+        if (y.length < 3) break;
+        ch = y.shift();
+        var ch1 = y.shift();
+        var ch2 = y.shift();
+        x.push(String.fromCharCode(((ch & 0x0f) << 12) + ((ch1 & 0x3f) << 6) + (ch2 & 0x3f)));
+      }
+    }
+  }
+
+  function _decode(s) {
+    var pads = 0,
+      i,
+      b10,
+      imax = s.length,
+      x = [],
+      y = [];
+
+    s = String(s);
+
+    if (imax === 0) {
+      return s;
+    }
+
+    if (imax % 4 !== 0) {
+      throw "Cannot decode base64";
+    }
+
+    if (s.charAt(imax - 1) === _PADCHAR) {
+      pads = 1;
+
+      if (s.charAt(imax - 2) === _PADCHAR) {
+        pads = 2;
+      }
+
+      // either way, we want to ignore this last block
+      imax -= 4;
+    }
+
+    for (i = 0; i < imax; i += 4) {
+      var ch1 = _getbyte64(s, i);
+      var ch2 = _getbyte64(s, i + 1);
+      var ch3 = _getbyte64(s, i + 2);
+      var ch4 = _getbyte64(s, i + 3);
+
+      b10 = (_getbyte64(s, i) << 18) | (_getbyte64(s, i + 1) << 12) | (_getbyte64(s, i + 2) << 6) | _getbyte64(s, i + 3);
+      y.push(b10 >> 16);
+      y.push((b10 >> 8) & 0xff);
+      y.push(b10 & 0xff);
+      _decode_chars(y, x);
+    }
+    switch (pads) {
+      case 1:
+        b10 = (_getbyte64(s, i) << 18) | (_getbyte64(s, i + 1) << 12) | (_getbyte64(s, i + 2) << 6);
+        y.push(b10 >> 16);
+        y.push((b10 >> 8) & 0xff);
+        break;
+
+      case 2:
+        b10 = (_getbyte64(s, i) << 18) | (_getbyte64(s, i + 1) << 12);
+        y.push(b10 >> 16);
+        break;
+    }
+    _decode_chars(y, x);
+    if (y.length > 0) throw "Cannot decode base64";
+    return x.join("");
+  }
+
+
+  function _get_chars(ch, y) {
+    if (ch < 0x80) y.push(ch);
+    else if (ch < 0x800) {
+      y.push(0xc0 + ((ch >> 6) & 0x1f));
+      y.push(0x80 + (ch & 0x3f));
+    } else {
+      y.push(0xe0 + ((ch >> 12) & 0xf));
+      y.push(0x80 + ((ch >> 6) & 0x3f));
+      y.push(0x80 + (ch & 0x3f));
+    }
+  }
+
+
+
+  function _encode(s) {
+    if (arguments.length !== 1) {
+      throw "SyntaxError: exactly one argument required";
+    }
+
+    s = String(s);
+    if (s.length === 0) {
+      return s;
+    }
+
+    //s = _encode_utf8(s);
+    var i,
+      b10,
+      y = [],
+      x = [],
+      len = s.length;
+    i = 0;
+    while (i < len) {
+      _get_chars(s.charCodeAt(i), y);
+      while (y.length >= 3) {
+        var ch1 = y.shift();
+        var ch2 = y.shift();
+        var ch3 = y.shift();
+        b10 = (ch1 << 16) | (ch2 << 8) | ch3;
+        x.push(_ALPHA.charAt(b10 >> 18));
+        x.push(_ALPHA.charAt((b10 >> 12) & 0x3F));
+        x.push(_ALPHA.charAt((b10 >> 6) & 0x3f));
+        x.push(_ALPHA.charAt(b10 & 0x3f));
+      }
+      i++;
+    }
+
+
+    switch (y.length) {
+      case 1:
+        var ch = y.shift();
+        b10 = ch << 16;
+        x.push(_ALPHA.charAt(b10 >> 18) + _ALPHA.charAt((b10 >> 12) & 0x3F) + _PADCHAR + _PADCHAR);
+        break;
+
+      case 2:
+        var ch1 = y.shift();
+        var ch2 = y.shift();
+        b10 = (ch1 << 16) | (ch2 << 8);
+        x.push(_ALPHA.charAt(b10 >> 18) + _ALPHA.charAt((b10 >> 12) & 0x3F) + _ALPHA.charAt((b10 >> 6) & 0x3f) + _PADCHAR);
+        break;
+    }
+
+    return x.join("");
+  }
+
+
+  // return {
+  //   decode: _decode,
+  //   encode: _encode,
+  //   VERSION: _VERSION
+  // };
+
+  function transformCode(params) {
+    return encodeURIComponent(_encode(params))
+  }
+
+export default transformCode
+
+

+ 7 - 37
utils/recordSource.js

@@ -1,43 +1,9 @@
-class TransCoding {
-  constructor(params) {
-    this.params = params
-    this.toBase64Table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
-    this.base64Pad = '='
-  }
-  toBase64() {
-    let result = ''
-    let length = this.params.length
-    let i
-
-    for (i = 0; i < (length - 2); i += 3) {
-      result += this.toBase64Table[this.params.charCodeAt(i) >> 2];
-      result += this.toBase64Table[((this.params.charCodeAt(i) & 0x03) << 4) + (this.params.charCodeAt(i + 1) >> 4)];
-      result += this.toBase64Table[((this.params.charCodeAt(i + 1) & 0x0f) << 2) + (this.params.charCodeAt(i + 2) >> 6)];
-      result += this.toBase64Table[this.params.charCodeAt(i + 2) & 0x3f];
-    }
-
-    if (length % 3) {
-      i = length - (length % 3);
-      result += this.toBase64Table[this.params.charCodeAt(i) >> 2];
-      if ((length % 3) == 2) {
-        result += this.toBase64Table[((this.params.charCodeAt(i) & 0x03) << 4) + (this.params.charCodeAt(i + 1) >> 4)];
-        result += this.toBase64Table[(this.params.charCodeAt(i + 1) & 0x0f) << 2];
-        result += this.base64Pad;
-      } else {
-        result += this.toBase64Table[(this.params.charCodeAt(i) & 0x03) << 4];
-        result += this.base64Pad + this.base64Pad;
-      }
-    }
-    return result;
-  }
-  encode() {
-    return encodeURIComponent(this.toBase64())
-  }
-}
+
+import transformCode from './base64'
 
 function recordSource(app, params) {
   let fromSource = app.globalData.fromSource
-  let currentSource = new TransCoding(params).encode()
+  let currentSource = transformCode(params)
   app.setFromSource(currentSource)
   return fromSource
 }
@@ -45,3 +11,7 @@ export default recordSource
 
 
 
+
+
+
+