| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- import {
- uploadFile
- } from "../../utils/upload"
- Page({
- data: {
- avatar: "https://6875-huihenduo-2gx127w7f837b584-1255802371.tcb.qcloud.la/miniapp-static/avatar-icon.jpg?sign=f5c66c94d189436f82353eb48cb01f08&t=1634538864",
- cameraIcon: "https://6875-huihenduo-2gx127w7f837b584-1255802371.tcb.qcloud.la/miniapp-static/camera-icon.png?sign=11a65871a2800cd04ecaa8991687fccd&t=1634607415",
- newCameraIcon: "https://6875-huihenduo-2gx127w7f837b584-1255802371.tcb.qcloud.la/miniapp-static/camera.png?sign=ad0fe8bead6a46cb20f45f792d4bed67&t=1645502416",
- userName: "",
- phone: "",
- shipName: "",
- shipMmsi: "",
- authModal: false,
- modalText: "位置",
- cw: '',
- ch: '',
- disabled: false,
- imgCache:''
- },
- takeBill() {
- if (this.data.disabled) return
- wx.showLoading({
- title: '正在上传...',
- })
- this.data.disabled = true
- const ctx = wx.createCameraContext()
- ctx.takePhoto({
- quality: 'high',
- success: (res) => {
- let src = res.tempImagePath
- let ch = res.height * 2
- let cw = res.width * 2
- wx.getImageInfo({
- src,
- success: e => {
- this.setData({
- ch,
- cw
- })
- const query = wx.createSelectorQuery();
- query.select('#myCanvas').node().exec((res) => {
- const canvas = res[0].node;
- const ctx = canvas.getContext('2d');
- const img = canvas.createImage();
- let h = ch / 2
- let w = cw / 2
- canvas.height = h * 0.85
- canvas.width = w * 0.9
- img.src = src
- img.onload = () => {
- ctx.drawImage(img, -w * 0.05, -h * 0.05);
- }
- this.saveImg(src)
- })
- }
- })
- }
- })
- },
- goBack() {
- wx.redirectTo({
- url: '/pages/takePhoto/takePhoto',
- })
- },
- async saveImg(src) {
- const query = wx.createSelectorQuery();
- const canvas = await new Promise((resolve, reject) => {
- query.select('#myCanvas')
- .fields({
- node: true,
- size: true
- })
- .exec(async (res) => {
- resolve(res[0].node);
- })
- });
- wx.canvasToTempFilePath({
- canvas,
- success: async e => {
- // let path = e.tempFilePath
- // wx.navigateTo({
- // url: `/pages/takeBill/success/success?src=${src}&path=${path}`
- // })
- // return
- this.setData({
- imgCache:e.tempFilePath
- })
- let res = await uploadFile(e.tempFilePath, {
- type: 0,
- userId: wx.getStorageSync('userId'),
- // userId: 89,
- location: ''
- })
- wx.hideLoading({
- success: (res) => {},
- })
- wx.showToast({
- title: res.msg,
- })
- setTimeout(() => {
- this.data.disabled = false
- this.goBack()
- }, 1000)
- },
- fail(res) {
- console.log('fail');
- }
- })
- },
- onLoad() {
- let userName = wx.getStorageSync('userName')
- let userId = wx.getStorageSync('userId')
- let phone = wx.getStorageSync('phone')
- let shipId = wx.getStorageSync('shipId')
- this.setData({
- userName,
- phone,
- userId,
- shipId
- })
- }
- })
|