wx-canvas-2d
Version:
微信小程序 canvas-2d 绘图工具,支持按需引用,支持内容配置,支持功能扩展,适配各种机型,超轻量,超易用,无需学习,直接上手。
1 lines • 3.06 kB
JavaScript
;function _typeof(t){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function ownKeys(e,t){var o,r=Object.keys(e);return Object.getOwnPropertySymbols&&(o=Object.getOwnPropertySymbols(e),t&&(o=o.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),r.push.apply(r,o)),r}function _objectSpread(e){for(var t=1;t<arguments.length;t++){var o=null!=arguments[t]?arguments[t]:{};t%2?ownKeys(Object(o),!0).forEach(function(t){_defineProperty(e,t,o[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(o)):ownKeys(Object(o)).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(o,t))})}return e}function _defineProperty(t,e,o){return(e=_toPropertyKey(e))in t?Object.defineProperty(t,e,{value:o,enumerable:!0,configurable:!0,writable:!0}):t[e]=o,t}function _toPropertyKey(t){t=_toPrimitive(t,"string");return"symbol"==_typeof(t)?t:t+""}function _toPrimitive(t,e){if("object"!=_typeof(t)||!t)return t;var o=t[Symbol.toPrimitive];if(void 0===o)return("string"===e?String:Number)(t);o=o.call(t,e||"default");if("object"!=_typeof(o))return o;throw new TypeError("@@toPrimitive must return a primitive value.")}var getAuth=function(r){return new Promise(function(o,e){wx.getSetting({success:function(e){function t(t){return{settings:e,code:t}}void 0!==e.authSetting["scope."+r]&&!0!==e.authSetting["scope."+r]?o(t(1)):void 0===e.authSetting["scope."+r]?o(t(2)):o(t(3))},fail:function(t){e(t)}})})},saveImageToPhotosAlbum=function(t){return new Promise(function(e,o){wx.saveImageToPhotosAlbum({filePath:t,success:function(t){e()},fail:function(t){o(t)}})})};module.exports={name:"save",handler:function(t){var i=this;return new Promise(function(o,r){var n=_objectSpread({x:0,y:0,width:0,height:0,destWidth:0,destHeight:0,modalOption:{}},t);wx.canvasToTempFilePath({x:n.x,y:n.y,width:n.width,height:n.height,destWidth:i.xDpr(n.destWidth),destHeight:i.xDpr(n.destHeight),canvas:i.canvas,success:function(t){var e=t.tempFilePath;getAuth("writePhotosAlbum").then(function(t){1===t.code?wx.showModal({title:n.modalOption.title||"获取权限",content:n.modalOption.content||"请前往开启相册权限",success:n.modalOption.success||function(t){t.confirm?(wx.openSetting(),i.debugLogout("用户前往授权页","error"),r(Error("用户前往授权页"))):t.cancel&&(i.debugLogout("用户拒绝授权","error"),r(Error("用户拒绝授权")))}}):[2,3].includes(t.code)&&saveImageToPhotosAlbum(e).then(function(t){i.debugLogout("保存图片到相册成功"),o({tempFilePath:e})}).catch(function(){i.debugLogout("保存图片到相册失败","error"),r(Error("保存图片到相册失败"))})}).catch(function(){i.debugLogout("获取设置信息失败","error"),r(Error("获取设置信息失败"))})},fail:function(){i.debugLogout("生成图片失败","error"),r(Error("生成图片失败"))}})})}};