酷炫小程序

app.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //app.js
  2. var http = require('http/request.js');
  3. var detail = require('http/detail.js');
  4. App({
  5. data: {
  6. loginInfo: null,
  7. hasLogin:false,
  8. userId:'',
  9. channel:"blackWhite",
  10. code:'',
  11. shareMsg: "找我,帮你省钱😝",
  12. failed: false,
  13. shareTicket: "",
  14. winHeight:''
  15. },
  16. onLaunch: function () {
  17. // 展示本地存储能力
  18. this.windowInfo();
  19. },
  20. //页面现实加载,获取群信息
  21. onShow: function (options) {
  22. this.data.shareTicket = options.shareTicket;
  23. },
  24. //获取系统消息
  25. windowInfo: function () {
  26. var that = this;
  27. wx.getSystemInfo({
  28. success: function (res) {
  29. that.data.winHeight=res.windowHeight
  30. }
  31. });
  32. },
  33. //检测当前用户登录态是否有效
  34. checkSession(info,callback) {
  35. var that = this;
  36. if (this.data.hasLogin == true) {
  37. callback();
  38. return;
  39. }
  40. wx.checkSession({
  41. success: function () {
  42. that.login(info,callback);
  43. },
  44. fail: function () {
  45. //登录态过期
  46. console.log("过期了")
  47. that.login(info,callback)//重新登录;
  48. }
  49. })
  50. },
  51. // 登录
  52. login(info,callback) {
  53. var that = this;
  54. wx.login({
  55. success: res => {
  56. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  57. if (res.code) {
  58. //发起网络请求
  59. that.data.loginInfo = Object.assign({code:res.code}, info)
  60. that.data.code = res.code;
  61. that.hostLogin(callback);
  62. } else {
  63. console.log("获取用户登录态失败!" + res.errMsg)
  64. that.login(info,callback);
  65. }
  66. }
  67. })
  68. },
  69. hostLogin(callback) {
  70. var that = this;
  71. wx.setStorageSync('flagElasticLayer', '0')
  72. wx.setStorageSync('userInfo', that.data.loginInfo)
  73. callback();
  74. // wx.request({
  75. // url: 'http://yhq.quyaqu.com/api/mini_program/login',
  76. // method: 'post',
  77. // header: { 'Content-Type': 'application/json' },
  78. // success: function (res) {
  79. // console.log(res)
  80. // },
  81. // fail: function (res) {
  82. // console.log(res)
  83. // }
  84. // })
  85. // this.func.req('/user/loginByWechatApplet', 'get', {}, this.data.loginInfo, function (res) {
  86. // console.log(res)
  87. // if (res.code == 0) {
  88. // that.data.userId=res.data.userId;
  89. // wx.setStorage({
  90. // key: "userId",
  91. // data: res.data.userId
  92. // })
  93. // that.data.userId = res.data.userId;
  94. that.data.hasLogin = true;
  95. // }
  96. // })
  97. },
  98. globalData: {
  99. userInfo: null
  100. },
  101. func: {
  102. req: http.req
  103. },
  104. detailFunc: {
  105. detail: detail.detail
  106. },
  107. //跳转详情页
  108. goToDetail: function (e,zuId) {
  109. var goods_id = e.currentTarget.dataset.goods_id;
  110. var name = e.currentTarget.dataset.name;
  111. var origin = 0;
  112. if (e.currentTarget.dataset.origin !== undefined) {
  113. origin = e.currentTarget.dataset.origin
  114. }
  115. var category_id = zuId;
  116. wx.navigateTo({
  117. url: '/pages/detail/detail?goods_id=' + goods_id + '&goods_name=' + name + '&origin=' + origin + "&category_id=" + category_id,
  118. })
  119. }
  120. })