邃芒智像相册
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

338 lines
7.1 KiB

  1. const app = getApp()
  2. import request from '../../utils/request'
  3. import Dialog from '@vant/weapp/dialog/dialog';
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. topTitle: "请添加一张正面照片",
  10. showScanLine: false,
  11. isUploaded: false,
  12. isSuccess: false,
  13. tempFilePath: "",
  14. onlineFilePath: "",
  15. id: ""
  16. },
  17. /**
  18. * 生命周期函数--监听页面加载
  19. */
  20. onLoad(options) {
  21. console.log(options, 'options');
  22. if (options.scene) {
  23. this.setData({
  24. id: options.scene
  25. })
  26. }
  27. },
  28. /**
  29. * 生命周期函数--监听页面显示
  30. */
  31. onShow() {
  32. app.tokenCallBack = res => {
  33. this.setData({
  34. isLogin: res
  35. })
  36. }
  37. },
  38. chooseImage(e) {
  39. const that = this
  40. const action = e ? e.currentTarget.dataset.action : false
  41. if (action && that.data.tempFilePath) {
  42. that.setData({
  43. showScanLine: true,
  44. isUploaded: true,
  45. isSuccess: false
  46. })
  47. that.imgCheckByFace()
  48. return
  49. }
  50. wx.chooseMedia({
  51. mediaType: ['image'],
  52. sourceType: ['camera', 'album'],
  53. camera: 'back',
  54. success: res => {
  55. that.setData({
  56. tempFilePath: res.tempFiles[0].tempFilePath,
  57. showScanLine: true,
  58. isUploaded: true,
  59. isSuccess: false
  60. })
  61. that.imgCheckByFace()
  62. }
  63. })
  64. },
  65. // 检查图片是否符合人脸规范
  66. imgCheckByFace() {
  67. const that = this
  68. const filePath = that.data.tempFilePath
  69. const BaseUrl = request.baseUrl
  70. wx.showLoading({
  71. title: '检测中 1/3',
  72. })
  73. wx.uploadFile({
  74. filePath,
  75. url: BaseUrl + "/api/userDigital/checkPhoto",
  76. name: 'file',
  77. formData: {
  78. user: "test",
  79. },
  80. success: res => {
  81. const code = JSON.parse(res.data).code
  82. if (code == 200) {
  83. that.imgCheckByBaidu()
  84. } else {
  85. wx.hideLoading()
  86. that.setData({
  87. showScanLine: false
  88. })
  89. wx.showToast({
  90. title: '图片不合规范,请重新上传',
  91. icon: "none"
  92. })
  93. }
  94. },
  95. fail: err => {
  96. wx.hideLoading()
  97. wx.showToast({
  98. title: '上传失败,请稍后再试',
  99. icon: "none"
  100. })
  101. }
  102. })
  103. },
  104. // 检查图片合法性
  105. imgCheckByBaidu() {
  106. const that = this
  107. const filePath = that.data.tempFilePath
  108. const BaseUrl = request.baseUrl
  109. wx.showLoading({
  110. title: '验证中 2/3',
  111. })
  112. wx.uploadFile({
  113. filePath,
  114. url: BaseUrl + "/api/baidu/checkPhoto",
  115. name: 'file',
  116. formData: {
  117. user: "test",
  118. },
  119. success: res => {
  120. const code = JSON.parse(res.data).code
  121. if (code == 200) {
  122. that.doUploadImg()
  123. } else {
  124. wx.hideLoading()
  125. that.setData({
  126. showScanLine: false
  127. })
  128. wx.showToast({
  129. title: '图片不合规范,请重新上传',
  130. icon: "none"
  131. })
  132. }
  133. },
  134. fail: err => {
  135. wx.hideLoading()
  136. wx.showToast({
  137. title: '上传失败,请稍后再试',
  138. icon: "none"
  139. })
  140. }
  141. })
  142. },
  143. // 上传图片
  144. doUploadImg() {
  145. const that = this
  146. const filePath = that.data.tempFilePath
  147. const BaseUrl = request.baseUrl
  148. wx.showLoading({
  149. title: '上传中 3/3',
  150. })
  151. wx.uploadFile({
  152. filePath,
  153. url: BaseUrl + "/api/upload/awsImgUpload",
  154. name: 'file',
  155. formData: {
  156. user: "test",
  157. },
  158. header: {
  159. "Content-Type": "multipart/form-data"
  160. },
  161. success: res => {
  162. console.log(res, 'res');
  163. const data = JSON.parse(res.data)
  164. if (data.code == 200) {
  165. wx.hideLoading()
  166. that.setData({
  167. showScanLine: false,
  168. isSuccess: true,
  169. onlineFilePath: data.data.url
  170. })
  171. wx.showModal({
  172. title: '照片验证成功!',
  173. content: '是否要使用这张照片?',
  174. confirmText: "使用",
  175. confirmColor: "#ff4f00",
  176. cancelText: "换一张",
  177. complete: (res) => {
  178. if (res.cancel) {
  179. that.chooseImage(false)
  180. }
  181. if (res.confirm) {
  182. that.confirmImage()
  183. }
  184. }
  185. })
  186. } else {
  187. wx.showToast({
  188. title: '服务器繁忙,请稍后再试',
  189. icon: "none"
  190. })
  191. wx.hideLoading()
  192. that.setData({
  193. showScanLine: false
  194. })
  195. }
  196. },
  197. fail: err => {
  198. wx.hideLoading()
  199. wx.showToast({
  200. title: '上传失败,请稍后再试',
  201. icon: "none"
  202. })
  203. }
  204. })
  205. },
  206. // 确认照片
  207. confirmImage() {
  208. const that = this
  209. const data = {
  210. id: this.data.id,
  211. image: this.data.onlineFilePath
  212. }
  213. request.post({
  214. url: "/api/screenImg/addImage",
  215. data
  216. }).then(res => {
  217. console.log(res, 'res');
  218. if (res.code == 200) {
  219. wx.showToast({
  220. title: '照片上传成功!请在终端机上查看!',
  221. icon: "none"
  222. })
  223. that.setData({
  224. topTitle: "上传成功!"
  225. })
  226. that.startSub()
  227. }
  228. }).catch(err => {
  229. console.log(err, 'err');
  230. })
  231. },
  232. startSub() {
  233. const that = this
  234. request.get({
  235. url: `/api/templateMsg/list?projectType=6&plat=1`
  236. }).then(res => {
  237. console.log(res, 'res');
  238. const id = res.data[0].templateId
  239. wx.showModal({
  240. title: '订阅消息',
  241. content: '作品生成完毕后将会通知您',
  242. confirmColor: "",
  243. confirmText: "好的",
  244. showCancel: false,
  245. complete: (res) => {
  246. if (res.cancel) {
  247. }
  248. if (res.confirm) {
  249. that.subscribe(id)
  250. }
  251. }
  252. })
  253. }).catch(err => {
  254. console.log(err, 'err');
  255. })
  256. },
  257. subscribe(templateId) {
  258. const arr = [templateId]
  259. wx.requestSubscribeMessage({
  260. //此处填写刚才申请模板的模板ID
  261. tmplIds: arr,
  262. success(res) {
  263. console.log(res);
  264. let failFlag = true;
  265. arr.forEach((item) => {
  266. if (res[item] == "accept") {
  267. wx.showToast({
  268. title: "订阅成功",
  269. icon: "success",
  270. });
  271. failFlag = false;
  272. }
  273. });
  274. if (failFlag) {
  275. wx.showModal({
  276. title: "提示",
  277. content:
  278. "订阅失败 ,您可以在 \n 小程序设置->通知管理 \n 中重新启用订阅功能",
  279. confirmText: "确定",
  280. showCancel: false,
  281. });
  282. }
  283. },
  284. fail(err) {
  285. console.log("订阅消息失败", err);
  286. wx.showToast({
  287. title: "订阅失败",
  288. icon: "none",
  289. });
  290. },
  291. });
  292. },
  293. /**
  294. * 页面相关事件处理函数--监听用户下拉动作
  295. */
  296. onPullDownRefresh() {
  297. },
  298. /**
  299. * 页面上拉触底事件的处理函数
  300. */
  301. onReachBottom() {
  302. },
  303. /**
  304. * 用户点击右上角分享
  305. */
  306. onShareAppMessage() {
  307. }
  308. })