html5 - WeChat exits the program, storage and cookies will be lost
大家讲道理
大家讲道理 2017-05-31 10:41:38
0
2
942

When WeChat exits the program, storage and cookies will be lost. How to solve the problem?

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

reply all(2)
習慣沉默

LocalStorage shouldn’t be lost, right?

刘奇

I also encountered this problem here. I don’t know whether the localstrorage is lost or there is no way to obtain it, especially when testing on the mobile phone. Anyway, it is certain that the WeChat browser has a lot of unstable performance. The solution is to use cookies.

//解决微信不定时localstrorage失效问题
//传入cookie
var setCookie = function(c_name,value,expiredays)
{
    var exdate=new Date()
    exdate.setDate(exdate.getDate()+expiredays)
    document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}

//取出cookie
var getCookie = function(c_name)
{
    if (document.cookie.length>0)
      {
      c_start=document.cookie.indexOf(c_name + "=")
      if (c_start!=-1)
        { 
        c_start=c_start + c_name.length+1 
        c_end=document.cookie.indexOf(";",c_start)
        if (c_end==-1) c_end=document.cookie.length
        return unescape(document.cookie.substring(c_start,c_end))
        } 
      }
    return ""
}

Of course, it mainly stores some key information, such as some constants, sessionId, loginToken, etc. If the user data is relatively large, I still use localstrorage. I just request the data again when needed, save and retrieve again.
The method used is rather clumsy, I hope it can help you, and I also hope that someone who can’t see it can give me some advice~

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template