upStudentCard.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <!-- 学生卡上传 -->
  2. <template>
  3. <view id="upStudentCard">
  4. <view class="max-box">
  5. <view>添加照片</view>
  6. <img src="@/images/icon_05.png" v-if="!imgUrl" @click="selectImage">
  7. <img :src="imgUrl" v-else @click="selectImage">
  8. </view>
  9. <view class="up-data-button" @click="upDataButton">上传</view>
  10. </view>
  11. </template>
  12. <script>
  13. import { config } from '@/api/request/config.js'
  14. export default {
  15. data() {
  16. return {
  17. imgUrl:"",
  18. imgData:{},
  19. }
  20. },
  21. onLoad() {
  22. },
  23. methods: {
  24. // 图片上传
  25. selectImage() {
  26. let self = this;
  27. wx.chooseImage({
  28. count: 1,
  29. sizeType: ["original", "compressed"],
  30. sourceType: ["album", "camera"],
  31. success: function(res) {
  32. let tempFilePaths = res.tempFilePaths[0];
  33. self.imgData = res.tempFilePaths[0];
  34. self.uploadImg(tempFilePaths);
  35. }
  36. });
  37. },
  38. async uploadImg(tempFilePaths){
  39. var self = this;
  40. uni.showLoading({
  41. title: '上传中',
  42. mask: true
  43. });
  44. uni.uploadFile({
  45. url: config.base_url+'/file/upload', //仅为示例,非真实的接口地址
  46. header:{'Authorization':uni.getStorageSync('token')},
  47. filePath: tempFilePaths,
  48. name: 'file',
  49. formData: {
  50. 'user': 'test'
  51. },
  52. success: (uploadFileRes) => {
  53. let res = JSON.parse(uploadFileRes.data);
  54. if(res.code == 200){
  55. self.imgUrl = config.base_url+res.data.url;
  56. self.realImgUrl = res.data.url;
  57. }else{
  58. uni.showToast({
  59. title: res.msg,
  60. icon:"none",
  61. mask:true,
  62. duration: 2000
  63. });
  64. }
  65. },
  66. fail: err => {},
  67. complete: () => {
  68. uni.hideLoading()
  69. }
  70. });
  71. },
  72. upDataButton(){
  73. let self = this;
  74. if(!this.imgUrl){
  75. uni.showToast({
  76. title: "请选择上传图片",
  77. icon:"none",
  78. mask:true,
  79. duration: 2000
  80. });
  81. return
  82. }
  83. uni.showModal({
  84. // title: '确认要退出吗?',
  85. content: '确认上传吗?',
  86. cancelColor:"#999",
  87. confirmColor:"#0183FA",
  88. success: function (res) {
  89. if (res.confirm) {
  90. self.commitCrad();
  91. console.log('用户点击确定');
  92. } else if (res.cancel) {
  93. console.log('用户点击取消');
  94. }
  95. }
  96. });
  97. },
  98. async commitCrad(){
  99. var self = this;
  100. uni.showLoading({
  101. title: '上传中',
  102. mask: true
  103. });
  104. uni.uploadFile({
  105. url: config.base_url+'/app/lab/api/commit/crad', //仅为示例,非真实的接口地址
  106. header:{'Authorization':uni.getStorageSync('token')},
  107. filePath: self.realImgUrl,
  108. name: 'file',
  109. formData: {
  110. 'file': 'test'
  111. },
  112. success: (uploadFileRes) => {
  113. let res = JSON.parse(uploadFileRes.data);
  114. if(res.code == 200){
  115. uni.showToast({
  116. title: "提交成功",
  117. icon:"none",
  118. mask:true,
  119. duration: 2000
  120. });
  121. setTimeout(function(){
  122. uni.navigateBack();
  123. },2000);
  124. }else{
  125. uni.showToast({
  126. title: res.msg,
  127. icon:"none",
  128. mask:true,
  129. duration: 2000
  130. });
  131. }
  132. },
  133. fail: err => {},
  134. complete: () => {
  135. uni.hideLoading()
  136. }
  137. });
  138. },
  139. }
  140. }
  141. </script>
  142. <style lang="stylus" scoped>
  143. #upStudentCard{
  144. height:100%;
  145. width:100%;
  146. .max-box{
  147. height:350rpx;
  148. background #fff
  149. view{
  150. line-height :95rpx;
  151. font-size:28rpx;
  152. color:#333333;
  153. margin-left:20rpx;
  154. }
  155. img{
  156. height:180rpx;
  157. width:180rpx;
  158. margin:5rpx 0 0 30rpx;
  159. }
  160. }
  161. .up-data-button{
  162. position absolute
  163. bottom:0;
  164. left:0;
  165. width: 750rpx;
  166. height: 120rpx;
  167. background: #0183FA;
  168. line-height:120rpx;
  169. text-align center
  170. color:#fff;
  171. font-size: 30rpx;
  172. }
  173. }
  174. </style>