upload.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <template>
  2. <view class="content">
  3. <view class="supplyInfo">
  4. <view class="flex">
  5. <label>输入文本:</label>
  6. <textarea v-model="msg" class="area card" maxlength="-1"></textarea>
  7. </view>
  8. <view class="update_photo">
  9. <view class="title"></view>
  10. <view>选择图片:</view>
  11. <view class="update_container card upload-parent-box">
  12. <view class="update_button display-flex">
  13. <view class="upload-box" @click="getImage('album')">
  14. <view class="img">
  15. <image src="/static/photo.png" class="photo"></image>
  16. </view>
  17. <view class="txt">上传</view>
  18. </view>
  19. <view
  20. class="display-flex upload-box-photo"
  21. v-for="(item, index) in uploadList"
  22. :key="index"
  23. >
  24. <image :src="item" mode="aspectFit" style="width: 100%; height: 100%" @click="showLarge(item)"/>
  25. <image src="/static/del.png" class="del-icon"mode="aspectFit"
  26. style="width: 30rpx; height: 30rpx" @click="delPhoto(index)"></image>
  27. </view>
  28. </view>
  29. </view>
  30. </view>
  31. <view class="flex">
  32. <label>支部:</label>
  33. <input type="text" v-model="department" class="input card" />
  34. </view>
  35. <view class="flex">
  36. <label>姓名:</label>
  37. <input type="text" v-model="name" class="input card" />
  38. </view>
  39. <button class="submit" @tap="submit">提交</button>
  40. </view>
  41. </view>
  42. </template>
  43. <script>
  44. import md5 from "@/common/md5.js";
  45. export default {
  46. data() {
  47. return {
  48. department: getApp().globalData.user_department,
  49. msg: '',
  50. name:getApp().globalData.user_real_name,
  51. uploadList: [],
  52. imgIdList: [],
  53. };
  54. },
  55. onLoad() {
  56. },
  57. methods: {
  58. submit() {
  59. if (
  60. this.name === "" ||
  61. this.department === "" ||
  62. this.msg === ""
  63. ) {
  64. uni.showToast({
  65. title: "内容不完善",
  66. icon: "error",
  67. });
  68. return;
  69. }
  70. let md5Sign = md5(
  71. "method=" +
  72. "activity" +
  73. "&timestamp=" +
  74. getApp().globalData.globalTimestamp +
  75. "&secret=" +
  76. getApp().globalData.secret
  77. );
  78. let url =
  79. getApp().globalData.shareUrl +
  80. "api/api.php" +
  81. "?method=activity&action=process&timestamp=" +
  82. getApp().globalData.globalTimestamp +
  83. "&sign=" +
  84. md5Sign;
  85. let postData = {
  86. openid :getApp().globalData.open_id,
  87. activity_id : 1 , //这里固定是1
  88. content : this.msg,
  89. department :this.department,
  90. user_name : this.name,
  91. attach_ids :this.imgIdList.join(), // 3,34,5,5,6
  92. };
  93. uni.request({
  94. url: url,
  95. method: "POST",
  96. header: {
  97. "content-type": "application/x-www-form-urlencoded",
  98. },
  99. data: postData,
  100. success: (res) => {
  101. if (res.data.code === 200) {
  102. uni.showToast({
  103. title: "上传成功",
  104. icon: "none",
  105. duration: 2000,
  106. })
  107. getApp().globalData.user_department = this.department;
  108. getApp().globalData.user_real_name = this.name;
  109. setTimeout(() => {
  110. uni.navigateBack({});
  111. }, 1500);
  112. } else {
  113. uni.showToast({
  114. title: res.data.msg,
  115. icon: "none",
  116. duration: 2500,
  117. });
  118. }
  119. },
  120. fail: () => {
  121. console.log("连接失败");
  122. },
  123. });
  124. },
  125. bindPickerChange(e) {
  126. this.index = e.detail.value;
  127. },
  128. getImage(type) {
  129. let that = this;
  130. if (that.uploadList.length >= 9) {
  131. uni.showToast({
  132. title: "最多上传9张图片",
  133. icon: "none",
  134. duration: 2500,
  135. });
  136. return;
  137. }
  138. uni.chooseImage({
  139. sourceType: [type],
  140. count: 9 - that.uploadList.length,
  141. sizeType: ["original", "compressed"], //可以指定是原图还是压缩图,默认二者都有
  142. success: (res) => {
  143. for (let i = 0; i < res.tempFilePaths.length; i++) {
  144. console.log(res.tempFilePaths[i])
  145. that.uploadList.push(res.tempFilePaths[i]);
  146. that.uploadFileRequest(res.tempFilePaths[i]);
  147. }
  148. },
  149. });
  150. },
  151. uploadFileRequest(fileVal) {
  152. uni.showLoading({
  153. title: "上传中",
  154. mask: true,
  155. });
  156. let that = this;
  157. let md5Sign = md5(
  158. "method=" +
  159. "upload" +
  160. "&timestamp=" +
  161. getApp().globalData.globalTimestamp +
  162. "&secret=" +
  163. getApp().globalData.secret
  164. );
  165. let url =
  166. getApp().globalData.shareUrl +
  167. "api/api.php" +
  168. "?method=upload&source=activity&id=1&timestamp=" +
  169. getApp().globalData.globalTimestamp +
  170. "&sign=" +
  171. md5Sign;
  172. uni.uploadFile({
  173. url: url, //需要设置为全局
  174. filePath: fileVal,
  175. name: "file",
  176. formData: {
  177. file: fileVal,
  178. },
  179. success: (res) => {
  180. let tmpres = JSON.parse(res.data);
  181. console.log(tmpres);
  182. uni.hideLoading();
  183. that.imgIdList.push(tmpres.data.id);
  184. },
  185. fail: (res) => {
  186. console.log("上传请求失败");
  187. console.log(res);
  188. },
  189. });
  190. },
  191. delPhoto(idx) {
  192. this.uploadList.splice(idx, 1);
  193. this.imgIdList.splice(idx, 1);
  194. },
  195. showLarge(src) {
  196. uni.previewImage({
  197. urls: [src],
  198. longPressActions: {
  199. itemList: ["发送给朋友", "保存图片"],
  200. success: function (data) {},
  201. fail: function (err) {
  202. console.log(err.errMsg);
  203. },
  204. },
  205. });
  206. },
  207. },
  208. };
  209. </script>
  210. <style lang="scss" scoped>
  211. .upload-parent-box {
  212. // height: 150rpx;
  213. padding: 3%;
  214. padding-top: 25rpx;
  215. padding-left: 20rpx;
  216. }
  217. .upload-box {
  218. display: flex;
  219. flex-flow: column;
  220. width: 25%;
  221. background-color: #e0e0e0;
  222. height: 140rpx;
  223. border-radius: 10rpx;
  224. padding-top: 15rpx;
  225. image {
  226. width: 60rpx !important;
  227. height: 60rpx !important;
  228. }
  229. }
  230. .upload-box-photo {
  231. width: 25%;
  232. height: 110rpx;
  233. border-radius: 10rpx;
  234. padding-top: 15rpx;
  235. position: relative;
  236. }
  237. .del-icon {
  238. position: absolute;
  239. right: 0;
  240. width: 30rpx;
  241. height: 30rpx;
  242. }
  243. .update_button {
  244. text-align: center;
  245. display: flex;
  246. flex-wrap: wrap;
  247. }
  248. .content {
  249. font-size: 28rpx;
  250. font-weight: 200;
  251. padding: 1% 2%;
  252. .title {
  253. font-size: 30rpx;
  254. margin: 4% 0;
  255. }
  256. label {
  257. display: inline-block;
  258. width: 25%;
  259. vertical-align: middle;
  260. }
  261. .card {
  262. background-color: rgb(248, 247, 247);
  263. border-radius: 10rpx;
  264. }
  265. .flex {
  266. display: flex;
  267. align-items: center;
  268. margin-bottom: 2%;
  269. }
  270. .input {
  271. padding: 0 2%;
  272. margin: 2% 0;
  273. display: inline-block;
  274. width: 80%;
  275. height: 70rpx;
  276. }
  277. .supplyInfo {
  278. // border-radius: 40rpx;
  279. padding: 4%;
  280. box-shadow: rgba(100, 100, 111, 0.2) 14rpx 14rpx 40rpx 14rpx;
  281. margin: 30rpx 0 30rpx 0;
  282. .area {
  283. height: 200rpx;
  284. padding: 20rpx;
  285. box-sizing: border-box;
  286. }
  287. .picker {
  288. width: 80%;
  289. height: 70rpx;
  290. background: rgb(248, 247, 247);
  291. display: flex;
  292. align-items: center;
  293. position: relative;
  294. .pick {
  295. width: 100%;
  296. }
  297. .picker_title {
  298. display: flex;
  299. width: 100%;
  300. margin-left: 30rpx;
  301. align-items: center;
  302. justify-content: space-between;
  303. .triangle-down {
  304. width: 0;
  305. height: 0;
  306. border-top: 15rpx solid rgb(173, 173, 173);
  307. border-left: 15rpx solid transparent;
  308. border-right: 15rpx solid transparent;
  309. position: absolute;
  310. right: 10rpx;
  311. }
  312. .pickername {
  313. font-weight: 100;
  314. }
  315. }
  316. }
  317. .upload-box {
  318. width: 25%;
  319. background-color: #e0e0e0;
  320. height: 110rpx;
  321. border-radius: 10rpx;
  322. padding-top: 15rpx;
  323. }
  324. .upload-box-photo {
  325. width: 25%;
  326. height: 110rpx;
  327. border-radius: 10rpx;
  328. padding-top: 15rpx;
  329. position: relative;
  330. }
  331. .del-icon {
  332. position: absolute;
  333. right: 0;
  334. width: 30rpx;
  335. height: 30rpx;
  336. }
  337. image {
  338. width: 60rpx;
  339. height: 60rpx;
  340. }
  341. .update_button {
  342. text-align: center;
  343. display: flex;
  344. }
  345. }
  346. .info {
  347. margin-top: 2%;
  348. border-radius: 40rpx;
  349. padding: 2% 4%;
  350. box-shadow: rgba(100, 100, 111, 0.2) 14rpx 14rpx 40rpx 14rpx;
  351. }
  352. .submit {
  353. color: white;
  354. font-weight: normal;
  355. width: 70%;
  356. border-radius: 20rpx;
  357. background-color: #02a7f0;
  358. margin: 50rpx auto;
  359. }
  360. }
  361. </style>