app.js 2.3 KB

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