face-photo.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const api_user = require("../../api/user.js");
  4. const common_config = require("../../common/config.js");
  5. const _sfc_main = {
  6. data() {
  7. return {
  8. mediaSource: "",
  9. // 拍摄的照片或视频路径
  10. isCameraReady: false,
  11. cameraContext: null,
  12. isPageLoaded: false,
  13. // 添加页面加载状态标记
  14. mode: "photo",
  15. // 默认为拍照模式,可选值:photo, video
  16. isRecording: false,
  17. // 是否正在录制视频
  18. recordingTime: 0,
  19. // 录制时间(秒)
  20. recordingTimer: null,
  21. // 录制计时器
  22. photoUrl: ""
  23. // 存储上传后返回的图片URL
  24. };
  25. },
  26. onReady() {
  27. this.cameraContext = common_vendor.index.createCameraContext();
  28. setTimeout(() => {
  29. this.isPageLoaded = true;
  30. }, 100);
  31. },
  32. methods: {
  33. // 切换拍照/录制模式
  34. switchMode(newMode) {
  35. if (this.mediaSource) {
  36. this.retakeMedia();
  37. }
  38. this.mode = newMode;
  39. },
  40. // 处理相机错误
  41. handleCameraError(e) {
  42. console.error("相机错误:", e);
  43. common_vendor.index.showToast({
  44. title: "相机初始化失败,请检查权限设置",
  45. icon: "none"
  46. });
  47. },
  48. // 拍照方法
  49. takePhoto() {
  50. if (!this.cameraContext) {
  51. common_vendor.index.showToast({
  52. title: "相机未就绪",
  53. icon: "none"
  54. });
  55. return;
  56. }
  57. common_vendor.index.showLoading({
  58. title: "拍照中..."
  59. });
  60. this.cameraContext.takePhoto({
  61. quality: "high",
  62. success: (res) => {
  63. this.mediaSource = res.tempImagePath;
  64. common_vendor.index.hideLoading();
  65. },
  66. fail: (err) => {
  67. console.error("拍照失败:", err);
  68. common_vendor.index.hideLoading();
  69. common_vendor.index.showToast({
  70. title: "拍照失败,请重试",
  71. icon: "none"
  72. });
  73. }
  74. });
  75. },
  76. // 开始录制视频
  77. startRecording() {
  78. if (!this.cameraContext) {
  79. common_vendor.index.showToast({
  80. title: "相机未就绪",
  81. icon: "none"
  82. });
  83. return;
  84. }
  85. this.isRecording = true;
  86. this.recordingTime = 0;
  87. this.recordingTimer = setInterval(() => {
  88. this.recordingTime++;
  89. if (this.recordingTime >= 60) {
  90. this.stopRecording();
  91. }
  92. }, 1e3);
  93. this.cameraContext.startRecord({
  94. success: () => {
  95. console.log("开始录制视频");
  96. },
  97. fail: (err) => {
  98. console.error("开始录制失败:", err);
  99. this.isRecording = false;
  100. clearInterval(this.recordingTimer);
  101. common_vendor.index.showToast({
  102. title: "录制失败,请重试",
  103. icon: "none"
  104. });
  105. }
  106. });
  107. },
  108. // 停止录制视频
  109. stopRecording() {
  110. if (!this.isRecording)
  111. return;
  112. clearInterval(this.recordingTimer);
  113. this.isRecording = false;
  114. common_vendor.index.showLoading({
  115. title: "处理中..."
  116. });
  117. this.cameraContext.stopRecord({
  118. success: (res) => {
  119. this.mediaSource = res.tempVideoPath;
  120. console.log(res);
  121. common_vendor.index.hideLoading();
  122. },
  123. fail: (err) => {
  124. console.error("停止录制失败:", err);
  125. common_vendor.index.hideLoading();
  126. common_vendor.index.showToast({
  127. title: "视频保存失败,请重试",
  128. icon: "none"
  129. });
  130. }
  131. });
  132. },
  133. // 格式化时间显示 (秒 -> MM:SS)
  134. formatTime(seconds) {
  135. const minutes = Math.floor(seconds / 60);
  136. const remainingSeconds = seconds % 60;
  137. return `${minutes.toString().padStart(2, "0")}:${remainingSeconds.toString().padStart(2, "0")}`;
  138. },
  139. // 重新拍照或录制
  140. retakeMedia() {
  141. this.mediaSource = "";
  142. this.recordingTime = 0;
  143. },
  144. // 继续流程
  145. continueProcess() {
  146. if (!this.mediaSource) {
  147. common_vendor.index.showToast({
  148. title: "请先完成拍照",
  149. icon: "none"
  150. });
  151. return;
  152. }
  153. common_vendor.index.showLoading({
  154. title: "上传中..."
  155. });
  156. this.submitPhotoUrl();
  157. },
  158. // 上传媒体文件方法 - 不再使用
  159. uploadMedia(callback) {
  160. const openid = JSON.parse(common_vendor.index.getStorageSync("userInfo")).openid || "";
  161. const tenant_id = 1;
  162. if (!openid || !tenant_id) {
  163. common_vendor.index.hideLoading();
  164. common_vendor.index.showToast({
  165. title: "用户信息不完整,请重新登录",
  166. icon: "none"
  167. });
  168. return;
  169. }
  170. common_vendor.index.uploadFile({
  171. url: `${common_config.apiBaseUrl}/api/upload/`,
  172. filePath: this.mediaSource,
  173. name: "file",
  174. formData: {
  175. openid,
  176. tenant_id
  177. },
  178. success: (uploadRes) => {
  179. const res = JSON.parse(uploadRes.data);
  180. if (res.code === 2e3) {
  181. const photoUrl = res.data.url || res.data.photoUrl || "";
  182. if (callback && typeof callback === "function") {
  183. callback(photoUrl);
  184. }
  185. } else {
  186. common_vendor.index.hideLoading();
  187. common_vendor.index.showToast({
  188. title: res.msg || "照片上传失败",
  189. icon: "none"
  190. });
  191. }
  192. },
  193. fail: (err) => {
  194. common_vendor.index.hideLoading();
  195. console.error("上传失败:", err);
  196. common_vendor.index.showToast({
  197. title: "网络错误,请重试",
  198. icon: "none"
  199. });
  200. }
  201. });
  202. },
  203. // 提交照片URL到指定接口 - 修改为直接上传文件
  204. submitPhotoUrl() {
  205. const openid = JSON.parse(common_vendor.index.getStorageSync("userInfo")).openid || "";
  206. const tenant_id = 1;
  207. if (!this.mediaSource) {
  208. common_vendor.index.hideLoading();
  209. common_vendor.index.showToast({
  210. title: "照片信息不完整,请重试",
  211. icon: "none"
  212. });
  213. return;
  214. }
  215. common_vendor.index.uploadFile({
  216. url: `${common_config.apiBaseUrl}/job/upload_posture_photo`,
  217. filePath: this.mediaSource,
  218. name: "photo_file",
  219. // 确保文件参数名称正确
  220. formData: {
  221. // 使用formData传递参数
  222. "application_id": common_vendor.index.getStorageSync("appId"),
  223. "tenant_id": tenant_id,
  224. "openid": openid,
  225. "description": "面部照片"
  226. },
  227. success: (uploadRes) => {
  228. console.log("照片上传成功:", uploadRes);
  229. let result;
  230. try {
  231. if (typeof uploadRes.data === "string") {
  232. result = JSON.parse(uploadRes.data);
  233. } else {
  234. result = uploadRes.data;
  235. }
  236. console.log("解析后的上传结果:", result);
  237. if (result.data && result.data.url) {
  238. this.photoUrl = result.data.url;
  239. }
  240. common_vendor.index.showToast({
  241. title: "照片上传成功",
  242. icon: "success"
  243. });
  244. setTimeout(() => {
  245. common_vendor.index.navigateTo({
  246. url: "/pages/identity-verify/identity-verify",
  247. fail: (err) => {
  248. console.error("页面跳转失败:", err);
  249. common_vendor.index.showToast({
  250. title: "页面跳转失败",
  251. icon: "none"
  252. });
  253. }
  254. });
  255. }, 1500);
  256. } catch (e) {
  257. console.error("解析上传结果失败:", e);
  258. common_vendor.index.showToast({
  259. title: "处理响应失败",
  260. icon: "none"
  261. });
  262. }
  263. },
  264. fail: (err) => {
  265. console.error("照片上传失败:", err);
  266. common_vendor.index.showToast({
  267. title: "照片上传失败,请重试",
  268. icon: "none"
  269. });
  270. },
  271. complete: () => {
  272. common_vendor.index.hideLoading();
  273. }
  274. });
  275. },
  276. updateLocalUserInfo() {
  277. api_user.getUserInfo().then((res) => {
  278. if (res.code === 200 && res.data) {
  279. let userInfo = {};
  280. try {
  281. userInfo = JSON.parse(common_vendor.index.getStorageSync("userInfo") || "{}");
  282. } catch (e) {
  283. console.error("解析本地存储用户信息失败:", e);
  284. userInfo = {};
  285. }
  286. const updatedUserInfo = {
  287. ...userInfo,
  288. ...res.data
  289. };
  290. common_vendor.index.setStorageSync("userInfo", JSON.stringify(updatedUserInfo));
  291. }
  292. }).catch((err) => {
  293. console.error("更新本地用户信息失败:", err);
  294. });
  295. }
  296. }
  297. };
  298. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  299. return common_vendor.e({
  300. a: !$data.mediaSource
  301. }, !$data.mediaSource ? {
  302. b: $data.mode,
  303. c: common_vendor.o((...args) => $options.handleCameraError && $options.handleCameraError(...args))
  304. } : $data.mode === "photo" ? {
  305. e: $data.mediaSource
  306. } : $data.mode === "video" ? {
  307. g: $data.mediaSource
  308. } : {}, {
  309. d: $data.mode === "photo",
  310. f: $data.mode === "video",
  311. h: $data.mode === "video" && $data.isRecording
  312. }, $data.mode === "video" && $data.isRecording ? {
  313. i: common_vendor.t($options.formatTime($data.recordingTime))
  314. } : {}, {
  315. j: !$data.mediaSource
  316. }, !$data.mediaSource ? common_vendor.e({
  317. k: $data.mode === "photo"
  318. }, $data.mode === "photo" ? {
  319. l: common_vendor.o((...args) => $options.takePhoto && $options.takePhoto(...args))
  320. } : $data.mode === "video" && !$data.isRecording ? {
  321. n: common_vendor.o((...args) => $options.startRecording && $options.startRecording(...args))
  322. } : $data.mode === "video" && $data.isRecording ? {
  323. p: common_vendor.o((...args) => $options.stopRecording && $options.stopRecording(...args))
  324. } : {}, {
  325. m: $data.mode === "video" && !$data.isRecording,
  326. o: $data.mode === "video" && $data.isRecording
  327. }) : {
  328. q: common_vendor.t($data.mode === "photo" ? "拍照" : "录制"),
  329. r: common_vendor.o((...args) => $options.retakeMedia && $options.retakeMedia(...args)),
  330. s: common_vendor.o((...args) => $options.continueProcess && $options.continueProcess(...args))
  331. }, {
  332. t: $data.isPageLoaded ? 1 : ""
  333. });
  334. }
  335. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
  336. wx.createPage(MiniProgramPage);