app.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. data: { code, appid },
  40. success (res) {
  41. wx.setStorage({
  42. data: '',
  43. key: 'token',
  44. })
  45. try { token = res.data.data.token; } catch (err) { };
  46. !!token ? wx.setStorage({
  47. data: 'JWT ' + token,
  48. key: 'token',
  49. }) : '';
  50. let data = res.data.data;
  51. let user = {
  52. lock: !!token,
  53. ...data
  54. }
  55. app.globalData = { ...app.globalData, user };
  56. resolve(token);
  57. }
  58. })
  59. })
  60. }
  61. user(this).then(res => login(this)).then(res => {
  62. if (!!res) {
  63. // this.globalData({})
  64. console.log(this.globalData)
  65. }
  66. }).catch(res => this.globalData.error(res))
  67. },
  68. globalData: {
  69. // 定义tabber
  70. tabber: {
  71. tabberlist: [{
  72. name: '首页',
  73. url: "/pages/index/index", //对应路径
  74. icon: "home-o",//不自定义图片,使用vant的图标
  75. }, {
  76. name: "我的",
  77. url: "/pages/login/login",
  78. src: "",
  79. icon: "friends-o",
  80. }],//跳转链接
  81. active: 0,//默认活动窗口
  82. activeColor: "#1989fa",//选中时的颜色
  83. inactiveColor: "#7d7e80",//未选中时的颜色
  84. },
  85. // 用户登录状态
  86. user: {
  87. lock: false,
  88. name: '未登录',
  89. tel: "",
  90. face: "https://img.yzcdn.cn/vant/cat.jpeg",
  91. },
  92. // 支付时,用户地址
  93. useraddress: {
  94. id: null
  95. },
  96. // 全局请求方法
  97. axios ({ url: _url = '', method: _method = 'post', data: _data = {}, success: _success = () => { }, complete: _complete = () => { } }) {
  98. let globalurl = "http://192.168.2.45:8887", error = (data) => {
  99. console.log(data)
  100. };
  101. return new Promise(resolve => {
  102. wx.request({
  103. url: globalurl + _url,
  104. method: _method,
  105. header: {
  106. 'Content-Type': 'application/x-www-form-urlencoded',
  107. 'Authorization': !wx.getStorageSync('token') ? '' : wx.getStorageSync('token')
  108. },
  109. success (res) {
  110. if (!!res.data.msg) {
  111. error(res.data.msg);
  112. return false;
  113. }
  114. _success(res)
  115. },
  116. data: _data,
  117. fail () {
  118. error("请求失败")
  119. },
  120. complete () {
  121. _complete();
  122. resolve()
  123. }
  124. })
  125. })
  126. },
  127. }
  128. })