upload.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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"></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. // uni.setStorageSync("supply_name", this.company.name);
  108. // uni.setStorageSync("supply_code", this.company.code);
  109. // uni.setStorageSync("supply_tel", this.company.tel);
  110. setTimeout(() => {
  111. uni.navigateBack({});
  112. }, 1500);
  113. } else {
  114. uni.showToast({
  115. title: res.data.msg,
  116. icon: "none",
  117. duration: 2500,
  118. });
  119. }
  120. },
  121. fail: () => {
  122. console.log("连接失败");
  123. },
  124. });
  125. },
  126. bindPickerChange(e) {
  127. this.index = e.detail.value;
  128. },
  129. getImage(type) {
  130. let that = this;
  131. if (that.uploadList.length >= 3) {
  132. uni.showToast({
  133. title: "最多上传3张图片",
  134. icon: "none",
  135. duration: 2500,
  136. });
  137. return;
  138. }
  139. uni.chooseImage({
  140. sourceType: [type],
  141. count: 3 - that.uploadList.length,
  142. sizeType: ["original", "compressed"], //可以指定是原图还是压缩图,默认二者都有
  143. success: (res) => {
  144. for (let i = 0; i < res.tempFilePaths.length; i++) {
  145. console.log(res.tempFilePaths[i])
  146. that.uploadList.push(res.tempFilePaths[i]);
  147. that.uploadFileRequest(res.tempFilePaths[i]);
  148. }
  149. },
  150. });
  151. },
  152. uploadFileRequest(fileVal) {
  153. uni.showLoading({
  154. title: "上传中",
  155. mask: true,
  156. });
  157. let that = this;
  158. let md5Sign = md5(
  159. "method=" +
  160. "upload" +
  161. "&timestamp=" +
  162. getApp().globalData.globalTimestamp +
  163. "&secret=" +
  164. getApp().globalData.secret
  165. );
  166. let url =
  167. getApp().globalData.shareUrl +
  168. "api/api.php" +
  169. "?method=upload&source=activity&id=1&timestamp=" +
  170. getApp().globalData.globalTimestamp +
  171. "&sign=" +
  172. md5Sign;
  173. uni.uploadFile({
  174. url: url, //需要设置为全局
  175. filePath: fileVal,
  176. name: "file",
  177. formData: {
  178. file: fileVal,
  179. },
  180. success: (res) => {
  181. let tmpres = JSON.parse(res.data);
  182. console.log(tmpres);
  183. uni.hideLoading();
  184. that.imgIdList.push(tmpres.data.id);
  185. },
  186. fail: (res) => {
  187. console.log("上传请求失败");
  188. console.log(res);
  189. },
  190. });
  191. },
  192. delPhoto(idx) {
  193. this.uploadList.splice(idx, 1);
  194. this.imgIdList.splice(idx, 1);
  195. },
  196. showLarge(src) {
  197. uni.previewImage({
  198. urls: [src],
  199. longPressActions: {
  200. itemList: ["发送给朋友", "保存图片"],
  201. success: function (data) {},
  202. fail: function (err) {
  203. console.log(err.errMsg);
  204. },
  205. },
  206. });
  207. },
  208. },
  209. };
  210. </script>
  211. <style lang="scss" scoped>
  212. .upload-parent-box {
  213. height: 150rpx;
  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. }
  247. .content {
  248. font-size: 28rpx;
  249. font-weight: 200;
  250. padding: 1% 2%;
  251. .title {
  252. font-size: 30rpx;
  253. margin: 4% 0;
  254. }
  255. label {
  256. display: inline-block;
  257. width: 25%;
  258. vertical-align: middle;
  259. }
  260. .card {
  261. background-color: rgb(248, 247, 247);
  262. border-radius: 10rpx;
  263. }
  264. .flex {
  265. display: flex;
  266. align-items: center;
  267. margin-bottom: 2%;
  268. }
  269. .input {
  270. padding: 0 2%;
  271. margin: 2% 0;
  272. display: inline-block;
  273. width: 80%;
  274. height: 70rpx;
  275. }
  276. .supplyInfo {
  277. // border-radius: 40rpx;
  278. padding: 4%;
  279. box-shadow: rgba(100, 100, 111, 0.2) 14rpx 14rpx 40rpx 14rpx;
  280. margin: 30rpx 0 30rpx 0;
  281. .area {
  282. height: 200rpx;
  283. padding: 20rpx;
  284. box-sizing: border-box;
  285. }
  286. .picker {
  287. width: 80%;
  288. height: 70rpx;
  289. background: rgb(248, 247, 247);
  290. display: flex;
  291. align-items: center;
  292. position: relative;
  293. .pick {
  294. width: 100%;
  295. }
  296. .picker_title {
  297. display: flex;
  298. width: 100%;
  299. margin-left: 30rpx;
  300. align-items: center;
  301. justify-content: space-between;
  302. .triangle-down {
  303. width: 0;
  304. height: 0;
  305. border-top: 15rpx solid rgb(173, 173, 173);
  306. border-left: 15rpx solid transparent;
  307. border-right: 15rpx solid transparent;
  308. position: absolute;
  309. right: 10rpx;
  310. }
  311. .pickername {
  312. font-weight: 100;
  313. }
  314. }
  315. }
  316. .upload-box {
  317. width: 25%;
  318. background-color: #e0e0e0;
  319. height: 110rpx;
  320. border-radius: 10rpx;
  321. padding-top: 15rpx;
  322. }
  323. .upload-box-photo {
  324. width: 25%;
  325. height: 110rpx;
  326. border-radius: 10rpx;
  327. padding-top: 15rpx;
  328. position: relative;
  329. }
  330. .del-icon {
  331. position: absolute;
  332. right: 0;
  333. width: 30rpx;
  334. height: 30rpx;
  335. }
  336. image {
  337. width: 60rpx;
  338. height: 60rpx;
  339. }
  340. .update_button {
  341. text-align: center;
  342. display: flex;
  343. }
  344. }
  345. .info {
  346. margin-top: 2%;
  347. border-radius: 40rpx;
  348. padding: 2% 4%;
  349. box-shadow: rgba(100, 100, 111, 0.2) 14rpx 14rpx 40rpx 14rpx;
  350. }
  351. .submit {
  352. color: white;
  353. font-weight: normal;
  354. width: 70%;
  355. border-radius: 20rpx;
  356. background-color: #02a7f0;
  357. margin: 50rpx auto;
  358. }
  359. }
  360. </style>