Yilong WeChat applet framework component example code
Since I am in the online travel industry, I am more concerned about the industry trends of OTAs. I researched and experienced eLong’s WeChat mini program a while ago. Although there are some flaws, the architecture components of the mini program are still Very good, so today we will take a brief look at the framework components of the Yilong WeChat Mini Program.
First, we divide the framework components of the Yilong WeChat applet into the following four parts for analysis:
1. Local components
2. Independent components
3. Integrated components
4. Network request
First look at the three dynamic renderings:
Overall, the directory structure is as follows:
[AppleScript] Plain text view copy code
├── README.MD ├── app.js ├── app.json ├── app.wxss ├── components ├── image ├── pages ├── service └── utils ├── api.js ├── cookie.js ├── data-center.js ├── overwrite.js ├── page-events.js ├── path.js ├── promise.js └── service.js
Instructions for using the framework
The framework wraps all WeChat native APIs for easy control and expansion.
[AppleScript] Plain text view copy code
//index.js var api = require("./utils/api.js")(); api.login({ success: function(res) { console.log(res); } });
[AppleScript] Plain text view copy code
//index.js var api = require("./utils/api.js")(); api.login({ success: function(res) { console.log(res); } });
For the following Endinterface, the framework provides service layer entry management, and the interface returns a Promiseobject.
[AppleScript] View the copy code in plain text
//demo.js var Service = require("../utils/service.js"); module.exports = { GetTime: Service({ url: 'https://xxx.xxx.xxx/api/getserverdate/', params: [], //参数列表 method: 'GET', noLoading: true, mockData: function() { //模拟数据 return new Date(); }, dataTransform: function(data) { //适配处理 return data; } }) };
[AppleScript] View the copy code in plain text
//index.js var service = require('service/demo'); //框架约定,所有的后端接口,要注册到对应的service文件中 var serverDate = service.GetTime( /*service可配置参数列表,这里传入相对应的参数*/ ).then(function(date) { that.setData({ serverDate: date }); });
Mini program The cookie mechanism is not supported. If you want to be compatible with existing back-end cookie processing (no changes), you can use the cookie mechanism simulated by the framework.
[AppleScript] Plain text view copy code
//index.js var COOKIE = require('./cookie.js'); var expire = new Date().getTime() + res.expire * 1000; COOKIE.set(key, value, expire);
[AppleScript] Plain text view copy code
//service.js //... headers["Cookie"] = Cookie.getAll(); //用户cookie将随http请求发送至服务器 //...
Page( ) Function is used to register a page. Accepts an object parameter, which specifies the initial data of the page, life cycle function, event processing function, etc. The framework rewrites Page, which makes it convenient With the expansion capability, you only need to wrap the original business code with a wrapper.
[AppleScript] Plain text view copy code
//微信小程序原生页面注册形式 Page({ data: {}, onLoad: function() {} }); //框架重写注册形式 var dirname = 'pages/index', overwrite = require('../../utils/overwrite.js'); (function(require, Page) { //重写require、Page Page({ data: {}, onLoad: function() {} }); })(overwrite.require(require, dirname), overwrite.Page);
globalData listening, the framework supports global event listening mechanism
[AppleScript] Plain text view copy code
//index.js var dirname = 'pages/index', overwrite = require('../../utils/overwrite.js'); (function(require, Page) { //获取应用实例 var app = getApp(); var service = require('service/demo'); Page({ data: { indate: '', indateText: '' }, onLoad: function() { this.listenerGlobalData('indate', function(indate) { this.data.indate = indate this.data.indateText = new Date(indate).format('MM-dd') }.bind(this)); } }) })(overwrite.require(require, dirname), overwrite.Page);
Event mechanism, jumps between pages can transfer data, the framework supports the transfer of data between pages, and can also jump through The event object returned by the interface listens to custom events.
[AppleScript] Plain text view copy code
//index页面 var event = api.Navigate.go({ url: '../list/index', params: { name: 'billy' } }); event.on("listok", function(params) { console.log(params); });
[AppleScript] Plain text view copy code
//http页面 Page({ onLoad: function(data) { if (data.name === 'billy') { this.fireEvent("listok", 'hello ' + data.name); } } });
Component usage instructions
Built-in components
The framework rewrites PageConstruction method, and has built-in some commonly used components, such as alert, picker, setLoading, among which alert and setLoading internally encapsulate the native wx.showModal and wx.showToast respectively. The encapsulation makes the calling parameters structured and convenient for business use. There is no need to introduce the page structure when using it, just call it directly; the picker needs to be introduced into the page presentation layer structure first. , transfer configuration items according to configuration requirements.
[AppleScript] Plain text view copy code
//setLoading this.setLoading(true);//ture/false //picker 引入表现层结构 <!--index.wxml--> <view class="container"> <view class="userinfo"> <text class="userinfo-nickname">{{current}}</text> </view> <include src="../../components/base.wxml" /> </view> //picker 使用 overwrite.picker({ content: "选择排序", init: this.data.sortIndex, data: this.data.sortList, bindtap: function(id, index) { if (that.data.sort != id) { that.setData({ sortIndex: index, current: this.data.sortList[index].text }); } }, bindcancel: function() { console.log('cancel') } }); //alert overwrite.alert({ content: '弹框对话框,参数配置详见文档说明', cancelText: '取消', bindconfirm: function() { console.log('确定'); }, bindcancel: function() { console.log('取消'); } });
Independent page component
Independent page component is actually A complete page unit (composed of js, wxml, wxss) is very simple to use. Just introduce the relevant js method and call the open component (callback can be passed for data exchange processing). --The implementation principle is that the js method provided by the component will open a new page (api.Navigate.go) and interact through registered events BehaviorData
[AppleScript] Pure Text view copy code
//index.js var dirname = 'pages/externalComponent', overwrite = require('../../utils/overwrite.js'); require('../../utils/dateFormat.js'); (function(require, Page) { //获取应用实例 var app = getApp(); var CalendarPlugin = require('components/calendar/index'); Page({ data: { date: { indate: new Date().format('yyyy-MM-dd'), outdate: new Date(+new Date + 3600000 * 24).format('yyyy-MM-dd') } }, openCalendar: function() { var that = this; CalendarPlugin({ begin: that.data.date.indate, end: that.data.date.outdate }, function(res) { that.data.date.indate = res.start.format('yyyy-MM-dd'); that.data.date.outdate = res.end.format('yyyy-MM-dd'); that.setData({ date: that.data.date }) }) }, onLoad: function(data) { } }) })(overwrite.require(require, dirname), overwrite.Page);
Page-level component
框架重写Page构造器,支持构建页面时配置一个或多个页面级组件,所谓页面级组件就是该组件的注册形式和页面一致(支持data数据,onLoad、onReady、onShow生命周期事件,fireEvent、showLoading等页面级方法),其实现原理是将组件的所有成员方法和成员属性和依附页面进行合并,并解决了重名冲突,使用者不用关系处理细节,只管像注册一个页面一样注册组件。--需要注意的是页面级别组件不可再次使用Page构造方法。
1、引入组件表现层结构
[AppleScript] 纯文本查看 复制代码
<!--index.wxml--> <view class="container"> <view class="userinfo"> <!--当前页面数据--> </view> <!--引入组件页面结构--> <include src="../../components/base.wxml" /> </view>
2、引入组件样式表
[AppleScript] 纯文本查看 复制代码
/**引入组件样式表**/ @import "filter/index.wxss"; page { background: #f4f4f4; }
3、注册页面时注入组件
[AppleScript] 纯文本查看 复制代码
/** * 集成组件dome */ var dirname = 'pages/internalComponent', overwrite = require('../../utils/overwrite.js'); (function(require, Page) { /*引入组件js*/ var filter = require('./filter/index'); Page({ /** * 默认数据 * @type {Object} */ data: {...}, onLoad: function(options) {}, }, [{//注入组件 component: filter, instanceName: 'typeFilter', props: { style: { top: '44px' } }, events: { onChange: 'filterChangedCallBack', onOpen: 'filterOpenedCallBack', onClose: 'filterClosedCallBack' } }, { component: filter, instanceName: 'categoryFilter', props: { style: { top: '44px' } }, events: { onChange: 'filterChangedCallBack', onOpen: 'filterOpenedCallBack', onClose: 'filterClosedCallBack' } }]) })(overwrite.require(require, dirname), overwrite.Page)页面级组件由*.js、*.wxml、*.wxss组成,分别由注册页面引入,其中js部分不可再次使用Page构造 [AppleScript] 纯文本查看 复制代码 ├── index.js ├── index.wxml └── index.wxss[AppleScript] 纯文本查看 复制代码 //页面级组件js声明 /** * 筛选器 */ var dirname = 'pages/internalComponent/filter', api = require('../../../utils/api.js')(dirname) var bgAnimation = api.createAnimation({ duration: 200 }), contentAnimation = api.createAnimation({ duration: 200 }); module.exports = { data: { items: [], selectedId: '', bgAnimation: {}, contentAnimation: {}, isOpen: false }, /** * 监听组件加载 * @param {Object} props */ onLoad: function(props) { this.setData({ style: props.style }) }, /** * 初始化 * @param {Array} items * @param {String | Number} selectedIndex */ init: function(items, selectedIndex) {}, /** * 选中 * @param {Object} e */ select: function(e) { } }
The above is the detailed content of Yilong WeChat applet framework component example code. 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

AI Hentai Generator
Generate AI Hentai for free.

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



Xianyu's official WeChat mini program has quietly been launched. In the mini program, you can post private messages to communicate with buyers/sellers, view personal information and orders, search for items, etc. If you are curious about what the Xianyu WeChat mini program is called, take a look now. What is the name of the Xianyu WeChat applet? Answer: Xianyu, idle transactions, second-hand sales, valuations and recycling. 1. In the mini program, you can post idle messages, communicate with buyers/sellers via private messages, view personal information and orders, search for specified items, etc.; 2. On the mini program page, there are homepage, nearby, post idle, messages, and mine. 5 functions; 3. If you want to use it, you must activate WeChat payment before you can purchase it;

WeChat applet implements picture upload function With the development of mobile Internet, WeChat applet has become an indispensable part of people's lives. WeChat mini programs not only provide a wealth of application scenarios, but also support developer-defined functions, including image upload functions. This article will introduce how to implement the image upload function in the WeChat applet and provide specific code examples. 1. Preparatory work Before starting to write code, we need to download and install the WeChat developer tools and register as a WeChat developer. At the same time, you also need to understand WeChat

To implement the drop-down menu effect in WeChat Mini Programs, specific code examples are required. With the popularity of mobile Internet, WeChat Mini Programs have become an important part of Internet development, and more and more people have begun to pay attention to and use WeChat Mini Programs. The development of WeChat mini programs is simpler and faster than traditional APP development, but it also requires mastering certain development skills. In the development of WeChat mini programs, drop-down menus are a common UI component, achieving a better user experience. This article will introduce in detail how to implement the drop-down menu effect in the WeChat applet and provide practical

Implementing picture filter effects in WeChat mini programs With the popularity of social media applications, people are increasingly fond of applying filter effects to photos to enhance the artistic effect and attractiveness of the photos. Picture filter effects can also be implemented in WeChat mini programs, providing users with more interesting and creative photo editing functions. This article will introduce how to implement image filter effects in WeChat mini programs and provide specific code examples. First, we need to use the canvas component in the WeChat applet to load and edit images. The canvas component can be used on the page

Use the WeChat applet to achieve the carousel switching effect. The WeChat applet is a lightweight application that is simple and efficient to develop and use. In WeChat mini programs, it is a common requirement to achieve carousel switching effects. This article will introduce how to use the WeChat applet to achieve the carousel switching effect, and give specific code examples. First, add a carousel component to the page file of the WeChat applet. For example, you can use the <swiper> tag to achieve the switching effect of the carousel. In this component, you can pass b

The official WeChat mini program of Xianyu has been quietly launched. It provides users with a convenient platform that allows you to easily publish and trade idle items. In the mini program, you can communicate with buyers or sellers via private messages, view personal information and orders, and search for the items you want. So what exactly is Xianyu called in the WeChat mini program? This tutorial guide will introduce it to you in detail. Users who want to know, please follow this article and continue reading! What is the name of the Xianyu WeChat applet? Answer: Xianyu, idle transactions, second-hand sales, valuations and recycling. 1. In the mini program, you can post idle messages, communicate with buyers/sellers via private messages, view personal information and orders, search for specified items, etc.; 2. On the mini program page, there are homepage, nearby, post idle, messages, and mine. 5 functions; 3.

How to use PHP to develop the second-hand transaction function of WeChat applet? As a popular mobile application development platform, WeChat applet is used by more and more developers. In WeChat mini programs, second-hand transactions are a common functional requirement. This article will introduce how to use PHP to develop the second-hand transaction function of the WeChat applet and provide specific code examples. 1. Preparation work Before starting development, you need to ensure that the following conditions are met: the development environment of the WeChat applet has been set up, including registering the AppID of the applet and setting it in the background of the applet.

To implement the picture rotation effect in WeChat Mini Program, specific code examples are required. WeChat Mini Program is a lightweight application that provides users with rich functions and a good user experience. In mini programs, developers can use various components and APIs to achieve various effects. Among them, the picture rotation effect is a common animation effect that can add interest and visual effects to the mini program. To achieve image rotation effects in WeChat mini programs, you need to use the animation API provided by the mini program. The following is a specific code example that shows how to
