comment.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. const util = require('../../../utils/util.js');
  2. const app = getApp();
  3. Page({
  4. data: {
  5. datas:{},
  6. imgUrl:[],
  7. uploadImg:[],
  8. star:5,
  9. cusStar:5,
  10. aid:'',
  11. title:'',
  12. orderid:'',
  13. type:'',
  14. act:'',
  15. content:'',//评价
  16. cid:'',
  17. one: [{ txt: '带队技能欠佳', cet: false }, { txt: '与宣传有差距', cet: false }, { txt: '活动不合理', cet: false }, { txt: '孩子体验一般', cet: false }, { txt: '吃住有待升级', cet: false }, { txt: '性价比一般', cet: false }],
  18. two: [{ txt: '正能量的小黄人', cet: false }, { txt: '领队专业细心', cet: false }, { txt: '教育意义明显', cet: false }, { txt: '活动安排有序', cet: false }, { txt: '安全措施到位', cet: false }, { txt: '性价比超值', cet: false }],
  19. chooseIndex:[],
  20. option:[],
  21. recommendArr: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],//推荐值数组
  22. recomLevel: 0,//推荐值
  23. },
  24. onLoad(options) {
  25. let orderid = options.orderid, type = options.otype, act = options.act || '';
  26. this.setData({ orderid, type, act});
  27. this.getData(orderid,type,act);
  28. },
  29. getData(orderid,type,act){
  30. let that = this;
  31. util.ajax({
  32. func:"v2/order/detail",
  33. data:{"orderid":orderid,"type":(type != undefined ? type : "")}
  34. },function(res){
  35. if(res.code == 0){
  36. let datas = res.data, star = 5, uploadImg = [], content = '', option = [], options = that.data.two, cid = '', recomLevel=0;
  37. if (!util.isObjEmpty(datas.comment)){
  38. // star = datas.comment.arating;
  39. cid = datas.comment.cid;
  40. // recomLevel = datas.comment.recomLevel;
  41. // uploadImg = datas.comment.images||[];
  42. // content = datas.comment.content.replace(/#([^#]+)#/g,"").replace(/<br\/>/g,'\n');
  43. // option = datas.comment.content.match(/#([^#]+)#/g);
  44. // star>=3?options = that.data.two : options = that.data.one;
  45. // for(let i in option){
  46. // option[i] = option[i].replace(/#/g, "");
  47. // for(let k in options){
  48. // if(option[i] == options[k].txt){
  49. // options[k].cet = true
  50. // }
  51. // }
  52. // }
  53. that.updateCommentData(datas.comment)
  54. }
  55. if (that.data.act !='edit') {
  56. that.getlocalData();
  57. }
  58. // if(star>=3){
  59. // that.setData({two:options})
  60. // }else{
  61. // that.setData({one:options})
  62. // }
  63. // that.setData({ star, uploadImg, aid: datas.aid, cid, content, datas, title: datas.title, recomLevel})
  64. that.setData({aid: datas.aid, cid, datas, title: datas.title,})
  65. }else
  66. util.showTips(res.reason);
  67. })
  68. },
  69. chooseImg(){
  70. let that = this, uploadImg = that.data.uploadImg, count = 8;
  71. wx.chooseImage({
  72. count:count,
  73. sizeType: ['compressed'],
  74. sourceType: ['album', 'camera'],
  75. success: function (res) {
  76. var imgUrl = res.tempFilePaths;
  77. if (count - (imgUrl.length + uploadImg.length) < 0) {
  78. util.showTips("最多只能上传" + count + "张图片");
  79. return false;
  80. }
  81. wx.showLoading({ title: '上传中…' });
  82. let img = [];
  83. that.data.upImg = [];
  84. for(let i in imgUrl){
  85. wx.uploadFile({
  86. url: util.config.apiServer + 'user/upload_img_app.do',
  87. filePath: imgUrl[i],
  88. name: 'avatarFile',
  89. formData:{
  90. "rid":app.globalData.userInfo['rid']
  91. },
  92. success: function(reason){
  93. let datas = JSON.parse(reason.data);
  94. if(reason.statusCode==200&&datas.code==0){
  95. img.push(datas.data.image);
  96. that.setData({ uploadCount: imgUrl.length, uploadImg, upImg: img});
  97. if (that.data.upImg.length == that.data.uploadCount) {
  98. that.data.uploadImg = [...that.data.upImg, ...that.data.uploadImg];
  99. that.setData({ uploadImg: that.data.uploadImg });
  100. wx.hideLoading();
  101. }
  102. }
  103. }
  104. })
  105. }
  106. }
  107. })
  108. },
  109. delImg(e){
  110. let index = e.currentTarget.dataset.index,uploadImg = this.data.uploadImg;
  111. uploadImg.splice(index,1);
  112. this.setData({uploadImg})
  113. },
  114. submit(){
  115. let that=this,
  116. data = {},
  117. uploadImg=this.data.uploadImg,
  118. act = this.data.act,
  119. userInfo = app.globalData.userInfo,
  120. star = this.data.star,
  121. content=this.data.content.replace(/\n/g,'<br/>'),
  122. option = this.data.two;
  123. if(star == 1 || star == 2){
  124. option = this.data.one;
  125. }
  126. for(let i in option){
  127. if(option[i].cet){
  128. content = content +'#'+option[i].txt+'#'
  129. }
  130. }
  131. let pages = getCurrentPages(), prevPage = pages[pages.length - 2];
  132. if (prevPage.route == "pages/order/detail/index"){
  133. prevPage.data.query.otype = 0;
  134. }
  135. data['aid'] = this.data.aid;
  136. data['orderid'] = this.data.orderid;
  137. data['arating'] = star;//活动评分
  138. data['crating'] = this.data.cusStar;//活动评分
  139. data['leaderrating'] = '5';//领队评分
  140. data['content'] = content;
  141. data['cid'] = this.data.cid;
  142. data['image'] = uploadImg?uploadImg.join(','):'';
  143. data['rid'] = userInfo ? userInfo['rid']:"";
  144. data['recomLevel'] = this.data.recomLevel;
  145. if (data.recomLevel<=0&&this.data.datas.atype == 0) {
  146. util.showTips("请对于本次活动体验后进行打分。");
  147. return false;
  148. }
  149. if(util.isEmpty(data.content)){
  150. util.showTips(this.data.datas.atype == 0?"请填写本次活动体验后的评价。":'请填写对此商品的评价。');
  151. return false;
  152. }
  153. util.ajax({
  154. func: (act == 'edit' ? "v2/comments/order/update" :"v2/comments/order/addcomment"),
  155. data:data,
  156. method: "POST",
  157. load:true,
  158. title:'提交中'
  159. },function(res){
  160. if(res.code == 0){
  161. if (act == 'edit' || that.data.datas.atype == 1){
  162. wx.navigateBack();
  163. }else{
  164. wx.redirectTo({
  165. url: '/pages/order/scratch/scratch?orderid='+that.data.orderid+'&coin='+res.data||0
  166. })
  167. }
  168. }else{
  169. util.showTips(res.reason);
  170. }
  171. });
  172. },
  173. // 获取的评论数据整理公共方法
  174. updateCommentData(commentData){
  175. let star = 5,cusStar=5, uploadImg = [], content = '', option = [], options = this.data.two,recomLevel=0;
  176. star = commentData.arating;
  177. cusStar = commentData.crating;
  178. recomLevel = commentData.recomLevel;
  179. uploadImg = commentData.images||[];
  180. content = commentData.content.replace(/#([^#]+)#/g,"").replace(/<br\/>/g,'\n');
  181. option = commentData.content.match(/#([^#]+)#/g);
  182. star>=3?options = this.data.two : options = this.data.one;
  183. for(let i in option){
  184. option[i] = option[i].replace(/#/g, "");
  185. for(let k in options){
  186. if(option[i] == options[k].txt){
  187. options[k].cet = true
  188. }
  189. }
  190. }
  191. if(star>=3){
  192. this.setData({two:options})
  193. }else{
  194. this.setData({one:options})
  195. }
  196. this.setData({star,cusStar,uploadImg,content,recomLevel})
  197. },
  198. // 获取本地草稿数据
  199. getlocalData(){
  200. if (!util.isObjEmpty(wx.getStorageSync("orderComment"))) {
  201. let localData=wx.getStorageSync("orderComment");
  202. if (localData.orderid==this.data.orderid) {
  203. this.updateCommentData(localData)
  204. }
  205. }
  206. },
  207. // 保存草稿至本地
  208. saveDataToLocal(){
  209. let that=this,commentData={},option = this.data.two;
  210. if (!util.isEmpty(this.data.content)||this.data.uploadImg.length>0) {
  211. commentData.orderid=this.data.orderid;
  212. commentData.saveDate=util.getNowFormatDate('yyyy-MM-dd hh:mm:ss');
  213. commentData.arating=this.data.star;
  214. commentData.crating=this.data.cusStar;
  215. commentData.recomLevel=this.data.recomLevel;
  216. commentData.images=this.data.uploadImg || '';
  217. let content=this.data.content.replace(/\n/g,'<br/>');
  218. if(this.data.star == 1 || this.data.star == 2){
  219. option = this.data.one;
  220. }
  221. for(let i in option){
  222. if(option[i].cet){
  223. content = content +'#'+option[i].txt+'#'
  224. }
  225. }
  226. commentData.content=content;
  227. wx.setStorageSync("orderComment", commentData);
  228. }
  229. },
  230. chooseOpt:function(e){
  231. var star = this.data.star,chooseIndex = this.data.chooseIndex,index=e.currentTarget.dataset.index,option = this.data.two;
  232. if(star == 1 || star == 2){
  233. option = this.data.one;
  234. }
  235. if(option[index].cet){
  236. option[index].cet = false;
  237. }else{
  238. option[index].cet = true;
  239. }
  240. if(star == 1 || star == 2){
  241. this.setData({one:option});
  242. }else{
  243. this.setData({two:option});
  244. }
  245. },
  246. bindblur:function(e){
  247. this.setData({content:e.detail.value});
  248. },
  249. bindstar:function(e){
  250. if (e.currentTarget.dataset.type=='star') {
  251. this.setData({star:e.target.dataset.index});
  252. }else{
  253. this.setData({cusStar:e.target.dataset.index});
  254. }
  255. },
  256. recommend(e) {
  257. this.setData({ recomLevel: e.target.dataset.number })
  258. },
  259. onUnload: function() {
  260. if (this.data.datas.atype=='0') {
  261. this.saveDataToLocal()
  262. }
  263. },
  264. onHide(){
  265. if (this.data.datas.atype=='0') {
  266. this.saveDataToLocal()
  267. }
  268. }
  269. })