When WeChat exits the program, storage and cookies will be lost. How to solve the problem?
怪我咯2017-05-31 10:43:38
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~