core.js 6.5 KB

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