cert.js 5.6 KB

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