index.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // pages/index/index.js
  2. import { wxSetSessionKey } from "../../utils/wxUtils";
  3. import { postApi } from "../../apis/api";
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. loginStatus: false,
  10. },
  11. async login() {
  12. if (!wx.getStorageSync("openId")) {
  13. wx.showToast({
  14. title: "正在初始化登录信息...",
  15. icon: "none",
  16. duration: 1500,
  17. });
  18. wxSetSessionKey();
  19. return;
  20. }
  21. wx.showLoading({
  22. title: "正在登录...",
  23. });
  24. let { data } = await postApi("/login/openid", {
  25. openId: wx.getStorageSync("openId"),
  26. });
  27. if (data.status == 0) {
  28. let obj = {
  29. ...data.result.userInfo,
  30. ...data.result.shipInfo,
  31. };
  32. Object.keys(obj).forEach(function (key) {
  33. wx.setStorageSync(key, obj[key]);
  34. });
  35. wx.setStorageSync("accessToken", data.result.tokenInfo.tokenValue);
  36. } else {
  37. wx.showToast({
  38. title: data.msg,
  39. icon: "none",
  40. duration: 1500,
  41. });
  42. }
  43. setTimeout(() => {
  44. wx.hideLoading();
  45. wx.switchTab({
  46. url: "/pages/takePhoto/takePhoto",
  47. });
  48. }, 1000);
  49. },
  50. onLoad: function (options) {
  51. if (wx.getStorageSync("userId") && wx.getStorageSync("shipName")) {
  52. this.setData({
  53. loginStatus: true,
  54. });
  55. }
  56. },
  57. onShow() {
  58. let v = wx.getAccountInfoSync();
  59. if (v.miniProgram.envVersion != "release") {
  60. wx.showToast({
  61. title: `当前环境:${
  62. v.miniProgram.envVersion == "develop" ? "开发版" : "体验版"
  63. }`,
  64. icon: "none",
  65. duration: 1000,
  66. });
  67. }
  68. },
  69. /**
  70. * 用户点击右上角分享
  71. */
  72. onShareAppMessage: function () {},
  73. });