index.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. regModalVisible: false,
  11. bindShipVisible: false,
  12. userInfo: {},
  13. shipInfo: {},
  14. shipName: "", // 新增:船舶名称
  15. shipMmsi: "", // 新增:船舶MMSI
  16. },
  17. async login() {
  18. if (!wx.getStorageSync("openId")) {
  19. wx.showToast({
  20. title: "正在初始化登录信息...",
  21. icon: "none",
  22. duration: 500,
  23. });
  24. wxSetSessionKey();
  25. return;
  26. }
  27. wx.showLoading({
  28. title: "正在登录...",
  29. mask: true,
  30. });
  31. let { data } = await postApi("/login/openid", {
  32. openId: wx.getStorageSync("openId"),
  33. });
  34. await new Promise((resolve) => {
  35. setTimeout(() => {
  36. resolve();
  37. }, 500);
  38. });
  39. wx.hideLoading();
  40. if (data.status == 0) {
  41. let obj = {
  42. ...data.result.userInfo,
  43. ...data.result.shipInfo,
  44. };
  45. Object.keys(obj).forEach(function (key) {
  46. wx.setStorageSync(key, obj[key]);
  47. });
  48. wx.setStorageSync(
  49. "accessToken",
  50. data?.result?.tokenInfo?.tokenValue || ""
  51. );
  52. wx.switchTab({
  53. url: "/pages/takePhoto/takePhoto",
  54. });
  55. } else {
  56. if (data?.result?.userInfo?.userId) {
  57. wx.setStorageSync(
  58. "accessToken",
  59. data?.result?.tokenInfo?.tokenValue || ""
  60. );
  61. wx.showModal({
  62. title: "提示",
  63. content: "您已注册但尚未绑定船舶,去绑定?",
  64. confirmText: "去绑定",
  65. cancelText: "取消",
  66. success: (res) => {
  67. // 使用箭头函数修正this指向
  68. if (res.confirm) {
  69. this.setData({
  70. bindShipVisible: true,
  71. });
  72. }
  73. },
  74. });
  75. } else {
  76. this.setData({
  77. regModalVisible: true,
  78. });
  79. }
  80. }
  81. },
  82. async init() {
  83. let { data } = await postApi("/login/openid", {
  84. openId: wx.getStorageSync("openId"),
  85. });
  86. },
  87. hideRegModal() {
  88. this.setData({
  89. regModalVisible: false,
  90. });
  91. },
  92. async goToRegister(e) {
  93. try {
  94. if (e.detail.errMsg == "getPhoneNumber:ok") {
  95. wx.showLoading({
  96. title: "正在登录...",
  97. mask: true,
  98. });
  99. let { data: data1 } = await postApi("/wx/getPhoneNumber", {
  100. code: e.detail.code,
  101. });
  102. console.log("data1", data1);
  103. if (data1.status === 0) {
  104. let { phoneNumber: phone } = data1.result;
  105. // phone = "15036112233";
  106. let { data: data2 } = await postApi("/login", {
  107. phone,
  108. openId: wx.getStorageSync("openId"),
  109. });
  110. console.log("data2", data2);
  111. await new Promise((resolve) => {
  112. setTimeout(() => {
  113. wx.hideLoading();
  114. resolve();
  115. }, 1500);
  116. });
  117. if (data2.status === 0) {
  118. wx.setStorageSync("phone", phone);
  119. wx.setStorageSync(
  120. "accessToken",
  121. data2?.result?.tokenInfo?.tokenValue || ""
  122. );
  123. } else {
  124. if (data2?.result?.userInfo?.userId) {
  125. wx.setStorageSync(
  126. "accessToken",
  127. data2?.result?.tokenInfo?.tokenValue || ""
  128. );
  129. wx.showModal({
  130. title: "提示",
  131. content: "您已注册但尚未绑定船舶,去绑定?",
  132. confirmText: "去绑定",
  133. cancelText: "取消",
  134. success: (res) => {
  135. // 使用箭头函数修正this指向
  136. if (res.confirm) {
  137. this.setData({
  138. bindShipVisible: true,
  139. regModalVisible: false, // 关闭注册弹窗
  140. });
  141. }
  142. },
  143. });
  144. } else {
  145. wx.showToast({
  146. title: data2.msg,
  147. icon: "none",
  148. duration: 5000,
  149. });
  150. }
  151. this.setData({
  152. regModalVisible: false,
  153. });
  154. }
  155. } else {
  156. wx.hideLoading();
  157. wx.showToast({
  158. title: "获取手机号失败",
  159. duration: 5000,
  160. });
  161. }
  162. } else {
  163. wx.showToast({
  164. title: "请授权以登录",
  165. icon: "error",
  166. });
  167. }
  168. } catch (error) {
  169. wx.showToast({
  170. title: "系统错误",
  171. icon: "none",
  172. duration: 5000,
  173. });
  174. console.error("Error:", error);
  175. }
  176. },
  177. onLoad: function (options) {},
  178. onShow() {
  179. let v = wx.getAccountInfoSync();
  180. if (v.miniProgram.envVersion != "release") {
  181. wx.showToast({
  182. title: `当前环境:${
  183. v.miniProgram.envVersion == "develop" ? "开发版" : "体验版"
  184. }`,
  185. icon: "none",
  186. duration: 1000,
  187. });
  188. }
  189. },
  190. /**
  191. * 用户点击右上角分享
  192. */
  193. onShareAppMessage: function () {},
  194. // 隐藏绑定船舶弹窗
  195. async hideBindShipModal() {
  196. this.setData({
  197. bindShipVisible: false,
  198. shipName: "", // 清空输入
  199. shipMmsi: "", // 清空输入
  200. });
  201. },
  202. // 确认绑定船舶
  203. async confirmBindShip() {
  204. const { shipName, shipMmsi } = this.data;
  205. if (!shipName) {
  206. wx.showToast({
  207. title: "请输入船舶名称",
  208. icon: "none",
  209. });
  210. return;
  211. }
  212. if (!shipMmsi) {
  213. wx.showToast({
  214. title: "请输入船舶MMSI",
  215. icon: "none",
  216. });
  217. return;
  218. }
  219. wx.showLoading({
  220. title: "正在绑定...",
  221. mask: true,
  222. });
  223. let { data } = await postApi("/register", {
  224. shipName,
  225. shipMmsi,
  226. });
  227. await new Promise((resolve) => {
  228. setTimeout(() => {
  229. resolve();
  230. }, 1500);
  231. });
  232. wx.hideLoading();
  233. if (data.status === 0) {
  234. let obj = {
  235. ...data.result.userInfo,
  236. ...data.result.shipInfo,
  237. };
  238. Object.keys(obj).forEach(function (key) {
  239. wx.setStorageSync(key, obj[key]);
  240. });
  241. wx.setStorageSync(
  242. "accessToken",
  243. data?.result?.tokenInfo?.tokenValue || ""
  244. );
  245. wx.showToast({
  246. title: "绑定成功",
  247. icon: "success",
  248. duration: 1500,
  249. });
  250. await new Promise((resolve) => {
  251. setTimeout(() => {
  252. resolve();
  253. }, 1500);
  254. });
  255. wx.switchTab({
  256. url: "/pages/takePhoto/takePhoto",
  257. });
  258. } else {
  259. wx.showToast({
  260. title: data.msg,
  261. icon: "none",
  262. duration: 5000,
  263. });
  264. }
  265. },
  266. goToExp() {
  267. wx.switchTab({
  268. url: "/pages/takePhoto/takePhoto",
  269. });
  270. },
  271. });