app.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. var Towxml = require('/assets/libs/towxml/main.js');
  2. App({
  3. //请将apiUrl换成你的地址,注意/addons/blog/wxapp.不能修改
  4. apiUrl: 'https://demo.fastadmin.net/addons/blog/wxapp.',
  5. si: 0,
  6. //小程序启动
  7. onLaunch: function () {
  8. var that = this;
  9. that.request('/common/init', {}, function (data, ret) {
  10. that.globalData.config = data.config;
  11. that.globalData.tabList = data.tabList;
  12. //如果需要一进入小程序就要求授权登录,可在这里发起调用
  13. //that.check(function (ret) { });
  14. }, function (data, ret) {
  15. that.error(ret.msg);
  16. });
  17. },
  18. //判断是否登录
  19. check: function (cb) {
  20. var that = this;
  21. if (this.globalData.userInfo) {
  22. typeof cb == "function" && cb(this.globalData.userInfo);
  23. } else {
  24. this.login(cb);
  25. }
  26. },
  27. //登录
  28. login: function (cb) {
  29. var that = this;
  30. var token = wx.getStorageSync('token') || '';
  31. //调用登录接口
  32. wx.login({
  33. success: function (res) {
  34. if (res.code) {
  35. //发起网络请求
  36. wx.getUserInfo({
  37. success: function (ures) {
  38. console.log(ures);
  39. that.globalData.userInfo = ures.userInfo;
  40. typeof cb == "function" && cb(that.globalData.userInfo);
  41. },
  42. fail: function (res) {
  43. that.showLoginModal(cb);
  44. }
  45. });
  46. } else {
  47. that.showLoginModal(cb);
  48. }
  49. }
  50. });
  51. },
  52. //显示登录或授权提示
  53. showLoginModal: function (cb) {
  54. var that = this;
  55. if (!that.globalData.userInfo) {
  56. //获取用户信息
  57. wx.getSetting({
  58. success: function (sres) {
  59. if (sres.authSetting['scope.userInfo']) {
  60. wx.showModal({
  61. title: '温馨提示',
  62. content: '当前无法获取到你的个人信息,部分操作可能受到限制',
  63. confirmText: "重新登录",
  64. cancelText: "暂不登录",
  65. success: function (res) {
  66. if (res.confirm) {
  67. that.login(cb);
  68. } else {
  69. console.log('用户暂不登录');
  70. }
  71. }
  72. });
  73. } else {
  74. wx.showModal({
  75. title: '温馨提示',
  76. content: '当前无法获取到你的个人信息,部分操作可能受到限制',
  77. confirmText: "去授权",
  78. cancelText: "暂不授权",
  79. success: function (res) {
  80. if (res.confirm) {
  81. wx.openSetting({
  82. success: function (sres) {
  83. that.check(cb);
  84. }
  85. });
  86. } else {
  87. console.log('用户暂不授权');
  88. }
  89. }
  90. });
  91. }
  92. }
  93. });
  94. } else {
  95. typeof cb == "function" && cb(that.globalData.userInfo);
  96. }
  97. },
  98. //发起网络请求
  99. request: function (url, data, success, error) {
  100. var that = this;
  101. if (typeof data == 'function') {
  102. success = data;
  103. error = success;
  104. data = {};
  105. }
  106. //移除最前的/
  107. while (url.charAt(0) === '/')
  108. url = url.slice(1);
  109. this.loading(true);
  110. let cookie = wx.getStorageSync('cookieKey');
  111. let header = {
  112. "Content-Type": "application/x-www-form-urlencoded"
  113. };
  114. if (cookie) {
  115. header.Cookie = cookie;
  116. }
  117. if (this.globalData.__token__) {
  118. data.__token__ = this.globalData.__token__;
  119. }
  120. data._ajax = 1;
  121. wx.request({
  122. url: this.apiUrl + url,
  123. data: data,
  124. method: 'post',
  125. header: header,
  126. success: function (res) {
  127. that.loading(false);
  128. var code, msg, json;
  129. if (res && res.header) {
  130. if (res.header['Set-Cookie']) {
  131. wx.setStorageSync('cookieKey', res.header['Set-Cookie']); //保存Cookie到Storage
  132. }
  133. if (res.header['__token__']) {
  134. that.globalData.__token__ = res.header['__token__'];
  135. }
  136. }
  137. if (res.statusCode === 200) {
  138. json = res.data;
  139. if (json.code === 1) {
  140. typeof success === 'function' && success(json.data, json);
  141. } else {
  142. typeof error === 'function' && error(json.data, json);
  143. }
  144. } else {
  145. json = typeof res.data === 'object' ? res.data : { code: 0, msg: '发生一个未知错误', data: null };
  146. typeof error === 'function' && error(json.data, json);
  147. }
  148. },
  149. fail: function (res) {
  150. that.loading(false);
  151. console.log("fail:", res);
  152. typeof error === 'function' && error(null, { code: 0, msg: '', data: null });
  153. }
  154. });
  155. },
  156. //文本提示
  157. info: function (msg, cb) {
  158. wx.showToast({
  159. title: msg,
  160. icon: 'none',
  161. duration: 2000,
  162. complete: function () {
  163. typeof cb == "function" && cb();
  164. }
  165. });
  166. },
  167. //成功提示
  168. success: function (msg, cb) {
  169. wx.showToast({
  170. title: msg,
  171. icon: 'success',
  172. image: '/assets/images/ok.png',
  173. duration: 2000,
  174. complete: function () {
  175. typeof cb == "function" && cb();
  176. }
  177. });
  178. },
  179. //错误提示
  180. error: function (msg, cb) {
  181. wx.showToast({
  182. title: msg,
  183. image: '/assets/images/error.png',
  184. duration: 2000,
  185. complete: function () {
  186. typeof cb == "function" && cb();
  187. }
  188. });
  189. },
  190. //警告提示
  191. warning: function (msg, cb) {
  192. wx.showToast({
  193. title: msg,
  194. image: '/assets/images/warning.png',
  195. duration: 2000,
  196. complete: function () {
  197. typeof cb == "function" && cb();
  198. }
  199. });
  200. },
  201. //Loading
  202. loading: function (msg) {
  203. if (typeof msg == 'boolean') {
  204. if (!msg) {
  205. if (!this.si) {
  206. return;
  207. }
  208. clearTimeout(this.si);
  209. wx.hideLoading({});
  210. return;
  211. }
  212. }
  213. msg = typeof msg == 'undefined' || typeof msg == 'boolean' ? '加载中' : msg;
  214. this.globalData.loading = true;
  215. if (this.si) {
  216. return;
  217. }
  218. this.si = setTimeout(function () {
  219. wx.showLoading({
  220. title: msg
  221. });
  222. }, 300);
  223. },
  224. towxml: new Towxml(),
  225. //全局信息
  226. globalData: {
  227. userInfo: null,
  228. config: null,
  229. __token__: null,
  230. tabList: [],
  231. }
  232. })