weapp-cookie
One line of code for wechat, Toutiao, Baidu, Alipay small program support cookie, portal: Github
Intro
The applets’ native 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). This library can implement cookies for your applets with a single line of code. To ensure that cookie-based service sessions do not become invalid, the session mechanism is shared with the Web end
Featrues
- One line of code makes the applet support cookies
- You can use the API to get and set cookies
- Supports domain/ PATH scopes
Install
npm install weapp-cookie --save
# Copy the NPM package to the vendor folder to avoid small programs that may not be able to find the file (tips: This step is not necessary with frameworks such as Wepy /mpvue)
cp -rf ./node_modules/ ./vendor/
Copy the code
Usage
Take wechat applets as an example, a line of code in the app.js root directory of the applets can be introduced
// app.js
import './vendor/weapp-cookie/index'
// Tips: Use wepy/mpvue to introduce app-cookie modules directly in portal JS
// import 'weapp-cookie'
App({
onLaunch: function () {}// ...
})
Copy the code
The original invocation method of WX. request will remain unchanged, but post-regenerative P-cookie will automatically proxy the underlying INTERFACE of WX. request 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) {
/* * If the interface is successfully invoked, app-cookie will automatically save all cookies generated by the server. SessionID) * and in all subsequent requests, to ensure that the cookie-based server session mechanism will not fail, * to achieve the shared session mechanism with the Web end (no longer need to manually maintain 3rd_session_key) */}})}})Copy the code
Cookie operations can be invoked through the API
import cookies from 'weapp-cookie'
/ / get a cookie
let token = cookies.get('csrf_token'.'example.com')
/ / set the cookie
let cookie = cookies.set('uid'.100, { domain: 'example.com' })
/ / delete the cookie
let isRemoved = cookies.remove('uid'.'example.com')
// Check whether cookies exist
let hasToken = cookies.has('uid'.'example.com')
/ /... Please refer to Api for details
Copy the code
Use and configure aliases: Due to the security mechanism of wechat applet, wx.request is not allowed to be rewritten in the applet plug-in environment. Therefore, built-in aliases or custom aliases are required to support cookie requests
import cookies from 'weapp-cookie'// Use the built-in alias wx.requestWithcookie ({url:'https://example.com/user/current',
success: function(res) {console.log(res)}}) // Configure custom alias cookies. Config ({requestAlias:'requestx'}) // Use the custom alias wx.requestx({url:'https://example.com/user/current',
success: function (res) {
console.log(res)
}
})
Copy the code
Api
CookieStore
import cookies from 'weapp-cookie'
/** * Obtain cookie value * @param {String} name Cookie name * @param {String} [domain] Specifies the domain name (optional) * @return {String} cookie value */
cookies.get(String name, String domain)
/** * Set cookie * @param {String} name Cookie name * @param {String} value Cookie value * @param {Object} options Cookie options * @param {String} options.domain Set domain name * @param {String} [options.path] * @param {Date} [options.expires] * @param {Number} [options.maxAge] * @param {Boolean} [options.httpOnly] * @return {Cookie} Cookie object */
cookies.set(String name, String value, Object options)
/** * Whether a cookie exists * @param {String} name Cookie name * @param {String} [domain] Specifies the domain name (optional, * @return {Boolean} Whether a domain name contains cokkie with the name name exists
cookies.has(String name, String domain)
/** * delete cookie * @param {Array} name Cookie key * @param {String} [domain] * @return {Boolean} Delete cookies with name in all domain names */
cookies.remove(String name, String domain)
/** * Obtain cookie object * @param {String} name Cookie name * @param {String} [domain] Specifies the domain name (optional) * @return {cookie} Cookie object */
cookies.getCookie(String name, String domain)
/** * get cookies JSON Object * @param {String} [domain] Specify domain name (optional, if not specified, get cookie value Object containing all domain names) * @return {Object} cookie JSON Object */
cookies.getCookies(String domain)
/** * Clear cookies * @param {String} [domain] Specify domain name (optional) * @return {Boolean} Clear cookies successfully */
cookies.clearCookies (domain)
@return {Object} obj structure JSON Object */
cookies.dir(domain)
Copy the code
Cookie
import cookies from 'weapp-cookie'
// Get the cookie object
let cookie = cookies.getCookie('uuid'.'example.com')
// ===== Cookie attribute =====
cookie.name: String
cookie.value: String
cookie.domain: String
cookie.path: String
cookie.expires: Date
cookie.maxAge: Number
cookie.httpOnly: Boolean
// ===== cookie method =====
/** * Validates cookie expiration * @return {Boolean} validates cookie expiration */
cookie.isExpired()
/** * verify that cookies can be persisted * @return {Boolean} verify that cookies can be persisted */
cookie.isPersistence()
Copy the code
If useful to you, welcome star ^_^