upload1.vue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <template>
  2. <view style="position: relative;">
  3. <!-- 胶囊 -->
  4. <mvBar :mysNavConfig="mysNavConfig"></mvBar>
  5. <view :style="{marginTop:navH}"></view>
  6. <view>
  7. <view style="margin-top: 40rpx;">
  8. <p style="margin-bottom: 20rpx;">上传销量截图</p>
  9. <htz-image-upload :max="1" mediaType="image" name="file" :chooseNum="1" v-model="imageData"
  10. @chooseSuccess="ceshiChooseSuccess">
  11. </htz-image-upload>
  12. </view>
  13. <view class="signup">
  14. <button type="default" class="but1" @click="submit()">
  15. 提交作业</button>
  16. </view>
  17. </view>
  18. <!-- <view v-if="onlyShow">
  19. <view style="margin: 20rpx;">
  20. <p>{{link_url}}</p>
  21. <view>
  22. <image :src="imageData" mode="scaleToFill"></image>
  23. </view>
  24. </view>
  25. </view> -->
  26. </view>
  27. </template>
  28. <script>
  29. import mvBar from "@/components/mys_navBar/mysNavBar";
  30. import htzImageUpload from '@/components/htz-image-upload/htz-image-upload.vue';
  31. import getPolicyEncode from '@/components/obs/getPolicy.js';
  32. import getSignature from '@/components/obs/GetSignature.js';
  33. export default {
  34. components: {
  35. mvBar,
  36. htzImageUpload,
  37. },
  38. data() {
  39. return {
  40. onlyShow: false,
  41. navH: getApp().globalData.navHeight,
  42. taskId: "",
  43. link_url: "",
  44. imageData: [],
  45. assignmentStatus: "",
  46. mysNavConfig: {
  47. /* 开启单页显示首页图标 */
  48. isHome: true,
  49. /* 固定导航 */
  50. navFixed: true,
  51. /* 标题 (屏幕中心居中 两边图标中心居中使用slot center1) */
  52. navTitle: {
  53. text: "上传销量截图",
  54. color: "",
  55. fontSize: "32rpx", // px upx rpx
  56. fontWeight: "normal", // 100 - 700
  57. },
  58. btnType: "type2",
  59. onLeftClick: '',
  60. /* type2 按钮 */
  61. type2Config: {
  62. // 左图标
  63. leftPath: "/static/img/png2.png",
  64. // 右图标
  65. rightPath: "/static/img/png4.png",
  66. // 圆角
  67. radius: "40rpx",
  68. },
  69. },
  70. }
  71. },
  72. onLoad(options) {
  73. let data = options.textObj.replace(/""/g, "");
  74. data = JSON.parse(decodeURIComponent(data))
  75. this.taskId = data.taskId
  76. this.assignmentStatus = data.assignmentStatus
  77. },
  78. methods: {
  79. submit() {
  80. if(this.imageData.length==0){
  81. uni.showToast({
  82. title:'必须上传截图',
  83. icon:'error',
  84. duration: 1000,
  85. })
  86. return
  87. }
  88. let that = this
  89. uni.showModal({
  90. title: '提示',
  91. content: '提交后无法修改并进行审核,确认提交?',
  92. success: async function(res) {
  93. if (res.confirm) {
  94. // 调用添加链接接口
  95. await that.addAssignmentApi()
  96. console.log("add success")
  97. uni.navigateBack()
  98. } else if (res.cancel) {
  99. console.log('用户点击取消');
  100. }
  101. }
  102. });
  103. },
  104. addAssignmentApi() {
  105. return this.$https.post('/youngee/c/api/t/assignment/add', {
  106. task_id: this.taskId,
  107. link_url: this.link_url,
  108. photo_url: this.imageData[0],
  109. })
  110. .then(res => {
  111. console.log("截图提交成功")
  112. })
  113. },
  114. ceshiChooseSuccess(tempFilePaths, e) { //选择图片返回
  115. console.log('ceshiChooseSuccess', tempFilePaths, e);
  116. if (e == 0) {
  117. this.store = 'talent_upload/' + this.guid() + '.png'
  118. } else if (e == 1) {
  119. this.store = 'talent_upload/' + this.guid() + '.mp4'
  120. }
  121. /****************
  122. 以下代码是自定义上传逻辑,仅供参考
  123. ***************/
  124. this.imgUpload1(tempFilePaths);
  125. /*******************************/
  126. console.log("imgdata" + this.imageData)
  127. },
  128. imgUpload1(tempFilePaths) {
  129. let that = this
  130. console.log('imgUpload', tempFilePaths)
  131. let config = {
  132. AccessKeyId: 'IVW21DTGIIUBBAGXKK0Y', //AK
  133. SecretKey: 'Y01nEQNcLOATMw7uJwrk3yOdQZ2fqLhSnXcOKVDE', //SK
  134. EndPoint: 'https://horastar.obs.cn-east-3.myhuaweicloud.com', //上传文件的路径
  135. };
  136. let fileName = this.store; //指定上传到OBS桶中的对象名
  137. // let fileName = "talent_upload/icon-arrow-right.png"
  138. let OBSPolicy = { //设定policy内容
  139. "expiration": "2089-12-31T12:00:00.000Z",
  140. "conditions": [{
  141. "bucket": "horastar"
  142. }, //Bucket name
  143. // {"bucket": "goin"},
  144. {
  145. 'key': fileName
  146. }
  147. ]
  148. }
  149. let policyEncoded = getPolicyEncode(OBSPolicy); //计算policy编码值
  150. let signature = getSignature(policyEncoded, config.SecretKey); //计算signature
  151. uni.uploadFile({
  152. //url: config.EndPoint,
  153. url: config.EndPoint,
  154. filePath: tempFilePaths[0],
  155. name: 'file',
  156. formData: {
  157. 'AccessKeyID': config.AccessKeyId,
  158. 'policy': policyEncoded,
  159. 'signature': signature,
  160. 'key': fileName,
  161. },
  162. success: function(res) {
  163. console.log(res.statusCode); //打印响应状态码
  164. if (res.statusCode == '204') {
  165. that.imageData.push(config.EndPoint + '/' + fileName);
  166. console.log('上传图片成功', res)
  167. let obs_url = config.EndPoint + '/' + fileName; //用你自己的 bucket 名替换星号
  168. console.log(obs_url)
  169. // that.formData.home_img = obs_url
  170. uni.showToast({
  171. title: '上传成功',
  172. icon: '成功'
  173. });
  174. } else {
  175. console.log('上传图片失败', res)
  176. uni.showToast({
  177. title: '上传失败',
  178. icon: '失败'
  179. });
  180. }
  181. },
  182. fail: function(e) {
  183. console.log(e);
  184. uni.showToast({
  185. title: '上传失败',
  186. icon: '失败'
  187. });
  188. }
  189. })
  190. },
  191. guid() {
  192. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
  193. var r = Math.random() * 16 | 0,
  194. v = c == 'x' ? r : (r & 0x3 | 0x8);
  195. return v.toString(16);
  196. });
  197. },
  198. }
  199. }
  200. </script>
  201. <style lang="scss" scoped>
  202. .signup {
  203. box-shadow: 0rpx 5rpx 40rpx #ccc;
  204. width: 100%;
  205. position: fixed;
  206. bottom: 0rpx;
  207. display: flex;
  208. height: 90rpx;
  209. padding-top: 2%;
  210. background-color: #FFFFFF;
  211. }
  212. .but1 {
  213. width: 60%;
  214. background-color: #f2d22d;
  215. border-radius: 20rpx;
  216. font-size: 36rpx;
  217. line-height: 200%;
  218. letter-spacing: 10rpx;
  219. font-weight: 500;
  220. height: 80%;
  221. &.on {
  222. background-color: #C0C0C0;
  223. }
  224. }
  225. </style>