|
- const app = getApp()
- import request from '../../utils/request'
- import Dialog from '@vant/weapp/dialog/dialog';
- Page({
-
- /**
- * 页面的初始数据
- */
- data: {
- topTitle: "请添加一张正面照片",
- showScanLine: false,
- isUploaded: false,
- isSuccess: false,
- tempFilePath: "",
- onlineFilePath: "",
- id: ""
- },
-
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad(options) {
- console.log(options, 'options');
- if (options.scene) {
- this.setData({
- id: options.scene
- })
- }
- },
-
-
- /**
- * 生命周期函数--监听页面显示
- */
- onShow() {
- app.tokenCallBack = res => {
- this.setData({
- isLogin: res
- })
- }
-
- },
-
- chooseImage(e) {
- const that = this
- const action = e ? e.currentTarget.dataset.action : false
-
- if (action && that.data.tempFilePath) {
- that.setData({
- showScanLine: true,
- isUploaded: true,
- isSuccess: false
- })
- that.imgCheckByFace()
- return
- }
-
- wx.chooseMedia({
- mediaType: ['image'],
- sourceType: ['camera', 'album'],
- camera: 'back',
- success: res => {
- that.setData({
- tempFilePath: res.tempFiles[0].tempFilePath,
- showScanLine: true,
- isUploaded: true,
- isSuccess: false
- })
- that.imgCheckByFace()
- }
- })
- },
-
- // 检查图片是否符合人脸规范
- imgCheckByFace() {
- const that = this
- const filePath = that.data.tempFilePath
- const BaseUrl = request.baseUrl
-
- wx.showLoading({
- title: '检测中 1/3',
- })
-
- wx.uploadFile({
- filePath,
- url: BaseUrl + "/api/userDigital/checkPhoto",
- name: 'file',
- formData: {
- user: "test",
- },
- success: res => {
- const code = JSON.parse(res.data).code
- if (code == 200) {
- that.imgCheckByBaidu()
- } else {
- wx.hideLoading()
- that.setData({
- showScanLine: false
- })
- wx.showToast({
- title: '图片不合规范,请重新上传',
- icon: "none"
- })
- }
- },
- fail: err => {
- wx.hideLoading()
- wx.showToast({
- title: '上传失败,请稍后再试',
- icon: "none"
- })
- }
- })
- },
-
- // 检查图片合法性
- imgCheckByBaidu() {
- const that = this
- const filePath = that.data.tempFilePath
- const BaseUrl = request.baseUrl
-
- wx.showLoading({
- title: '验证中 2/3',
- })
-
- wx.uploadFile({
- filePath,
- url: BaseUrl + "/api/baidu/checkPhoto",
- name: 'file',
- formData: {
- user: "test",
- },
- success: res => {
- const code = JSON.parse(res.data).code
- if (code == 200) {
- that.doUploadImg()
- } else {
- wx.hideLoading()
- that.setData({
- showScanLine: false
- })
- wx.showToast({
- title: '图片不合规范,请重新上传',
- icon: "none"
- })
- }
- },
- fail: err => {
- wx.hideLoading()
- wx.showToast({
- title: '上传失败,请稍后再试',
- icon: "none"
- })
- }
- })
- },
-
- // 上传图片
- doUploadImg() {
- const that = this
- const filePath = that.data.tempFilePath
- const BaseUrl = request.baseUrl
-
- wx.showLoading({
- title: '上传中 3/3',
- })
-
- wx.uploadFile({
- filePath,
- url: BaseUrl + "/api/upload/awsImgUpload",
- name: 'file',
- formData: {
- user: "test",
- },
- header: {
- "Content-Type": "multipart/form-data"
- },
- success: res => {
- console.log(res, 'res');
- const data = JSON.parse(res.data)
- if (data.code == 200) {
- wx.hideLoading()
- that.setData({
- showScanLine: false,
- isSuccess: true,
- onlineFilePath: data.data.url
- })
- wx.showModal({
- title: '照片验证成功!',
- content: '是否要使用这张照片?',
- confirmText: "使用",
- confirmColor: "#ff4f00",
- cancelText: "换一张",
- complete: (res) => {
- if (res.cancel) {
- that.chooseImage(false)
- }
-
- if (res.confirm) {
- that.confirmImage()
- }
- }
- })
- } else {
- wx.showToast({
- title: '服务器繁忙,请稍后再试',
- icon: "none"
- })
- wx.hideLoading()
- that.setData({
- showScanLine: false
- })
- }
- },
- fail: err => {
- wx.hideLoading()
- wx.showToast({
- title: '上传失败,请稍后再试',
- icon: "none"
- })
- }
- })
- },
-
- // 确认照片
- confirmImage() {
- const that = this
- const data = {
- id: this.data.id,
- image: this.data.onlineFilePath
- }
-
- request.post({
- url: "/api/screenImg/addImage",
- data
- }).then(res => {
- console.log(res, 'res');
- if (res.code == 200) {
- wx.showToast({
- title: '照片上传成功!请在终端机上查看!',
- icon: "none"
- })
- that.setData({
- topTitle: "上传成功!"
- })
- that.startSub()
- }
- }).catch(err => {
- console.log(err, 'err');
- })
- },
-
- startSub() {
- const that = this
- request.get({
- url: `/api/templateMsg/list?projectType=6&plat=1`
- }).then(res => {
- console.log(res, 'res');
- const id = res.data[0].templateId
- wx.showModal({
- title: '订阅消息',
- content: '作品生成完毕后将会通知您',
- confirmColor: "",
- confirmText: "好的",
- showCancel: false,
- complete: (res) => {
- if (res.cancel) {
-
- }
-
- if (res.confirm) {
- that.subscribe(id)
- }
- }
- })
- }).catch(err => {
- console.log(err, 'err');
- })
- },
-
- subscribe(templateId) {
- const arr = [templateId]
- wx.requestSubscribeMessage({
- //此处填写刚才申请模板的模板ID
- tmplIds: arr,
- success(res) {
- console.log(res);
- let failFlag = true;
- arr.forEach((item) => {
- if (res[item] == "accept") {
- wx.showToast({
- title: "订阅成功",
- icon: "success",
- });
- failFlag = false;
- }
- });
- if (failFlag) {
- wx.showModal({
- title: "提示",
- content:
- "订阅失败 ,您可以在 \n 小程序设置->通知管理 \n 中重新启用订阅功能",
- confirmText: "确定",
- showCancel: false,
- });
- }
- },
- fail(err) {
- console.log("订阅消息失败", err);
- wx.showToast({
- title: "订阅失败",
- icon: "none",
- });
- },
- });
- },
-
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh() {
-
- },
-
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom() {
-
- },
-
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage() {
-
- }
- })
|