sharePage.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // pages/cert/cert.js
  2. import { postApi, getApi } from "../../../apis/api";
  3. Page({
  4. data: {
  5. shipName: "",
  6. mmsi: "",
  7. shipCerts: [],
  8. crewCerts: [],
  9. certs: [],
  10. contacts: [],
  11. introduce: {},
  12. accessToken: wx.getStorageSync("accessToken") || "",
  13. },
  14. async getCerts(token) {
  15. if (!token) {
  16. wx.showToast({
  17. title: "无效的分享链接",
  18. icon: "none",
  19. });
  20. return;
  21. }
  22. let { data } = await postApi("/ship/cert/share/list", {
  23. token,
  24. });
  25. console.log(data);
  26. if (data.status === 0) {
  27. // 处理船舶基本信息
  28. const shipInfo = data.result;
  29. // 处理船舶证书信息
  30. const formattedShipCerts = shipInfo.shipCerts.map((cert) => {
  31. // 获取第一个有效期(如果有多个,默认使用第一个)
  32. const expireDate =
  33. cert.certValidities && cert.certValidities.length > 0
  34. ? cert.certValidities[0].endValidTime
  35. .split(" ")[0]
  36. .replace(/\//g, "-")
  37. : "";
  38. // 处理所有的certFileViewUrls,确保它们都被正确格式化
  39. const formattedViewUrls =
  40. cert.certFileViewUrls && cert.certFileViewUrls.length > 0
  41. ? cert.certFileViewUrls.map((url) =>
  42. url.trim().replace(/` /g, "").replace(/ `/g, "")
  43. )
  44. : ["暂无"];
  45. return {
  46. name: cert.certTypeName,
  47. photoUrls: formattedViewUrls,
  48. photoUrl: formattedViewUrls[0], // 保留单张图片的兼容性
  49. expireDate: expireDate,
  50. };
  51. });
  52. // 处理船员证书信息
  53. const formattedCrewCerts = shipInfo.shipCrewCerts.map((crew) => {
  54. // 处理船员的证书
  55. const crewCertificates = crew.certs.map((cert) => {
  56. // 处理所有的viewUrls,确保它们都被正确格式化
  57. const formattedViewUrls =
  58. cert.viewUrls && cert.viewUrls.length > 0
  59. ? cert.viewUrls.map((url) =>
  60. url.trim().replace(/` /g, "").replace(/ `/g, "")
  61. )
  62. : ["暂无"];
  63. return {
  64. name: cert.docTypeName,
  65. photoUrls: formattedViewUrls,
  66. photoUrl: formattedViewUrls[0], // 保留单张图片的兼容性
  67. expireDate: cert.expiryAt
  68. ? cert.expiryAt.split(" ")[0].replace(/\//g, "-")
  69. : "",
  70. };
  71. });
  72. // 找到医疗报告和服务簿的URL
  73. const medicalReportCert = crew.certs.find((cert) => cert.docType === 2);
  74. const serviceBookCert = crew.certs.find((cert) => cert.docType === 3);
  75. // 处理医疗报告的所有URL
  76. const medicalReportUrls =
  77. medicalReportCert &&
  78. medicalReportCert.viewUrls &&
  79. medicalReportCert.viewUrls.length > 0
  80. ? medicalReportCert.viewUrls.map((url) =>
  81. url.trim().replace(/` /g, "").replace(/ `/g, "")
  82. )
  83. : ["暂无"];
  84. // 处理服务簿的所有URL
  85. const serviceBookUrls =
  86. serviceBookCert &&
  87. serviceBookCert.viewUrls &&
  88. serviceBookCert.viewUrls.length > 0
  89. ? serviceBookCert.viewUrls.map((url) =>
  90. url.trim().replace(/` /g, "").replace(/ `/g, "")
  91. )
  92. : ["暂无"];
  93. return {
  94. name: crew.fullName,
  95. medicalReportUrls: medicalReportUrls,
  96. medicalReport: medicalReportUrls[0], // 保留单张图片的兼容性
  97. serviceBookUrls: serviceBookUrls,
  98. serviceBook: serviceBookUrls[0], // 保留单张图片的兼容性
  99. certificates: crewCertificates,
  100. };
  101. });
  102. this.setData({
  103. shipName: shipInfo.shipName,
  104. mmsi: shipInfo.mmsi,
  105. shipCerts: formattedShipCerts,
  106. crewCerts: formattedCrewCerts,
  107. certs: data.result,
  108. });
  109. } else {
  110. this.setData({
  111. shipCerts: [],
  112. crewCerts: [],
  113. certs: [],
  114. });
  115. wx.showToast({
  116. title: data.msg,
  117. icon: "none",
  118. });
  119. }
  120. },
  121. async getContacts() {
  122. let { data } = await getApi("/ship/cert/operation/contacts");
  123. if (data.status === 0) {
  124. this.setData({
  125. contacts: data.result,
  126. });
  127. } else {
  128. this.setData({
  129. contacts: [],
  130. });
  131. wx.showToast({
  132. title: data.msg,
  133. icon: "none",
  134. });
  135. }
  136. },
  137. async getIntroduce() {
  138. let { data } = await getApi("/ship/cert/operation/introduce");
  139. if (data.status === 0) {
  140. this.setData({
  141. introduce: data.result,
  142. });
  143. } else {
  144. this.setData({
  145. introduce: {},
  146. });
  147. wx.showToast({
  148. title: data.msg,
  149. icon: "none",
  150. });
  151. }
  152. },
  153. makePhoneCall(e) {
  154. wx.makePhoneCall({
  155. phoneNumber: e.currentTarget.dataset.phone,
  156. });
  157. },
  158. previewImage(e) {
  159. const url = e.currentTarget.dataset.url;
  160. const urls = e.currentTarget.dataset.urls;
  161. // 如果是暂无,则不预览
  162. if (url === "暂无") {
  163. return;
  164. }
  165. // 如果提供了多个URL,则使用它们
  166. if (
  167. urls &&
  168. Array.isArray(JSON.parse(urls)) &&
  169. JSON.parse(urls).length > 0
  170. ) {
  171. const urlsArray = JSON.parse(urls);
  172. wx.previewImage({
  173. current: url, // 当前显示的图片
  174. urls: urlsArray,
  175. });
  176. } else {
  177. // 兼容旧版本,只有单张图片
  178. wx.previewImage({
  179. urls: [url],
  180. });
  181. }
  182. },
  183. copy(e) {
  184. wx.setClipboardData({
  185. data: e.currentTarget.dataset.phone,
  186. success(res) {
  187. wx.showToast({
  188. title: "复制成功",
  189. });
  190. },
  191. });
  192. },
  193. onLoad(options) {
  194. this.getCerts(options.token);
  195. // this.getContacts();
  196. // this.getIntroduce();
  197. },
  198. onShow() {},
  199. });