123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- var dever =
- {
- //初始化
- init : function()
- {
- if (!this.config) {
- this.config = require('config.js').config;
- this.setting = {};
- this.log('init config', this.config);
- wx.getSystemInfo({
- success: e => {
- this.config.bar = {};
- this.config.bar.status = e.statusBarHeight;
- this.config.bar.custom = e.platform == 'android' ? e.statusBarHeight + 50 : e.statusBarHeight + 45;
- }
- })
- }
- return this;
- }
- //设置title
- ,title : function(title)
- {
- wx.setNavigationBarTitle({
- title: title
- })
- }
- //获取模板里传过来的data
- ,getData : function(event)
- {
- return event.currentTarget.dataset;
- }
- //保存数据到存储器
- ,save : function(key, value)
- {
- wx.setStorageSync(key, value);
- this.log('save ' + key, value);
- }
- //读取保存数据到存储器
- ,getSave: function (key)
- {
- wx.getStorageSync(key)
- }
- //登录验证
- ,login : function(self, func)
- {
- var dever = this;
- wx.checkSession({
- success() {
- // session_key 未过期,并且在本生命周期一直有效
- func();
- },
- fail() {
- // session_key 已经失效,需要重新执行登录流程
- dever.wxLogin(func);
- }
- })
- }
- ,wxLogin : function(func)
- {
- wx.login({
- success(res) {
- if (res.code) {
- var success = function (data, res) {
- dever.save('user', data);
- //func();
- };
- self.request('passport.applet.bind', { code: res.code }, { success: success });
- } else {
- console.log('登录失败!' + res.errMsg)
- }
- }
- }) // 重新登录
- }
- //设置服务器传过来的公共值
- ,setSetting : function(self, value)
- {
- this.setting = value;
- this.log('init server setting', this.setting);
- self.set('setting', this.setting);
- if (value.title) {
- this.title(value.title);
- } else if(value.name) {
- this.title(value.name);
- }
- }
- //获取包
- ,package : function(name)
- {
- return require('package/' + name + '.js');
- }
- //html解析
- ,html : function(self, html)
- {
- var wxParse = this.package('wxParse/wxParse');
- wxParse.wxParse('html', 'html', html, self, 5);
- }
- //提示框
- ,alert : function(msg, title)
- {
- wx.showToast({
- title: msg,
- icon: 'success',
- duration: 3000
- });
- }
- //回退 delta 返回的页面数,如果 delta 大于现有页面数,则返回到首页。
- ,goBack: function(delta)
- {
- if(typeof delta == 'undefined') delta = 1;
- wx.navigateBack({
- delta: delta
- })
- }
- //log
- ,log : function(title, msg)
- {
- if (this.config.debug) {
- console.log('dever debug -- ' + title, msg);
- }
- }
- //加载购物车
- ,carts: function(self, save, path)
- {
- this.package('carts').load(self, save, path);
- }
- }
- var page =
- {
- dever : dever.init()
- ,data: {}
- /**
- * 生命周期函数--监听页面加载
- */
- ,onLoad: function (options)
- {
- this.setConfig();
- this.stopPullDown();
- if (this.hasOwnProperty('oLoad')) {
- this.oLoad(options);
- }
- }
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- ,onReady: function ()
- {
- if (this.hasOwnProperty('oReady')) {
- this.oReady();
- }
- }
- /**
- * 生命周期函数--监听页面显示
- */
- ,onShow: function ()
- {
- if (this.hasOwnProperty('oShow')) {
- this.oShow();
- }
- }
- /**
- * 生命周期函数--监听页面隐藏
- */
- ,onHide: function ()
- {
- if (this.hasOwnProperty('oHide')) {
- this.oHide();
- }
- }
- /**
- * 生命周期函数--监听页面卸载
- */
- ,onUnload: function ()
- {
- if (this.hasOwnProperty('oUnload')) {
- this.oUnload();
- }
- }
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- ,onPullDownRefresh: function ()
- {
- wx.showNavigationBarLoading();
- this.onLoad();
- }
- /**
- * 停止下拉刷新
- */
- ,stopPullDown: function ()
- {
- wx.hideNavigationBarLoading();
- wx.stopPullDownRefresh();
- }
- /**
- * 用户点击右上角分享
- */
- ,onShareAppMessage: function ()
- {
- if (this.hasOwnProperty('oShare')) {
- this.oShare();
- }
- }
- //设置模板变量
- ,set : function(key, value)
- {
- var data = {};
- if (key.indexOf('.') != -1) {
- var t = key.split('.');
- key = t[0];
- data[key] = self.data[key];
- data[key][t[1]] = value;
- } else {
- data[key] = value;
- }
- this.sets(data);
- }
- //设置列表页模板变量
- ,setList: function (value)
- {
- this.set('list', value);
- }
- //设置详情页模板变量
- ,setView: function (value)
- {
- this.set('view', value);
- }
- //设置模板变量
- ,sets: function (data)
- {
- this.dever.log('data', data);
- this.setData(data);
- }
- //设置基本配置模板变量
- ,setConfig: function ()
- {
- this.set('config', this.dever.config);
- }
- //获取请求
- ,request : function(url, param, callback, method)
- {
- if (!method) {
- method = 'get';
- }
- return this.dever.package('network').request(this, url, param, callback, method);
- }
- //获取参数
- ,param : function(e)
- {
- return e.currentTarget.dataset;
- }
- //页面跳转
- ,location: function (uri, param)
- {
- var p = '';
- for(var i in param) {
- p += i + '='+param[i]+'&'
- }
- var url = '/template/' + uri + '/index?' + p;
- this.dever.log('location', url);
- wx.navigateTo({
- url: url
- })
- }
- }
- var app =
- {
- onLaunch: function ()
- {
- if (this.hasOwnProperty('oLaunch')) {
- this.oLaunch();
- }
- },
- onShow: function ()
- {
- if (this.hasOwnProperty('oShow')) {
- this.oShow();
- }
- },
- onHide: function ()
- {
- if (this.hasOwnProperty('oHide')) {
- this.oHide();
- }
- },
- dever: dever,
- //这里只能是复制,不能是引用
- page: function()
- {
- return Object.assign({}, page);
- }
- }
- module.exports = {
- app: app
- }
|