Home >WeChat Applet >Mini Program Development >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!