app.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //app.js
  2. import {
  3. getMpOpenId
  4. } from "api/home"
  5. App({
  6. onLaunch: function () {
  7. // 展示本地存储能力
  8. var logs = wx.getStorageSync('logs') || []
  9. logs.unshift(Date.now())
  10. wx.setStorageSync('logs', logs)
  11. // 登录
  12. wx.login({
  13. success: res => {
  14. // console.log("code:"+res.code);
  15. let code = res.code;
  16. // console.log("app.js code="+res.code);
  17. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  18. getMpOpenId({
  19. code: code,
  20. stationId:1
  21. }).then(res => { // handle success
  22. // console.log("app.js openid:"+res.openid);
  23. // console.log("app.js unoiid " + res.unionid)
  24. this.mpOpenId = res.openid;
  25. this.globalData.unionId = res.unionId
  26. this.unionId = res.unionid
  27. // console.log(this.globalData)
  28. // console.log(this)
  29. }).catch(error => { // handle error
  30. console.log(error);
  31. })
  32. }
  33. })
  34. // 获取用户信息
  35. wx.getSetting({
  36. success: res => {
  37. if (res.authSetting['scope.userInfo']) {
  38. // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
  39. wx.getUserInfo({
  40. success: res => {
  41. // 可以将 res 发送给后台解码出 unionId
  42. this.globalData.userInfo = res.userInfo
  43. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  44. // 所以此处加入 callback 以防止这种情况
  45. if (this.userInfoReadyCallback) {
  46. this.userInfoReadyCallback(res)
  47. }
  48. }
  49. })
  50. }
  51. }
  52. })
  53. },
  54. globalData: {
  55. mpVersion:null,//小程序当前是开发版?体验版?正式版?
  56. userInfo: null, //用户信息
  57. phoneNumber:null,//用户手机号
  58. mpOpenId: null,
  59. unionId:null,
  60. appId:'wxe1135cd390b38a54',//小程序的appid
  61. stationId: null, //加油站ID
  62. stationName: null, //油站名称
  63. picUrl: null, //油站图片URL
  64. mno: null, //油站绑定的商户号
  65. oilGunNum: null, //油枪号
  66. oilGunName: null, //油品名
  67. oilPrice: null, //油品价格
  68. orderNo: null, //订单号
  69. orderAmount: null, //订单金额
  70. manJianAmount: null, //满减金额
  71. liJianAmount: null, //立减金额
  72. youHuiAmount: null, //优惠券金额
  73. payAmount: null //实付金额
  74. }
  75. })