app.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //app.js
  2. import {
  3. getMpOpenId
  4. } from "api/home"
  5. import { appId, stationId } from "./setting"
  6. App({
  7. onLaunch: function () {
  8. // 展示本地存储能力
  9. var logs = wx.getStorageSync('logs') || []
  10. logs.unshift(Date.now())
  11. wx.setStorageSync('logs', logs)
  12. // 登录
  13. wx.login({
  14. success: res => {
  15. // console.log("code:" + res);
  16. let code = res.code;
  17. // console.log("app.js code="+res.code);
  18. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  19. getMpOpenId({
  20. code: code,
  21. stationId
  22. }).then(res => { // handle success
  23. console.log(res)
  24. // console.log("app.js openid:"+res.openid);
  25. // console.log("app.js unoiid " + res.unionid)
  26. this.globalData.openId = res.openId;
  27. this.globalData.unionId = res.unionId;
  28. console.log(this.globalData.unionId)
  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. openId:null,
  59. unionId:null,
  60. appId,
  61. stationId, //加油站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. })