task-upload-purchase.vue 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <template>
  2. <view class="page" :style="{ '--font-scale': fontScale }">
  3. <view class="nav-bar">
  4. <view class="nav-left" @tap="goBack">
  5. <text class="nav-icon">‹</text>
  6. <text class="nav-title">外部商品打卡</text>
  7. </view>
  8. </view>
  9. <scroll-view class="content" scroll-y>
  10. <!-- 任务说明 -->
  11. <view class="card">
  12. <view class="card-header">
  13. <text class="card-icon">🛒</text>
  14. <text class="card-title">{{ taskTitle }}</text>
  15. </view>
  16. <view class="card-body">
  17. <text class="task-desc">按以下步骤完成本次外部平台商品购买打卡:</text>
  18. <view class="step-list">
  19. <view class="step-item">
  20. <text class="step-num">1</text>
  21. <text class="step-text">前往外部平台完成商品购买</text>
  22. </view>
  23. <view class="step-item">
  24. <text class="step-num">2</text>
  25. <text class="step-text">收到商品后,拍摄实物照片</text>
  26. </view>
  27. <view class="step-item">
  28. <text class="step-num">3</text>
  29. <text class="step-text">上传照片,提交完成打卡任务</text>
  30. </view>
  31. </view>
  32. </view>
  33. </view>
  34. <!-- 拍照上传 -->
  35. <view class="card">
  36. <view class="card-header">
  37. <text class="card-icon">📸</text>
  38. <text class="card-title">上传商品实物照片</text>
  39. </view>
  40. <view class="card-body">
  41. <view class="upload-area" @tap="choosePhoto">
  42. <image v-if="photoUrl" :src="photoUrl" mode="aspectFill" class="photo-preview" />
  43. <view v-else class="upload-placeholder">
  44. <text class="upload-icon">📷</text>
  45. <text class="upload-text">拍照 / 相册选择商品实物照片</text>
  46. </view>
  47. </view>
  48. <view class="submit-btn" :class="{ submitting: submitting }" @tap="submit">
  49. <text>{{ submitting ? '提交中...' : '✓ 提交完成打卡' }}</text>
  50. </view>
  51. </view>
  52. </view>
  53. <view class="bottom-spacer"></view>
  54. </scroll-view>
  55. </view>
  56. </template>
  57. <script>
  58. import config from '../../config'
  59. import fontScaleMixin from '@/mixins/font-scale.js'
  60. export default {
  61. mixins: [fontScaleMixin],
  62. data() {
  63. return {
  64. taskId: null,
  65. memberId: null,
  66. taskTitle: '',
  67. photoUrl: '',
  68. submitting: false
  69. }
  70. },
  71. onLoad(options) {
  72. this.taskId = options && options.taskId ? parseInt(options.taskId) : null
  73. this.taskTitle = options && options.title ? decodeURIComponent(options.title) : '外部平台商品任务'
  74. var role = uni.getStorageSync('currentRole') || uni.getStorageSync('role') || 'parent'
  75. if (role !== 'child') {
  76. this.memberId = uni.getStorageSync('currentChildId') || uni.getStorageSync('currentMemberId') || uni.getStorageSync('userId') || null
  77. } else {
  78. this.memberId = uni.getStorageSync('currentMemberId') || null
  79. }
  80. },
  81. methods: {
  82. goBack() { uni.navigateBack() },
  83. choosePhoto: function() {
  84. var self = this
  85. uni.chooseImage({
  86. count: 1,
  87. sizeType: ['compressed'],
  88. sourceType: ['album', 'camera'],
  89. success: function(res) {
  90. self.photoUrl = res.tempFilePaths[0]
  91. }
  92. })
  93. },
  94. uploadFile: function(filePath) {
  95. var self = this
  96. return new Promise(function(resolve, reject) {
  97. uni.uploadFile({
  98. url: config.API_BASE_URL + '/api/media/upload',
  99. filePath: filePath,
  100. name: 'file',
  101. header: { 'Authorization': 'Bearer ' + uni.getStorageSync('token') },
  102. success: function(res) {
  103. try {
  104. var data = JSON.parse(res.data)
  105. if (data && data.code === 200 && data.data && data.data.url) {
  106. resolve(data.data.url)
  107. } else if (data && data.url) {
  108. resolve(data.url)
  109. } else {
  110. resolve('')
  111. }
  112. } catch (e) {
  113. resolve('')
  114. }
  115. },
  116. fail: function() { reject() }
  117. })
  118. })
  119. },
  120. submit: async function() {
  121. var self = this
  122. if (self.submitting) return
  123. if (!self.photoUrl) { uni.showToast({ title: '请先上传商品实物照片', icon: 'none' }); return }
  124. if (!self.memberId) { uni.showToast({ title: '成员ID无效', icon: 'none' }); return }
  125. self.submitting = true
  126. var photoUrl = ''
  127. try { photoUrl = await self.uploadFile(self.photoUrl) } catch (e) {}
  128. try {
  129. const res = await new Promise(function(resolve, reject) {
  130. uni.request({
  131. url: config.API_BASE_URL + '/api/tasks/' + self.taskId + '/complete',
  132. method: 'POST',
  133. data: { memberId: self.memberId, photoUrl: photoUrl, content: '外部平台商品实物已上传', contentType: 'image' },
  134. header: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + uni.getStorageSync('token') },
  135. success: function(r) { r.statusCode && r.statusCode !== 200 ? reject(r) : resolve(r) },
  136. fail: function() { reject() }
  137. })
  138. })
  139. if (res.data && res.data.code === 200) {
  140. uni.showToast({ title: '任务已完成', icon: 'success' })
  141. setTimeout(function() { uni.navigateBack() }, 800)
  142. } else {
  143. uni.showToast({ title: (res.data && res.data.message) || '提交失败', icon: 'none' })
  144. }
  145. } catch (e) {
  146. uni.showToast({ title: '提交失败,请重试', icon: 'none' })
  147. }
  148. self.submitting = false
  149. }
  150. }
  151. }
  152. </script>
  153. <style scoped lang="scss">
  154. .page { min-height: 100vh; background: $facade-fill-gray-50; }
  155. .nav-bar { height: 88rpx; padding: 24rpx 24rpx 0; display: flex; align-items: flex-end; }
  156. .nav-left { display: flex; align-items: center; gap: 10rpx; }
  157. .nav-icon { font-size: calc(48rpx * var(--font-scale, 1)); color: $facade-text-strong; line-height: 1; }
  158. .nav-title { font-size: calc(34rpx * var(--font-scale, 1)); font-weight: 600; color: $facade-text-strong; }
  159. .content { padding-bottom: 40rpx; }
  160. .card { margin: 16rpx 20rpx; background: $app-surface; border-radius: 24rpx; overflow: hidden; box-shadow: 0 4rpx 20rpx rgba(0,0,0,0.05); }
  161. .card-header { display: flex; align-items: center; gap: 10rpx; padding: 20rpx 24rpx 8rpx; border-bottom: 1rpx solid $facade-fill-2; background: linear-gradient(90deg, $uni-primary-pale, $app-surface); }
  162. .card-icon { font-size: calc(32rpx * var(--font-scale, 1)); }
  163. .card-title { font-size: calc(30rpx * var(--font-scale, 1)); font-weight: 600; color: $facade-text-strong; }
  164. .card-body { padding: 24rpx; }
  165. .task-desc { font-size: calc(26rpx * var(--font-scale, 1)); color: $facade-text-slate; line-height: 1.6; display: block; margin-bottom: 20rpx; }
  166. .step-list { display: flex; flex-direction: column; gap: 16rpx; }
  167. .step-item { display: flex; align-items: center; gap: 16rpx; }
  168. .step-num {
  169. width: 44rpx; height: 44rpx; border-radius: 50%;
  170. background: linear-gradient(135deg, $dim-body, $brand-primary);
  171. color: $app-surface; font-size: calc(24rpx * var(--font-scale, 1)); font-weight: 600;
  172. display: flex; align-items: center; justify-content: center;
  173. flex-shrink: 0;
  174. }
  175. .step-text { font-size: calc(26rpx * var(--font-scale, 1)); color: $facade-text-gray-700; }
  176. .upload-area {
  177. width: 100%; height: 320rpx;
  178. border: 2rpx dashed $app-border-strong; border-radius: 16rpx;
  179. display: flex; align-items: center; justify-content: center;
  180. background: $facade-fill-gray-50; overflow: hidden;
  181. margin-bottom: 24rpx;
  182. }
  183. .photo-preview { width: 100%; height: 100%; }
  184. .upload-placeholder { display: flex; flex-direction: column; align-items: center; gap: 12rpx; }
  185. .upload-icon { font-size: calc(72rpx * var(--font-scale, 1)); }
  186. .upload-text { font-size: calc(26rpx * var(--font-scale, 1)); color: $facade-text-gray-400; }
  187. .submit-btn {
  188. background: linear-gradient(135deg, $dim-body, $brand-primary);
  189. color: $app-surface; text-align: center; padding: 22rpx 0;
  190. border-radius: 16rpx; font-size: calc(30rpx * var(--font-scale, 1)); font-weight: 600;
  191. box-shadow: 0 6rpx 20rpx rgba(249,115,22,0.3);
  192. }
  193. .submit-btn:active { transform: scale(0.98); }
  194. .submitting { opacity: 0.6; }
  195. .bottom-spacer { height: 40rpx; }
  196. </style>