app.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. //引入公共方法
  2. const dever = require('./dever/core.js');
  3. App({
  4. /**
  5. * 保存邀请人id
  6. */
  7. saveInviteUid(uid){
  8. dever.save('invite_uid', uid);
  9. },
  10. /**
  11. * 获取邀请人id
  12. */
  13. getInviteUid(){
  14. let inviteUid = dever.getSave('invite_uid')
  15. return inviteUid == null ? '': inviteUid;
  16. },
  17. /**
  18. * 判断是否登录状态
  19. */
  20. isLogin: function (){
  21. return dever.getSign() == '' ? false : true;
  22. },
  23. /**
  24. * 判断是否授权
  25. */
  26. isAuth: function(scope, fun){
  27. wx.getSetting({
  28. withCredentials: true,
  29. success(res) {
  30. if (res.authSetting[scope]) {
  31. fun(true);
  32. } else {
  33. fun(false)
  34. }
  35. }
  36. })
  37. },
  38. /**
  39. * 获取用户id
  40. */
  41. getUid: function(){
  42. return dever.getUid();
  43. },
  44. getLoginInfo: function(){
  45. return dever.getLoginInfo();
  46. },
  47. /**
  48. * 设置模板变量[单个]
  49. */
  50. set: function (self, key, value) {
  51. var data = {};
  52. data[key] = value;
  53. this.sets(self, data);
  54. },
  55. /**
  56. * 设置模板变量[多个]
  57. */
  58. sets: function (self, data) {
  59. self.setData(data);
  60. },
  61. /**
  62. * 设置模板变量[列表]
  63. */
  64. setList: function (self, value, isAppend) {
  65. if (isAppend) {
  66. let list = self.data.list;
  67. for (var i = 0; i < value.length; i++) {
  68. list.push(value[i]);
  69. }
  70. this.set(self, 'list', list);
  71. } else {
  72. this.set(self, 'list', value);
  73. }
  74. },
  75. /**
  76. * 获取模板里传过来的data
  77. */
  78. getData: function (event) {
  79. return event.currentTarget.dataset;
  80. },
  81. /**
  82. * Toast提示框 3s消失
  83. */
  84. toast: function (msg, title) {
  85. wx.showToast({
  86. title: msg,
  87. icon: 'none',
  88. duration: 3000
  89. });
  90. },
  91. /**
  92. * 解析html
  93. */
  94. html: function (bindName, data, target) {
  95. var wxParse = dever.package('wxParse/wxParse');
  96. wxParse.wxParse(bindName, 'html', data, target, 0);
  97. },
  98. /**
  99. * 跳转页面
  100. */
  101. location: function (url) {
  102. dever.log('location', url);
  103. wx.navigateTo({
  104. url: '../../template/' + url,
  105. })
  106. },
  107. /**
  108. * 重定向页面
  109. */
  110. redirect: function (url) {
  111. dever.log('redirect', url);
  112. wx.redirectTo({
  113. url: '../../template/' + url,
  114. })
  115. },
  116. /**
  117. * 回退 delta 返回的页面数,如果 delta 大于现有页面数,则返回到首页
  118. */
  119. goBack: function (delta) {
  120. if (typeof delta != 'number') delta = 1;
  121. wx.navigateBack({
  122. delta: delta
  123. })
  124. },
  125. /**
  126. * 开始下拉刷新
  127. */
  128. startPullDown: function (self) {
  129. wx.showNavigationBarLoading();
  130. self.onLoad();
  131. },
  132. /**
  133. * 停止下拉刷新
  134. */
  135. stopPullDown: function () {
  136. wx.hideNavigationBarLoading();
  137. wx.stopPullDownRefresh();
  138. },
  139. /**
  140. * 显示加载中[数据交互动画]
  141. */
  142. showLoading: function () {
  143. wx.showLoading({
  144. title: '加载中',
  145. mask: true
  146. })
  147. },
  148. /**
  149. * 隐藏加载中
  150. */
  151. hideLoading: function () {
  152. wx.hideLoading();
  153. },
  154. /**
  155. * http请求
  156. * url 请求路径
  157. * param 请求参数 {}
  158. * callback 回调方法 {success fail complete}
  159. * method 请求方式 OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
  160. */
  161. request: function (url, param, callback, method){
  162. if (!param) param = {};
  163. //是否返回json格式的数据1为返回2为不返回
  164. param['json'] = 1;
  165. //用于登录后的uid验证,每次都要传入
  166. param['signature'] = dever.getSign();
  167. var that = this;
  168. console.log(url, param);
  169. dever.package('network').request(dever, url, param, {
  170. success: function(res){
  171. if (res.data.status == 1) {
  172. let data = res.data.data;
  173. if (data.config) {
  174. dever.setSetting(data.config);
  175. }
  176. if (url == 'passport.applet.bind'){
  177. dever.saveLoginInfo(data);
  178. }
  179. callback.success(data, res);
  180. } else {
  181. callback.fail(res);
  182. }
  183. },
  184. fail: function(res){
  185. callback.fail(res);
  186. },
  187. complete: function(res){
  188. if(callback.complete){
  189. callback.complete(res);
  190. }
  191. that.stopPullDown();
  192. dever.log('request ' + url, res);
  193. }
  194. }, method);
  195. }
  196. })