sharePosts.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <template>
  2. <view class="sharePosts">
  3. <zs-header title="研学心得" color="#000"></zs-header>
  4. <view class="inputPosts">
  5. <u--input border="none" fontSize="44rpx" placeholderClass="placeholderClass" :customStyle="{
  6. borderBottom: '1px solid #f0f0f0',
  7. height: '120rpx'
  8. }" placeholder="请填写标题" v-model="form.communityTitle"></u--input>
  9. <u--textarea autoHeight border="none" v-model="form.communityDetail" placeholder="快来记录你的心路历程"></u--textarea>
  10. <u-upload uploadIcon="plus" width="164" height="164" :fileList="fileList" @afterRead="afterRead"
  11. @delete="deletePic" name="1" :maxCount="9" @deletePic="deletePic" @oversize="oversize" accept="image"
  12. :maxSize="10485760" previewImage="true"></u-upload>
  13. </view>
  14. <view class="buy-box">
  15. <button class="buy-btn" :loading="loading" type="default" @click="handleUpLoad">确定</button>
  16. </view>
  17. </view>
  18. </template>
  19. <script>
  20. import {
  21. uploadImg, finishUploadImg,
  22. } from '@/api/common.js';
  23. import { goodsSaveCommunityItem,goodsCommunityItemDetail } from "@/api/study"
  24. import crypto from 'crypto-js';
  25. import { Base64 } from 'js-base64';
  26. export default {
  27. data() {
  28. return {
  29. fileList: [],
  30. loading: false,
  31. form: {
  32. id: '',
  33. orderNo: '',
  34. communityTitle: '',
  35. communityDetail: '',
  36. urls: []
  37. }
  38. };
  39. },
  40. methods: {
  41. oversize() {
  42. uni.showToast({
  43. title: '图片过大',
  44. icon: 'none'
  45. });
  46. },
  47. async afterRead(event) {
  48. console.log(event)
  49. const { file } = event
  50. this.fileList.push(
  51. {
  52. ...file,
  53. status: 'uploading',
  54. message: '上传中'
  55. }
  56. )
  57. uploadImg({
  58. "fineName": file.url.substr(file.url.lastIndexOf('/') + 1)
  59. }).then(res => {
  60. if (res.state == 'Success') {
  61. const date = new Date();
  62. date.setHours(date.getHours() + 1);
  63. const policyText = {
  64. expiration: date.toISOString(), // 设置policy过期时间。
  65. conditions: [
  66. // 限制上传大小。
  67. ["content-length-range", 0, 1024 * 1024 * 1024],
  68. ],
  69. };
  70. let fileName = file.url.substr(file.url.lastIndexOf('/') + 1);
  71. const host = 'https://' + res.content.bucket + '.' + res.content.endPoint;
  72. const policy = Base64.encode(JSON.stringify(policyText));
  73. const signature = crypto.enc.Base64.stringify(crypto.HmacSHA1(policy, res.content.token.accessKeySecret));;
  74. const filePath = file.url; // 待上传文件的文件路径。
  75. let paths = res.content.paths[0]
  76. // paths.pop()
  77. uni.uploadFile({
  78. url: host, // 开发者服务器的URL。
  79. filePath: filePath,
  80. name: 'file', // 必须填file。
  81. formData: {
  82. key: paths.join('/'),
  83. policy,
  84. success_action_status: '200', //让服务端返回200,不然,默认会返回204
  85. OSSAccessKeyId: res.content.token.accessKeyId,
  86. signature,
  87. 'x-oss-security-token': res.content.token.securityToken // 使用STS签名时必传。
  88. },
  89. success: (res1) => {
  90. if (res1.statusCode === 200) {
  91. console.log('上传成功');
  92. // 告知服务器上传成功地址
  93. finishUploadImg(
  94. [{
  95. path: res.content.paths[0]
  96. }]).then(msg => {
  97. this.fileList.forEach((item, index) => {
  98. if (item.thumb == file.thumb) {
  99. this.fileList[index].status = 'success'
  100. this.fileList[index].message = ''
  101. this.fileList[index].url = host + '/' + paths.join('/')
  102. }
  103. })
  104. console.log(this.fileList)
  105. })
  106. }
  107. },
  108. fail: err => {
  109. console.log(err);
  110. }
  111. });
  112. }
  113. })
  114. },
  115. deletePic({ index }) {
  116. this.fileList.splice(index, 1)
  117. },
  118. handleUpLoad() {
  119. if (this.form.communityTitle && this.form.communityDetail) {
  120. this.form.urls = this.fileList.map(item => item.url)
  121. this.loading = true
  122. uni.showLoading({
  123. title: '保存中'
  124. });
  125. goodsSaveCommunityItem({
  126. ...this.form,
  127. urls: this.fileList.map(e => e.url)
  128. }).then(res => {
  129. this.loading = false
  130. uni.hideLoading();
  131. if (res.state == 'Success') {
  132. uni.showToast({
  133. title: '上传成功',
  134. icon: 'success'
  135. });
  136. setTimeout(() => {
  137. uni.navigateBack()
  138. }, 1000)
  139. }
  140. })
  141. } else {
  142. uni.showToast({
  143. title: '请填写标题和内容',
  144. icon: 'none'
  145. });
  146. }
  147. },
  148. getPostDetail(id,orderNo){
  149. goodsCommunityItemDetail(id).then(res=>{
  150. if(res.state == 'Success'){
  151. this.form = res.content
  152. this.form.id = id
  153. this.form.orderNo = orderNo
  154. this.fileList = res.content.urls.map(e=>{
  155. return {
  156. url:e,
  157. status:'success'
  158. }
  159. })
  160. }
  161. })
  162. }
  163. },
  164. onLoad({ orderNo,id }) {
  165. this.form.orderNo = orderNo;
  166. if(id){
  167. this.getPostDetail(id,orderNo)
  168. }
  169. }
  170. };
  171. </script>
  172. <style lang="scss" scoped>
  173. .sharePosts {
  174. background: #FFFFFF;
  175. .inputPosts {
  176. margin-top: 174rpx;
  177. padding: 20rpx;
  178. border-top: 1px solid #f0f0f0;
  179. }
  180. }
  181. /deep/ .placeholderClass {
  182. color: #AAAAAA;
  183. font-size: 44rpx;
  184. }
  185. /deep/ .u-textarea {
  186. padding: 40rpx 0 !important;
  187. }
  188. .buy-box {
  189. position: fixed;
  190. bottom: 0%;
  191. left: 0%;
  192. width: 100%;
  193. background: #fff;
  194. padding: 10rpx 24rpx env(safe-area-inset-bottom);
  195. box-sizing: border-box;
  196. .buy-btn {
  197. width: 702rpx;
  198. height: 80rpx;
  199. line-height: 80rpx;
  200. text-align: center;
  201. background: #3B83FF;
  202. border-radius: 40rpx;
  203. font-weight: 600;
  204. font-size: 28rpx;
  205. color: #FFFFFF;
  206. }
  207. }
  208. </style>