app.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 => wx.lxd.Notify({
  65. message: res,
  66. icon: "warn-o"
  67. }))
  68. },
  69. globalData: {
  70. // 定义tabber
  71. tabber: {
  72. tabberlist: [{
  73. name: '首页',
  74. url: "/pages/index/index", //对应路径
  75. icon: "home-o",//不自定义图片,使用vant的图标
  76. }, {
  77. name: "我的",
  78. url: "/pages/login/login",
  79. src: "",
  80. icon: "friends-o",
  81. }],//跳转链接
  82. active: 0,//默认活动窗口
  83. activeColor: "#1989fa",//选中时的颜色
  84. inactiveColor: "#7d7e80",//未选中时的颜色
  85. },
  86. // 用户登录状态
  87. user: {
  88. lock: false,
  89. name: '未登录',
  90. tel: "",
  91. face: "https://img.yzcdn.cn/vant/cat.jpeg",
  92. },
  93. // 支付时,用户地址
  94. useraddress: {
  95. id: null
  96. },
  97. // 全局请求方法
  98. axios ({ url: _url = '', token: _token = true, method: _method = 'post', data: _data = {}, success: _success = () => { }, complete: _complete = () => { } }) {
  99. let globalurl = "http://192.168.2.45:8887", error = (data) => {
  100. wx.lxd.Notify({
  101. message: data,
  102. icon: "warn-o"
  103. })
  104. };
  105. globalurl = "https://lsr.zzly.vip";
  106. let header_token = {};
  107. if (_token) header_token = { 'Authorization': !wx.getStorageSync('token') ? '' : wx.getStorageSync('token') };
  108. return new Promise(resolve => {
  109. wx.request({
  110. url: globalurl + _url,
  111. method: _method,
  112. header: {
  113. 'Content-Type': 'application/x-www-form-urlencoded',
  114. ...header_token
  115. },
  116. success (res) {
  117. if (!!res.data.msg) {
  118. error(res.data.msg);
  119. return false;
  120. }
  121. _success(res)
  122. },
  123. data: _data,
  124. fail () {
  125. error("请求失败")
  126. },
  127. complete () {
  128. _complete();
  129. resolve()
  130. }
  131. })
  132. })
  133. },
  134. // 用户登录状态
  135. token () {
  136. let state = wx.getStorageSync('token');
  137. if (!!state) return true;
  138. return false
  139. }
  140. }
  141. })