user-order-comments.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <template>
  2. <view>
  3. <view class="padding-main">
  4. <view v-if="detail != null">
  5. <form @submit="formSubmit" class="form-container">
  6. <view v-for="(item, index) in detail.items" :key="index" class="form-gorup oh">
  7. <view class="oh">
  8. <navigator :url="item.goods_url" hover-class="none">
  9. <image class="goods-image fl radius br margin-right-lg" :src="item.images" mode="aspectFill"></image>
  10. </navigator>
  11. <view class="item-base fl margin-top-sm">
  12. <block v-for="(tv, ti) in [1,2,3,4,5]" :key="ti">
  13. <image class="xingxing-icon va-m" :src="common_static_url+'stars' + (form_rating_list[index] != undefined && form_rating_list[index] >= tv ? '-active' : '') + '-icon.png'" mode="aspectFill" @tap="rating_event" :data-index="index" :data-value="tv"></image>
  14. </block>
  15. <text v-if="form_rating_list[index] != undefined" class="cr-gray va-m margin-left-lg">{{rating_msg[form_rating_list[index]-1]}}</text>
  16. </view>
  17. </view>
  18. <view class="margin-top-main br-t">
  19. <textarea @input="form_content_event" :data-index="index" placeholder-class="cr-grey" class="cr-base" placeholder="宝贝满足你的期待吗?说说它的优点和美中不足的地方吧" maxlength="230"></textarea>
  20. <view class="form-container-upload oh">
  21. <view class="form-upload-data fl">
  22. <block v-if="(form_images_list[index] || null) != null && form_images_list[index].length > 0">
  23. <view v-for="(iv, ix) in form_images_list[index]" :key="ix" class="item fl">
  24. <text class="delete-icon" @tap="upload_delete_event" :data-index="index" :data-ix="ix">x</text>
  25. <image :src="iv" @tap="upload_show_event" :data-index="index" :data-ix="ix" mode="aspectFill" class="padding-xs dis-block"></image>
  26. </view>
  27. </block>
  28. </view>
  29. <image v-if="(form_images_list[index] || null) == null || form_images_list[index].length < 3" class="upload-icon" :src="common_static_url+'upload-icon.png'" mode="aspectFill" @tap="file_upload_event" :data-index="index"></image>
  30. </view>
  31. </view>
  32. </view>
  33. <view class="form-gorup anonymous">
  34. <switch class="va-m" name="is_anonymous" @change="anonymous_event"></switch>
  35. <text class="cr-base va-m">匿名</text>
  36. <text class="fr cr-gray margin-top-lg">{{anonymous_msg_list[anonymous_value]}}</text>
  37. </view>
  38. <view class="form-gorup form-gorup-submit">
  39. <button form-type="submit" class="bg-main br-main cr-white round text-size" type="default" hover-class="none" :disabled="form_button_disabled">提交</button>
  40. </view>
  41. </form>
  42. </view>
  43. <view v-else>
  44. <!-- 提示信息 -->
  45. <component-no-data :propStatus="data_list_loding_status" :propMsg="data_list_loding_msg"></component-no-data>
  46. </view>
  47. </view>
  48. </view>
  49. </template>
  50. <script>
  51. const app = getApp();
  52. import componentNoData from "../../components/no-data/no-data";
  53. import componentBottomLine from "../../components/bottom-line/bottom-line";
  54. var common_static_url = app.globalData.get_static_url('common');
  55. export default {
  56. data() {
  57. return {
  58. common_static_url: common_static_url,
  59. data_list_loding_status: 1,
  60. data_list_loding_msg: '',
  61. params: null,
  62. detail: null,
  63. editor_path_type: '',
  64. rating_msg: ['非常差', '差', '一般', '好', '非常好'],
  65. anonymous_value: 0,
  66. anonymous_msg_list: ['你写的评论会以匿名的形式展现', '你写的评论会以昵称的形式展现'],
  67. form_rating_list: [],
  68. form_images_list: [],
  69. form_content_list: [],
  70. form_button_disabled: false
  71. };
  72. },
  73. components: {
  74. componentNoData,
  75. componentBottomLine
  76. },
  77. props: {},
  78. onLoad(params) {
  79. this.setData({
  80. params: params
  81. });
  82. },
  83. onShow() {
  84. // 数据加载
  85. this.init();
  86. // 分享菜单处理
  87. app.globalData.page_share_handle();
  88. },
  89. // 下拉刷新
  90. onPullDownRefresh() {
  91. this.init();
  92. },
  93. methods: {
  94. init() {
  95. var self = this;
  96. uni.showLoading({
  97. title: '加载中...'
  98. });
  99. this.setData({
  100. data_list_loding_status: 1
  101. });
  102. uni.request({
  103. url: app.globalData.get_request_url("comments", "order"),
  104. method: 'POST',
  105. data: {
  106. id: this.params.id
  107. },
  108. dataType: 'json',
  109. success: res => {
  110. uni.hideLoading();
  111. uni.stopPullDownRefresh();
  112. if (res.data.code == 0) {
  113. var data = res.data.data;
  114. self.setData({
  115. editor_path_type: data.editor_path_type || '',
  116. detail: data.data,
  117. data_list_loding_status: 3,
  118. data_list_loding_msg: ''
  119. });
  120. } else {
  121. self.setData({
  122. data_list_loding_status: 2,
  123. data_list_loding_msg: res.data.msg
  124. });
  125. if (app.globalData.is_login_check(res.data, self, 'init')) {
  126. app.globalData.showToast(res.data.msg);
  127. }
  128. }
  129. },
  130. fail: () => {
  131. uni.hideLoading();
  132. uni.stopPullDownRefresh();
  133. self.setData({
  134. data_list_loding_status: 2,
  135. data_list_loding_msg: '服务器请求出错'
  136. });
  137. app.globalData.showToast('服务器请求出错');
  138. }
  139. });
  140. },
  141. // 上传图片预览
  142. upload_show_event(e) {
  143. var index = e.currentTarget.dataset.index;
  144. var ix = e.currentTarget.dataset.ix;
  145. uni.previewImage({
  146. current: this.form_images_list[index][ix],
  147. urls: this.form_images_list[index]
  148. });
  149. },
  150. // 图片删除
  151. upload_delete_event(e) {
  152. var index = e.currentTarget.dataset.index;
  153. var ix = e.currentTarget.dataset.ix;
  154. var self = this;
  155. uni.showModal({
  156. title: '温馨提示',
  157. content: '删除后不可恢复、继续吗?',
  158. success(res) {
  159. if (res.confirm) {
  160. var list = self.form_images_list;
  161. list[index].splice(ix, 1);
  162. self.setData({
  163. form_images_list: list
  164. });
  165. }
  166. }
  167. });
  168. },
  169. // 文件上传
  170. file_upload_event(e) {
  171. // 数据初始化
  172. var index = e.currentTarget.dataset.index;
  173. var temp_list = this.form_images_list;
  174. var length = this.detail.items.length;
  175. for (var i = 0; i < length; i++) {
  176. if (temp_list[i] == undefined) {
  177. temp_list[i] = [];
  178. }
  179. }
  180. this.setData({
  181. form_images_list: temp_list
  182. });
  183. // 处理上传文件
  184. var self = this;
  185. uni.chooseImage({
  186. count: 3,
  187. success(res) {
  188. var success = 0;
  189. var fail = 0;
  190. var length = res.tempFilePaths.length;
  191. var count = 0;
  192. self.upload_one_by_one(index, res.tempFilePaths, success, fail, count, length);
  193. }
  194. });
  195. },
  196. // 采用递归的方式上传多张
  197. upload_one_by_one(index, img_paths, success, fail, count, length) {
  198. var self = this;
  199. if ((self.form_images_list[index] || null) == null || self.form_images_list[index].length < 3) {
  200. uni.uploadFile({
  201. url: app.globalData.get_request_url("index", "ueditor"),
  202. filePath: img_paths[count],
  203. name: 'upfile',
  204. formData: {
  205. action: 'uploadimage',
  206. path_type: self.editor_path_type
  207. },
  208. success: function(res) {
  209. success++;
  210. if (res.statusCode == 200) {
  211. var data = typeof res.data == 'object' ? res.data : JSON.parse(res.data);
  212. if (data.code == 0 && (data.data.url || null) != null) {
  213. var list = self.form_images_list;
  214. if ((list[index] || null) == null) {
  215. list[index] = [];
  216. }
  217. list[index].push(data.data.url);
  218. self.setData({
  219. form_images_list: list
  220. });
  221. } else {
  222. app.globalData.showToast(data.msg);
  223. }
  224. }
  225. },
  226. fail: function(e) {
  227. fail++;
  228. },
  229. complete: function(e) {
  230. count++;
  231. // 下一张
  232. if (count >= length) {
  233. // 上传完毕,作一下提示
  234. //app.showToast('上传成功' + success +'张', 'success');
  235. } else {
  236. // 递归调用,上传下一张
  237. self.upload_one_by_one(index, img_paths, success, fail, count, length);
  238. }
  239. }
  240. });
  241. }
  242. },
  243. // 是否匿名事件
  244. anonymous_event(e) {
  245. this.setData({
  246. anonymous_value: e.detail.value == true ? 1 : 0
  247. });
  248. },
  249. // 评分事件
  250. rating_event(e) {
  251. // 参数
  252. var index = e.currentTarget.dataset.index;
  253. var value = e.currentTarget.dataset.value;
  254. // 数据初始化/赋值
  255. var temp_list = this.form_rating_list;
  256. var length = this.detail.items.length;
  257. for (var i = 0; i < length; i++) {
  258. if (index == i) {
  259. temp_list[i] = value;
  260. }
  261. }
  262. this.setData({
  263. form_rating_list: temp_list
  264. });
  265. },
  266. // 评论内容
  267. form_content_event(e) {
  268. // 参数
  269. var index = e.currentTarget.dataset.index;
  270. var value = e.detail.value;
  271. // 数据初始化/赋值
  272. var temp_list = this.form_content_list;
  273. var length = this.detail.items.length;
  274. for (var i = 0; i < length; i++) {
  275. if (temp_list[i] == undefined) {
  276. temp_list[i] = '';
  277. }
  278. if (index == i) {
  279. temp_list[i] = value;
  280. }
  281. }
  282. this.setData({
  283. form_content_list: temp_list
  284. });
  285. },
  286. // 表单
  287. formSubmit(e) {
  288. // 商品数量
  289. var length = this.detail.items.length;
  290. // 评分校验
  291. var count = this.form_rating_list.length;
  292. if (count < length) {
  293. app.globalData.showToast('请先评分');
  294. return false;
  295. }
  296. var max = Math.max.apply(null, this.form_rating_list);
  297. var min = Math.min.apply(null, this.form_rating_list);
  298. if (min < 1 || max > 5) {
  299. app.globalData.showToast('还有未评分');
  300. return false;
  301. }
  302. // 内容校验
  303. var count = this.form_content_list.length;
  304. if (count < length) {
  305. app.globalData.showToast('请填写评论内容');
  306. return false;
  307. }
  308. for (var i in this.form_content_list) {
  309. var count = this.form_content_list[i].length;
  310. if (count < 6 || count > 230) {
  311. app.globalData.showToast('评论内容 6~230 个字符之间');
  312. return false;
  313. }
  314. }
  315. // 图片校验
  316. if (this.form_images_list.length > 0) {
  317. for (var i in this.form_images_list) {
  318. if (this.form_images_list[i].length > 3) {
  319. app.globalData.showToast('每项评论图片不能超过3张');
  320. return false;
  321. }
  322. }
  323. }
  324. // 表单数据
  325. var form_data = e.detail.value;
  326. form_data['is_anonymous'] = form_data['is_anonymous'] == true ? 1 : 0;
  327. form_data['id'] = this.detail.id;
  328. form_data['goods_id'] = JSON.stringify(this.detail.items.map(function(v) {
  329. return v.goods_id;
  330. }));
  331. form_data['rating'] = JSON.stringify(this.form_rating_list);
  332. form_data['content'] = JSON.stringify(this.form_content_list);
  333. form_data['images'] = this.form_images_list.length > 0 ? JSON.stringify(this.form_images_list) : '';
  334. // 提交表单
  335. var self = this;
  336. uni.showLoading({
  337. title: '处理中...'
  338. });
  339. self.setData({
  340. form_button_disabled: true
  341. });
  342. uni.request({
  343. url: app.globalData.get_request_url("commentssave", "order"),
  344. method: 'POST',
  345. data: form_data,
  346. dataType: 'json',
  347. success: res => {
  348. uni.hideLoading();
  349. if (res.data.code == 0) {
  350. app.globalData.showToast(res.data.msg, 'success');
  351. setTimeout(function() {
  352. uni.navigateBack();
  353. }, 2000);
  354. } else {
  355. self.setData({
  356. form_button_disabled: false
  357. });
  358. app.globalData.showToast(res.data.msg);
  359. }
  360. },
  361. fail: () => {
  362. uni.hideLoading();
  363. self.setData({
  364. form_button_disabled: false
  365. });
  366. app.globalData.showToast('服务器请求出错');
  367. }
  368. });
  369. }
  370. }
  371. };
  372. </script>
  373. <style>
  374. @import './user-order-comments.css';
  375. </style>