fabuArticle.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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 style="font-weight: normal;" 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. let img_str = '';
  297. if (this.img_list.length > 0) {
  298. for (let i = 0; i < this.img_list.length; i++) {
  299. img_str += this.img_list[i] + "|";
  300. }
  301. }
  302. // 校验图片数量和作品选择
  303. if (this.img_list.length < 1 && !this.selectedWork.id) {
  304. uni.showToast({
  305. title: '请上传 图片 或 选择一个作品',
  306. icon: 'none'
  307. });
  308. return;
  309. } else {
  310. if (!this.selectedWork.id) {
  311. // 校验图片字符串有效性(可选,因为已经检查了图片数量)
  312. if (!img_str || img_str.trim().length < 2) {
  313. uni.showToast({
  314. title: '请确保至少上传一张有效图片作为封面',
  315. icon: 'none'
  316. });
  317. return;
  318. }
  319. // 校验图片数量不超过 5 张
  320. if (this.img_list.length > 5) {
  321. uni.showToast({
  322. title: '最多只能上传 5 张图片',
  323. icon: 'none'
  324. });
  325. return;
  326. }
  327. }
  328. }
  329. let that = this;
  330. this.is_submit = 1;
  331. uni.request({
  332. url: this.$apiHost + '/Article/add',
  333. data: {
  334. uuid: getApp().globalData.uuid,
  335. id: this.ainfo.id,
  336. title: this.ainfo.title,
  337. content: this.ainfo.content,
  338. img_list: img_str,
  339. work_id: this.selectedWork.id
  340. },
  341. //data: formdata, // 这里传入你的参数(json格式)
  342. method: 'POST',
  343. header: {
  344. 'Content-Type': 'application/x-www-form-urlencoded',
  345. 'sign': getApp().globalData.headerSign
  346. },
  347. dataType: 'json',
  348. success: (res) => {
  349. console.log("---", res.data);
  350. uni.showToast({
  351. title: res.data.str,
  352. icon: 'none'
  353. });
  354. if (res.data.success == "yes") {
  355. // if (that.ainfo.id > 0) {
  356. console.log(1);
  357. setTimeout(function() {
  358. uni.$emit('switchToMyPage', {
  359. type: 'article'
  360. });
  361. uni.switchTab({
  362. url: '/pages/my/my'
  363. })
  364. }, 500);
  365. // } else {
  366. console.log(2);
  367. that.ainfo.title = '';
  368. that.ainfo.content = '';
  369. that.ainfo.num = '';
  370. that.img_list = [];
  371. // }
  372. } else {}
  373. this.is_submit = 0;
  374. },
  375. fail: (err) => {
  376. console.log('登录失败:', err);
  377. this.is_submit = 0;
  378. },
  379. complete: (com) => {}
  380. })
  381. },
  382. select(e) {
  383. console.log('选择文件:', e)
  384. },
  385. upload(type) {
  386. if (this.img_list.length >= 5) {
  387. uni.showToast({
  388. title: '最多只能上传5张图片',
  389. icon: 'none'
  390. });
  391. return
  392. }
  393. uni.showActionSheet({
  394. itemList: ['拍照', '从相册选择'],
  395. success: (res) => {
  396. const sourceType = res.tapIndex === 0 ? 'camera' : 'album';
  397. if (plus.os.name.toLowerCase() === 'android') {
  398. this.checkRights(sourceType);
  399. } else {
  400. this.upload2('list', sourceType);
  401. }
  402. }
  403. });
  404. },
  405. async checkRights(sourceType) {
  406. try {
  407. let hasPermission = false;
  408. if (sourceType === 'camera') {
  409. hasPermission = await permission.request(permission.PermissionType.CAMERA, {
  410. title: '“萌创星球”想访问你的相机',
  411. describe: '萌创星球想访问您的摄像头,便于拍摄获取图片来与其他用户进行交流'
  412. });
  413. if (!hasPermission) {
  414. uni.showToast({
  415. title: '未获得相机权限',
  416. icon: 'none'
  417. });
  418. return;
  419. }
  420. } else {
  421. hasPermission = await permission.request(permission.PermissionType.PHOTO_LIBRARY, {
  422. title: '“萌创星球”想访问你的照片图库',
  423. describe: '萌创星球想访问您本地照片图库,便于获取图片来与其他用户进行交流'
  424. });
  425. if (!hasPermission) {
  426. uni.showToast({
  427. title: '未获得相册权限',
  428. icon: 'none'
  429. });
  430. return;
  431. }
  432. }
  433. // 权限获取成功后,继续执行上传
  434. this.upload2('list', sourceType);
  435. } catch (error) {
  436. console.error('权限检查失败:', error);
  437. uni.showToast({
  438. title: '权限检查失败',
  439. icon: 'none'
  440. });
  441. }
  442. },
  443. upload2(type, sourceType) {
  444. console.log("----upload");
  445. var _self = this;
  446. uni.chooseImage({
  447. count: 5,
  448. sizeType: ['compressed'],
  449. sourceType: [sourceType],
  450. success: function(res) {
  451. console.log('res:', res)
  452. for (let index = 0; index < res.tempFilePaths.length; index++) {
  453. let filepath = "";
  454. // #ifdef H5
  455. filepath = res.tempFiles[index].path;
  456. // #endif
  457. // #ifdef APP-PLUS
  458. filepath = res.tempFilePaths[index];
  459. // #endif
  460. _self.imglocal = filepath; //res.tempFilePaths[0]
  461. const uploadTask = uni.uploadFile({
  462. url: _self.$apiHost + '/Xweb/upload_img?skey=' + _self
  463. .skey, // post请求地址
  464. filePath: filepath,
  465. name: 'file', // 待确认
  466. success: function(uploadFileRes) {
  467. let resdata = JSON.parse(uploadFileRes.data)
  468. console.log('Success1:', uploadFileRes);
  469. if (resdata.success == 'no') {
  470. uni.showToast({
  471. title: resdata.str,
  472. icon: 'none'
  473. });
  474. return;
  475. }
  476. if (resdata.code == 0) {
  477. console.log('Success2:', resdata.data.path);
  478. if (type == 'avatar') {
  479. _self.img2 = resdata.data.path;
  480. } else if (type == 'list') {
  481. _self.img_list.push(resdata.data.path)
  482. console.log('_self.img_list:', _self.img_list);
  483. } else if (type == 'list2') {
  484. _self.img_list2.push(resdata.data.path)
  485. } else if (type == 'list1') {
  486. _self.img_list1.push(resdata.data.path)
  487. }
  488. }
  489. },
  490. fail: function(uploadFileFail) {
  491. console.log('Error:', uploadFileFail.data);
  492. },
  493. complete: () => {
  494. console.log('Complete:');
  495. }
  496. });
  497. }
  498. },
  499. error: function(e) {
  500. console.log(e);
  501. }
  502. });
  503. },
  504. deleteWorkInfo() {
  505. this.selectedWork = {}
  506. }
  507. }
  508. }
  509. </script>
  510. <style scoped lang="scss">
  511. @import 'fabuArticle.scss';
  512. </style>