core.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. ,login : function(self, func)
  45. {
  46. wx.checkSession({
  47. success() {
  48. // session_key 未过期,并且在本生命周期一直有效
  49. //func();
  50. },
  51. fail() {
  52. // session_key 已经失效,需要重新执行登录流程
  53. wx.login({
  54. success(res) {
  55. if (res.code) {
  56. var success = function (data, res) {
  57. console.log(data);
  58. //func();
  59. };
  60. self.request('passport.applet.bind', {code:res.code}, { success: success });
  61. } else {
  62. console.log('登录失败!' + res.errMsg)
  63. }
  64. }
  65. }) // 重新登录
  66. }
  67. })
  68. }
  69. //设置服务器传过来的公共值
  70. ,setSetting : function(self, value)
  71. {
  72. this.setting = value;
  73. this.log('init server setting', this.setting);
  74. self.set('setting', this.setting);
  75. if (value.title) {
  76. this.title(value.title);
  77. } else if(value.name) {
  78. this.title(value.name);
  79. }
  80. }
  81. //获取包
  82. ,package : function(name)
  83. {
  84. return require('package/' + name + '.js');
  85. }
  86. //html解析
  87. ,html : function(self, html)
  88. {
  89. var wxParse = this.package('wxParse/wxParse');
  90. wxParse.wxParse('html', 'html', html, self, 5);
  91. }
  92. //提示框
  93. ,alert : function(msg, title)
  94. {
  95. wx.showToast({
  96. title: msg,
  97. icon: 'success',
  98. duration: 3000
  99. });
  100. }
  101. //回退 delta 返回的页面数,如果 delta 大于现有页面数,则返回到首页。
  102. ,goBack: function(delta)
  103. {
  104. if(typeof delta == 'undefined') delta = 1;
  105. wx.navigateBack({
  106. delta: delta
  107. })
  108. }
  109. //log
  110. ,log : function(title, msg)
  111. {
  112. if (this.config.debug) {
  113. console.log('dever debug -- ' + title, msg);
  114. }
  115. }
  116. //加载购物车
  117. ,carts: function(self, save, path)
  118. {
  119. this.package('carts').load(self, save, path);
  120. }
  121. }
  122. var page =
  123. {
  124. dever : dever.init()
  125. ,data: {}
  126. /**
  127. * 生命周期函数--监听页面加载
  128. */
  129. ,onLoad: function (options)
  130. {
  131. this.setConfig();
  132. this.stopPullDown();
  133. if (this.hasOwnProperty('oLoad')) {
  134. this.oLoad(options);
  135. }
  136. }
  137. /**
  138. * 生命周期函数--监听页面初次渲染完成
  139. */
  140. ,onReady: function ()
  141. {
  142. if (this.hasOwnProperty('oReady')) {
  143. this.oReady();
  144. }
  145. }
  146. /**
  147. * 生命周期函数--监听页面显示
  148. */
  149. ,onShow: function ()
  150. {
  151. if (this.hasOwnProperty('oShow')) {
  152. this.oShow();
  153. }
  154. }
  155. /**
  156. * 生命周期函数--监听页面隐藏
  157. */
  158. ,onHide: function ()
  159. {
  160. if (this.hasOwnProperty('oHide')) {
  161. this.oHide();
  162. }
  163. }
  164. /**
  165. * 生命周期函数--监听页面卸载
  166. */
  167. ,onUnload: function ()
  168. {
  169. if (this.hasOwnProperty('oUnload')) {
  170. this.oUnload();
  171. }
  172. }
  173. /**
  174. * 页面相关事件处理函数--监听用户下拉动作
  175. */
  176. ,onPullDownRefresh: function ()
  177. {
  178. wx.showNavigationBarLoading();
  179. this.onLoad();
  180. }
  181. /**
  182. * 停止下拉刷新
  183. */
  184. ,stopPullDown: function ()
  185. {
  186. wx.hideNavigationBarLoading();
  187. wx.stopPullDownRefresh();
  188. }
  189. /**
  190. * 用户点击右上角分享
  191. */
  192. ,onShareAppMessage: function ()
  193. {
  194. if (this.hasOwnProperty('oShare')) {
  195. this.oShare();
  196. }
  197. }
  198. //设置模板变量
  199. ,set : function(key, value)
  200. {
  201. var data = {};
  202. if (key.indexOf('.') != -1) {
  203. var t = key.split('.');
  204. key = t[0];
  205. data[key] = self.data[key];
  206. data[key][t[1]] = value;
  207. } else {
  208. data[key] = value;
  209. }
  210. this.sets(data);
  211. }
  212. //设置列表页模板变量
  213. ,setList: function (value)
  214. {
  215. this.set('list', value);
  216. }
  217. //设置详情页模板变量
  218. ,setView: function (value)
  219. {
  220. this.set('view', value);
  221. }
  222. //设置模板变量
  223. ,sets: function (data)
  224. {
  225. this.dever.log('data', data);
  226. this.setData(data);
  227. }
  228. //设置基本配置模板变量
  229. ,setConfig: function ()
  230. {
  231. this.set('config', this.dever.config);
  232. }
  233. //获取请求
  234. ,request : function(url, param, callback, method)
  235. {
  236. if (!method) {
  237. method = 'get';
  238. }
  239. return this.dever.package('network').request(this, url, param, callback, method);
  240. }
  241. //获取参数
  242. ,param : function(e)
  243. {
  244. return e.currentTarget.dataset;
  245. }
  246. //页面跳转
  247. ,location: function (uri, param)
  248. {
  249. var p = '';
  250. for(var i in param) {
  251. p += i + '='+param[i]+'&'
  252. }
  253. var url = '/template/' + uri + '/index?' + p;
  254. this.dever.log('location', url);
  255. wx.navigateTo({
  256. url: url
  257. })
  258. }
  259. }
  260. var app =
  261. {
  262. onLaunch: function ()
  263. {
  264. if (this.hasOwnProperty('oLaunch')) {
  265. this.oLaunch();
  266. }
  267. },
  268. onShow: function ()
  269. {
  270. if (this.hasOwnProperty('oShow')) {
  271. this.oShow();
  272. }
  273. },
  274. onHide: function ()
  275. {
  276. if (this.hasOwnProperty('oHide')) {
  277. this.oHide();
  278. }
  279. },
  280. dever: dever,
  281. //这里只能是复制,不能是引用
  282. page: function()
  283. {
  284. return Object.assign({}, page);
  285. }
  286. }
  287. module.exports = {
  288. app: app
  289. }