知识学堂 > 课程 > 小程序显示当前页面地址

小程序显示当前页面地址

发布日期:2023/9/13 来源:聚恒【返回】

小程序需要显示当前页面地址在调试器中,就需要在app.js中手动添加监听

//监听路由切换
    //间接实现全局设置分享内容
    wx.onAppRoute(function (res) {
      console.log(res)
      //获取加载的页面
      let pages = getCurrentPages(),
        //获取当前页面的对象
        view = pages[pages.length - 1],
        data;
      data = view.data;
      data.timeStart = Date.now() //在当前页面的data中,记录进入页面的时间
      // if(!data.addonHide) {
      var oldOnHide = view.onHide // 记录单独在某些页面onhide里做的一些操作,防止全局覆盖原有页面操作
      // data.addonHide = true
      view['onHide'= function (view) { //全局添加的一些操作
        // 做一些我们自己想做的事情
        let timeEnd = Date.now() // 记录onhide时候的时间戳(用户离开页面的操作有两种(跳转页面走的是onhide,直接关闭小程序走的是onUnload )下面onUnload 不做解释,同样的操作)
        var stayTime = timeEnd - data.timeStart // 获取停留时间
        console.log("输出2:" + res.path)
        console.log("输出3:" + stayTime)
        console.log("输出4:" + data.timeStart)
        console.log("输出5:" + timeEnd)
        console.log("输出6:" +  JSON.stringify(res.query))


        wx.request({
          url: 'https://**********/app.ashx',
          method: 'post',
          data: {
            types: "jiankong",
            yemian: res.path,
            chuancan: JSON.stringify(res.query),
            shijian: stayTime,
            openId: wx.getStorageSync('openId'),
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
            
          },
          fail: function (e) {
            wx.showToast({
              title: '网络异常!',
              duration: 2000
            });
          },
          complete: function () {
    
          }
        });


        // 调用原来的onHide方法
        // oldOnHide.call(that, res) 
      }
      var oldOnUnload = view.onUnload
      view['onUnload'= function (view) {
        // 做一些我们自己想做的事情
        let timeEnd = Date.now()
        var stayTime = timeEnd - data.timeStart
        console.log("输出2:" + res.path)
        console.log("输出3:" + stayTime)
        console.log("输出4:" + data.timeStart)
        console.log("输出5:" + timeEnd)
        console.log("输出6:" +  JSON.stringify(res.query))
        
        wx.request({
          url: 'https://*********/app.ashx',
          method: 'post',
          data: {
            types: "jiankong",
            yemian: res.path,
            chuancan: JSON.stringify(res.query),
            shijian: stayTime,
            openId: wx.getStorageSync('openId'),
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
            
          },
          fail: function (e) {
            wx.showToast({
              title: '网络异常!',
              duration: 2000
            });
          },
          complete: function () {
    
          }
        });
        // 调用原来的onUnload 方法
        //  oldOnUnload.call(that, res)
      }
      // }
    })

这里附带了页面停留时间监听

<%@ WebHandler Language="C#" Class="index" %>


using System;
using System.Net;
using System.Web;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using LitJson;
using System.Web.Script.Serialization;


public class index : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        if (context.Request.Form["types"] == "jiankong")
        {
            context.Response.Write(tijiao(context.Request.Form["yemian"], context.Request.Form["chuancan"], context.Request.Form["shijian"], context.Request.Form["openId"]));
        }
    }
    /// <summary>
    /// 查询个人中心
    /// </summary>
    /// <param name="openId"></param>
    /// <returns></returns>
    public string tijiao(string yemian, string chuancan, string shijian, string openId)
    {
        string a = "0";
        try
        {
            BLL.User_MXManager User_MX_bll = new BLL.User_MXManager();
        User_MX_bll.Insert_User_MX("title,shortcontent,content,addTime,hot,grade,state,typeid,clickcount,field1", "'"+yemian+"','"+chuancan+"','','" + DateTime.Now + "',"+shijian+",0,0,0,0,'" + openId + "'");
        }
        catch (Exception ex)
        {
            a = "0";
        }
        JavaScriptSerializer jss = new JavaScriptSerializer();
        return jss.Serialize(a);
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}