order-comment.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // pages/order-comment/order-comment.js
  2. var api = require('../../../api.js');
  3. var app = getApp();
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. goods_list: []
  10. },
  11. /**
  12. * 生命周期函数--监听页面加载
  13. */
  14. onLoad: function (options) {
  15. app.pageOnLoad(this);
  16. var page = this;
  17. page.setData({
  18. order_id: options.id,
  19. });
  20. wx.showLoading({
  21. title: "正在加载",
  22. mask: true,
  23. });
  24. app.request({
  25. url: api.book.comment_preview,
  26. data: {
  27. order_id: options.id,
  28. },
  29. success: function (res) {
  30. wx.hideLoading();
  31. if (res.code == 1) {
  32. wx.showModal({
  33. title: "提示",
  34. content: res.msg,
  35. showCancel: false,
  36. success: function (e) {
  37. if (e.confirm) {
  38. wx.navigateBack();
  39. }
  40. }
  41. });
  42. }
  43. if (res.code == 0) {
  44. console.log(res.data.goods_list);
  45. for (var i in res.data.goods_list) {
  46. res.data.goods_list[i].score = 3;
  47. res.data.goods_list[i].content = "";
  48. res.data.goods_list[i].pic_list = [];
  49. res.data.goods_list[i].uploaded_pic_list = [];
  50. }
  51. page.setData({
  52. goods_list: res.data.goods_list,
  53. });
  54. }
  55. }
  56. });
  57. },
  58. setScore: function (e) {
  59. var page = this;
  60. var index = e.currentTarget.dataset.index;
  61. var score = e.currentTarget.dataset.score;
  62. var goods_list = page.data.goods_list;
  63. goods_list[index].score = score;
  64. page.setData({
  65. goods_list: goods_list,
  66. });
  67. },
  68. contentInput: function (e) {
  69. var page = this;
  70. var index = e.currentTarget.dataset.index;
  71. page.data.goods_list[index].content = e.detail.value;
  72. page.setData({
  73. goods_list: page.data.goods_list,
  74. });
  75. },
  76. chooseImage: function (e) {
  77. var page = this;
  78. var index = e.currentTarget.dataset.index;
  79. var max_count = 6;
  80. var goods_list = page.data.goods_list;
  81. var current_count = goods_list[index].pic_list.length;
  82. wx.chooseImage({
  83. count: (max_count - current_count),
  84. success: function (res) {
  85. goods_list[index].pic_list = goods_list[index].pic_list.concat(res.tempFilePaths);
  86. page.setData({
  87. goods_list: goods_list,
  88. });
  89. }
  90. });
  91. },
  92. deleteImage: function (e) {
  93. var page = this;
  94. var index = e.currentTarget.dataset.index;
  95. var pic_index = e.currentTarget.dataset.picIndex;
  96. var goods_list = page.data.goods_list;
  97. goods_list[index].pic_list.splice(pic_index, 1);
  98. page.setData({
  99. goods_list: goods_list
  100. });
  101. },
  102. commentSubmit: function (e) {
  103. var page = this;
  104. wx.showLoading({
  105. title: "正在提交",
  106. mask: true,
  107. });
  108. var goods_list = page.data.goods_list;
  109. uploadImages(0);
  110. function uploadImages(i) {
  111. if (i == goods_list.length) {
  112. return submit();
  113. }
  114. var complete_count = 0;
  115. if (!goods_list[i].pic_list.length || goods_list[i].pic_list.length == 0) {
  116. return uploadImages((i + 1));
  117. }
  118. for (var j in goods_list[i].pic_list) {
  119. (function (j) {
  120. wx.uploadFile({
  121. url: api.default.upload_image,
  122. name: "image",
  123. filePath: goods_list[i].pic_list[j],
  124. complete: function (e) {
  125. if (e.data) {
  126. var res = JSON.parse(e.data);
  127. if (res.code == 0) {
  128. goods_list[i].uploaded_pic_list[j] = res.data.url;
  129. }
  130. }
  131. complete_count++;
  132. if (complete_count == goods_list[i].pic_list.length) {
  133. return uploadImages((i + 1));
  134. }
  135. }
  136. });
  137. })(j);
  138. }
  139. }
  140. function submit() {
  141. app.request({
  142. url: api.book.submit_comment,
  143. method: "post",
  144. data: {
  145. order_id: page.data.order_id,
  146. goods_list: JSON.stringify(goods_list),
  147. },
  148. success: function (res) {
  149. wx.hideLoading();
  150. if (res.code == 0) {
  151. wx.showModal({
  152. title: "提示",
  153. content: res.msg,
  154. showCancel: false,
  155. success: function (e) {
  156. if (e.confirm) {
  157. wx.redirectTo({
  158. url: "/pages/book/order/order?status=2",
  159. });
  160. }
  161. }
  162. });
  163. }
  164. if (res.code == 1) {
  165. wx.showToast({
  166. title: res.msg,
  167. image: "/images/icon-warning.png",
  168. });
  169. }
  170. }
  171. });
  172. }
  173. },
  174. /**
  175. * 生命周期函数--监听页面初次渲染完成
  176. */
  177. onReady: function () {
  178. },
  179. /**
  180. * 生命周期函数--监听页面显示
  181. */
  182. onShow: function () {
  183. },
  184. /**
  185. * 生命周期函数--监听页面隐藏
  186. */
  187. onHide: function () {
  188. },
  189. /**
  190. * 生命周期函数--监听页面卸载
  191. */
  192. onUnload: function () {
  193. },
  194. /**
  195. * 页面相关事件处理函数--监听用户下拉动作
  196. */
  197. onPullDownRefresh: function () {
  198. },
  199. /**
  200. * 页面上拉触底事件的处理函数
  201. */
  202. onReachBottom: function () {
  203. },
  204. });