123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- define(['$', 'template', 'autosize', 'order', 'api', 'config', 'native', 'user'], function ($, template, autosize, order, api, config, native, user) {
- function versionCompare(v1, v2) {
- v1 = v1.split('.');
- v2 = v2.split('.');
- var len = Math.max(v1.length, v2.length);
- for (var i = 0; i < len; i++) {
- v1[i] = "undefined"===typeof v1[i] ? 0 : parseInt(v1[i], 10);
- v2[i] = "undefined"===typeof v2[i] ? 0 : parseInt(v2[i], 10);
- if(v1[i] > v2[i]){return 1;}
- if(v1[i] < v2[i]){return -1;}
- }
- return 0;
- }
- var cachePageData = {
- bucket: 'pics',
- token: ''
- }
- var images = {
- localId: [],
- serverId: [],
- list: [],
- count: 9
- };
- return {
- body: '',
- init: function (panelData, dtd) {
- var $view = this;
- var $doc = $(document);
- //新增评价
- $.newTouch('.btn-post', function(event) {
- if ($(this).hasClass('disable')) {
- return false;
- }
- var pushData = panelData.pushData;
- var orderID = pushData.orderID;
- var textarea = $('.review-content', $view);
- var score = $('.star-item.enable', $view).length;
- $('.review-content', $view).blur();
- $doc.trigger('spa:openloader');
- api.postReview({
- user_id: user.id,
- order_id: orderID,
- content: textarea.val(),
- score: score,
- pics: JSON.stringify(images.serverId)
- }, function (res) {
- $('.review-content', $view).val('');
- $doc.trigger('spa:closeloader');
- panelData.postReview = true;
- $view.trigger('spa:closepanel');
- });
- }, $view);
- // 选择
- $.newTouch('.img-upload-btn', function(event) {
- if (config.isAndroid || config.isIOS) {
- native.chooseImage({
- sourceType: ['album', 'camera'],
- count: images['count'],
- clip: false,
- clipSize: 320
- }, function(res){
- if (res.success) {
- images.localId = res.data.localIds;
- var i = 0, length = images.localId.length;
- function upload() {
- native.uploadImage({
- localId: images.localId[i],
- isShowProgressTips: true
- }, function(res) {
- if (res.success) {
- images['count'] --;
- images.serverId.push(res.data);
- images.list.push(res.data.url);
- var html = '<div class="width-percent-25">'+
- '<div class="img-preview">' +
- '<img src="' + res.data.url + '?imageView2/5/w/60/h/60' + '" class="img-preview-item" data-src="'+ res.data.url +'">' +
- '<span class="btn-preview-close"></span>' +
- '</div>' +
- '</div>';
- $('.img-upload').before(html);
- i ++;
- if (i < length) {
- upload();
- }
- if (images['count'] < 1) {
- $('.img-upload-btn').hide();
- }
- }
- })
- }
- upload();
- }
- });
- }
- }, $view);
- // 微信上传
- $view.on('change', '#wx-upload', function(event) {
- if (config.isWX) {
- var files = event.target.files;
- if (files.length > 9) return;
- if (files.length > images['count']) return;
- var defaults = {
- qiniu_upload_url : 'http://up.qiniu.com',
- bucket: cachePageData.bucket,
- url_prefix: 'http://'+ cachePageData.bucket +'.qiniudn.com',
- }
- for (var i = 0; i < files.length; i++) {
- var formData, startDate;
- formData = new FormData();
- formData.append('token', cachePageData.token);
- formData.append('file', files[i]);
- var name = files[i].name;
- var point = name.lastIndexOf('.'),
- type = name.substr(point),
- key = Math.random().toString(16).substring(2) + (+new Date()) + type;
- formData.append('key', key);
- $.ajax({
- url: 'http://up.qiniu.com' ,
- type: 'POST',
- data: formData,
- async: false,
- cache: false,
- contentType: false,
- processData: false,
- success: function (data) {
- images['count'] --;
- var url = defaults.url_prefix+'/'+data.key;
- var param = {
- "width": 1,
- "height": 1,
- "url": url
- }
- images.serverId.push(param);
- images.list.push(url);
- var html = '<div class="width-percent-25">'+
- '<div class="img-preview">' +
- '<img src="' + url + '?imageView2/5/w/60/h/60" class="img-preview-item" data-src="'+ url +'">' +
- '<span class="btn-preview-close"></span>' +
- '</div>' +
- '</div>';
- $('.img-upload').before(html);
- if (images['count'] < 1) {
- $('.img-upload-btn').hide();
- }
- },
- error: function (data) {
- console.log(data);
- }
- });
- }
- }
- });
- // 预览图片
- $.newTouch('.img-preview-item', function(event) {
- // event.preventDefault();
- var current = $(this).attr('data-src');
- native.previewImage(images.list, current);
- }, $view);
- // 删除图片
- $.newTouch('.btn-preview-close', function(event) {
- event.preventDefault();
- // event.stopPropagation();
- var i = $(this).parents('.width-percent-25').index();
- images.serverId.splice(i, 1);
- images.list.splice(i, 1);
- images['count'] ++;
- if (images['count'] > 0) {
- $('.img-upload-btn').show();
- }
- $(this).parents('.width-percent-25').hide('500');
- // $(this).parents('.width-percent-25').remove();
- }, $view);
- //切换评分
- $.newTouch('.star-item', function(event) {
- var enable = true;
- var that = this;
- $(this).parent('.box').find('.star-item').each(function() {
- if (enable) {
- $(this).addClass('enable');
- } else {
- $(this).removeClass('enable');
- }
- if (that == this) {
- enable = false;
- }
- });
- }, $view);
- dtd.resolve();
- },
- beforeopen: function (panelData, dtd) {
- var $view = this;
- var pushData = panelData.pushData;
- $('.spa-page-bg', $view).css({
- opacity: 0
- }).transition({
- opacity: 0.6
- });
- images = {
- localId: [],
- serverId: [],
- list: [],
- count: 9
- };
- if (config.test) {
- cachePageData.bucket = 'test'
- }
- api.gettoken({
- request_from: 'weixin',
- user_id: user.id,
- bucket: cachePageData.bucket
- }, function(res){
- if (res.success) {
- cachePageData.token = res.data.token
- }
- });
- var tpl = template('review/post', {
- cacheReview: order.cacheReview,
- showInput: config.isWX,
- showUpload: versionCompare(config.appVersion, '2.6') >=0 || config.isWX,
- score: 5
- });
- $view.find('.spa-page-body').html(tpl);
- //自适应输入框
- var textarea = $('.review-content', $view);
- textarea.on('keyup, resize, input', function(event) {
- adjustPostButton.call(this);
- });
- autosize(textarea);
- adjustPostButton.call(textarea);
- dtd.resolve();
- function adjustPostButton(){
- var postButton = $('.btn-post', $view);
- if ($(this).val().length >= 10) {
- if (postButton.hasClass('disable')) {
- postButton.removeClass('disable');
- }
- } else {
- if (!postButton.hasClass('disable')) {
- postButton.addClass('disable');
- }
- }
- }
- },
- beforeclose: function (panelData, dtd) {
- var $view = this;
- $('.review-content', $view).blur();
- var cacheText = $('.review-content', $view).val();
- order.set('cacheReview', cacheText);
- $('.spa-page-bg', $view).transition({
- opacity: 0
- });
- dtd.resolve();
- },
- afterclose: function (panelData) {
- var $view = this;
- var pushData = panelData.pushData;
- if (panelData.postReview && pushData.didPostReview) {
- panelData.postReview = false;
- pushData.didPostReview();
- }
- }
- }
- })
|