face-photo.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 = JSON.parse(common_vendor.index.getStorageSync("userInfo")).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 = JSON.parse(common_vendor.index.getStorageSync("userInfo")).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. console.log(this.mediaSource);
  216. common_vendor.index.uploadFile({
  217. url: `${common_config.apiBaseUrl}/job/upload_posture_photo`,
  218. filePath: this.mediaSource,
  219. name: "photo",
  220. // 确保文件参数名称正确
  221. formData: {
  222. // 使用formData传递参数
  223. "application_id": common_vendor.index.getStorageSync("appId"),
  224. "tenant_id": tenant_id,
  225. "openid": openid,
  226. "description": "面部照片"
  227. },
  228. success: (uploadRes) => {
  229. console.log("照片上传成功:", uploadRes);
  230. let result;
  231. try {
  232. if (typeof uploadRes.data === "string") {
  233. result = JSON.parse(uploadRes.data);
  234. } else {
  235. result = uploadRes.data;
  236. }
  237. console.log("解析后的上传结果:", result);
  238. if (result.data && result.data.url) {
  239. this.photoUrl = result.data.url;
  240. }
  241. common_vendor.index.showToast({
  242. title: "照片上传成功",
  243. icon: "success"
  244. });
  245. setTimeout(() => {
  246. common_vendor.index.navigateTo({
  247. url: "/pages/identity-verify/identity-verify",
  248. fail: (err) => {
  249. console.error("页面跳转失败:", err);
  250. common_vendor.index.showToast({
  251. title: "页面跳转失败",
  252. icon: "none"
  253. });
  254. }
  255. });
  256. }, 1500);
  257. } catch (e) {
  258. console.error("解析上传结果失败:", e);
  259. common_vendor.index.showToast({
  260. title: "处理响应失败",
  261. icon: "none"
  262. });
  263. }
  264. },
  265. fail: (err) => {
  266. console.error("照片上传失败:", err);
  267. common_vendor.index.showToast({
  268. title: "照片上传失败,请重试",
  269. icon: "none"
  270. });
  271. },
  272. complete: () => {
  273. common_vendor.index.hideLoading();
  274. }
  275. });
  276. },
  277. updateLocalUserInfo() {
  278. api_user.getUserInfo().then((res) => {
  279. if (res.code === 200 && res.data) {
  280. let userInfo = {};
  281. try {
  282. userInfo = JSON.parse(common_vendor.index.getStorageSync("userInfo") || "{}");
  283. } catch (e) {
  284. console.error("解析本地存储用户信息失败:", e);
  285. userInfo = {};
  286. }
  287. const updatedUserInfo = {
  288. ...userInfo,
  289. ...res.data
  290. };
  291. common_vendor.index.setStorageSync("userInfo", JSON.stringify(updatedUserInfo));
  292. }
  293. }).catch((err) => {
  294. console.error("更新本地用户信息失败:", err);
  295. });
  296. }
  297. }
  298. };
  299. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  300. return common_vendor.e({
  301. a: !$data.mediaSource
  302. }, !$data.mediaSource ? {
  303. b: $data.mode,
  304. c: common_vendor.o((...args) => $options.handleCameraError && $options.handleCameraError(...args))
  305. } : $data.mode === "photo" ? {
  306. e: $data.mediaSource
  307. } : $data.mode === "video" ? {
  308. g: $data.mediaSource
  309. } : {}, {
  310. d: $data.mode === "photo",
  311. f: $data.mode === "video",
  312. h: $data.mode === "video" && $data.isRecording
  313. }, $data.mode === "video" && $data.isRecording ? {
  314. i: common_vendor.t($options.formatTime($data.recordingTime))
  315. } : {}, {
  316. j: !$data.mediaSource
  317. }, !$data.mediaSource ? common_vendor.e({
  318. k: $data.mode === "photo"
  319. }, $data.mode === "photo" ? {
  320. l: common_vendor.o((...args) => $options.takePhoto && $options.takePhoto(...args))
  321. } : $data.mode === "video" && !$data.isRecording ? {
  322. n: common_vendor.o((...args) => $options.startRecording && $options.startRecording(...args))
  323. } : $data.mode === "video" && $data.isRecording ? {
  324. p: common_vendor.o((...args) => $options.stopRecording && $options.stopRecording(...args))
  325. } : {}, {
  326. m: $data.mode === "video" && !$data.isRecording,
  327. o: $data.mode === "video" && $data.isRecording
  328. }) : {
  329. q: common_vendor.t($data.mode === "photo" ? "拍照" : "录制"),
  330. r: common_vendor.o((...args) => $options.retakeMedia && $options.retakeMedia(...args)),
  331. s: common_vendor.o((...args) => $options.continueProcess && $options.continueProcess(...args))
  332. }, {
  333. t: $data.isPageLoaded ? 1 : ""
  334. });
  335. }
  336. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
  337. wx.createPage(MiniProgramPage);