app.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. //app.js
  2. App({
  3. onLaunch: function () {
  4. // 展示本地存储能力
  5. var logs = wx.getStorageSync('logs') || []
  6. logs.unshift(Date.now())
  7. wx.setStorageSync('logs', logs)
  8. var that = this;
  9. // 登录
  10. wx.showLoading({
  11. title: '加载中...',
  12. mask: true,
  13. })
  14. wx.login({
  15. success: res => {
  16. // 发送 res.code 到后台换取 openId, sessionKey, unionId
  17. if (res.code) {
  18. that.globalData.code = res.code;
  19. //获取openId
  20. // 需要放到服务端去请求
  21. try {
  22. wx.request({
  23. url: that.globalData.contentpath + 'api/train/appAuth?js_code=' + res.code,
  24. method: 'GET',
  25. header: { 'content-type': 'application/json' },
  26. success: function (result) {
  27. console.log(result);
  28. if (result.statusCode === 200) {
  29. const res = result.data;
  30. that.globalData.openid = res.openid;
  31. that.globalData.userInfo = res.user;
  32. } else {
  33. wx.showToast({ title: '未请求到用户', icon: 'none', duration: 0, mask: true })
  34. }
  35. },
  36. fail: function (error) {
  37. wx.showToast({ title: '未请求到用户', icon: 'none', duration: 0, mask: true })
  38. console.info("获取用户openId失败");
  39. console.info(error);
  40. },
  41. complete() {
  42. wx.hideLoading()
  43. },
  44. })
  45. } catch (error) {
  46. wx.showToast({
  47. title: '用户信息无效',
  48. icon: "none"
  49. })
  50. }
  51. }
  52. }
  53. })
  54. },
  55. globalData: {
  56. userInfo: null,
  57. openid: null,
  58. unionid: null,
  59. contentpath: 'https://jytz.jilinjobs.cn/', //http://127.0.0.1:2001/
  60. cookie: null,
  61. code: null,
  62. }
  63. })