app.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // app.js
  2. App({
  3. onLaunch () {
  4. // 用户默认登录
  5. // 微信小程序流程
  6. function user (app) {
  7. return new Promise((resolve, reject) => {
  8. const accountinfo = wx.getAccountInfoSync();
  9. app.globalData.appid = accountinfo.miniProgram.appId;
  10. // login登录
  11. wx.login({
  12. success: res => {
  13. let code = res.code;
  14. app.globalData.code = code;
  15. // 账号信息
  16. wx.getUserInfo({
  17. success: function (res) {
  18. let { encryptedData, iv, userInfo } = res;
  19. app.globalData.encryptedData = encryptedData;
  20. app.globalData.iv = iv;
  21. app.globalData.avatarUrl = userInfo.avatarUrl;
  22. resolve();
  23. }
  24. })
  25. },
  26. fail: function () {
  27. reject("wx.login获取失败")
  28. },
  29. })
  30. })
  31. }
  32. //用户自动登录
  33. function login (app) {
  34. return new Promise((resolve, reject) => {
  35. let { code, appid } = app.globalData, token = '';
  36. app.globalData.axios({
  37. url: "/account/code2Session/",
  38. method: "post",
  39. token: false,
  40. data: { code, appid },
  41. success (res) {
  42. wx.setStorage({
  43. data: '',
  44. key: 'token',
  45. })
  46. try { token = res.data.data.token; } catch (err) { };
  47. !!token ? wx.setStorage({
  48. data: 'JWT ' + token,
  49. key: 'token',
  50. }) : '';
  51. let data = res.data.data;
  52. let user = {
  53. lock: !!token,
  54. ...data
  55. }
  56. app.globalData = { ...app.globalData, user };
  57. resolve(token);
  58. }
  59. })
  60. })
  61. }
  62. user(this).then(res => login(this)).then(res => {
  63. this.globalData.codeSession = true;
  64. }).catch(res => {
  65. if (wx.hasOwnProperty("lxd") && wx.lxd.hasOwnProprety("Notify")) {
  66. wx.lxd.Notify({
  67. message: res,
  68. icon: "warn-o"
  69. })
  70. } else {
  71. wx.showModal({
  72. confirmText: '确认',
  73. content: res,
  74. showCancel: false,
  75. title: '注意',
  76. })
  77. }
  78. }
  79. )
  80. },
  81. globalData: {
  82. // 定义tabber
  83. tabber: {
  84. tabberlist: [{
  85. name: '我要订餐',
  86. url: "/pages/index/index", //对应路径
  87. icon: "hot-o",//不自定义图片,使用vant的图标
  88. },
  89. {
  90. name: '请假管理',
  91. url: "/pages/leave/leave", //对应路径
  92. icon: "todo-list-o",//不自定义图片,使用vant的图标
  93. },
  94. {
  95. name: "我的",
  96. url: "/pages/login/login",
  97. src: "",
  98. icon: "friends-o",
  99. }],//跳转链接
  100. active: 0,//默认活动窗口
  101. activeColor: "#1989fa",//选中时的颜色
  102. inactiveColor: "#7d7e80",//未选中时的颜色
  103. },
  104. // 用户登录状态
  105. user: {
  106. lock: false,
  107. name: '未登录',
  108. tel: "",
  109. face: "https://img.yzcdn.cn/vant/cat.jpeg",
  110. },
  111. // 支付时,用户地址
  112. useraddress: {
  113. id: null
  114. },
  115. // 广告
  116. hot: true,
  117. // 全局请求方法
  118. axios ({ url: _url = '', token: _token = true, method: _method = 'post', data: _data = {}, success: _success = () => { }, complete: _complete = () => { } }) {
  119. let globalurl = "http://192.168.2.45:8888", error = (data) => {
  120. if (wx.hasOwnProprety("lxd") && wx.lxd.hasOwnProperty("Notify")) {
  121. wx.lxd.Notify({
  122. message: data,
  123. icon: "warn-o"
  124. })
  125. } else {
  126. wx.showModal({
  127. confirmText: '确认',
  128. content: data,
  129. showCancel: false,
  130. title: '注意',
  131. })
  132. }
  133. };
  134. globalurl = "https://lsr.zzly.vip";
  135. let header_token = {};
  136. if (_token) header_token = { 'Authorization': !wx.getStorageSync('token') ? '' : wx.getStorageSync('token') };
  137. return new Promise(resolve => {
  138. wx.request({
  139. url: globalurl + _url,
  140. method: _method,
  141. header: {
  142. 'Content-Type': 'application/x-www-form-urlencoded',
  143. ...header_token
  144. },
  145. success (res) {
  146. if (!!res.data.msg) {
  147. error(res.data.msg);
  148. return false;
  149. }
  150. _success(res)
  151. },
  152. data: _data,
  153. fail () {
  154. error("请求失败")
  155. },
  156. complete () {
  157. _complete();
  158. resolve()
  159. }
  160. })
  161. })
  162. },
  163. // 用户登录状态
  164. token () {
  165. let state = wx.getStorageSync('token');
  166. return !!state;
  167. }
  168. }
  169. })