app.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.code);
  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("app.js openid:"+res.openid);
  24. // console.log("app.js unoiid " + res.unionid)
  25. this.mpOpenId = res.openid;
  26. this.globalData.unionId = res.unionId
  27. this.unionId = res.unionid
  28. // console.log(this.globalData)
  29. // console.log(this)
  30. }).catch(error => { // handle error
  31. console.log(error);
  32. })
  33. }
  34. })
  35. // 获取用户信息
  36. wx.getSetting({
  37. success: res => {
  38. if (res.authSetting['scope.userInfo']) {
  39. // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
  40. wx.getUserInfo({
  41. success: res => {
  42. // 可以将 res 发送给后台解码出 unionId
  43. this.globalData.userInfo = res.userInfo
  44. // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
  45. // 所以此处加入 callback 以防止这种情况
  46. if (this.userInfoReadyCallback) {
  47. this.userInfoReadyCallback(res)
  48. }
  49. }
  50. })
  51. }
  52. }
  53. })
  54. },
  55. globalData: {
  56. mpVersion:null,//小程序当前是开发版?体验版?正式版?
  57. userInfo: null, //用户信息
  58. phoneNumber:null,//用户手机号
  59. mpOpenId: null,
  60. unionId:null,
  61. appId,
  62. stationId: null, //加油站ID
  63. stationName: null, //油站名称
  64. picUrl: null, //油站图片URL
  65. mno: null, //油站绑定的商户号
  66. oilGunNum: null, //油枪号
  67. oilGunName: null, //油品名
  68. oilPrice: null, //油品价格
  69. orderNo: null, //订单号
  70. orderAmount: null, //订单金额
  71. manJianAmount: null, //满减金额
  72. liJianAmount: null, //立减金额
  73. youHuiAmount: null, //优惠券金额
  74. payAmount: null //实付金额
  75. }
  76. })