fabuArticle.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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. export default {
  97. components: {},
  98. data() {
  99. return {
  100. skey: '',
  101. uinfo: {},
  102. is_first: 0,
  103. pid: 0,
  104. sel: 0,
  105. pop_sel: false,
  106. cate: '',
  107. cid: 0,
  108. catename: '',
  109. img2: '',
  110. cateNames: [],
  111. cate_list: [],
  112. CateIndex: 0,
  113. nameList1: [],
  114. nameList2: [],
  115. content: '',
  116. cate: '',
  117. ainfo: {
  118. id: 0,
  119. title: '',
  120. content: '',
  121. name: '',
  122. telphone: '',
  123. address: '',
  124. contact: '',
  125. num: '',
  126. },
  127. TypeArray: ['请选择', '线下服务', '电话咨询', '远程服务', '视频咨询', '其他服务'],
  128. TypeIndex: 0,
  129. TypeArray2: ['请选择', '次', '天', '小时', '分钟', '周', '月', '年', '单', '份'],
  130. TypeIndex2: 0,
  131. img_list: [],
  132. img_list2: [],
  133. img_list1: [],
  134. visible: false,
  135. maskCloseAble: true,
  136. str: '',
  137. defaultValue: '',
  138. listData: [],
  139. cateTag: [],
  140. money: '',
  141. item_gz: -1,
  142. item_time: [],
  143. item_dx: [],
  144. is_top: false,
  145. is_agree: 0,
  146. selectedWork: {}
  147. }
  148. },
  149. onLoad(params) {
  150. if (getApp().globalData.skey != "") {
  151. this.skey = getApp().globalData.skey;
  152. } else {
  153. this.skey = params.skey || ''; //1234567xef
  154. }
  155. if (params.id) {
  156. this.id = params.id;
  157. } else {
  158. this.id = -1;
  159. }
  160. let that = this;
  161. // this.getCate();
  162. that.getInfo();
  163. // this.getData();
  164. // 添加事件监听
  165. uni.$on('selectItem', this.handleSelect);
  166. },
  167. onUnload() {
  168. uni.$off('selectItem', this.handleSelect);
  169. },
  170. onShow() {
  171. let that = this;
  172. },
  173. methods: {
  174. goBack() {
  175. uni.navigateBack();
  176. },
  177. handleSelect(item) {
  178. console.log(item, '收到修改的数据');
  179. this.selectedWork = item;
  180. },
  181. onManage() {
  182. // 处理管理按钮点击
  183. // uni.redirectTo({
  184. // url: '/pages/make/manageArticle'
  185. // })
  186. uni.navigateTo({
  187. url: '/pages/make/relatedWorks'
  188. })
  189. },
  190. onPublish() {
  191. this.onSubmit();
  192. },
  193. copyKd(item_data) {
  194. if (item_data != "") {
  195. uni.setClipboardData({
  196. data: item_data, // e是需要设置的内容
  197. success: function () {
  198. uni.showToast({
  199. title: '网址已复制',
  200. icon: 'none'
  201. })
  202. }
  203. })
  204. }
  205. },
  206. agreeChk() {
  207. if (this.is_agree == 0) {
  208. this.is_agree = 1;
  209. } else {
  210. this.is_agree = 0;
  211. }
  212. },
  213. showPicker() {
  214. this.pop_sel = true;
  215. },
  216. open() {
  217. this.visible = true
  218. },
  219. confirm(val) {
  220. console.log(val)
  221. this.defaultValue = val.code
  222. this.ainfo.city = val.desc
  223. this.visible = false
  224. },
  225. cancel() {
  226. this.visible = false
  227. },
  228. delIt(ind) {
  229. let arr = [];
  230. if (this.img_list.length > 0) {
  231. for (let i = 0; i < this.img_list.length; i++) {
  232. if (i == ind) { } else {
  233. arr.push(this.img_list[i]);
  234. }
  235. }
  236. this.img_list = arr;
  237. }
  238. },
  239. getInfo() {
  240. var that = this;
  241. uni.request({
  242. url: this.$apiHost + '/Article/getinfo', //仅为示例,并非真实接口地址。
  243. data: {
  244. uuid: getApp().globalData.uuid,
  245. id: this.id
  246. },
  247. header: {
  248. 'content-type': 'application/json' //自定义请求头信息
  249. },
  250. success: (res) => {
  251. console.log("====", res.data);
  252. this.ainfo = res.data.article;
  253. that.img_list1 = [];
  254. that.img_list = [];
  255. that.img_list2 = [];
  256. this.selectedWork = res.data.work_info;
  257. if (res.data.article.images.length > 1) {
  258. let arr = res.data.article.images.split("|");
  259. if (arr.length > 0) {
  260. for (let i = 0; i < arr.length; i++) {
  261. if (arr[i].length > 10) {
  262. that.img_list.push(arr[i]);
  263. }
  264. }
  265. }
  266. }
  267. }
  268. });
  269. },
  270. checkField(str, tips) {
  271. if (str.length < 2) {
  272. uni.showToast({
  273. title: tips,
  274. icon: 'none'
  275. });
  276. return false;
  277. }
  278. return true;
  279. },
  280. onSubmit() {
  281. console.log("skey", this.skey);
  282. // 校验标题
  283. if (this.checkField(this.ainfo.title, "请输入名称") == false) {
  284. return;
  285. }
  286. // 校验内容
  287. if (this.checkField(this.ainfo.content, "请输入具体内容") == false) {
  288. return;
  289. }
  290. // 校验是否正在提交
  291. if (this.is_submit > 0) {
  292. return;
  293. }
  294. // 校验图片数量和作品选择
  295. if (this.img_list.length < 1 && !this.selectedWork.id) {
  296. uni.showToast({
  297. title: '请上传 图片 或 选择一个作品',
  298. icon: 'none'
  299. });
  300. return;
  301. }else{
  302. // 校验图片字符串有效性(可选,因为已经检查了图片数量)
  303. if (!img_str || img_str.trim().length < 2) {
  304. uni.showToast({
  305. title: '请确保至少上传一张有效图片作为封面',
  306. icon: 'none'
  307. });
  308. return;
  309. }
  310. // 校验图片数量不超过 5 张
  311. if (this.img_list.length > 5) {
  312. uni.showToast({
  313. title: '最多只能上传 5 张图片',
  314. icon: 'none'
  315. });
  316. return;
  317. }
  318. }
  319. // 构建图片字符串
  320. let img_str = '';
  321. if (this.img_list.length > 0) {
  322. for (let i = 0; i < this.img_list.length; i++) {
  323. img_str += this.img_list[i] + "|";
  324. }
  325. }
  326. let that = this;
  327. this.is_submit = 1;
  328. uni.request({
  329. url: this.$apiHost + '/Article/add',
  330. data: {
  331. uuid: getApp().globalData.uuid,
  332. id: this.ainfo.id,
  333. title: this.ainfo.title,
  334. content: this.ainfo.content,
  335. img_list: img_str,
  336. work_id: this.selectedWork.id
  337. },
  338. //data: formdata, // 这里传入你的参数(json格式)
  339. method: 'POST',
  340. header: {
  341. 'Content-Type': 'application/x-www-form-urlencoded',
  342. 'sign': getApp().globalData.headerSign
  343. },
  344. dataType: 'json',
  345. success: (res) => {
  346. console.log("---", res.data);
  347. uni.showToast({
  348. title: res.data.str,
  349. icon: 'none'
  350. });
  351. if (res.data.success == "yes") {
  352. // if (that.ainfo.id > 0) {
  353. console.log(1);
  354. setTimeout(function () {
  355. uni.$emit('switchToMyPage', { type: 'article' });
  356. uni.switchTab({ url: '/pages/my/my' })
  357. }, 500);
  358. // } else {
  359. console.log(2);
  360. that.ainfo.title = '';
  361. that.ainfo.content = '';
  362. that.ainfo.num = '';
  363. that.img_list = [];
  364. // }
  365. } else { }
  366. this.is_submit = 0;
  367. },
  368. fail: (err) => {
  369. console.log('登录失败:', err);
  370. this.is_submit = 0;
  371. },
  372. complete: (com) => { }
  373. })
  374. },
  375. select(e) {
  376. console.log('选择文件:', e)
  377. },
  378. upload(type) {
  379. if (this.img_list.length >= 5) {
  380. uni.showToast({
  381. title: '最多只能上传5张图片',
  382. icon: 'none'
  383. });
  384. return
  385. }
  386. this.upload2(type)
  387. },
  388. upload2(type) {
  389. console.log("----upload");
  390. // this.$refs.files.upload();
  391. // 图片选择上传upload() {
  392. var _self = this;
  393. // 图片选择,只支持一次选择一张图片
  394. uni.chooseImage({
  395. count: 5,
  396. sizeType: ['compressed'], //可以指定是原图还是压缩图,默认二者都有
  397. sourceType: ['album', 'camera'], //从相册、相机选择
  398. success: function (res) {
  399. console.log('res:', res)
  400. for (let index = 0; index < res.tempFilePaths.length; index++) {
  401. let filepath = "";
  402. // #ifdef H5
  403. filepath = res.tempFiles[index].path;
  404. // #endif
  405. // #ifdef APP-PLUS
  406. filepath = res.tempFilePaths[index];
  407. // #endif
  408. _self.imglocal = filepath; //res.tempFilePaths[0]
  409. const uploadTask = uni.uploadFile({
  410. url: _self.$apiHost + '/Xweb/upload_img?skey=' + _self.skey, // post请求地址
  411. filePath: filepath,
  412. name: 'file', // 待确认
  413. success: function (uploadFileRes) {
  414. let resdata = JSON.parse(uploadFileRes.data)
  415. console.log('Success1:', uploadFileRes);
  416. if (resdata.success == 'no') {
  417. uni.showToast({
  418. title: resdata.str,
  419. icon: 'none'
  420. });
  421. return;
  422. }
  423. if (resdata.code == 0) {
  424. console.log('Success2:', resdata.data.path);
  425. if (type == 'avatar') {
  426. _self.img2 = resdata.data.path;
  427. } else if (type == 'list') {
  428. _self.img_list.push(resdata.data.path)
  429. console.log('_self.img_list:', _self.img_list);
  430. } else if (type == 'list2') {
  431. _self.img_list2.push(resdata.data.path)
  432. } else if (type == 'list1') {
  433. _self.img_list1.push(resdata.data.path)
  434. }
  435. }
  436. },
  437. fail: function (uploadFileFail) {
  438. console.log('Error:', uploadFileFail.data);
  439. },
  440. complete: () => {
  441. console.log('Complete:');
  442. }
  443. });
  444. }
  445. },
  446. error: function (e) {
  447. console.log(e);
  448. }
  449. });
  450. },
  451. deleteWorkInfo() {
  452. this.selectedWork = {}
  453. }
  454. }
  455. }
  456. </script>
  457. <style scoped lang="scss">
  458. @import 'fabuArticle.scss';
  459. </style>