core.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. var dever =
  2. {
  3. //初始化
  4. init : function()
  5. {
  6. if (!this.config) {
  7. this.config = require('config.js').config;
  8. this.setting = {};
  9. this.log('init config', this.config);
  10. wx.getSystemInfo({
  11. success: e => {
  12. this.config.bar = {};
  13. this.config.bar.status = e.statusBarHeight;
  14. this.config.bar.custom = e.platform == 'android' ? e.statusBarHeight + 50 : e.statusBarHeight + 45;
  15. }
  16. })
  17. }
  18. return this;
  19. }
  20. //设置title
  21. ,title : function(title)
  22. {
  23. wx.setNavigationBarTitle({
  24. title: title
  25. })
  26. }
  27. //获取模板里传过来的data
  28. ,getData : function(event)
  29. {
  30. return event.currentTarget.dataset;
  31. }
  32. //保存数据到存储器
  33. ,save : function(key, value)
  34. {
  35. wx.setStorageSync(key, value);
  36. this.log('save ' + key, value);
  37. }
  38. //读取保存数据到存储器
  39. ,getSave: function (key)
  40. {
  41. wx.getStorageSync(key)
  42. }
  43. //设置服务器传过来的公共值
  44. ,setSetting : function(value)
  45. {
  46. this.setting = value;
  47. this.log('init server setting', this.setting);
  48. if (value.title) {
  49. this.title(value.title);
  50. } else if(value.name) {
  51. this.title(value.name);
  52. }
  53. }
  54. //获取包
  55. ,package : function(name)
  56. {
  57. return require('package/' + name + '.js');
  58. }
  59. //html解析
  60. ,html : function()
  61. {
  62. var wxParse = this.package('wxParse');
  63. }
  64. //提示框
  65. ,alert : function(msg, title)
  66. {
  67. wx.showToast({
  68. title: msg,
  69. icon: 'success',
  70. duration: 3000
  71. });
  72. }
  73. //跳转
  74. ,location : function(url)
  75. {
  76. this.log('location', url);
  77. wx.navigateTo({
  78. url: '../../template/' + url,
  79. })
  80. }
  81. //回退 delta 返回的页面数,如果 delta 大于现有页面数,则返回到首页。
  82. ,goBack: function(delta)
  83. {
  84. if(typeof delta == 'undefined') delta = 1;
  85. wx.navigateBack({
  86. delta: delta
  87. })
  88. }
  89. //log
  90. ,log : function(title, msg)
  91. {
  92. if (this.config.debug) {
  93. console.log('dever debug -- ' + title, msg);
  94. }
  95. }
  96. //加载购物车
  97. ,carts: function(self, save, path)
  98. {
  99. this.package('carts').load(self, save, path);
  100. }
  101. }
  102. var page =
  103. {
  104. dever : dever.init()
  105. ,data: {}
  106. /**
  107. * 生命周期函数--监听页面加载
  108. */
  109. ,onLoad: function (options) {
  110. this.setConfig();
  111. this.stopPullDown();
  112. if (this.hasOwnProperty('oLoad')) {
  113. this.oLoad(options);
  114. }
  115. }
  116. /**
  117. * 生命周期函数--监听页面初次渲染完成
  118. */
  119. ,onReady: function () {
  120. if (this.hasOwnProperty('oReady')) {
  121. this.oReady();
  122. }
  123. }
  124. /**
  125. * 生命周期函数--监听页面显示
  126. */
  127. ,onShow: function () {
  128. if (this.hasOwnProperty('oShow')) {
  129. this.oShow();
  130. }
  131. }
  132. /**
  133. * 生命周期函数--监听页面隐藏
  134. */
  135. ,onHide: function () {
  136. if (this.hasOwnProperty('oHide')) {
  137. this.oHide();
  138. }
  139. }
  140. /**
  141. * 生命周期函数--监听页面卸载
  142. */
  143. ,onUnload: function () {
  144. if (this.hasOwnProperty('oUnload')) {
  145. this.oUnload();
  146. }
  147. }
  148. /**
  149. * 页面相关事件处理函数--监听用户下拉动作
  150. */
  151. ,onPullDownRefresh: function () {
  152. wx.showNavigationBarLoading();
  153. this.onLoad();
  154. }
  155. /**
  156. * 停止下拉刷新
  157. */
  158. ,stopPullDown: function () {
  159. wx.hideNavigationBarLoading();
  160. wx.stopPullDownRefresh();
  161. }
  162. /**
  163. * 用户点击右上角分享
  164. */
  165. ,onShareAppMessage: function () {
  166. if (this.hasOwnProperty('oShare')) {
  167. this.oShare();
  168. }
  169. }
  170. //设置模板变量
  171. ,set : function(key, value)
  172. {
  173. var data = {};
  174. data[key] = value;
  175. this.sets(data);
  176. }
  177. //设置列表页模板变量
  178. ,setList: function (value)
  179. {
  180. this.set('list', value);
  181. }
  182. //设置详情页模板变量
  183. ,setView: function (value)
  184. {
  185. this.set('view', value);
  186. }
  187. //设置模板变量
  188. ,sets: function (data)
  189. {
  190. this.dever.log('data', data);
  191. this.setData(data);
  192. }
  193. //设置基本配置模板变量
  194. ,setConfig: function ()
  195. {
  196. this.set('config', this.dever.config);
  197. }
  198. //获取请求
  199. ,request : function(url, param, callback, method)
  200. {
  201. if (!method) {
  202. method = 'get';
  203. }
  204. return this.dever.package('network').request(this, url, param, callback, method);
  205. }
  206. }
  207. var app =
  208. {
  209. onLaunch: function () {
  210. if (this.hasOwnProperty('oLaunch')) {
  211. this.oLaunch();
  212. }
  213. },
  214. onShow: function () {
  215. if (this.hasOwnProperty('oShow')) {
  216. this.oShow();
  217. }
  218. },
  219. onHide: function () {
  220. if (this.hasOwnProperty('oHide')) {
  221. this.oHide();
  222. }
  223. },
  224. dever: dever,
  225. page: page
  226. }
  227. module.exports = {
  228. app: app
  229. }