// pages/upload/upload.js
const { api, rapi, formatTime, mapApi, mockApi, imgName, getUserInfo, wxLogin, alertTip, wxscan, authAgainByUser, userLocation } = require('../../lib/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgScan: imgName('imgadd.png'),
    imgClose: imgName('close.png'),
    imgUrl:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let urlPath=wx.getStorageSync('user').userMapp.idImg
    if(urlPath){
      this.setData({
        imgScan:urlPath
      })
    }
  },
  upimg(){
    let that = this
    
    wx.chooseImage({
      count: 1, // 默认9  
      sizeType: ['original'], // 可以指定是原图还是压缩图，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片  
        let tempFilePath = res.tempFilePaths[0]
        that.setData({
          imgUrl: tempFilePath,
        })
       
      }
    })  
  },
  deleteImg(){
    this.setData({
      imgUrl: ''
    })
  },
  sureChange(){
    let imgUrl = this.data.imgUrl
    let that=this
    if (imgUrl){
      wx.showLoading({
        title: '上传中。。',
      })
      wx.uploadFile({
        url: 'https://zhizuxia.zhizukj.com/ydb/app/manage/file/upload.json',
        filePath: imgUrl,
        name: 'file',
        success(res) {
          let upUrl=JSON.parse(res.data).picUrl
          api.post('user/editInfo',{
            id:wx.getStorageSync('user').userMapp.id,
            idImg:upUrl
          }).then(res=>{
            let newPath = wx.getStorageSync('user')
            newPath.userMapp.idImg = upUrl
            wx.setStorageSync('user', newPath)
            alertTip('修改成功',()=>{
              wx.navigateBack()
            })
          })
          .catch(err=>{
            alertTip(err)
          })
         
        },
        fail(err) {
          alertTip('上传失败')
        },
        complete(res) {
          wx.hideLoading()
        }
      })
      return
    }
    alertTip('请选择半身照')    
  }
})