face-photo.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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.uploadMedia((url) => {
  157. this.photoUrl = url;
  158. this.submitPhotoUrl();
  159. });
  160. },
  161. // 上传媒体文件方法
  162. uploadMedia(callback) {
  163. const openid = JSON.parse(common_vendor.index.getStorageSync("userInfo")).openid || "";
  164. const tenant_id = 1;
  165. if (!openid || !tenant_id) {
  166. common_vendor.index.hideLoading();
  167. common_vendor.index.showToast({
  168. title: "用户信息不完整,请重新登录",
  169. icon: "none"
  170. });
  171. return;
  172. }
  173. common_vendor.index.uploadFile({
  174. url: `${common_config.apiBaseUrl}/api/upload/`,
  175. filePath: this.mediaSource,
  176. name: "file",
  177. formData: {
  178. openid,
  179. tenant_id
  180. },
  181. success: (uploadRes) => {
  182. const res = JSON.parse(uploadRes.data);
  183. if (res.code === 2e3) {
  184. const photoUrl = res.data.url || res.data.photoUrl || "";
  185. if (callback && typeof callback === "function") {
  186. callback(photoUrl);
  187. }
  188. } else {
  189. common_vendor.index.hideLoading();
  190. common_vendor.index.showToast({
  191. title: res.msg || "照片上传失败",
  192. icon: "none"
  193. });
  194. }
  195. },
  196. fail: (err) => {
  197. common_vendor.index.hideLoading();
  198. console.error("上传失败:", err);
  199. common_vendor.index.showToast({
  200. title: "网络错误,请重试",
  201. icon: "none"
  202. });
  203. }
  204. });
  205. },
  206. // 提交照片URL到指定接口
  207. submitPhotoUrl() {
  208. const openid = JSON.parse(common_vendor.index.getStorageSync("userInfo")).openid || "";
  209. const tenant_id = 1;
  210. if (!this.photoUrl) {
  211. common_vendor.index.hideLoading();
  212. common_vendor.index.showToast({
  213. title: "照片信息不完整,请重试",
  214. icon: "none"
  215. });
  216. return;
  217. }
  218. api_user.fillUserInfo({
  219. application_id: common_vendor.index.getStorageSync("appId"),
  220. openid,
  221. tenant_id,
  222. avatar: this.photoUrl
  223. }).then((res) => {
  224. common_vendor.index.hideLoading();
  225. console.log(res);
  226. common_vendor.index.showToast({
  227. title: "照片上传成功",
  228. icon: "success"
  229. });
  230. setTimeout(() => {
  231. common_vendor.index.navigateTo({
  232. url: "/pages/identity-verify/identity-verify",
  233. fail: (err) => {
  234. console.error("页面跳转失败:", err);
  235. common_vendor.index.showToast({
  236. title: "页面跳转失败",
  237. icon: "none"
  238. });
  239. }
  240. });
  241. }, 1500);
  242. }).catch((err) => {
  243. common_vendor.index.hideLoading();
  244. console.error("提交失败:", err);
  245. common_vendor.index.showToast({
  246. title: "网络错误,请重试",
  247. icon: "none"
  248. });
  249. });
  250. },
  251. updateLocalUserInfo() {
  252. api_user.getUserInfo().then((res) => {
  253. if (res.code === 200 && res.data) {
  254. let userInfo = {};
  255. try {
  256. userInfo = JSON.parse(common_vendor.index.getStorageSync("userInfo") || "{}");
  257. } catch (e) {
  258. console.error("解析本地存储用户信息失败:", e);
  259. userInfo = {};
  260. }
  261. const updatedUserInfo = {
  262. ...userInfo,
  263. ...res.data
  264. };
  265. common_vendor.index.setStorageSync("userInfo", JSON.stringify(updatedUserInfo));
  266. }
  267. }).catch((err) => {
  268. console.error("更新本地用户信息失败:", err);
  269. });
  270. }
  271. }
  272. };
  273. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  274. return common_vendor.e({
  275. a: !$data.mediaSource
  276. }, !$data.mediaSource ? {
  277. b: $data.mode,
  278. c: common_vendor.o((...args) => $options.handleCameraError && $options.handleCameraError(...args))
  279. } : $data.mode === "photo" ? {
  280. e: $data.mediaSource
  281. } : $data.mode === "video" ? {
  282. g: $data.mediaSource
  283. } : {}, {
  284. d: $data.mode === "photo",
  285. f: $data.mode === "video",
  286. h: $data.mode === "video" && $data.isRecording
  287. }, $data.mode === "video" && $data.isRecording ? {
  288. i: common_vendor.t($options.formatTime($data.recordingTime))
  289. } : {}, {
  290. j: !$data.mediaSource
  291. }, !$data.mediaSource ? common_vendor.e({
  292. k: $data.mode === "photo"
  293. }, $data.mode === "photo" ? {
  294. l: common_vendor.o((...args) => $options.takePhoto && $options.takePhoto(...args))
  295. } : $data.mode === "video" && !$data.isRecording ? {
  296. n: common_vendor.o((...args) => $options.startRecording && $options.startRecording(...args))
  297. } : $data.mode === "video" && $data.isRecording ? {
  298. p: common_vendor.o((...args) => $options.stopRecording && $options.stopRecording(...args))
  299. } : {}, {
  300. m: $data.mode === "video" && !$data.isRecording,
  301. o: $data.mode === "video" && $data.isRecording
  302. }) : {
  303. q: common_vendor.t($data.mode === "photo" ? "拍照" : "录制"),
  304. r: common_vendor.o((...args) => $options.retakeMedia && $options.retakeMedia(...args)),
  305. s: common_vendor.o((...args) => $options.continueProcess && $options.continueProcess(...args))
  306. }, {
  307. t: $data.isPageLoaded ? 1 : ""
  308. });
  309. }
  310. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render]]);
  311. wx.createPage(MiniProgramPage);