排队支付小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.5 KiB

  1. const app = getApp();
  2. const Crypto = require('./lib/crypto1/crypto');
  3. const Base64 = require('./lib/crypto1/base64');
  4. const IMG_SIZE_TYPE = {
  5. TYPE_WIDTH: (750 / 570) * 100 | 0,
  6. TYPE_HEIGHT: (750 / 900) * 100 | 0,
  7. TYPE_EQUAL: (750 / 750) * 100 | 0
  8. };
  9. module.exports = {
  10. // 是否是本地默认打卡图片
  11. isDefaultRecordImage(imgPath) {
  12. return imgPath && imgPath.startsWith('/img');
  13. },
  14. // 添加图片裁剪参数
  15. addImageCutParam (owidth, oheight, imgPath) {
  16. let ratio = owidth / oheight, width = owidth, height = oheight;
  17. let result = {};
  18. if ((ratio * 100 | 0) >= IMG_SIZE_TYPE.TYPE_WIDTH) {
  19. width = parseInt(oheight * 750 / 570);
  20. imgPath += "?imageView2/1/w/" + width + "/h/" + (height);
  21. result.width = width;
  22. result.height = height;
  23. } else if ((ratio * 100 | 0) <= IMG_SIZE_TYPE.TYPE_HEIGHT) {
  24. height = parseInt(width * 900 / 750);
  25. imgPath += "?imageView2/1/w/" + (width) + "/h/" + height;
  26. result.width = width;
  27. result.height = height;
  28. } else {
  29. imgPath += "?imageView2/1/w/" + (owidth) + "/h/" + (owidth);
  30. result.width = result.height = width;
  31. }
  32. result.imgPath = imgPath;
  33. return result;
  34. },
  35. // 添加图片缩放、裁剪参数
  36. addImageCutParamAli(owidth, oheight, imgPath) {
  37. const MAX_LEN = 4096;
  38. let ratio = owidth / oheight, width = owidth, height = oheight;
  39. imgPath += '?x-oss-process=image';
  40. // 图片宽高大于4096时,需要先进行缩放
  41. if (owidth > MAX_LEN || oheight > MAX_LEN) {
  42. imgPath += `/resize,h_${MAX_LEN},w_${MAX_LEN}`;
  43. // 这里需要重新计算缩放后的图片大小
  44. if (owidth > oheight) {
  45. width = MAX_LEN;
  46. let zoomRatio = width / owidth;
  47. height = parseInt(zoomRatio * oheight);
  48. } else if (owidth < oheight) {
  49. height = MAX_LEN;
  50. let zoomRatio = height / oheight;
  51. width = parseInt(zoomRatio * owidth);
  52. } else {
  53. width = height = MAX_LEN;
  54. }
  55. }
  56. // 裁剪
  57. ratio = width / height;
  58. let result = {};
  59. if ((ratio * 100 | 0) >= IMG_SIZE_TYPE.TYPE_WIDTH) {
  60. width = parseInt(oheight * 750 / 570);
  61. imgPath += `/crop,w_${width},h_${height},g_center`;
  62. result.width = width;
  63. result.height = height;
  64. } else if ((ratio * 100 | 0) <= IMG_SIZE_TYPE.TYPE_HEIGHT) {
  65. height = parseInt(width * 900 / 750);
  66. imgPath += `/crop,w_${width},h_${height},g_center`;
  67. result.width = width;
  68. result.height = height;
  69. } else {
  70. imgPath += `/crop,w_${width},h_${width},g_center`;
  71. result.width = result.height = width;
  72. }
  73. result.imgPath = imgPath;
  74. return result;
  75. },
  76. getMultipartParams() {
  77. let policyText = {
  78. "expiration": "2020-01-01T12:00:00.000Z", // 设置该Policy的失效时间,超过这个失效时间之后,就没有办法通过这个policy上传文件了
  79. "conditions": [
  80. ["content-length-range", 0, 1048576000] // 设置上传文件的大小限制
  81. ]
  82. };
  83. let policyBase64 = Base64.encode(JSON.stringify(policyText));
  84. let bytes = Crypto.HMAC(Crypto.SHA1, policyBase64, app.globalData.config.aliAccessKey, { asBytes: true }) ;
  85. let signature = Crypto.util.bytesToBase64(bytes);
  86. return {
  87. 'Filename': 'wx-applet-fittime/' + '${filename}',
  88. 'key': '',
  89. 'policy': policyBase64,
  90. 'OSSAccessKeyId': app.globalData.config.aliAccessId,
  91. 'success_action_status': '200', // 让服务端返回200,不然,默认会返回204
  92. 'signature': signature
  93. };
  94. }
  95. };