comments.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <template>
  2. <view>
  3. <!--mine/comments/comments.wxml-->
  4. <view class="page pdlr20">
  5. <block v-for="(item, index) in products" :key="index">
  6. <view class="info bgfff r20 mt20">
  7. <view class="spinfo dflex">
  8. <image :src="item.pic" mode="aspectFit"></image>
  9. <view class="flex">
  10. <view class="name">{{item.title}}</view>
  11. <view v-if="item.propertiesList" class="guige dflex">规格:
  12. <text v-for="(itemGuige, index2) in item.propertiesList" :key="index2" class="guige"
  13. decode="true">{{itemGuige.value}}&nbsp;
  14. </text>
  15. </view>
  16. </view>
  17. </view>
  18. <view class="ping dflex">
  19. <view>商品评分</view>
  20. <view class="star dflex">
  21. <image v-for="(it, idx) in [1, 2, 3, 4, 5]" :key="idx"
  22. :src="item.score >= it ? '/static/pages/images/star_h.png' : '/static/pages/images/star.png'"
  23. :data-index="index" :data-idx="idx" @tap="addProScores"></image>
  24. </view>
  25. <text
  26. v-if="item.score > 0">{{item.score == 1 ? '非常差' : item.score == 2 ? '差' : item.score == 3 ? '一般' : item.score == 4 ? '好' : '非常好'}}</text>
  27. </view>
  28. </view>
  29. <view class="con bgfff r20 mt20">
  30. <view class="tit">产品评价</view>
  31. <view class="text">
  32. <textarea @input="textareaInput" :data-index="index" :value="item.evaluateText" maxlength="200"
  33. name="text"></textarea>
  34. <view>0/250</view>
  35. </view>
  36. <view class="imgbox dflex">
  37. <view v-for="(it, idx1) in item.buyShow" :key="idx1" class="img">
  38. <image :src="item.buyShow[idx1]" mode="aspectFit"></image>
  39. <image src="/mine/static/mine/images/imagecuo.png" class="cuo" @tap="cleanimage"
  40. :data-index="index" :data-idx="idx1"></image>
  41. </view>
  42. <view class="upload" @tap="choiceImages" :data-index="index" v-if="item.buyShow.length<3">
  43. <image src="/static/pages/images/upload.png"></image>
  44. </view>
  45. </view>
  46. </view>
  47. </block>
  48. <view class="pingjia bgfff r20 mt20">
  49. <view class="ping dflex">
  50. <view>服务态度</view>
  51. <view class="star dflex">
  52. <image v-for="(it, idx) in [1, 2, 3, 4, 5]" :key="idx"
  53. :src="serviceScore >= it ? '/static/pages/images/star_h.png' : '/static/pages/images/star.png'"
  54. :data-idx="idx" @tap="addServiceScores"></image>
  55. </view>
  56. <text
  57. v-if="serviceScore > 0">{{serviceScore == 1 ? '非常差' : serviceScore == 2 ? '差' : serviceScore == 3 ? '一般' : serviceScore == 4 ? '好' : '非常好'}}</text>
  58. </view>
  59. </view>
  60. </view>
  61. <button class="submit" @tap="updateScore" :disabled="isDisabled">{{isDisabled ? '已评价' : '发表评价'}}</button>
  62. <canvas canvas-id="canvas" :style="'width:' + cWidth + 'px;height:' + cHeight +'px;position: absolute;left:-1000px;top:-1000px;'"></canvas>
  63. </view>
  64. </template>
  65. <script>
  66. // mine/comments/comments.js
  67. const app = getApp();
  68. const req = require("../../utils/request.js");
  69. export default {
  70. data() {
  71. return {
  72. scoreList: [],
  73. serviceScore: 0,
  74. products: "",
  75. cWidth: 0,
  76. cHeight: 0
  77. };
  78. },
  79. components: {},
  80. props: {},
  81. onLoad: function(options) {
  82. this.id = options.id;
  83. let that = this;
  84. req.getRequest('/api/order/detail', {
  85. id: this.id
  86. }, data => {
  87. let products = [];
  88. data.products.map(it => {
  89. it.bindId = it.id;
  90. it.bindType = 1;
  91. it.score = 5;
  92. it.scoreType = 1;
  93. it.evaluateText = '';
  94. it.buyShow = [];
  95. products.push(it);
  96. });
  97. that.setData({
  98. products: products
  99. });
  100. // console.log('订单详情', this.products);
  101. });
  102. },
  103. onShow: function() {
  104. let that = this;
  105. },
  106. methods: {
  107. addProScores(event) {
  108. const index = event.currentTarget.dataset.index;
  109. const idx = event.currentTarget.dataset.idx;
  110. const page = this.products;
  111. let d = {};
  112. d['products[' + index + '].score'] = parseInt(idx) + 1;
  113. this.setData(d);
  114. },
  115. textareaInput(event) {
  116. const index = event.currentTarget.dataset.index;
  117. let d = {};
  118. d['products[' + index + '].evaluateText'] = event.detail.value;
  119. this.setData(d);
  120. },
  121. // req.loadIng("上传中");
  122. // req.uploadFile('/api/upload', allList[count].filePath, res => {
  123. // let avatar = that.products[index].buyShow;
  124. // let d = {};
  125. // d['products[' + index + '].buyShow'] = avatar
  126. // .concat(res.src);
  127. // that.setData(d);
  128. // uni.showToast({
  129. // title: '上传成功',
  130. // icon: 'success',
  131. // duration: 2000
  132. // });
  133. // })
  134. //上传图片
  135. choiceImages(event) {
  136. let that = this;
  137. let index = event.currentTarget.dataset.index;
  138. let products = that.products[index];
  139. wx.chooseImage({
  140. count: 9,
  141. sourceType: ['album', 'camera'],
  142. sizeType: ['compressed'],
  143. async success(res) {
  144. console.log(res);
  145. let list = res.tempFiles;
  146. console.log(list);
  147. let allList = [];
  148. for (let i = 0; i < list.length; i++) {
  149. //两M
  150. if (list[i].size > 50000) {
  151. var path = await that.compressImg(list[i].path)
  152. var data = {
  153. formData: {
  154. flag: 726
  155. },
  156. filePath: path,
  157. name: 'file'
  158. }
  159. allList.push(data);
  160. } else {
  161. var data = {
  162. formData: {
  163. flag: 722
  164. },
  165. filePath: list[i].path,
  166. name: 'file'
  167. }
  168. allList.push(data);
  169. }
  170. }
  171. console.log(allList)
  172. let successUp = 0;
  173. let failUp = 0;
  174. let count = 0
  175. // 多图上传 allList:图片数组 successUp:上传成功次数 现在上传的下标
  176. that.uploadImgs(allList, successUp, failUp, count,products,index);
  177. },
  178. fail(err) {}
  179. })
  180. },
  181. //通过canvans缩放长宽来压缩图片 path:图片本地路径 limitNum:压缩到长宽多大
  182. async compressImg(path, multiple = 2, limitNum = 600) {
  183. console.log('图片链接',path)
  184. let that = this;
  185. return new Promise((resolve, reject) => {
  186. wx.getImageInfo({
  187. src: path,
  188. success(res) {
  189. var canvasWidth = res.width //图片原始长宽
  190. var canvasHeight = res.height
  191. console.log('图片的基本信息', res)
  192. // while循环 保证宽高在100以内 Math.trunc()去掉小数点右边数
  193. while (canvasWidth > limitNum || canvasHeight > limitNum) {
  194. canvasWidth = Math.trunc(res.width / multiple)
  195. canvasHeight = Math.trunc(res.height / multiple)
  196. multiple++
  197. }
  198. //设置画布长宽
  199. that.setData({
  200. cWidth: canvasWidth,
  201. cHeight: canvasHeight
  202. })
  203. //----------绘制图形并取出图片路径--------------
  204. var ctx = wx.createCanvasContext('canvas')
  205. ctx.drawImage(res.path, 0, 0, canvasWidth, canvasHeight)
  206. ctx.draw(false, setTimeout(() => {
  207. wx.canvasToTempFilePath({
  208. canvasId: 'canvas',
  209. destWidth: canvasWidth,
  210. destHeight: canvasHeight,
  211. success: function (res) {
  212. console.log(res.tempFilePath) //图片路径
  213. resolve(res.tempFilePath)
  214. },
  215. fail: function (res) {
  216. console.log(res.errMsg)
  217. }
  218. })
  219. }, 100))
  220. },
  221. fail: function (res) {
  222. console.log(res.errMsg)
  223. },
  224. })
  225. })
  226. },
  227. //多张图片上传
  228. uploadImgs: function (allList, successUp, failUp, count,products,index) {
  229. console.log(allList, successUp, failUp, count)
  230. let that = this;
  231. let length = allList.length; //总共上传的数量
  232. let releaseImageList = products;
  233. var promise = Promise.all(allList.map(tempFilePath => {
  234. return new Promise(function(resolve, reject) {
  235. req.loadIng("上传中");
  236. req.uploadFile('/api/upload', tempFilePath.filePath, res => {
  237. // console.log(res)
  238. let avatar = that.products[index].buyShow;
  239. let d = {};
  240. d['products[' + index + '].buyShow'] = avatar
  241. .concat(res.src);
  242. that.setData(d);
  243. uni.showToast({
  244. title: '上传成功',
  245. icon: 'success',
  246. duration: 2000
  247. });
  248. });
  249. });
  250. }));
  251. promise.then(function(results) {
  252. // console.log(results);
  253. }).catch(function(err) {
  254. // console.log(err);
  255. });
  256. },
  257. uploadImg(event) {
  258. let that = this;
  259. let index = event.currentTarget.dataset.index;
  260. let products = that.products[index];
  261. uni.chooseImage({
  262. count: products.buyShow.length - 3,
  263. sizeType: ['compressed'],
  264. sourceType: ['album', 'camera'],
  265. crop: {
  266. quality: 40
  267. },
  268. success: function({
  269. tempFilePaths
  270. }) {
  271. var promise = Promise.all(tempFilePaths.map(tempFilePath => {
  272. return new Promise(function(resolve, reject) {
  273. req.loadIng("上传中");
  274. req.uploadFile('/api/upload', tempFilePath, res => {
  275. // console.log(res)
  276. let avatar = that.products[index].buyShow;
  277. let d = {};
  278. d['products[' + index + '].buyShow'] = avatar
  279. .concat(res.src);
  280. that.setData(d);
  281. uni.showToast({
  282. title: '上传成功',
  283. icon: 'success',
  284. duration: 2000
  285. });
  286. });
  287. });
  288. }));
  289. promise.then(function(results) {
  290. // console.log(results);
  291. }).catch(function(err) {
  292. // console.log(err);
  293. });
  294. }
  295. });
  296. },
  297. // 删除图片
  298. cleanimage(event) {
  299. let index = event.currentTarget.dataset.index;
  300. let idx = event.currentTarget.dataset.idx;
  301. let arr = this.products[index].buyShow;
  302. arr.splice(idx, 1);
  303. this.setData({
  304. ['products[' + index + '].buyShow']: arr
  305. });
  306. },
  307. addServiceScores(event) {
  308. const index = event.currentTarget.dataset.idx;
  309. this.setData({
  310. serviceScore: parseInt(index) + 1
  311. });
  312. },
  313. updateScore() {
  314. if (this.serviceScore == 0) return req.msg('请对服务态度进行评分');
  315. let borrow = {
  316. uid: req.getStorage('userInfo').id,
  317. orderId: this.id,
  318. isOpen: 1
  319. };
  320. let scoreList = this.scoreList;
  321. let products = this.products;
  322. products.map(item => {
  323. scoreList = scoreList.concat({
  324. bindId: item.id,
  325. bindType: item.bindType,
  326. score: item.score,
  327. scoreType: item.scoreType,
  328. evaluateText: item.evaluateText,
  329. buyShow: item.buyShow.length > 0 ? item.buyShow.join(',') : ''
  330. });
  331. });
  332. scoreList = scoreList.concat({
  333. bindId: this.id,
  334. bindType: 5,
  335. score: this.serviceScore,
  336. scoreType: 3,
  337. evaluateText: '',
  338. buyShow: ''
  339. });
  340. borrow.scoreList = JSON.stringify(scoreList);
  341. // console.log(scoreList);
  342. req.postRequest('/api/sysScore/save', borrow, () => {
  343. req.msg('评分成功', () => {
  344. uni.navigateBack();
  345. });
  346. });
  347. }
  348. }
  349. };
  350. </script>
  351. <style>
  352. @import "./comments.css";
  353. </style>