postReview.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. define(['$', 'template', 'autosize', 'order', 'api', 'config', 'native', 'user'], function ($, template, autosize, order, api, config, native, user) {
  2. function versionCompare(v1, v2) {
  3. v1 = v1.split('.');
  4. v2 = v2.split('.');
  5. var len = Math.max(v1.length, v2.length);
  6. for (var i = 0; i < len; i++) {
  7. v1[i] = "undefined"===typeof v1[i] ? 0 : parseInt(v1[i], 10);
  8. v2[i] = "undefined"===typeof v2[i] ? 0 : parseInt(v2[i], 10);
  9. if(v1[i] > v2[i]){return 1;}
  10. if(v1[i] < v2[i]){return -1;}
  11. }
  12. return 0;
  13. }
  14. var cachePageData = {
  15. bucket: 'pics',
  16. token: ''
  17. }
  18. var images = {
  19. localId: [],
  20. serverId: [],
  21. list: [],
  22. count: 9
  23. };
  24. return {
  25. body: '',
  26. init: function (panelData, dtd) {
  27. var $view = this;
  28. var $doc = $(document);
  29. //新增评价
  30. $.newTouch('.btn-post', function(event) {
  31. if ($(this).hasClass('disable')) {
  32. return false;
  33. }
  34. var pushData = panelData.pushData;
  35. var orderID = pushData.orderID;
  36. var textarea = $('.review-content', $view);
  37. var score = $('.star-item.enable', $view).length;
  38. $('.review-content', $view).blur();
  39. $doc.trigger('spa:openloader');
  40. api.postReview({
  41. user_id: user.id,
  42. order_id: orderID,
  43. content: textarea.val(),
  44. score: score,
  45. pics: JSON.stringify(images.serverId)
  46. }, function (res) {
  47. $('.review-content', $view).val('');
  48. $doc.trigger('spa:closeloader');
  49. panelData.postReview = true;
  50. $view.trigger('spa:closepanel');
  51. });
  52. }, $view);
  53. // 选择
  54. $.newTouch('.img-upload-btn', function(event) {
  55. if (config.isAndroid || config.isIOS) {
  56. native.chooseImage({
  57. sourceType: ['album', 'camera'],
  58. count: images['count'],
  59. clip: false,
  60. clipSize: 320
  61. }, function(res){
  62. if (res.success) {
  63. images.localId = res.data.localIds;
  64. var i = 0, length = images.localId.length;
  65. function upload() {
  66. native.uploadImage({
  67. localId: images.localId[i],
  68. isShowProgressTips: true
  69. }, function(res) {
  70. if (res.success) {
  71. images['count'] --;
  72. images.serverId.push(res.data);
  73. images.list.push(res.data.url);
  74. var html = '<div class="width-percent-25">'+
  75. '<div class="img-preview">' +
  76. '<img src="' + res.data.url + '?imageView2/5/w/60/h/60' + '" class="img-preview-item" data-src="'+ res.data.url +'">' +
  77. '<span class="btn-preview-close"></span>' +
  78. '</div>' +
  79. '</div>';
  80. $('.img-upload').before(html);
  81. i ++;
  82. if (i < length) {
  83. upload();
  84. }
  85. if (images['count'] < 1) {
  86. $('.img-upload-btn').hide();
  87. }
  88. }
  89. })
  90. }
  91. upload();
  92. }
  93. });
  94. }
  95. }, $view);
  96. // 微信上传
  97. $view.on('change', '#wx-upload', function(event) {
  98. if (config.isWX) {
  99. var files = event.target.files;
  100. if (files.length > 9) return;
  101. if (files.length > images['count']) return;
  102. var defaults = {
  103. qiniu_upload_url : 'http://up.qiniu.com',
  104. bucket: cachePageData.bucket,
  105. url_prefix: 'http://'+ cachePageData.bucket +'.qiniudn.com',
  106. }
  107. for (var i = 0; i < files.length; i++) {
  108. var formData, startDate;
  109. formData = new FormData();
  110. formData.append('token', cachePageData.token);
  111. formData.append('file', files[i]);
  112. var name = files[i].name;
  113. var point = name.lastIndexOf('.'),
  114. type = name.substr(point),
  115. key = Math.random().toString(16).substring(2) + (+new Date()) + type;
  116. formData.append('key', key);
  117. $.ajax({
  118. url: 'http://up.qiniu.com' ,
  119. type: 'POST',
  120. data: formData,
  121. async: false,
  122. cache: false,
  123. contentType: false,
  124. processData: false,
  125. success: function (data) {
  126. images['count'] --;
  127. var url = defaults.url_prefix+'/'+data.key;
  128. var param = {
  129. "width": 1,
  130. "height": 1,
  131. "url": url
  132. }
  133. images.serverId.push(param);
  134. images.list.push(url);
  135. var html = '<div class="width-percent-25">'+
  136. '<div class="img-preview">' +
  137. '<img src="' + url + '?imageView2/5/w/60/h/60" class="img-preview-item" data-src="'+ url +'">' +
  138. '<span class="btn-preview-close"></span>' +
  139. '</div>' +
  140. '</div>';
  141. $('.img-upload').before(html);
  142. if (images['count'] < 1) {
  143. $('.img-upload-btn').hide();
  144. }
  145. },
  146. error: function (data) {
  147. console.log(data);
  148. }
  149. });
  150. }
  151. }
  152. });
  153. // 预览图片
  154. $.newTouch('.img-preview-item', function(event) {
  155. // event.preventDefault();
  156. var current = $(this).attr('data-src');
  157. native.previewImage(images.list, current);
  158. }, $view);
  159. // 删除图片
  160. $.newTouch('.btn-preview-close', function(event) {
  161. event.preventDefault();
  162. // event.stopPropagation();
  163. var i = $(this).parents('.width-percent-25').index();
  164. images.serverId.splice(i, 1);
  165. images.list.splice(i, 1);
  166. images['count'] ++;
  167. if (images['count'] > 0) {
  168. $('.img-upload-btn').show();
  169. }
  170. $(this).parents('.width-percent-25').hide('500');
  171. // $(this).parents('.width-percent-25').remove();
  172. }, $view);
  173. //切换评分
  174. $.newTouch('.star-item', function(event) {
  175. var enable = true;
  176. var that = this;
  177. $(this).parent('.box').find('.star-item').each(function() {
  178. if (enable) {
  179. $(this).addClass('enable');
  180. } else {
  181. $(this).removeClass('enable');
  182. }
  183. if (that == this) {
  184. enable = false;
  185. }
  186. });
  187. }, $view);
  188. dtd.resolve();
  189. },
  190. beforeopen: function (panelData, dtd) {
  191. var $view = this;
  192. var pushData = panelData.pushData;
  193. $('.spa-page-bg', $view).css({
  194. opacity: 0
  195. }).transition({
  196. opacity: 0.6
  197. });
  198. images = {
  199. localId: [],
  200. serverId: [],
  201. list: [],
  202. count: 9
  203. };
  204. if (config.test) {
  205. cachePageData.bucket = 'test'
  206. }
  207. api.gettoken({
  208. request_from: 'weixin',
  209. user_id: user.id,
  210. bucket: cachePageData.bucket
  211. }, function(res){
  212. if (res.success) {
  213. cachePageData.token = res.data.token
  214. }
  215. });
  216. var tpl = template('review/post', {
  217. cacheReview: order.cacheReview,
  218. showInput: config.isWX,
  219. showUpload: versionCompare(config.appVersion, '2.6') >=0 || config.isWX,
  220. score: 5
  221. });
  222. $view.find('.spa-page-body').html(tpl);
  223. //自适应输入框
  224. var textarea = $('.review-content', $view);
  225. textarea.on('keyup, resize, input', function(event) {
  226. adjustPostButton.call(this);
  227. });
  228. autosize(textarea);
  229. adjustPostButton.call(textarea);
  230. dtd.resolve();
  231. function adjustPostButton(){
  232. var postButton = $('.btn-post', $view);
  233. if ($(this).val().length >= 10) {
  234. if (postButton.hasClass('disable')) {
  235. postButton.removeClass('disable');
  236. }
  237. } else {
  238. if (!postButton.hasClass('disable')) {
  239. postButton.addClass('disable');
  240. }
  241. }
  242. }
  243. },
  244. beforeclose: function (panelData, dtd) {
  245. var $view = this;
  246. $('.review-content', $view).blur();
  247. var cacheText = $('.review-content', $view).val();
  248. order.set('cacheReview', cacheText);
  249. $('.spa-page-bg', $view).transition({
  250. opacity: 0
  251. });
  252. dtd.resolve();
  253. },
  254. afterclose: function (panelData) {
  255. var $view = this;
  256. var pushData = panelData.pushData;
  257. if (panelData.postReview && pushData.didPostReview) {
  258. panelData.postReview = false;
  259. pushData.didPostReview();
  260. }
  261. }
  262. }
  263. })