index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // components/upload/index.js
  2. const qiniuUploader = require("../../utils/qiniuUploader");
  3. const Parse = getApp().Parse
  4. const company = getApp().globalData.company
  5. /**
  6. * @class NovaUpload
  7. * @desc 上传组件
  8. * @memberof module:components
  9. * @desc
  10. * # 一、组件引用
  11. * 模块引用:如app.json根组件引用
  12. * ``` json
  13. * "usingComponents": {
  14. * "upload": "/components/upload/index",
  15. * }
  16. * ```
  17. * 上传令牌获取:xxxpage.js
  18. * ``` js
  19. * async getUptoken() {
  20. * // 根据config.js全局配置company的ID来获取上传口令
  21. let res = await Parse.Cloud.run('qiniu_uptoken', {
  22. company: getApp().globalData.company
  23. })
  24. console.log(Object.keys(res));
  25. console.log(res);
  26. this.setData({
  27. uptokenURL: res.uptoken,
  28. domain: res.domain,
  29. uploadURL: res.zoneUrl
  30. })
  31. },
  32. async onLoad(){
  33. this.getUptoken()
  34. }
  35. * ```
  36. # 二、组件示例
  37. * 模板标签:xxxpage.wxml 页面中使用<upload>标签传参使用
  38. ## 文件上传
  39. * ``` html
  40. * <!-- 文件上传 -->
  41. <view class="text-title">请上传房产证内页或购房合同内页,需露出署名</view>
  42. <view class="title-text1">(您上传的资料仅作认证使用,并交加密处理)</view>
  43. <view class="alboum">
  44. <upload bind:onChangeFile="changeFile" fileList="{{fileList}}" accept="all" maxCount="3" uploadURL="{{uploadURL}}" domain="{{domain}}" uptokenURL="{{uptokenURL}}" />
  45. </view>
  46. * ```
  47. * ``` html
  48. ## 多图上传
  49. * <!-- 多图上传 -->
  50. * <view class="alboum">
  51. <view class="alboum-text">评价图片</view>
  52. <upload bind:onChangeFile="changeFile" fileList="{{fileList}}" accept="image" maxCount="6" uploadURL="{{uploadURL}}" domain="{{domain}}" uptokenURL="{{uptokenURL}}"></upload>
  53. </view>
  54. * ```
  55. * ``` html
  56. ## 单图上传
  57. * <!-- 单图上传 -->
  58. <view class="top-figure">店铺首图</view>
  59. <upload bind:onChangeFile="changeFile" fileList="{{imageList}}" accept="image" maxCount="1" uploadURL="{{uploadURL}}" domain="{{domain}}" uptokenURL="{{uptokenURL}}"></upload>
  60. * ```
  61. */
  62. Component({
  63. /**
  64. * 组件的属性列表
  65. * @memberof module:components.NovaUpload
  66. * @type {object}
  67. * @property {string} accept - 图片地址
  68. * @property {string} icon - 图标
  69. * @property {number} maxCount - 最大数量
  70. * @property {string} loadImg - 未上传前图片格式样式
  71. * @property {string} text - 未上传前文字解释
  72. */
  73. properties: {
  74. accept: {
  75. type: String,
  76. value: "image"
  77. },
  78. icon: {
  79. type: String,
  80. value: "plus"
  81. },
  82. maxCount: {
  83. type: Number,
  84. value: 1
  85. },
  86. maxSize: {
  87. type: Number,
  88. value: null
  89. },
  90. uploadURL: {
  91. type: String,
  92. value: ""
  93. },
  94. domain: {
  95. type: String,
  96. value: ""
  97. },
  98. uptokenURL: {
  99. type: String,
  100. value: ""
  101. },
  102. fileList: {
  103. type: Array,
  104. value: []
  105. },
  106. loadImg: {
  107. type: String,
  108. value: ""
  109. },
  110. text: {
  111. type: String,
  112. value: ""
  113. },
  114. },
  115. /**
  116. * 组件的初始数据
  117. */
  118. data: {
  119. fileList: [],
  120. capture: ['album', 'camera']
  121. },
  122. /**
  123. * 组件的方法列表
  124. * @memberof module:components.NovaUpload
  125. * @type {object}
  126. * @method afterRead - 读取后执行
  127. */
  128. methods: {
  129. /**
  130. * 读取后执行
  131. * @memberof module:components.NovaUpload
  132. * @param {*} event - 事件参数
  133. */
  134. afterRead(event) {
  135. let that = this;
  136. const {
  137. file
  138. } = event.detail;
  139. file.forEach(item => {
  140. let tempFilePaths = item.url;
  141. qiniuUploader.upload(
  142. tempFilePaths,
  143. (res) => {
  144. let {
  145. imageURL,
  146. size
  147. } = res
  148. const {
  149. fileList = []
  150. } = this.data;
  151. fileList.push({
  152. url: imageURL,
  153. size: size
  154. });
  155. this.setData({
  156. fileList
  157. });
  158. this.triggerEvent("onChangeFile", this.data.fileList);
  159. },
  160. (error) => {
  161. console.log("error: " + error);
  162. }, {
  163. region: "SCN",
  164. uploadURL: that.data.uploadURL,
  165. domain: that.data.domain,
  166. uptoken: that.data.uptokenURL,
  167. }
  168. );
  169. })
  170. },
  171. deleteImg(e) {
  172. let fileList = this.data.fileList;
  173. fileList.splice(e.detail.index, 1);
  174. this.setData({
  175. fileList
  176. });
  177. this.triggerEvent("onChangeFile", this.data.fileList);
  178. },
  179. getUptoken() {
  180. return Parse.Cloud.run('qiniu_uptoken', {
  181. company: company
  182. })
  183. },
  184. },
  185. // attachment 表, 资源链接加上companyId
  186. lifetimes: {
  187. async created() {},
  188. async attached() {
  189. let uploadData = await this.getUptoken()
  190. if (uploadData) {
  191. this.setData({
  192. uploadURL: uploadData.zoneUrl,
  193. domain: uploadData.domain,
  194. uptokenURL: uploadData.uptoken,
  195. })
  196. }
  197. },
  198. moved() {},
  199. detached() {},
  200. ready: function () {}
  201. },
  202. })