index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. const app = getApp();
  2. import util from '../utils/util.js'
  3. Page({
  4. data: {
  5. appAssetsUrl: app.appAssetsUrl,
  6. images: [],
  7. count: 6,
  8. addedCount: 0,
  9. params: {
  10. workId: '',
  11. stuId: util.getUserId(),
  12. phone: '',
  13. context: '',
  14. pictureUrl: '',
  15. stars: 0,
  16. isAnonymity: []
  17. },
  18. },
  19. onLoad(options) {
  20. const user = wx.getStorageSync("USER");
  21. this.setData({
  22. ['params.workId']: options.workId,
  23. ['params.phone']: user.phone
  24. })
  25. },
  26. chooseImage() {
  27. var that = this;
  28. wx.chooseImage({
  29. count: 6 - that.data.addedCount,
  30. sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有
  31. sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
  32. success: async function(res) {
  33. // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
  34. // that.setData({
  35. // images: that.data.images.concat(res.tempFilePaths),
  36. // addedCount: that.data.addedCount + res.tempFilePaths.length,
  37. // });
  38. for(let i in res.tempFilePaths){
  39. try{
  40. let res2 = await that.uploadFile(res.tempFilePaths[i],i,res.tempFilePaths.length);
  41. if(res2.code == 0){
  42. that.setData({
  43. images: that.data.images.concat(res2.data),
  44. addedCount: that.data.addedCount + 1,
  45. });
  46. }
  47. }catch(e){
  48. }
  49. }
  50. }
  51. })
  52. },
  53. //微信限制了只能一次上传一个文件,并且不能同时上传,所以用promise封装
  54. uploadFile(path,index,total){
  55. return new Promise((resolve,reject)=>{
  56. wx.showLoading({
  57. title: `正在上传 (${index*1+1}/${total})`
  58. })
  59. wx.uploadFile({
  60. url: app.apiRoot + 'file/upload', //仅为示例,非真实的接口地址
  61. filePath: path, //数据来源
  62. name: 'file',
  63. header: {},
  64. formData: {
  65. file: path,
  66. },
  67. success(res) {
  68. let resObj = JSON.parse(res.data);
  69. resolve(resObj);
  70. wx.hideLoading();
  71. },
  72. fail(err) {
  73. reject(err);
  74. wx.hideLoading();
  75. wx.showToast({
  76. title: '上传失败',
  77. icon: 'fail'
  78. })
  79. },
  80. })
  81. })
  82. },
  83. // 删除图片
  84. deleteImage(e) {
  85. this.data.images.splice(e.detail, 1)
  86. this.setData({
  87. images: this.data.images,
  88. addedCount: this.data.addedCount - 1
  89. })
  90. },
  91. clickStar(e) {
  92. this.setData({
  93. params: {
  94. ...this.data.params,
  95. stars: e.currentTarget.dataset.stars
  96. }
  97. })
  98. },
  99. radioChange(e) {
  100. this.setData({
  101. ['params.isAnonymity']: e.detail.value
  102. })
  103. },
  104. inputContext(e){
  105. this.setData({
  106. ['params.context']: e.detail.value
  107. })
  108. },
  109. submit(){
  110. if(this.data.params.context === ''){
  111. wx.showToast({
  112. title: '请填写您的评价',
  113. icon: 'none'
  114. })
  115. return ;
  116. }
  117. if(this.data.params.stars <= 0){
  118. wx.showToast({
  119. title: '请选择星级',
  120. icon: 'none'
  121. })
  122. return ;
  123. }
  124. wx.showLoading({
  125. title: '提交中...',
  126. })
  127. app._post_form('appraise', '', {
  128. ...this.data.params,
  129. pictureUrl: this.data.images.length>0?this.data.images.join(','):'',
  130. isAnonymity: this.data.params.isAnonymity.length>0
  131. },
  132. function(res) {
  133. if (res.code == 0) {
  134. wx.showToast({
  135. title: '评价成功',
  136. icon: 'none'
  137. })
  138. //上一个页面对象
  139. let pages = getCurrentPages();
  140. let prevpage = pages[pages.length - 2];
  141. prevpage.loadList(true);
  142. setTimeout(()=>{
  143. wx.navigateBack({
  144. delta:1
  145. })
  146. },1000)
  147. }
  148. })
  149. }
  150. })