core.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //公共方法,写的匆忙。暂时先扔这里吧。
  2. var dever =
  3. {
  4. //初始化
  5. init : function()
  6. {
  7. if (!this.config) {
  8. this.config = require('config.js').config;
  9. this.setting = {};
  10. this.log('init config', this.config);
  11. }
  12. }
  13. //设置title
  14. ,title : function(title)
  15. {
  16. wx.setNavigationBarTitle({
  17. title: title
  18. })
  19. }
  20. //获取模板里传过来的data
  21. ,getData : function(event)
  22. {
  23. return event.currentTarget.dataset;
  24. }
  25. //保存数据到存储器
  26. ,save : function(key, value)
  27. {
  28. wx.setStorageSync(key, value);
  29. this.log('save ' + key, value);
  30. }
  31. //读取保存数据到存储器
  32. ,getSave: function (key) {
  33. wx.getStorageSync(key)
  34. }
  35. //设置服务器传过来的公共值
  36. ,setSetting : function(value)
  37. {
  38. this.setting = value;
  39. this.log('init server setting', this.setting);
  40. if (value.title) {
  41. this.title(value.title);
  42. } else if(value.name) {
  43. this.title(value.name);
  44. }
  45. }
  46. //设置模板变量
  47. ,set : function(self, key, value)
  48. {
  49. var data = {};
  50. data[key] = value;
  51. this.sets(self, data);
  52. }
  53. //设置模板变量
  54. ,setList: function (self, value) {
  55. this.set(self, 'list', value);
  56. }
  57. //设置模板变量
  58. , setView: function (self, value) {
  59. this.set(self, 'view', value);
  60. }
  61. //设置模板变量
  62. ,sets: function (self, data) {
  63. this.log('data', data);
  64. self.setData(data);
  65. }
  66. //获取包
  67. ,package : function(name)
  68. {
  69. return require('package/' + name + '.js');
  70. }
  71. //html解析
  72. ,html : function()
  73. {
  74. var wxParse = this.package('wxParse');
  75. }
  76. //获取请求
  77. ,request : function(url, param, callback, method)
  78. {
  79. if (!method) {
  80. method = 'get';
  81. }
  82. return this.package('network').request(this, url, param, callback, method);
  83. }
  84. //提示框
  85. ,alert : function(msg, title)
  86. {
  87. wx.showToast({
  88. title: msg,
  89. icon: 'success',
  90. duration: 3000
  91. });
  92. }
  93. //跳转
  94. ,location : function(url)
  95. {
  96. this.log('location', url);
  97. wx.navigateTo({
  98. url: '../../template/' + url,
  99. })
  100. }
  101. //log
  102. ,log : function(title, msg)
  103. {
  104. if (this.config.debug) {
  105. console.log('dever debug -- ' + title, msg);
  106. }
  107. }
  108. //开始下拉刷新
  109. ,startPullDown : function(self)
  110. {
  111. wx.showNavigationBarLoading();
  112. self.onLoad();
  113. }
  114. //停止下拉刷新
  115. ,stopPullDown: function () {
  116. wx.hideNavigationBarLoading();
  117. wx.stopPullDownRefresh();
  118. }
  119. //加载购物车
  120. ,carts: function(self, save, path)
  121. {
  122. this.package('carts').load(self, save, path);
  123. }
  124. }
  125. dever.init();
  126. module.exports = {
  127. load: dever
  128. }