app.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // var kyjplugin = requirePlugin("kyjplugin");
  2. // let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
  3. // var uniacid = JSON.stringify(extConfig) != "{}" ? extConfig.uniacid : '1';
  4. // wx.setStorageSync("store_id", uniacid);
  5. // console.log('uniacid:' + uniacid);
  6. var siteinfo = require("siteinfo.js"),
  7. _api_root = "";
  8. let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {};
  9. var uniacid = JSON.stringify(extConfig) != "{}" ? extConfig.uniacid : '1';
  10. siteinfo.uniacid = uniacid;
  11. siteinfo.acid = uniacid;
  12. wx.setStorageSync("store_id", uniacid);
  13. console.log('uniacid:' + uniacid);
  14. if (-1 != siteinfo.acid) {
  15. var siteroot = siteinfo.siteroot.substr(0, siteinfo.siteroot.indexOf("app/index.php"));
  16. _api_root = _api_root = siteroot + "core/web/index.php?_acid=" + siteinfo.acid + "&r=api/"
  17. } else _api_root = siteinfo.apiroot;
  18. App({
  19. onLaunch: function (options) {
  20. // kyjplugin._init(options);
  21. var that = this
  22. console.log('App Launch')
  23. var logs = wx.getStorageSync('logs') || []
  24. logs.unshift(Date.now())
  25. wx.setStorageSync('logs', logs)
  26. wx.login({
  27. success: res => {
  28. }
  29. })
  30. wx.getSetting({
  31. success: res => {
  32. if (res.authSetting['scope.userInfo']) {
  33. wx.getUserInfo({
  34. success: res => {
  35. this.globalData.userInfo = res.userInfo
  36. if (this.userInfoReadyCallback) {
  37. this.userInfoReadyCallback(res)
  38. }
  39. }
  40. })
  41. }
  42. }
  43. })
  44. },
  45. onShow: function () {
  46. // kyjplugin._start();
  47. console.log('App Show')
  48. },
  49. onHide: function () {
  50. console.log('App Hide')
  51. },
  52. login: require("utils/login.js"),
  53. request1: require("utils/request.js"),
  54. request: function (object) {
  55. var token = wx.getStorageSync("token");
  56. var user = wx.getStorageSync("user");
  57. if (token) {
  58. if (!object.data) {
  59. object.data = {};
  60. }
  61. };
  62. wx.request({
  63. url: object.url,
  64. header: object.header || {
  65. // 'content-type': 'application/x-www-form-urlencoded',
  66. 'Accept': 'application/json',
  67. 'Authorization': 'Bearer ' + token
  68. },
  69. data: object.data || {},
  70. method: object.method || "GET",
  71. dataType: object.dataType || "json",
  72. success: function (res) {
  73. if (res.data.status_code == 401) {
  74. getApp().login();
  75. } else {
  76. if (object.success)
  77. object.success(res);
  78. };
  79. },
  80. fail: function (res) {
  81. if (object.fail)
  82. object.fail(res);
  83. },
  84. complete: function (res) {
  85. if (object.complete)
  86. object.complete(res);
  87. }
  88. });
  89. },
  90. login: function () {
  91. wx.login({
  92. success: function (res) {
  93. if (res.code) {
  94. var code = res.code;
  95. wx.getUserInfo({
  96. success: function (res) {
  97. console.log(res);
  98. getApp().request({
  99. url: server + "/api/auth/login",
  100. method: "post",
  101. data: {
  102. code: code,
  103. nickname: res.userInfo.nickName,
  104. avatar: res.userInfo.avatarUrl
  105. },
  106. success: function (res) {
  107. if (res.data.status_code == 0) {
  108. wx.setStorageSync("token", res.data.data.token);
  109. wx.setStorageSync("user", {
  110. nickname: res.data.data.user.nickname,
  111. avatar: res.data.data.user.headimgurl,
  112. });
  113. } else {
  114. }
  115. },
  116. fail: function (res) {
  117. }
  118. });
  119. },
  120. fail: function (res) {
  121. wx.showModal({
  122. title: '用户未授权',
  123. content: '如需正常使用功能,请按确定并在授权管理中选中“用户信息”,然后点按确定。最后再重新进入小程序即可正常使用。',
  124. success: function (res) {
  125. if (res.confirm) {
  126. wx.openSetting({
  127. success: function success(res) {
  128. console.log('openSetting success', res.authSetting);
  129. }
  130. });
  131. }
  132. }
  133. })
  134. }
  135. });
  136. } else {
  137. }
  138. },
  139. fail: function (res) {
  140. }
  141. });
  142. },
  143. siteInfo: require("siteinfo.js"),
  144. utils: require("utils/utils.js"),
  145. globalData: {
  146. hasLogin: false,
  147. store_id: uniacid,
  148. userInfo: null
  149. }
  150. });