core.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. return wx.getStorageSync(key);
  34. }
  35. //保存登录信息
  36. ,saveLoginInfo: function(value){
  37. this.save('applet.bind', value);
  38. }
  39. //获取登录信息
  40. ,getLoginInfo: function(){
  41. return this.getSave('applet.bind');
  42. }
  43. //获取登录签名串儿
  44. ,getSignature: function(){
  45. return this.getLoginInfo() != null ? this.getLoginInfo().signature : '';
  46. }
  47. //设置服务器传过来的公共值
  48. ,setSetting : function(value)
  49. {
  50. this.setting = value;
  51. this.log('init server setting', this.setting);
  52. if (value.title) {
  53. this.title(value.title);
  54. } else if(value.name) {
  55. this.title(value.name);
  56. }
  57. }
  58. //设置模板变量
  59. ,set : function(self, key, value)
  60. {
  61. var data = {};
  62. data[key] = value;
  63. this.sets(self, data);
  64. }
  65. //设置模板变量
  66. ,setList: function (self, value) {
  67. this.set(self, 'list', value);
  68. }
  69. //设置模板变量
  70. ,sets: function (self, data) {
  71. this.log('data', data);
  72. self.setData(data);
  73. }
  74. //列表分页追加
  75. , appendList: function (self, value) {
  76. var list = self.data.list;
  77. for(var i=0; i<value.length; i++){
  78. list.push(value[i]);
  79. }
  80. this.setList(self, list);
  81. }
  82. //设置模板变量
  83. , setView: function (self, value) {
  84. this.set(self, 'view', value);
  85. }
  86. //获取包
  87. ,package : function(name)
  88. {
  89. return require('package/' + name + '.js');
  90. }
  91. //html解析
  92. , html: function (bindName, data, target)
  93. {
  94. var wxParse = this.package('wxParse/wxParse');
  95. wxParse.wxParse(bindName, 'html', data, target, 0);
  96. }
  97. //获取请求
  98. ,request : function(url, param, callback, method)
  99. {
  100. if (!method) {
  101. method = 'get';
  102. }
  103. return this.package('network').request(this, url, param, callback, method);
  104. }
  105. //提示框
  106. ,alert : function(msg, title)
  107. {
  108. wx.showToast({
  109. title: msg,
  110. icon: 'none',
  111. duration: 3000
  112. });
  113. }
  114. //跳转
  115. ,location : function(url)
  116. {
  117. this.log('location', url);
  118. wx.navigateTo({
  119. url: '../../template/' + url,
  120. })
  121. }
  122. /**
  123. * 重定向页面
  124. */
  125. ,redirect: function(url){
  126. this.log('redirect', url);
  127. wx.redirectTo({
  128. url: '../../template/' + url,
  129. })
  130. }
  131. //回退 delta 返回的页面数,如果 delta 大于现有页面数,则返回到首页。
  132. ,goBack: function(delta){
  133. if(typeof delta != 'number') delta = 1;
  134. wx.navigateBack({
  135. delta: delta
  136. })
  137. }
  138. //log
  139. ,log : function(title, msg)
  140. {
  141. if (this.config.debug) {
  142. console.log('dever debug -- ' + title, msg);
  143. }
  144. }
  145. //开始下拉刷新
  146. ,startPullDown : function(self)
  147. {
  148. wx.showNavigationBarLoading();
  149. self.onLoad();
  150. }
  151. //停止下拉刷新
  152. ,stopPullDown: function () {
  153. wx.hideNavigationBarLoading();
  154. wx.stopPullDownRefresh();
  155. }
  156. //加载购物车
  157. ,carts: function(self, save, path)
  158. {
  159. this.package('carts').load(self, save, path);
  160. }
  161. //显示加载中...
  162. ,showLoading: function(){
  163. wx.showLoading({
  164. title: '加载中',
  165. mask: true
  166. })
  167. }
  168. //隐藏加载中...
  169. ,hideLoading: function(){
  170. wx.hideLoading();
  171. }
  172. //检查用户是否登录状态,暂支持微信登录
  173. ,isLogin: function(fun){
  174. wx.checkSession({
  175. success: function(res){
  176. fun(true);
  177. },
  178. fail: function(res){
  179. fun(false);
  180. }
  181. })
  182. }
  183. }
  184. dever.init();
  185. module.exports = {
  186. load: dever
  187. }