fabuArticle.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. <view class="content-title">
  16. <input class="edit" v-model="ainfo.title" maxlength="20" placeholder="标题(必填)" />
  17. </view>
  18. <view class="content-textarea">
  19. <textarea class="edit2" auto-height v-model="ainfo.content" :maxlength="1000"
  20. placeholder="添加描述"></textarea>
  21. </view>
  22. <view class="agree">
  23. <text class="xy">{{ ainfo.content.length }}/1000</text>
  24. </view>
  25. <!-- 上限五张图片 -->
  26. <view class="img_list">
  27. <view class="image" v-for="(item, index) in img_list" :key="index">
  28. <image class="upimg" :src="item" mode="aspectFill"></image>
  29. <image class="del" src="@/static/icon/img-del.png" @click="delIt(index)"></image>
  30. </view>
  31. <view class="image" @click="upload('list')" v-if="img_list.length < 9">
  32. <!-- <text class="txt">+</text> -->
  33. <image class="txt" src="../../static/icon/sy_icon_jiahao02.png"></image>
  34. </view>
  35. </view>
  36. </view>
  37. <view class="worksBox">
  38. <view class="optionalWorks" @click="onManage">
  39. <view class="title">
  40. 可选作品
  41. <text v-if="id < 1">(图片或作品必选一个发布)</text>
  42. </view>
  43. <view class="selectLeft">
  44. <text class="prompt" v-if="id > 1">已选择一个作品</text>
  45. <image class="rightArrow" src="../../static/me/wd_icon_jiantou2.png"></image>
  46. </view>
  47. </view>
  48. <view class="activeItem" v-if="selectedWork.images">
  49. <view class="img-box">
  50. <image :src="selectedWork.images"></image>
  51. </view>
  52. <view class="info">
  53. <view class="title">{{ selectedWork.title }}</view>
  54. </view>
  55. <view class="select-box" @click="deleteWorkInfo()">
  56. <image src="../../static/icon/close.png"></image>
  57. </view>
  58. </view>
  59. </view>
  60. <view class="nav-bar" v-if="false">
  61. <view class="left">
  62. <view class="uni-btn-icon" @click="goBack">&#xe601;</view>
  63. </view>
  64. <view class="center">发表想法</view>
  65. <view class="right">
  66. <view class="btn" @click="onManage">管理</view>
  67. <view class="btn" @click="onSubmit" v-if="id < 1">发布</view>
  68. <view class="btn" @click="onSubmit" v-else>更新</view>
  69. </view>
  70. </view>
  71. <view class="user-list" v-if="false">
  72. <view class="content">
  73. <input class="edit" v-model="ainfo.title" maxlength="20" placeholder="填写标题会有更多赞哦~" />
  74. </view>
  75. <view class="content">
  76. <textarea class="edit2" v-model="ainfo.content" :maxlength="1000"
  77. placeholder="美好的一天,写点什么吧..."></textarea>
  78. </view>
  79. <view class="agree">
  80. <text>还可以输入<text class="xy">{{ 1000 - ainfo.content.length }}</text>字</text>
  81. </view>
  82. <view class="img_list">
  83. <view class="image" v-for="(item, index) in img_list" :key="index">
  84. <image class="upimg" :src="item" mode="aspectFill"></image>
  85. <image class="del" src="../../static/del.svg" @click="delIt(index)"></image>
  86. </view>
  87. <view class="image" @click="upload('list')" v-if="img_list.length < 9">
  88. <text class="txt">+</text>
  89. </view>
  90. </view>
  91. <view style="display:flex;flex-direction: column;height:200rpx;"></view>
  92. </view>
  93. </view>
  94. </template>
  95. <script>
  96. import permission from '@/common/permission.js';
  97. export default {
  98. components: {},
  99. data() {
  100. return {
  101. skey: '',
  102. uinfo: {},
  103. is_first: 0,
  104. pid: 0,
  105. sel: 0,
  106. pop_sel: false,
  107. cate: '',
  108. cid: 0,
  109. catename: '',
  110. img2: '',
  111. cateNames: [],
  112. cate_list: [],
  113. CateIndex: 0,
  114. nameList1: [],
  115. nameList2: [],
  116. content: '',
  117. cate: '',
  118. ainfo: {
  119. id: 0,
  120. title: '',
  121. content: '',
  122. name: '',
  123. telphone: '',
  124. address: '',
  125. contact: '',
  126. num: '',
  127. },
  128. TypeArray: ['请选择', '线下服务', '电话咨询', '远程服务', '视频咨询', '其他服务'],
  129. TypeIndex: 0,
  130. TypeArray2: ['请选择', '次', '天', '小时', '分钟', '周', '月', '年', '单', '份'],
  131. TypeIndex2: 0,
  132. img_list: [],
  133. img_list2: [],
  134. img_list1: [],
  135. visible: false,
  136. maskCloseAble: true,
  137. str: '',
  138. defaultValue: '',
  139. listData: [],
  140. cateTag: [],
  141. money: '',
  142. item_gz: -1,
  143. item_time: [],
  144. item_dx: [],
  145. is_top: false,
  146. is_agree: 0,
  147. selectedWork: {}
  148. }
  149. },
  150. onLoad(params) {
  151. if (getApp().globalData.skey != "") {
  152. this.skey = getApp().globalData.skey;
  153. } else {
  154. this.skey = params.skey || ''; //1234567xef
  155. }
  156. if (params.id) {
  157. this.id = params.id;
  158. } else {
  159. this.id = -1;
  160. }
  161. let that = this;
  162. // this.getCate();
  163. that.getInfo();
  164. // this.getData();
  165. // 添加事件监听
  166. uni.$on('selectItem', this.handleSelect);
  167. },
  168. onUnload() {
  169. uni.$off('selectItem', this.handleSelect);
  170. },
  171. onShow() {
  172. let that = this;
  173. },
  174. methods: {
  175. goBack() {
  176. uni.navigateBack();
  177. },
  178. handleSelect(item) {
  179. console.log(item, '收到修改的数据');
  180. this.selectedWork = item;
  181. },
  182. onManage() {
  183. // 处理管理按钮点击
  184. // uni.redirectTo({
  185. // url: '/pages/make/manageArticle'
  186. // })
  187. uni.navigateTo({
  188. url: '/pages/make/relatedWorks'
  189. })
  190. },
  191. onPublish() {
  192. this.onSubmit();
  193. },
  194. copyKd(item_data) {
  195. if (item_data != "") {
  196. uni.setClipboardData({
  197. data: item_data, // e是需要设置的内容
  198. success: function () {
  199. uni.showToast({
  200. title: '网址已复制',
  201. icon: 'none'
  202. })
  203. }
  204. })
  205. }
  206. },
  207. agreeChk() {
  208. if (this.is_agree == 0) {
  209. this.is_agree = 1;
  210. } else {
  211. this.is_agree = 0;
  212. }
  213. },
  214. showPicker() {
  215. this.pop_sel = true;
  216. },
  217. open() {
  218. this.visible = true
  219. },
  220. confirm(val) {
  221. console.log(val)
  222. this.defaultValue = val.code
  223. this.ainfo.city = val.desc
  224. this.visible = false
  225. },
  226. cancel() {
  227. this.visible = false
  228. },
  229. delIt(ind) {
  230. let arr = [];
  231. if (this.img_list.length > 0) {
  232. for (let i = 0; i < this.img_list.length; i++) {
  233. if (i == ind) { } else {
  234. arr.push(this.img_list[i]);
  235. }
  236. }
  237. this.img_list = arr;
  238. }
  239. },
  240. getInfo() {
  241. var that = this;
  242. uni.request({
  243. url: this.$apiHost + '/Article/getinfo', //仅为示例,并非真实接口地址。
  244. data: {
  245. uuid: getApp().globalData.uuid,
  246. id: this.id
  247. },
  248. header: {
  249. 'content-type': 'application/json' //自定义请求头信息
  250. },
  251. success: (res) => {
  252. console.log("====", res.data);
  253. this.ainfo = res.data.article;
  254. that.img_list1 = [];
  255. that.img_list = [];
  256. that.img_list2 = [];
  257. this.selectedWork = res.data.work_info;
  258. if (res.data.article.images.length > 1) {
  259. let arr = res.data.article.images.split("|");
  260. if (arr.length > 0) {
  261. for (let i = 0; i < arr.length; i++) {
  262. if (arr[i].length > 10) {
  263. that.img_list.push(arr[i]);
  264. }
  265. }
  266. }
  267. }
  268. }
  269. });
  270. },
  271. checkField(str, tips) {
  272. if (str.length < 2) {
  273. uni.showToast({
  274. title: tips,
  275. icon: 'none'
  276. });
  277. return false;
  278. }
  279. return true;
  280. },
  281. onSubmit() {
  282. console.log("skey", this.skey);
  283. // 校验标题
  284. if (this.checkField(this.ainfo.title, "请输入名称") == false) {
  285. return;
  286. }
  287. // 校验内容
  288. if (this.checkField(this.ainfo.content, "请输入具体内容") == false) {
  289. return;
  290. }
  291. // 校验是否正在提交
  292. if (this.is_submit > 0) {
  293. return;
  294. }
  295. // 校验图片数量和作品选择
  296. if (this.img_list.length < 1 && !this.selectedWork.id) {
  297. uni.showToast({
  298. title: '请上传 图片 或 选择一个作品',
  299. icon: 'none'
  300. });
  301. return;
  302. }else{
  303. // 校验图片字符串有效性(可选,因为已经检查了图片数量)
  304. if (!img_str || img_str.trim().length < 2) {
  305. uni.showToast({
  306. title: '请确保至少上传一张有效图片作为封面',
  307. icon: 'none'
  308. });
  309. return;
  310. }
  311. // 校验图片数量不超过 5 张
  312. if (this.img_list.length > 5) {
  313. uni.showToast({
  314. title: '最多只能上传 5 张图片',
  315. icon: 'none'
  316. });
  317. return;
  318. }
  319. }
  320. // 构建图片字符串
  321. let img_str = '';
  322. if (this.img_list.length > 0) {
  323. for (let i = 0; i < this.img_list.length; i++) {
  324. img_str += this.img_list[i] + "|";
  325. }
  326. }
  327. let that = this;
  328. this.is_submit = 1;
  329. uni.request({
  330. url: this.$apiHost + '/Article/add',
  331. data: {
  332. uuid: getApp().globalData.uuid,
  333. id: this.ainfo.id,
  334. title: this.ainfo.title,
  335. content: this.ainfo.content,
  336. img_list: img_str,
  337. work_id: this.selectedWork.id
  338. },
  339. //data: formdata, // 这里传入你的参数(json格式)
  340. method: 'POST',
  341. header: {
  342. 'Content-Type': 'application/x-www-form-urlencoded',
  343. 'sign': getApp().globalData.headerSign
  344. },
  345. dataType: 'json',
  346. success: (res) => {
  347. console.log("---", res.data);
  348. uni.showToast({
  349. title: res.data.str,
  350. icon: 'none'
  351. });
  352. if (res.data.success == "yes") {
  353. // if (that.ainfo.id > 0) {
  354. console.log(1);
  355. setTimeout(function () {
  356. uni.$emit('switchToMyPage', { type: 'article' });
  357. uni.switchTab({ url: '/pages/my/my' })
  358. }, 500);
  359. // } else {
  360. console.log(2);
  361. that.ainfo.title = '';
  362. that.ainfo.content = '';
  363. that.ainfo.num = '';
  364. that.img_list = [];
  365. // }
  366. } else { }
  367. this.is_submit = 0;
  368. },
  369. fail: (err) => {
  370. console.log('登录失败:', err);
  371. this.is_submit = 0;
  372. },
  373. complete: (com) => { }
  374. })
  375. },
  376. select(e) {
  377. console.log('选择文件:', e)
  378. },
  379. upload(type) {
  380. if (this.img_list.length >= 5) {
  381. uni.showToast({
  382. title: '最多只能上传5张图片',
  383. icon: 'none'
  384. });
  385. return
  386. }
  387. uni.showActionSheet({
  388. itemList: ['拍照', '从相册选择'],
  389. success: (res) => {
  390. const sourceType = res.tapIndex === 0 ? 'camera' : 'album';
  391. this.checkRights(sourceType);
  392. }
  393. });
  394. },
  395. async checkRights(sourceType) {
  396. try {
  397. let hasPermission = false;
  398. if (sourceType === 'camera') {
  399. hasPermission = await permission.request(permission.PermissionType.CAMERA, {
  400. title: '“萌创星球”想访问你的相机',
  401. describe: '萌创星球想访问您的摄像头,便于拍摄获取图片来与其他用户进行交流'
  402. });
  403. if (!hasPermission) {
  404. uni.showToast({
  405. title: '未获得相机权限',
  406. icon: 'none'
  407. });
  408. return;
  409. }
  410. } else {
  411. hasPermission = await permission.request(permission.PermissionType.PHOTO_LIBRARY, {
  412. title: '“萌创星球”想访问你的照片图库',
  413. describe: '萌创星球想访问您本地照片图库,便于获取图片来与其他用户进行交流'
  414. });
  415. if (!hasPermission) {
  416. uni.showToast({
  417. title: '未获得相册权限',
  418. icon: 'none'
  419. });
  420. return;
  421. }
  422. }
  423. // 权限获取成功后,继续执行上传
  424. this.upload2(type, sourceType);
  425. } catch (error) {
  426. console.error('权限检查失败:', error);
  427. uni.showToast({
  428. title: '权限检查失败',
  429. icon: 'none'
  430. });
  431. }
  432. },
  433. upload2(type, sourceType) {
  434. console.log("----upload");
  435. var _self = this;
  436. uni.chooseImage({
  437. count: 5,
  438. sizeType: ['compressed'],
  439. sourceType: [sourceType],
  440. success: function(res) {
  441. console.log('res:', res)
  442. for (let index = 0; index < res.tempFilePaths.length; index++) {
  443. let filepath = "";
  444. // #ifdef H5
  445. filepath = res.tempFiles[index].path;
  446. // #endif
  447. // #ifdef APP-PLUS
  448. filepath = res.tempFilePaths[index];
  449. // #endif
  450. _self.imglocal = filepath; //res.tempFilePaths[0]
  451. const uploadTask = uni.uploadFile({
  452. url: _self.$apiHost + '/Xweb/upload_img?skey=' + _self.skey, // post请求地址
  453. filePath: filepath,
  454. name: 'file', // 待确认
  455. success: function (uploadFileRes) {
  456. let resdata = JSON.parse(uploadFileRes.data)
  457. console.log('Success1:', uploadFileRes);
  458. if (resdata.success == 'no') {
  459. uni.showToast({
  460. title: resdata.str,
  461. icon: 'none'
  462. });
  463. return;
  464. }
  465. if (resdata.code == 0) {
  466. console.log('Success2:', resdata.data.path);
  467. if (type == 'avatar') {
  468. _self.img2 = resdata.data.path;
  469. } else if (type == 'list') {
  470. _self.img_list.push(resdata.data.path)
  471. console.log('_self.img_list:', _self.img_list);
  472. } else if (type == 'list2') {
  473. _self.img_list2.push(resdata.data.path)
  474. } else if (type == 'list1') {
  475. _self.img_list1.push(resdata.data.path)
  476. }
  477. }
  478. },
  479. fail: function (uploadFileFail) {
  480. console.log('Error:', uploadFileFail.data);
  481. },
  482. complete: () => {
  483. console.log('Complete:');
  484. }
  485. });
  486. }
  487. },
  488. error: function (e) {
  489. console.log(e);
  490. }
  491. });
  492. },
  493. deleteWorkInfo() {
  494. this.selectedWork = {}
  495. }
  496. }
  497. }
  498. </script>
  499. <style scoped lang="scss">
  500. @import 'fabuArticle.scss';
  501. </style>