initiateCrowdfunding.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. <template>
  2. <view class="page">
  3. <view class="nav-bar">
  4. <view class="left">
  5. <view class="uni-btn-icon" @click="goBack">&#xe601;</view>
  6. <view class="center">发表想法</view>
  7. </view>
  8. <view class="right">
  9. <!-- <view class="btn" @click="onManage">管理</view> -->
  10. <view class="btn" @click="onSubmit" v-if="id && id < 1">发布</view>
  11. <view class="btn" @click="onSubmit" v-else>更新</view>
  12. </view>
  13. </view>
  14. <view class="enterContentDetails">
  15. <!-- 上限五张图片或一个视频 -->
  16. <view class="img_list">
  17. <view class="image" v-for="(item, index) in img_list" :key="index">
  18. <image class="upimg" :src="item" mode="aspectFill"></image>
  19. <image class="del" src="@/static/icon/img-del.png" @click="delIt(index)"></image>
  20. <view class="loading" v-if="item.startsWith('http') === false">
  21. <view class="loading-icon"></view>
  22. </view>
  23. </view>
  24. <view v-if="video_path" class="image">
  25. <video :src="video_path" controls style="width:100%;height:100%"></video>
  26. <image class="del" src="@/static/icon/img-del.png" @click="delVideo"></image>
  27. </view>
  28. <view class="image" @click="upload('list')" v-if="img_list.length < 9">
  29. <image class="txt" src="../../static/icon/sy_icon_jiahao02.png"></image>
  30. </view>
  31. </view>
  32. <view class="content-title">
  33. <input class="edit" v-model="ainfo.title" maxlength="20" placeholder="标题(必填)" />
  34. </view>
  35. <view class="content-textarea">
  36. <textarea class="edit2" auto-height v-model="ainfo.content" :maxlength="1000"
  37. placeholder="添加描述"></textarea>
  38. </view>
  39. <view class="agree">
  40. <text class="xy">{{ ainfo.content.length }}/1000</text>
  41. </view>
  42. </view>
  43. <view class="worksBox scale-tap" @click="upload4">
  44. <view class="optionalWorks">
  45. <view class="title">
  46. 上传详情页
  47. <text style="font-weight: normal;">(支持长图)</text>
  48. </view>
  49. </view>
  50. <view class="select-box">
  51. <image src="../../static/icon/upload-icon.png"></image>
  52. </view>
  53. </view>
  54. </view>
  55. </template>
  56. <script>
  57. import permission from '@/common/permission.js';
  58. export default {
  59. components: {},
  60. data() {
  61. return {
  62. skey: '',
  63. pop_sel: false,
  64. cid: 0,
  65. catename: '',
  66. img2: '',
  67. ainfo: {
  68. id: 0,
  69. title: '',
  70. content: '',
  71. name: '',
  72. telphone: '',
  73. address: '',
  74. contact: '',
  75. num: '',
  76. },
  77. img_list: [],
  78. video_path: '',
  79. is_agree: 0,
  80. selectedWork: {},
  81. content_images: [],
  82. }
  83. },
  84. onLoad(params) {
  85. if (getApp().globalData.skey != "") {
  86. this.skey = getApp().globalData.skey;
  87. } else {
  88. this.skey = params.skey || ''; //1234567xef
  89. }
  90. if (params.id) {
  91. this.id = params.id;
  92. } else {
  93. this.id = -1;
  94. }
  95. let that = this;
  96. // this.getCate();
  97. that.getInfo();
  98. // this.getData();
  99. // 添加事件监听
  100. uni.$on('selectItem', this.handleSelect);
  101. },
  102. onUnload() {
  103. uni.$off('selectItem', this.handleSelect);
  104. },
  105. onShow() {
  106. let that = this;
  107. },
  108. methods: {
  109. goBack() {
  110. uni.navigateBack();
  111. },
  112. handleSelect(item) {
  113. this.selectedWork = item;
  114. },
  115. delIt(ind) {
  116. let arr = [];
  117. if (this.img_list.length > 0) {
  118. for (let i = 0; i < this.img_list.length; i++) {
  119. if (i !== ind) {
  120. arr.push(this.img_list[i]);
  121. }
  122. }
  123. this.img_list = arr;
  124. }
  125. },
  126. getInfo() {
  127. var that = this;
  128. uni.request({
  129. url: this.$apiHost + '/Article/getinfo',
  130. data: {
  131. uuid: getApp().globalData.uuid,
  132. id: this.id
  133. },
  134. header: {
  135. 'content-type': 'application/json'
  136. },
  137. success: (res) => {
  138. this.ainfo = res.data.article;
  139. this.img_list = [];
  140. this.selectedWork = res.data.work_info;
  141. if (res.data.article.images.length > 1) {
  142. let arr = res.data.article.images.split("|");
  143. if (arr.length > 0) {
  144. for (let i = 0; i < arr.length; i++) {
  145. if (arr[i].length > 10) {
  146. this.img_list.push(arr[i]);
  147. }
  148. }
  149. }
  150. }
  151. }
  152. });
  153. },
  154. checkField(str, tips) {
  155. if (str.length < 2) {
  156. uni.showToast({ title: tips, icon: 'none' });
  157. return false;
  158. }
  159. return true;
  160. },
  161. onSubmit() {
  162. if (this.checkField(this.ainfo.title, "请输入名称") == false) return;
  163. if (this.checkField(this.ainfo.content, "请输入具体内容") == false) return;
  164. if (this.is_submit > 0) return;
  165. let img_str = '';
  166. if (this.img_list.length > 0) {
  167. for (let i = 0; i < this.img_list.length; i++) {
  168. img_str += this.img_list[i] + "|";
  169. }
  170. }
  171. if (this.img_list.length < 1 && !this.selectedWork.id) {
  172. uni.showToast({ title: '请上传 图片 或 选择一个作品', icon: 'none' });
  173. return;
  174. } else {
  175. if (!this.selectedWork.id) {
  176. if (!img_str || img_str.trim().length < 2) {
  177. uni.showToast({ title: '请确保至少上传一张有效图片作为封面', icon: 'none' });
  178. return;
  179. }
  180. if (this.img_list.length > 5) {
  181. uni.showToast({ title: '最多只能上传 5 张图片', icon: 'none' });
  182. return;
  183. }
  184. }
  185. }
  186. let that = this;
  187. let contentImagesStr = '';
  188. if (this.content_images.length > 0) {
  189. for (let i = 0; i < this.content_images.length; i++) {
  190. contentImagesStr += this.content_images[i] + "|";
  191. }
  192. }
  193. this.is_submit = 1;
  194. console.log( {
  195. uuid: getApp().globalData.uuid,
  196. id: this.ainfo.id,
  197. title: this.ainfo.title,
  198. content: this.ainfo.content,
  199. images: img_str,
  200. content_images: contentImagesStr,
  201. video_url: this.video_path,
  202. },9999999999);
  203. uni.request({
  204. url: this.$apiHost + '/crowdfund/seji/save',
  205. data: {
  206. uuid: getApp().globalData.uuid,
  207. id: this.ainfo.id,
  208. title: this.ainfo.title,
  209. content: this.ainfo.content,
  210. images: img_str,
  211. content_images: contentImagesStr,
  212. video_url: this.video_path,
  213. },
  214. method: 'POST',
  215. header: {
  216. 'Content-Type': 'application/x-www-form-urlencoded',
  217. 'sign': getApp().globalData.headerSign
  218. },
  219. dataType: 'json',
  220. success: (res) => {
  221. uni.showToast({ title: res.data.str, icon: 'none' });
  222. if (res.data.success == "yes") {
  223. setTimeout(function () {
  224. uni.$emit('switchToMyPage', { type: 'article' });
  225. uni.switchTab({ url: '/pages/my/my' })
  226. }, 500);
  227. that.ainfo.title = '';
  228. that.ainfo.content = '';
  229. that.ainfo.num = '';
  230. that.img_list = [];
  231. }
  232. this.is_submit = 0;
  233. },
  234. fail: (err) => {
  235. this.is_submit = 0;
  236. },
  237. complete: (com) => { }
  238. })
  239. },
  240. async upload(type) {
  241. // 权限弹窗校验
  242. let hasPermission = false;
  243. try {
  244. hasPermission = await permission.request('photoLibrary', {
  245. title: '相册/视频权限说明',
  246. describe: '便于您上传图片或视频,请您确认授权,否则无法使用该功能'
  247. });
  248. if (!hasPermission) {
  249. uni.showToast({ title: '未获得相册权限', icon: 'none' });
  250. return;
  251. }
  252. } catch (e) {
  253. uni.showToast({ title: '权限检查失败', icon: 'none' });
  254. return;
  255. }
  256. if (this.img_list.length >= 9 && !this.video_path) {
  257. uni.showToast({ title: '最多只能上传9张图片', icon: 'none' });
  258. return;
  259. }
  260. uni.chooseMedia({
  261. count: 9 - this.img_list.length,
  262. mediaType: ['image', 'video'],
  263. sourceType: ['album', 'camera'],
  264. maxDuration: 60,
  265. success: (res) => {
  266. console.log(res, 'res');
  267. if (res.tempFiles && res.tempFiles.length > 0) {
  268. // 判断是否有视频
  269. let videoFiles = res.tempFiles.filter(file => file.fileType === 'video');
  270. if (videoFiles.length > 0) {
  271. // 如果多选视频,只处理第一个并提示
  272. if (videoFiles.length > 1) {
  273. uni.showToast({ title: '只能上传一个视频,已自动选取第一个', icon: 'none' });
  274. }
  275. // 如果已存在视频,替换并提示
  276. if (this.video_path) {
  277. uni.showToast({ title: '已替换为新上传的视频', icon: 'none' });
  278. }
  279. this.upload2(type, videoFiles[0].tempFilePath, 'video', '/Xweb/upload_video?skey=');
  280. console.log(videoFiles, 'videoFiles');
  281. }
  282. let imageFiles = res.tempFiles.filter(file => file.fileType === 'image');
  283. console.log(imageFiles, 'imageFiles');
  284. if (imageFiles.length > 0) {
  285. // 多图全部上传
  286. imageFiles.forEach(img => {
  287. this.upload2(type, img.tempFilePath, 'image', '/Xweb/upload_img?skey=');
  288. });
  289. }
  290. }
  291. }
  292. });
  293. },
  294. delVideo() {
  295. this.video_path = '';
  296. },
  297. upload2(type, filepath, fileType, url) {
  298. console.log(filepath, 'filepath');
  299. console.log(fileType, 'fileType');
  300. // 上传前先显示临时图
  301. if (fileType === 'image') {
  302. this.img_list.push(filepath);
  303. } else if (fileType === 'video') {
  304. this.video_path = filepath;
  305. }
  306. let uploadUrl = this.$apiHost + url + this.skey;
  307. console.log({
  308. url: uploadUrl,
  309. filePath: filepath,
  310. name: 'file',
  311. }, 99999);
  312. uni.uploadFile({
  313. url: uploadUrl,
  314. filePath: filepath,
  315. name: 'file',
  316. success: (uploadFileRes) => {
  317. console.log(uploadFileRes, 'uploadFileRes');
  318. let resdata = JSON.parse(uploadFileRes.data);
  319. if (resdata.success == 'no') {
  320. uni.showToast({ title: resdata.str, icon: 'none' });
  321. // 上传失败时移除临时图
  322. if (fileType === 'image') {
  323. this.img_list.pop();
  324. } else if (fileType === 'video') {
  325. this.video_path = '';
  326. }
  327. return;
  328. }
  329. if (resdata.code == 0) {
  330. if (fileType === 'image') {
  331. // 替换临时图为服务端路径
  332. const index = this.img_list.indexOf(filepath);
  333. if (index !== -1) {
  334. this.img_list[index] = resdata.data.path;
  335. }
  336. } else if (fileType === 'video') {
  337. this.video_path = resdata.data.path;
  338. }
  339. }
  340. },
  341. fail: () => {
  342. // 上传失败时移除临时图
  343. if (fileType === 'image') {
  344. this.img_list.pop();
  345. } else if (fileType === 'video') {
  346. this.video_path = '';
  347. }
  348. }
  349. });
  350. },
  351. upload4(type) {
  352. if (this.content_images.length >= 5) {
  353. uni.showToast({
  354. title: '最多只能上传5张图片',
  355. icon: 'none'
  356. });
  357. return
  358. }
  359. uni.showActionSheet({
  360. itemList: ['拍照', '从相册选择'],
  361. success: (res) => {
  362. const sourceType = res.tapIndex === 0 ? 'camera' : 'album';
  363. this.checkRights1(sourceType);
  364. }
  365. });
  366. },
  367. async checkRights1(sourceType) {
  368. try {
  369. let hasPermission = false;
  370. if (sourceType === 'camera') {
  371. hasPermission = await permission.request(permission.PermissionType.CAMERA, {
  372. title: '“萌创星球”想访问你的相机',
  373. describe: '萌创星球想访问您的摄像头,便于拍摄获取图片来与其他用户进行交流'
  374. });
  375. if (!hasPermission) {
  376. uni.showToast({
  377. title: '未获得相机权限',
  378. icon: 'none'
  379. });
  380. return;
  381. }
  382. } else {
  383. hasPermission = await permission.request(permission.PermissionType.PHOTO_LIBRARY, {
  384. title: '“萌创星球”想访问你的照片图库',
  385. describe: '萌创星球想访问您本地照片图库,便于获取图片来与其他用户进行交流'
  386. });
  387. if (!hasPermission) {
  388. uni.showToast({
  389. title: '未获得相册权限',
  390. icon: 'none'
  391. });
  392. return;
  393. }
  394. }
  395. // 权限获取成功后,继续执行上传
  396. this.upload5('list', sourceType);
  397. } catch (error) {
  398. console.error('权限检查失败:', error);
  399. uni.showToast({
  400. title: '权限检查失败',
  401. icon: 'none'
  402. });
  403. }
  404. },
  405. upload5(type, sourceType) {
  406. console.log("----upload");
  407. var _self = this;
  408. uni.chooseImage({
  409. count: 5,
  410. sizeType: ['compressed'],
  411. sourceType: [sourceType],
  412. success: function (res) {
  413. console.log('res:', res)
  414. for (let index = 0; index < res.tempFilePaths.length; index++) {
  415. let filepath = "";
  416. // #ifdef H5
  417. filepath = res.tempFiles[index].path;
  418. // #endif
  419. // #ifdef APP-PLUS
  420. filepath = res.tempFilePaths[index];
  421. // #endif
  422. _self.imglocal = filepath; //res.tempFilePaths[0]
  423. const uploadTask = uni.uploadFile({
  424. url: _self.$apiHost + '/Xweb/upload_img?skey=' + _self.skey, // post请求地址
  425. filePath: filepath,
  426. name: 'file', // 待确认
  427. success: function (uploadFileRes) {
  428. let resdata = JSON.parse(uploadFileRes.data)
  429. console.log('Success1:', uploadFileRes);
  430. if (resdata.success == 'no') {
  431. uni.showToast({
  432. title: resdata.str,
  433. icon: 'none'
  434. });
  435. return;
  436. }
  437. if (resdata.code == 0) {
  438. console.log('Success2:', resdata.data.path);
  439. _self.content_images.push(resdata.data.path)
  440. console.log('_self.content_images:', _self.content_images);
  441. .0
  442. }
  443. },
  444. fail: function (uploadFileFail) {
  445. console.log('Error:', uploadFileFail.data);
  446. },
  447. complete: () => {
  448. console.log('Complete:');
  449. }
  450. });
  451. }
  452. },
  453. error: function (e) {
  454. console.log(e);
  455. }
  456. });
  457. },
  458. }
  459. }
  460. </script>
  461. <style scoped lang="scss">
  462. page {
  463. position: absolute;
  464. left: 0;
  465. top: 0;
  466. width: 100%;
  467. display: flex;
  468. background-color: #f2f6f2;
  469. min-height: 100vh;
  470. }
  471. .status_bar {
  472. height: 44rpx;
  473. width: 100%;
  474. }
  475. .page {
  476. background-color: #f2f6f2;
  477. display: flex;
  478. flex-direction: column;
  479. width: 750rpx;
  480. min-height: 100vh;
  481. }
  482. .nav-bar {
  483. width: calc(100ve - 73rpx);
  484. height: calc(96rpx + var(--status-bar-height));
  485. display: flex;
  486. align-items: center;
  487. justify-content: space-between;
  488. padding: 0;
  489. padding-right: 40rpx;
  490. padding-left: 33rpx;
  491. padding-top: var(--status-bar-height);
  492. box-sizing: border-box;
  493. .left {
  494. display: flex;
  495. align-items: center;
  496. font-family: PingFang SC-Bold;
  497. font-weight: 400;
  498. font-size: 32rpx;
  499. color: #1f1f1f;
  500. .uni-btn-icon {
  501. font-size: 45rpx;
  502. font-weight: bold;
  503. color: #1f1f1f;
  504. transition: color 0.2s;
  505. margin-right: 20rpx;
  506. &:active {
  507. color: #2b85e4;
  508. }
  509. }
  510. }
  511. .right {
  512. display: flex;
  513. align-items: center;
  514. font-size: 28rpx;
  515. font-family: "PingFang SC-Bold";
  516. font-weight: 400;
  517. .btn {
  518. border-radius: 26rpx;
  519. background: #1f1f1f;
  520. padding: 6rpx 32rpx;
  521. color: #acf934;
  522. font-size: 28rpx;
  523. }
  524. }
  525. }
  526. .enterContentDetails {
  527. width: calc(100% - 40rpx);
  528. min-height: 726rpx;
  529. background: #ffffff;
  530. border-radius: 20rpx;
  531. margin: 0 20rpx;
  532. padding: 0rpx 24rpx 24rpx 24rpx;
  533. box-sizing: border-box;
  534. .edit {
  535. height: 92rpx;
  536. border-bottom: 2rpx solid #f2f6f2;
  537. }
  538. .edit2 {
  539. min-height: 350rpx;
  540. width: 100%;
  541. }
  542. .content-textarea {
  543. padding-top: 24rpx;
  544. }
  545. ::v-deep.input-placeholder,
  546. ::v-deep.textarea-placeholder {
  547. font-family: PingFang SC;
  548. font-weight: 400;
  549. font-size: 32rpx;
  550. color: #999999;
  551. }
  552. .agree {
  553. font-family: PingFang SC, PingFang SC;
  554. font-weight: 400;
  555. font-size: 24rpx;
  556. color: #999999;
  557. display: flex;
  558. align-items: center;
  559. justify-content: flex-end;
  560. }
  561. .img_list {
  562. width: 100%;
  563. display: grid;
  564. grid-template-columns: repeat(3, 1fr);
  565. row-gap: 20rpx;
  566. flex-wrap: wrap;
  567. margin-top: 15rpx;
  568. .image {
  569. display: flex;
  570. width: 208rpx;
  571. height: 208rpx;
  572. position: relative;
  573. box-sizing: border-box;
  574. justify-content: center;
  575. align-items: center;
  576. background: #f2f6f2;
  577. border-radius: 16rpx;
  578. .txt {
  579. font-size: 120rpx;
  580. color: #999;
  581. width: 40rpx;
  582. height: 40rpx;
  583. }
  584. .upimg {
  585. position: absolute;
  586. left: 0;
  587. top: 0;
  588. width: 100%;
  589. height: 100%;
  590. border-radius: 8rpx;
  591. }
  592. .del {
  593. width: 40rpx;
  594. height: 40rpx;
  595. position: absolute;
  596. right: 0rpx;
  597. top: 0rpx;
  598. }
  599. .loading {
  600. position: absolute;
  601. left: 0;
  602. top: 0;
  603. width: 100%;
  604. height: 100%;
  605. background: rgba(0, 0, 0, 0.5);
  606. display: flex;
  607. align-items: center;
  608. justify-content: center;
  609. border-radius: 8rpx;
  610. .loading-icon {
  611. width: 40rpx;
  612. height: 40rpx;
  613. border: 4rpx solid #fff;
  614. border-top-color: transparent;
  615. border-radius: 50%;
  616. animation: spin 1s linear infinite;
  617. }
  618. }
  619. }
  620. }
  621. }
  622. .worksBox {
  623. border-radius: 20rpx;
  624. overflow: hidden;
  625. margin: 0 24rpx;
  626. background: #fff;
  627. margin-top: 20rpx;
  628. padding-left: 24rpx;
  629. display: flex;
  630. align-items: center;
  631. justify-content: space-between;
  632. .optionalWorks {
  633. display: flex;
  634. align-items: center;
  635. justify-content: space-between;
  636. padding: 24rpx;
  637. height: 88rpx;
  638. padding-left: 0;
  639. .title {
  640. font-family: "PingFang SC-Bold";
  641. font-weight: bold;
  642. font-size: 28rpx;
  643. text {
  644. color: #bbb;
  645. font-size: 24rpx;
  646. padding-left: 10rpx;
  647. }
  648. }
  649. }
  650. .select-box {
  651. width: 64rpx;
  652. height: 64rpx;
  653. image {
  654. width: 64rpx;
  655. height: 64rpx;
  656. }
  657. }
  658. }
  659. @keyframes spin {
  660. to {
  661. transform: rotate(360deg);
  662. }
  663. }
  664. </style>