123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- //公共方法,写的匆忙。暂时先扔这里吧。
- var dever =
- {
- //初始化
- init : function()
- {
- if (!this.config) {
- this.config = require('config.js').config;
- this.setting = {};
- this.log('init config', this.config);
- }
- }
- //设置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) {
- return wx.getStorageSync(key);
- }
- //保存登录信息
- ,saveLoginInfo: function(value){
- this.save('applet.bind', value);
- }
- //获取登录信息
- ,getLoginInfo: function(){
- return this.getSave('applet.bind');
- }
- //获取登录签名串儿
- ,getSignature: function(){
- return this.getLoginInfo() != null ? this.getLoginInfo().signature : '';
- }
- //设置服务器传过来的公共值
- ,setSetting : function(value)
- {
- this.setting = value;
- this.log('init server setting', this.setting);
- if (value.title) {
- this.title(value.title);
- } else if(value.name) {
- this.title(value.name);
- }
- }
- //设置模板变量
- ,set : function(self, key, value)
- {
- var data = {};
- data[key] = value;
- this.sets(self, data);
- }
- //设置模板变量
- ,setList: function (self, value) {
- this.set(self, 'list', value);
- }
- //设置模板变量
- ,sets: function (self, data) {
- this.log('data', data);
- self.setData(data);
- }
- //列表分页追加
- , appendList: function (self, value) {
- var list = self.data.list;
- for(var i=0; i<value.length; i++){
- list.push(value[i]);
- }
- this.setList(self, list);
- }
- //设置模板变量
- , setView: function (self, value) {
- this.set(self, 'view', value);
- }
- //获取包
- ,package : function(name)
- {
- return require('package/' + name + '.js');
- }
- //html解析
- , html: function (bindName, data, target)
- {
- var wxParse = this.package('wxParse/wxParse');
- wxParse.wxParse(bindName, 'html', data, target, 0);
- }
- //获取请求
- ,request : function(url, param, callback, method)
- {
- if (!method) {
- method = 'get';
- }
- return this.package('network').request(this, url, param, callback, method);
- }
- //提示框
- ,alert : function(msg, title)
- {
- wx.showToast({
- title: msg,
- icon: 'none',
- duration: 3000
- });
- }
- //跳转
- ,location : function(url)
- {
- this.log('location', url);
- wx.navigateTo({
- url: '../../template/' + url,
- })
- }
- /**
- * 重定向页面
- */
- ,redirect: function(url){
- this.log('redirect', url);
- wx.redirectTo({
- url: '../../template/' + url,
- })
- }
- //回退 delta 返回的页面数,如果 delta 大于现有页面数,则返回到首页。
- ,goBack: function(delta){
- if(typeof delta != 'number') delta = 1;
- wx.navigateBack({
- delta: delta
- })
- }
- //log
- ,log : function(title, msg)
- {
- if (this.config.debug) {
- console.log('dever debug -- ' + title, msg);
- }
- }
- //开始下拉刷新
- ,startPullDown : function(self)
- {
- wx.showNavigationBarLoading();
- self.onLoad();
- }
- //停止下拉刷新
- ,stopPullDown: function () {
- wx.hideNavigationBarLoading();
- wx.stopPullDownRefresh();
- }
- //加载购物车
- ,carts: function(self, save, path)
- {
- this.package('carts').load(self, save, path);
- }
- //显示加载中...
- ,showLoading: function(){
- wx.showLoading({
- title: '加载中',
- mask: true
- })
- }
-
- //隐藏加载中...
- ,hideLoading: function(){
- wx.hideLoading();
- }
- //检查用户是否登录状态,暂支持微信登录
- ,isLogin: function(fun){
- wx.checkSession({
- success: function(res){
- fun(true);
- },
- fail: function(res){
- fun(false);
- }
- })
- }
- }
- dever.init();
- module.exports = {
- load: dever
- }
|