face-photo.js 9.7 KB

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