WeChat applet supports cookie code implementation
The content of this article is about the code implementation of WeChat applet supporting cookies. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
weapp-cookie
One line of code allows the WeChat applet to support cookies, portal: github
Intro
WeChat’s native wx.request network request interface does not support traditional cookies, but sometimes our existing back-end interface does rely on cookies (such as server user login state), this library can be used One line of code implements the Cookie mechanism for your mini program to ensure that the cookie-based service session will not expire and shares the session mechanism with the web side
Featrues
-
One line of code allows the applet to support cookies
You can use the API to obtain and set cookies
-
Support domain/path scope
Install
npm install weapp-cookie --save # 将 npm 包复制到 vendor 文件夹,避免小程序可能不能找到文件(tips:使用 wepy/mpvue 等框架无需此步) cp -rf ./node_modules/ ./vendor/
Usage
#Introduce one line of code in app.js in the root directory of the mini program.
// app.js import './vendor/weapp-cookie/index' // tips: 使用 wepy/mpvue 可以直接在入口 js 引入 weapp-cookie 模块 // import 'weapp-cookie' App({ onLaunch: function () { } // ... })
The original wx.request calling method remains unchanged. After introduction, weapp-cookie will automatically proxy wx.request interface access at the bottom layer to support cookie storage and sending
// pages/home/index.js Page({ onLoad: function () { wx.request({ url: 'https://example.com/login', data: { username: 'admin', password: '123456' }, success: function (res) { /* * 接口调用成功后 weapp-cookie 会自动保存后端发送的所有Cookie(比如:SessionID) * 并在后续的所有请求中带上,以保证基于 cookie 的服务器会话机制不会失效, * 实现与 web 端共用会话机制(无需再手动维护 3rd_session_key) */ } }) } })
cookie operations Can be called through api
import cookies from 'weapp-cookie' // 获取 cookie let token = cookies.get('csrf_token', 'example.com') // 设置 cookie let cookie = cookies.set('uid', 100, { domain: 'example.com' }) // 删除 cookie let isRemoved = cookies.remove('uid', 'example.com') // 判断是否存在 cookie let hasToken = cookies.has('uid', 'example.com') // ... 详情请参考 Api
Api
CookieStore
import cookies from 'weapp-cookie' /** * 获取 cookie 值 * @param {String} name cookie 名称 * @param {String} [domain] 指定域名(可选) * @return {String} cookie 值 */ cookies.get(String name, String domain) /** * 设置 cookie * @param {String} name cookie 名称 * @param {String} value cookie 值 * @param {Object} options cookie 选项 * @param {String} options.domain 设置域名 * @param {String} [options.path] * @param {Date} [options.expires] * @param {Number} [options.maxAge] * @param {Boolean} [options.httpOnly] * @return {Cookie} cookie 对象 */ cookies.set(String name, String value, Object options) /** * 是否存在某个 cookie * @param {String} name cookie 名称 * @param {String} [domain] 指定域名(可选,不指定则任意域名包含名称为 name 的 cokkie 即为存在) * @return {Boolean} 是否存在 */ cookies.has(String name, String domain) /** * 删除 cookie * @param {Array} name cookie 键 * @param {String} [domain] 指定域名(可选,不指定则删除所有域名中名称为 name 的 cookie) * @return {Boolean} 是否删除成功 */ cookies.remove(String name, String domain) /** * 获取 cookie 对象 * @param {String} name cookie 名称 * @param {String} [domain] 指定域名(可选) * @return {Cookie} cookie 对象 */ cookies.getCookie(String name, String domain) /** * 获取 cookies JSON对象 * @param {String} [domain] 指定域名(可选,不指定则获取包含所有域名的 cookie 值对象) * @return {Object} cookie JSON对象 */ cookies.getCookies(String domain) /** * 清除 cookie * @param {String} [domain] 指定域名(可选,不指定则清除所有域名 cookie) * @return {Boolean} 是否清除成功 */ cookies.clearCookies (domain) /** * 获取所有存储的域名和 cookies 结构 * @return {Object} obj 结构JSON对象 */ cookies.dir(domain)
Cookie
import cookies from 'weapp-cookie' // 获取 cookie 对象 let cookie = cookies.getCookie('uuid', 'example.com') // ===== cookie 属性 ===== cookie.name: String cookie.value: String cookie.domain: String cookie.path: String cookie.expires: Date cookie.maxAge: Number cookie.httpOnly: Boolean // ===== cookie 方法 ===== /** * 验证 cookie 是否过期 * @return {Boolean} 是否过期 */ cookie.isExpired() /** * 验证 cookie 是否可持久化 * @return {Boolean} 是否可持久化 */ cookie.isPersistence()
The above is the detailed content of WeChat applet supports cookie code implementation. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Cookies are usually stored in the cookie folder of the browser. Cookie files in the browser are usually stored in binary or SQLite format. If you open the cookie file directly, you may see some garbled or unreadable content, so it is best to use Use the cookie management interface provided by your browser to view and manage cookies.

Cookies on your computer are stored in specific locations on your browser, depending on the browser and operating system used: 1. Google Chrome, stored in C:\Users\YourUsername\AppData\Local\Google\Chrome\User Data\Default \Cookies etc.

Cookies on the mobile phone are stored in the browser application of the mobile device: 1. On iOS devices, Cookies are stored in Settings -> Safari -> Advanced -> Website Data of the Safari browser; 2. On Android devices, Cookies Stored in Settings -> Site settings -> Cookies of Chrome browser, etc.

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

With the popularity of the Internet, we use browsers to surf the Internet have become a way of life. In the daily use of browsers, we often encounter situations where we need to enter account passwords, such as online shopping, social networking, emails, etc. This information needs to be recorded by the browser so that it does not need to be entered again the next time you visit. This is when cookies come in handy. What are cookies? Cookie refers to a small data file sent by the server to the user's browser and stored locally. It contains user behavior of some websites.

Common problems and solutions for cookie settings, specific code examples are required. With the development of the Internet, cookies, as one of the most common conventional technologies, have been widely used in websites and applications. Cookie, simply put, is a data file stored on the user's computer that can be used to store the user's information on the website, including login name, shopping cart contents, website preferences, etc. Cookies are an essential tool for developers, but at the same time, cookie settings are often encountered

In our daily use of computers and the Internet, we are often exposed to cookies. A cookie is a small text file that saves records of our visits to the website, preferences and other information. This information may be used by the website to better serve us. But sometimes, we need to find cookie information to find the content we want. So how do we find cookies in the browser? First, we need to understand where the cookie exists. in browser

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service
