123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- const app = getApp();
- import util from '../utils/util.js'
- Page({
- data: {
- appAssetsUrl: app.appAssetsUrl,
- images: [],
- count: 6,
- addedCount: 0,
- params: {
- workId: '',
- stuId: util.getUserId(),
- phone: '',
- context: '',
- pictureUrl: '',
- stars: 0,
- isAnonymity: []
- },
- },
- onLoad(options) {
- const user = wx.getStorageSync("USER");
- this.setData({
- ['params.workId']: options.workId,
- ['params.phone']: user.phone
- })
- },
- chooseImage() {
- var that = this;
- wx.chooseImage({
- count: 6 - that.data.addedCount,
- sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有
- sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
- success: async function(res) {
- // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
- // that.setData({
- // images: that.data.images.concat(res.tempFilePaths),
- // addedCount: that.data.addedCount + res.tempFilePaths.length,
- // });
- for(let i in res.tempFilePaths){
- try{
- let res2 = await that.uploadFile(res.tempFilePaths[i],i,res.tempFilePaths.length);
- if(res2.code == 0){
- that.setData({
- images: that.data.images.concat(res2.data),
- addedCount: that.data.addedCount + 1,
- });
- }
- }catch(e){
-
- }
- }
- }
- })
- },
- //微信限制了只能一次上传一个文件,并且不能同时上传,所以用promise封装
- uploadFile(path,index,total){
- return new Promise((resolve,reject)=>{
- wx.showLoading({
- title: `正在上传 (${index*1+1}/${total})`
- })
- wx.uploadFile({
- url: app.apiRoot + 'file/upload', //仅为示例,非真实的接口地址
- filePath: path, //数据来源
- name: 'file',
- header: {},
- formData: {
- file: path,
- },
- success(res) {
- let resObj = JSON.parse(res.data);
- resolve(resObj);
- wx.hideLoading();
- },
- fail(err) {
- reject(err);
- wx.hideLoading();
- wx.showToast({
- title: '上传失败',
- icon: 'fail'
- })
- },
- })
- })
- },
- // 删除图片
- deleteImage(e) {
- this.data.images.splice(e.detail, 1)
- this.setData({
- images: this.data.images,
- addedCount: this.data.addedCount - 1
- })
- },
- clickStar(e) {
- this.setData({
- params: {
- ...this.data.params,
- stars: e.currentTarget.dataset.stars
- }
- })
- },
- radioChange(e) {
- this.setData({
- ['params.isAnonymity']: e.detail.value
- })
- },
- inputContext(e){
- this.setData({
- ['params.context']: e.detail.value
- })
- },
- submit(){
- if(this.data.params.context === ''){
- wx.showToast({
- title: '请填写您的评价',
- icon: 'none'
- })
- return ;
- }
- if(this.data.params.stars <= 0){
- wx.showToast({
- title: '请选择星级',
- icon: 'none'
- })
- return ;
- }
- wx.showLoading({
- title: '提交中...',
- })
- app._post_form('appraise', '', {
- ...this.data.params,
- pictureUrl: this.data.images.length>0?this.data.images.join(','):'',
- isAnonymity: this.data.params.isAnonymity.length>0
- },
- function(res) {
- if (res.code == 0) {
- wx.showToast({
- title: '评价成功',
- icon: 'none'
- })
- //上一个页面对象
- let pages = getCurrentPages();
- let prevpage = pages[pages.length - 2];
- prevpage.loadList(true);
- setTimeout(()=>{
- wx.navigateBack({
- delta:1
- })
- },1000)
- }
- })
- }
- })
|