image-picker.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * 统一图片选择器(微信小程序支持「从聊天记录选择图片」)
  3. *
  4. * 背景:
  5. * uni.chooseImage 只能从 album(手机相册)或 camera(拍照)选图,
  6. * 无法直接选择聊天记录里的图片;微信小程序要从聊天记录选图,
  7. * 必须调用 uni.chooseMessageFile({ type: 'image' })。
  8. * 为保证小程序端所有图片上传入口都能从聊天记录选图,统一使用本方法。
  9. *
  10. * 用法与 uni.chooseImage 基本一致(回调风格):
  11. * pickImage({
  12. * count: 1,
  13. * sizeType: ['compressed'],
  14. * sourceType: ['album', 'camera'], // 可选,默认 ['album', 'camera']
  15. * success: function (res) {
  16. * // res.tempFilePaths: 图片本地临时路径数组
  17. * // res.tempFiles: [{ path, size, name, type }]
  18. * },
  19. * fail: function (err) {}
  20. * })
  21. *
  22. * 说明:
  23. * 微信小程序下会先弹出 ActionSheet:拍照 / 从相册选择 / 从聊天记录选择,
  24. * 前两项走 uni.chooseImage,最后一项走 uni.chooseMessageFile。
  25. * 非微信平台(H5/App 等)不支持聊天记录选图,保持原 uni.chooseImage 行为。
  26. */
  27. export function pickImage(options) {
  28. var opts = options || {}
  29. var count = opts.count || 9
  30. var sizeType = opts.sizeType || ['original', 'compressed']
  31. var sourceType = opts.sourceType || ['album', 'camera']
  32. var success = opts.success
  33. var fail = opts.fail
  34. var complete = opts.complete
  35. function onSuccess(res) {
  36. if (success) success(res)
  37. if (complete) complete(res)
  38. }
  39. function onFail(err) {
  40. if (fail) fail(err)
  41. if (complete) complete(err)
  42. }
  43. // sourceType 只支持单个来源时(如 ['camera'])直接调起对应入口
  44. function callChooseImage(types) {
  45. uni.chooseImage({
  46. count: count,
  47. sizeType: sizeType,
  48. sourceType: types,
  49. success: onSuccess,
  50. fail: onFail
  51. })
  52. }
  53. // #ifdef MP-WEIXIN
  54. // 微信小程序:先选来源,再从聊天记录 / 相册 / 相机取图
  55. var actionList = []
  56. if (sourceType.indexOf('camera') > -1) actionList.push({ label: '拍照', type: 'camera' })
  57. if (sourceType.indexOf('album') > -1) actionList.push({ label: '从相册选择', type: 'album' })
  58. actionList.push({ label: '从聊天记录选择', type: 'chat' })
  59. var itemList = []
  60. for (var i = 0; i < actionList.length; i++) itemList.push(actionList[i].label)
  61. uni.showActionSheet({
  62. itemList: itemList,
  63. success: function (sheetRes) {
  64. var action = actionList[sheetRes.tapIndex]
  65. if (!action) return
  66. if (action.type !== 'chat') {
  67. callChooseImage([action.type])
  68. return
  69. }
  70. if (typeof uni.chooseMessageFile !== 'function') {
  71. // 低版本基础库不支持聊天记录选图,降级为相册选图
  72. callChooseImage(['album'])
  73. return
  74. }
  75. uni.chooseMessageFile({
  76. count: count,
  77. type: 'image',
  78. success: function (res) {
  79. var files = []
  80. var tempFiles = res.tempFiles || []
  81. for (var j = 0; j < tempFiles.length; j++) {
  82. files.push({
  83. path: tempFiles[j].path,
  84. size: tempFiles[j].size,
  85. name: tempFiles[j].name,
  86. type: tempFiles[j].type
  87. })
  88. }
  89. var paths = []
  90. for (var k = 0; k < files.length; k++) paths.push(files[k].path)
  91. onSuccess({ tempFilePaths: paths, tempFiles: files })
  92. },
  93. fail: onFail
  94. })
  95. },
  96. fail: onFail
  97. })
  98. // #endif
  99. // #ifndef MP-WEIXIN
  100. // 非微信平台不支持聊天记录选图,保持原行为
  101. callChooseImage(sourceType)
  102. // #endif
  103. }