cert.js 6.0 KB

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