|
楼主 |
发表于 2017-8-18 17:58:24
|
显示全部楼层
本帖最后由 eye215 于 2017-8-18 18:26 编辑
JS完整代码- define(function(require) {
- var $ = require("jquery");
- var justep = require("$UI/system/lib/justep");
- require("$UI/system/lib/cordova/cordova");
- require("cordova!cordova-plugin-camera"); // 相机
- require("cordova!cordova-plugin-file");
- require("cordova!cordova-plugin-inappbrowser");
- require("cordova!com.synconset.imagepicker");
- require("cordova!cordova-plugin-media-capture");
- require("cordova!cordova-plugin-streaming-media"); // 视频播放
- var Model = function() {
- this.callParent();
- };
- // 拍照
- Model.prototype.onCamera = function(event) {
- var self = this;
- if (!navigator.camera) {
- return false;
- }
- var fileData = self.comp("fileData");
- if (fileData.count >= 9) {
- justep.Util.hint("只能上传9张图片!", {
- "type" : "danger",
- "delay" : 1000,
- "position" : "middle"
- });
- return;
- }
- function onSuccess(imageURI) {
- self.dealWithPic(imageURI);
- }
- function onFail(message) {
- alert(message);
- }
- navigator.camera.getPicture(onSuccess, onFail, {
- quality : 50,
- saveToPhotoAlbum : true
- });
- }
- Model.prototype.onVideo = function(event) {
- var self = this;
- var options = {
- limit : 1,
- quality : 1,
- duration : 20
- };
- var captureSuccess = function(mediaFiles) {
- var i, len;
- var fileData = self.comp("fileData");
- for (i = 0, len = mediaFiles.length; i < len; i++) {
- var mediaFile = mediaFiles[i];
- self.video = mediaFile;
- var data = {
- "fileType" : "video",
- "fileInfo" : [ mediaFile ]
- }
- fileData.loadData([ data ]);
- }
- }
- var captureError = function(){
- alert('Error: ' + JSON.stringify(error.code));
- }
- navigator.device.capture.captureVideo(captureSuccess, captureError, options);
- }
- // 相册
- Model.prototype.onPhotos = function(event) {
- var self = this;
- if (!navigator.camera) {
- return false;
- }
- var fileData = self.comp("fileData");
- if (fileData.count >= 9) {
- justep.Util.hint("只能上传9张图片!", {
- "type" : "danger",
- "delay" : 1000,
- "position" : "middle"
- });
- return;
- }
- document.addEventListener("deviceready", onDeviceReady, false);
- function onDeviceReady() {
- imagePicker.getPictures(successCallback, errorCallback, {
- "maximumImagesCount" : 9,// 最大图片选择数量
- "width" : 1000,// 图片的高度和宽度限定,如果都写成0,则按照图片大小显示,不为0且大于该值则进行等比例压缩
- "height" : 1000,
- "quallity" : 50,// 图片质量,取值为0-100
- "outputType" : 0
- // 导出图片的类型,默认为FILE_URI(实际值为0),BASE64_STRING (实际值为1)
- });
- }
- function successCallback(imageURI) {
- if (imageURI.length > 0) {
- var dfds = [];
- for (var i = 0; i < imageURI.length; i++) {
- var dfd = self.dealWithPic(imageURI[i]);
- dfds.push(dfd);
- }
- $.when.apply($, dfds).done(function() {
- // me.comp("demoPopOver").hide();
- });
- } else {
- justep.Util.hint("您未选择图片", {
- "type" : "danger",
- "delay" : 1000,
- "position" : "middle"
- });
- }
- }
- function errorCallback(error) {
- justep.Util.hint("相册照片上传失败");
- }
- }
- // 删除按钮
- Model.prototype.onFileDel = function(event) {
- var parentRow = event.bindingContext.$parent;
- this.comp("fileData").deleteData(parentRow);
- }
- // 播放网络视频
- Model.prototype.onPlay = function(event) {
- var videoUrl = "http://www.runoob.com/try/demo_source/movie.mp4";// http://vjs.zencdn.net/v/oceans.ogv
- // Play a video with callbacks
- var options = {
- successCallback : function() {
- // console.log("Video was closed without error.");
- },
- errorCallback : function(errMsg) {
- // console.log("Error! " + errMsg);
- },
- orientation : 'landscape',
- shouldAutoClose : false, // true(default)/false
- controls : true
- // true(default)/false. Used to hide controls on fullscreen
- };
- window.plugins.streamingMedia.playVideo(videoUrl, options);
- }
- // 播放本地视频
- Model.prototype.onLocalPlay = function(event) {
- var videoUrl = this.video;
- if (videoUrl == '') {
- return;
- }
- // Play a video with callbacks
- var options = {
- successCallback : function() {
- // console.log("Video was closed without error.");
- },
- errorCallback : function(errMsg) {
- // console.log("Error! " + errMsg);
- },
- orientation : 'landscape',
- shouldAutoClose : false, // true(default)/false
- controls : true
- // true(default)/false. Used to hide controls on fullscreen
- };
- window.plugins.streamingMedia.playVideo(videoUrl.fullPath, options);
- }
- // 获得图片的二进制流
- Model.prototype.dealWithPic = function(imageURI, imgname) {
- var self = this;
- var dfd = $.Deferred();
- window.resolveLocalFileSystemURI(imageURI, function(entry) {
- entry.file(win, fail);
- function win(file) {
- var reader = new FileReader();
- reader.readAsDataURL(file);
- // reader.readAsBinaryString(file);
- // reader.readAsArrayBuffer(file);
- reader.onloadend = function(e) {
- var imgUrl = entry.toInternalURL();
- var fileData = self.comp("fileData");
- var image = {};
- image.name = imageURI.substr(imageURI.lastIndexOf('/') + 1);
- image.type = 'image/jpeg';
- // image.size = '';
- image.fullPath = imageURI;
- image.src = imgUrl;
- fileData.loadData([ {
- "fileType" : "image",
- "fileInfo" : [ image ]
- } ], true);
- };
- }
- var fail = function(evt) {
- justep.Util.hint("上传图片失败", {
- "type" : "danger",
- "delay" : 1000,
- "position" : "middle"
- });
- };
- }, function() {
- });
- return dfd.promise();
- };
- return Model;
- });
复制代码
|
|