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. console.log(event.target);
  99. if (config.isWX) {
  100. var files = event.target.files;
  101. if (files.length > 9) return;
  102. if (files.length > images['count']) return;
  103. var defaults = {
  104. qiniu_upload_url : 'http://up.qiniu.com',
  105. bucket: cachePageData.bucket,
  106. url_prefix: 'http://'+ cachePageData.bucket +'.qiniudn.com',
  107. }
  108. for (var i = 0; i < files.length; i++) {
  109. var formData, startDate;
  110. formData = new FormData();
  111. formData.append('token', cachePageData.token);
  112. formData.append('file', files[i]);
  113. var name = files[i].name;
  114. var point = name.lastIndexOf('.'),
  115. type = name.substr(point),
  116. key = Math.random().toString(16).substring(2) + (+new Date()) + type;
  117. formData.append('key', key);
  118. $.ajax({
  119. url: 'http://up.qiniu.com' ,
  120. type: 'POST',
  121. data: formData,
  122. async: false,
  123. cache: false,
  124. contentType: false,
  125. processData: false,
  126. success: function (data) {
  127. images['count'] --;
  128. var url = defaults.url_prefix+'/'+data.key;
  129. var param = {
  130. "width": 1,
  131. "height": 1,
  132. "url": url
  133. }
  134. images.serverId.push(param);
  135. images.list.push(url);
  136. var html = '<div class="width-percent-25">'+
  137. '<div class="img-preview">' +
  138. '<img src="' + url + '?imageView2/5/w/60/h/60" class="img-preview-item" data-src="'+ url +'">' +
  139. '<span class="btn-preview-close"></span>' +
  140. '</div>' +
  141. '</div>';
  142. $('.img-upload').before(html);
  143. if (images['count'] < 1) {
  144. $('.img-upload-btn').hide();
  145. }
  146. },
  147. error: function (data) {
  148. console.log(data);
  149. }
  150. });
  151. }
  152. }
  153. });
  154. // 预览图片
  155. $.newTouch('.img-preview-item', function(event) {
  156. // event.preventDefault();
  157. var current = $(this).attr('data-src');
  158. native.previewImage(images.list, current);
  159. }, $view);
  160. // 删除图片
  161. $.newTouch('.btn-preview-close', function(event) {
  162. event.preventDefault();
  163. // event.stopPropagation();
  164. var i = $(this).parents('.width-percent-25').index();
  165. images.serverId.splice(i, 1);
  166. images.list.splice(i, 1);
  167. images['count'] ++;
  168. if (images['count'] > 0) {
  169. $('.img-upload-btn').show();
  170. }
  171. $(this).parents('.width-percent-25').hide('500');
  172. // $(this).parents('.width-percent-25').remove();
  173. }, $view);
  174. //切换评分
  175. $.newTouch('.star-item', function(event) {
  176. var enable = true;
  177. var that = this;
  178. $(this).parent('.box').find('.star-item').each(function() {
  179. if (enable) {
  180. $(this).addClass('enable');
  181. } else {
  182. $(this).removeClass('enable');
  183. }
  184. if (that == this) {
  185. enable = false;
  186. }
  187. });
  188. }, $view);
  189. dtd.resolve();
  190. },
  191. beforeopen: function (panelData, dtd) {
  192. var $view = this;
  193. var pushData = panelData.pushData;
  194. $('.spa-page-bg', $view).css({
  195. opacity: 0
  196. }).transition({
  197. opacity: 0.6
  198. });
  199. images = {
  200. localId: [],
  201. serverId: [],
  202. list: [],
  203. count: 9
  204. };
  205. if (config.test) {
  206. cachePageData.bucket = 'test'
  207. }
  208. api.gettoken({
  209. request_from: 'weixin',
  210. user_id: user.id,
  211. bucket: cachePageData.bucket
  212. }, function(res){
  213. if (res.success) {
  214. cachePageData.token = res.data.token
  215. }
  216. });
  217. var tpl = template('review/post', {
  218. cacheReview: order.cacheReview,
  219. showInput: config.isWX,
  220. showUpload: versionCompare(config.appVersion, '2.6') >=0 || config.isWX,
  221. score: 5
  222. });
  223. $view.find('.spa-page-body').html(tpl);
  224. //自适应输入框
  225. var textarea = $('.review-content', $view);
  226. textarea.on('keyup, resize, input', function(event) {
  227. adjustPostButton.call(this);
  228. });
  229. autosize(textarea);
  230. adjustPostButton.call(textarea);
  231. dtd.resolve();
  232. function adjustPostButton(){
  233. var postButton = $('.btn-post', $view);
  234. if ($(this).val().length >= 10) {
  235. if (postButton.hasClass('disable')) {
  236. postButton.removeClass('disable');
  237. }
  238. } else {
  239. if (!postButton.hasClass('disable')) {
  240. postButton.addClass('disable');
  241. }
  242. }
  243. }
  244. },
  245. beforeclose: function (panelData, dtd) {
  246. var $view = this;
  247. $('.review-content', $view).blur();
  248. var cacheText = $('.review-content', $view).val();
  249. order.set('cacheReview', cacheText);
  250. $('.spa-page-bg', $view).transition({
  251. opacity: 0
  252. });
  253. dtd.resolve();
  254. },
  255. afterclose: function (panelData) {
  256. var $view = this;
  257. var pushData = panelData.pushData;
  258. if (panelData.postReview && pushData.didPostReview) {
  259. panelData.postReview = false;
  260. pushData.didPostReview();
  261. }
  262. }
  263. }
  264. })