| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- // pages/index/index.js
- import { wxSetSessionKey } from "../../utils/wxUtils";
- import { postApi } from "../../apis/api";
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- loginStatus: false,
- regModalVisible: false,
- bindShipVisible: false,
- userInfo: {},
- shipInfo: {},
- shipName: "", // 新增:船舶名称
- shipMmsi: "", // 新增:船舶MMSI
- },
- async login() {
- if (!wx.getStorageSync("openId")) {
- wx.showToast({
- title: "正在初始化登录信息...",
- icon: "none",
- duration: 500,
- });
- wxSetSessionKey();
- return;
- }
- wx.showLoading({
- title: "正在登录...",
- mask: true,
- });
- let { data } = await postApi("/login/openid", {
- openId: wx.getStorageSync("openId"),
- });
- await new Promise((resolve) => {
- setTimeout(() => {
- resolve();
- }, 500);
- });
- wx.hideLoading();
- if (data.status == 0) {
- let obj = {
- ...data.result.userInfo,
- ...data.result.shipInfo,
- };
- Object.keys(obj).forEach(function (key) {
- wx.setStorageSync(key, obj[key]);
- });
- wx.setStorageSync(
- "accessToken",
- data?.result?.tokenInfo?.tokenValue || ""
- );
- wx.switchTab({
- url: "/pages/takePhoto/takePhoto",
- });
- } else {
- if (data?.result?.userInfo?.userId) {
- wx.setStorageSync(
- "accessToken",
- data?.result?.tokenInfo?.tokenValue || ""
- );
- wx.showModal({
- title: "提示",
- content: "您已注册但尚未绑定船舶,去绑定?",
- confirmText: "去绑定",
- cancelText: "取消",
- success: (res) => {
- // 使用箭头函数修正this指向
- if (res.confirm) {
- this.setData({
- bindShipVisible: true,
- });
- }
- },
- });
- } else {
- this.setData({
- regModalVisible: true,
- });
- }
- }
- },
- async init() {
- let { data } = await postApi("/login/openid", {
- openId: wx.getStorageSync("openId"),
- });
- },
- hideRegModal() {
- this.setData({
- regModalVisible: false,
- });
- },
- async goToRegister(e) {
- try {
- if (e.detail.errMsg == "getPhoneNumber:ok") {
- wx.showLoading({
- title: "正在登录...",
- mask: true,
- });
- let { data: data1 } = await postApi("/wx/getPhoneNumber", {
- code: e.detail.code,
- });
- console.log("data1", data1);
- if (data1.status === 0) {
- let { phoneNumber: phone } = data1.result;
- // phone = "15036112233";
- let { data: data2 } = await postApi("/login", {
- phone,
- openId: wx.getStorageSync("openId"),
- });
- console.log("data2", data2);
- await new Promise((resolve) => {
- setTimeout(() => {
- wx.hideLoading();
- resolve();
- }, 1500);
- });
- if (data2.status === 0) {
- wx.setStorageSync("phone", phone);
- wx.setStorageSync(
- "accessToken",
- data2?.result?.tokenInfo?.tokenValue || ""
- );
- } else {
- if (data2?.result?.userInfo?.userId) {
- wx.setStorageSync(
- "accessToken",
- data2?.result?.tokenInfo?.tokenValue || ""
- );
- wx.showModal({
- title: "提示",
- content: "您已注册但尚未绑定船舶,去绑定?",
- confirmText: "去绑定",
- cancelText: "取消",
- success: (res) => {
- // 使用箭头函数修正this指向
- if (res.confirm) {
- this.setData({
- bindShipVisible: true,
- regModalVisible: false, // 关闭注册弹窗
- });
- }
- },
- });
- } else {
- wx.showToast({
- title: data2.msg,
- icon: "none",
- duration: 5000,
- });
- }
- this.setData({
- regModalVisible: false,
- });
- }
- } else {
- wx.hideLoading();
- wx.showToast({
- title: "获取手机号失败",
- duration: 5000,
- });
- }
- } else {
- wx.showToast({
- title: "请授权以登录",
- icon: "error",
- });
- }
- } catch (error) {
- wx.showToast({
- title: "系统错误",
- icon: "none",
- duration: 5000,
- });
- console.error("Error:", error);
- }
- },
- onLoad: function (options) {},
- onShow() {
- let v = wx.getAccountInfoSync();
- if (v.miniProgram.envVersion != "release") {
- wx.showToast({
- title: `当前环境:${
- v.miniProgram.envVersion == "develop" ? "开发版" : "体验版"
- }`,
- icon: "none",
- duration: 1000,
- });
- }
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage: function () {},
- // 隐藏绑定船舶弹窗
- async hideBindShipModal() {
- this.setData({
- bindShipVisible: false,
- shipName: "", // 清空输入
- shipMmsi: "", // 清空输入
- });
- },
- // 确认绑定船舶
- async confirmBindShip() {
- const { shipName, shipMmsi } = this.data;
- if (!shipName) {
- wx.showToast({
- title: "请输入船舶名称",
- icon: "none",
- });
- return;
- }
- if (!shipMmsi) {
- wx.showToast({
- title: "请输入船舶MMSI",
- icon: "none",
- });
- return;
- }
- wx.showLoading({
- title: "正在绑定...",
- mask: true,
- });
- let { data } = await postApi("/register", {
- shipName,
- shipMmsi,
- });
- await new Promise((resolve) => {
- setTimeout(() => {
- resolve();
- }, 1500);
- });
- wx.hideLoading();
- if (data.status === 0) {
- let obj = {
- ...data.result.userInfo,
- ...data.result.shipInfo,
- };
- Object.keys(obj).forEach(function (key) {
- wx.setStorageSync(key, obj[key]);
- });
- wx.setStorageSync(
- "accessToken",
- data?.result?.tokenInfo?.tokenValue || ""
- );
- wx.showToast({
- title: "绑定成功",
- icon: "success",
- duration: 1500,
- });
- await new Promise((resolve) => {
- setTimeout(() => {
- resolve();
- }, 1500);
- });
- wx.switchTab({
- url: "/pages/takePhoto/takePhoto",
- });
- } else {
- wx.showToast({
- title: data.msg,
- icon: "none",
- duration: 5000,
- });
- }
- },
- goToExp() {
- wx.switchTab({
- url: "/pages/takePhoto/takePhoto",
- });
- },
- });
|