goods-detail.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. <template>
  2. <view class="app-layout">
  3. <app-layout :overflow="overflow">
  4. <view class="show" v-if="list.length == 0">
  5. <image @click="showMenu(0)" src='./../image/add-detail.png'></image>
  6. <view>快来添加内容,来介绍你的商品吧!</view>
  7. </view>
  8. <view v-else>
  9. <view class="list-item dir-top-nowrap" v-for="(item,index) in list" :key="index">
  10. <image v-if="item.type =='line'" class="line" :src="adminImg.line"></image>
  11. <view v-if="item.type =='img' && item.pic_url == ''" class="loading main-center dir-top-nowrap cross-center">
  12. <image class="loading-img" src="/static/image/loading.gif"></image>
  13. <view class="loading-text">图片上传中,请稍后...</view>
  14. </view>
  15. <image v-if="item.type =='img' && item.pic_url != ''" :style="[{'height':`${item.height}`}]" @load="imageLoad(index,$event)" :src="item.pic_url"></image>
  16. <view v-if="item.type=='text'" class="txt"><text>{{item.text}}</text></view>
  17. <view class="choose dir-right-nowrap">
  18. <view @click="showMenu(index)" class="choose-item">插入</view>
  19. <view v-if="item.type=='text'" @click="editText(index,item)" class="choose-item">编辑</view>
  20. <view v-if="item.type=='img'" @click="toggle(index)" class="choose-item">替换</view>
  21. <view @click="move(1,index,item)" v-if="index != list.length - 1" class="choose-item">下移</view>
  22. <view @click="move(2,index,item)" v-if="index != 0" class="choose-item">上移</view>
  23. <view @click="toRemove(index)" class="choose-item">删除</view>
  24. </view>
  25. </view>
  26. <view :class="['placeholder', `${iphone_x? 'iphone_x':''}`]"></view>
  27. <view :class="['add', `${iphone_x? 'iphone_x':''}`]">
  28. <view @click="save">保存</view>
  29. </view>
  30. </view>
  31. <view @click="showMenu" v-if="textDialog || menu || is_delete" class="dialog">
  32. <view v-if="menu" class="menu">
  33. <view class="menu-list main-center">
  34. <view v-if="!toToggle" @click.stop="addLine" class="menu-item">
  35. <image src="./../image/line.png"></image>
  36. <view>分割线</view>
  37. </view>
  38. <view v-if="!toToggle" @click.stop="showText" class="menu-item">
  39. <image src="./../image/txt.png"></image>
  40. <view>文本</view>
  41. </view>
  42. <view @click="add('album')" class="menu-item">
  43. <image src="./../image/image.png"></image>
  44. <view>图片</view>
  45. </view>
  46. <view class="toggle-placeholder" v-if="toToggle"></view>
  47. <view @click="add('camera')" class="menu-item">
  48. <image src="./../image/camera.png"></image>
  49. <view>相机</view>
  50. </view>
  51. </view>
  52. <image @click="showMenu" class="close" src="/static/image/icon/close.png"></image>
  53. </view>
  54. <view @click.stop="" v-if="textDialog" class="textarea">
  55. <textarea focus maxlength="-1" @confirm="toList" show-confirm-bar confirm-type="done" v-model="txt"></textarea>
  56. </view>
  57. <view class="dialog-item" v-if="is_delete">
  58. <view class="dialog-title">提示</view>
  59. <view class="dialog-txt">是否删除该组件</view>
  60. <view class="main-center btn-area">
  61. <view class="submit-btn" @click='cancel'>取消</view>
  62. <view class="line"></view>
  63. <view class="submit-btn be-submit" @click='remove'>确认</view>
  64. </view>
  65. </view>
  66. </view>
  67. </app-layout>
  68. </view>
  69. </template>
  70. <script>
  71. import { mapState } from "vuex";
  72. import uploadFile from '../../../core/upload.js';
  73. export default {
  74. data() {
  75. return {
  76. tempFilePaths: '',
  77. imgheight: 0,
  78. menu: false,
  79. overflow: false,
  80. list: [],
  81. txt: '',
  82. textDialog: false,
  83. is_delete: false,
  84. toToggle: false,
  85. index: 0,
  86. reEdit: false,
  87. loadingImg: false,
  88. iphone_x: false
  89. }
  90. },
  91. computed: {
  92. ...mapState({
  93. userInfo: state => state.user.info,
  94. adminImg: state => state.mallConfig.__wxapp_img.app_admin
  95. })
  96. },
  97. methods: {
  98. toList() {
  99. this.textDialog = false;
  100. let item = {
  101. type:'text',
  102. text: this.txt
  103. };
  104. if(this.reEdit) {
  105. this.list.splice(this.index,1,item);
  106. this.reEdit = false;
  107. }else {
  108. this.list.splice(this.index,0,item);
  109. }
  110. },
  111. editText(index,item) {
  112. this.txt = item.text;
  113. this.reEdit = true;
  114. this.textDialog = true;
  115. this.index = index;
  116. },
  117. move(type,index,item) {
  118. this.list.splice(index,1);
  119. if(type == 1) {
  120. this.list.splice(index + 1,0,item);
  121. }else {
  122. this.list.splice(index - 1,0,item);
  123. }
  124. },
  125. toRemove(index) {
  126. this.txt = null;
  127. this.is_delete = true;
  128. this.index = index;
  129. },
  130. cancel() {
  131. this.is_delete = false;
  132. },
  133. remove() {
  134. this.is_delete = false;
  135. this.list.splice(this.index,1);
  136. },
  137. showMenu(index) {
  138. if(index > -1) {
  139. this.index = index + 1;
  140. this.menu = true;
  141. this.txt = '';
  142. this.toToggle = false;
  143. }else {
  144. if(this.txt.length > 0) {
  145. this.toList();
  146. }else {
  147. this.textDialog = false;
  148. }
  149. this.menu = false;
  150. }
  151. },
  152. toggle(index) {
  153. this.index = index;
  154. this.toToggle = true;
  155. this.menu = true;
  156. },
  157. addLine() {
  158. let item = {type:'line'}
  159. this.list.splice(this.index,0,item)
  160. this.menu = false;
  161. },
  162. showText() {
  163. this.textDialog = !this.textDialog;
  164. this.menu = false;
  165. },
  166. add(sourceType) {
  167. let that = this;
  168. this.menu = false;
  169. // #ifdef MP
  170. uni.chooseImage({
  171. count: 1,
  172. sizeType: ['original', 'compressed'],
  173. sourceType: [sourceType],
  174. success(res) {
  175. // tempFilePath可以作为img标签的src属性显示图片
  176. const tempFilePaths = res.tempFilePaths;
  177. let list = that.list;
  178. tempFilePaths.forEach(function (row, index) {
  179. let para = {
  180. pic_url: '',
  181. type: 'img',
  182. id: 0
  183. };
  184. that.loadingImg = true;
  185. that.toToggle ? list.splice(that.index,1,para) : list.splice(that.index,0,para);
  186. that.list = list;
  187. let fileName = '';
  188. // #ifdef MP-BAIDU
  189. fileName = row.substr(row.lastIndexOf('/') + 1);
  190. // #endif
  191. uploadFile({
  192. url: that.$api.upload.file,
  193. filePath: row,
  194. fileType: 'image',
  195. mch_id: 0,
  196. fileName: fileName
  197. }).then(res => {
  198. let imgInfo = undefined;
  199. // #ifndef MP-BAIDU
  200. imgInfo = JSON.parse(res.data);
  201. // #endif
  202. // #ifdef MP-BAIDU
  203. imgInfo = res.data;
  204. // #endif
  205. if (imgInfo.code == 0) {
  206. for(let i in list) {
  207. if(list[i].type === 'img' && list[i].pic_url === '') {
  208. list[i].pic_url = imgInfo.data.url;
  209. list[i].id = imgInfo.data.id;
  210. }
  211. }
  212. }
  213. that.list = list;
  214. if (index == tempFilePaths.length - 1) {
  215. that.loadingImg = false;
  216. uni.hideLoading();
  217. }
  218. });
  219. })
  220. }
  221. })
  222. // #endif
  223. // #ifdef H5
  224. uni.chooseImage({
  225. count: 1,
  226. sizeType: ['original', 'compressed'],
  227. sourceType: [sourceType],
  228. success: function(e) {
  229. uni.showLoading({
  230. mask: true,
  231. title: '上传中...'
  232. });
  233. let list = that.list;
  234. let image = new Image();
  235. image.src = e.tempFilePaths;
  236. let para = {
  237. pic_url: '',
  238. type: 'img',
  239. id: 0
  240. };
  241. that.loadingImg = true;
  242. that.toToggle ? list.splice(that.index,1,para) : list.splice(that.index,0,para);
  243. that.list = list;
  244. image.onload = () => {
  245. let canvas = document.createElement("canvas");
  246. canvas.width = image.width;
  247. canvas.height = image.height;
  248. let ctx = canvas.getContext("2d");
  249. ctx.drawImage(image, 0, 0, image.width, image.height);
  250. let ext = image.src.substring(image.src.lastIndexOf(".") + 1).toLowerCase();
  251. let dataURL = canvas.toDataURL("image/" + ext);
  252. uploadFile({
  253. url: that.$api.upload.file,
  254. maxNum: 1,
  255. success: function({res, header}) {
  256. that.$request({
  257. url: that.$api.upload.file + '&name=base64',
  258. header: header,
  259. method: 'post',
  260. data: {
  261. database: dataURL
  262. }
  263. }).then(res => {
  264. uni.hideLoading();
  265. if (res.code === 0) {
  266. for(let i in list) {
  267. if(list[i].type === 'img' && list[i].pic_url === '') {
  268. list[i].pic_url = res.data.url;
  269. list[i].id = res.data.id;
  270. }
  271. }
  272. that.loadingImg = false;
  273. uni.hideLoading();
  274. } else {
  275. uni.showModal({
  276. title: '',
  277. content: res.msg,
  278. showCancel: false,
  279. });
  280. }
  281. })
  282. }
  283. });
  284. };
  285. }
  286. });
  287. // #endif
  288. },
  289. imageLoad(index,e) {
  290. let $height = e.detail.height; //获取图片真实高度
  291. let $width = e.detail.width; //获取图片真实宽度
  292. let height = $height * (750 / $width);
  293. this.list[index].height = height + 'rpx';
  294. this.$forceUpdate();
  295. },
  296. clear(index) {
  297. this.img.splice(index,1)
  298. this.$storage.setStorageSync('goods_detail', this.list);
  299. },
  300. save() {
  301. if(!this.loadingImg) {
  302. this.$storage.setStorageSync('goods_detail', this.list);
  303. setTimeout(function() {
  304. uni.navigateBack();
  305. }, 500)
  306. }else {
  307. uni.showToast({
  308. title: '上传图片中,请稍后...',
  309. icon: 'none',
  310. duration: 1000
  311. });
  312. }
  313. }
  314. },
  315. onLoad() { this.$commonLoad.onload();
  316. let that = this;
  317. this.list = this.$storage.getStorageSync('goods_detail') ? this.$storage.getStorageSync('goods_detail') : [];
  318. uni.getSystemInfo({
  319. success: function (res) {
  320. if(res.model.indexOf('iPhone X') > -1 || res.model.indexOf('iPhone 11') > -1 || res.model.indexOf('iPhone11') > -1 || res.model.indexOf('iPhone12') > -1 || res.model.indexOf('Unknown Device') > -1) {
  321. that.iphone_x = true;
  322. }
  323. }
  324. })
  325. }
  326. }
  327. </script>
  328. <style scoped lang="scss">
  329. #editor {
  330. width: 100%;
  331. height: 100%;
  332. background-color: #fff;
  333. }
  334. .app-layout {
  335. position: absolute;
  336. top: 0;
  337. left: 0;
  338. height: 100%;
  339. width: 100%;
  340. background: #fff;
  341. }
  342. .app-layout /deep/ .app-layout {
  343. background: #FFFFFF;
  344. }
  345. .show {
  346. margin-top: #{220rpx};
  347. width: 100%;
  348. text-align: center;
  349. color: #353535;
  350. font-size: #{32rpx};
  351. image {
  352. width: #{310rpx};
  353. height: #{310rpx};
  354. margin-bottom: #{90rpx};
  355. }
  356. }
  357. .dialog {
  358. background-color: rgba(0,0,0,.3);
  359. position: fixed;
  360. top: 0;
  361. left: 0;
  362. width: 100%;
  363. height: 100%;
  364. z-index: 20;
  365. .menu {
  366. height: #{420rpx};
  367. width: 100%;
  368. position: fixed;
  369. bottom: 0;
  370. left: 0;
  371. z-index: 21;
  372. background-color: #fff;
  373. border-top-left-radius: #{16rpx};
  374. border-top-right-radius: #{16rpx};
  375. padding-top: #{68rpx};
  376. .toggle-placeholder {
  377. height: #{194rpx};
  378. width: #{134rpx};
  379. }
  380. .menu-list {
  381. margin-left: #{23rpx};
  382. .menu-item {
  383. margin: 0 #{28rpx};
  384. width: #{120rpx};
  385. text-align: center;
  386. color: #353535;
  387. font-size: #{28rpx};
  388. image {
  389. height: #{120rpx};
  390. width: #{120rpx};
  391. margin-bottom: #{20rpx};
  392. }
  393. }
  394. }
  395. .close {
  396. width: #{30rpx};
  397. height: #{30rpx};
  398. margin: #{88rpx} auto 0;
  399. display: block;
  400. }
  401. }
  402. .textarea {
  403. position: fixed;
  404. top: #{25rpx};
  405. left: #{24rpx};
  406. z-index: 50;
  407. width: #{702rpx};
  408. height: #{300rpx};
  409. background-color: #fff;
  410. border-radius: #{16rpx};
  411. padding: #{24rpx} #{28rpx};
  412. color: #353535;
  413. font-size: #{28rpx};
  414. textarea {
  415. width: 100%;
  416. height: 100%;
  417. }
  418. }
  419. .dialog-item {
  420. padding-top: #{35rpx};
  421. position: fixed;
  422. top: 30%;
  423. left: 0;
  424. right: 0;
  425. margin: 0 auto;
  426. width: #{620rpx};
  427. border-radius: #{16rpx};
  428. background-color: #fff;
  429. .dialog-title {
  430. font-size: #{32rpx};
  431. color: #353535;
  432. margin-bottom: #{6rpx};
  433. text-align: center;
  434. }
  435. .dialog-txt {
  436. margin: #{40rpx} auto;
  437. text-align: center;
  438. font-size: #{32rpx};
  439. color: #353535;
  440. }
  441. .btn-area {
  442. height: #{88rpx};
  443. position: relative;
  444. border-top: #{1rpx} solid #e2e2e2;
  445. .line {
  446. height: #{32rpx};
  447. width: #{1rpx};
  448. background-color: #e2e2e2;
  449. position: absolute;
  450. top: #{28rpx};
  451. left: 0;
  452. right: 0;
  453. margin: 0 auto;
  454. }
  455. .submit-btn {
  456. height: #{88rpx};
  457. line-height: #{88rpx};
  458. font-size: #{32rpx};
  459. color: #666;
  460. width: #{310rpx};
  461. text-align: center;
  462. }
  463. .submit-btn.be-submit {
  464. color: #446dfd;
  465. }
  466. }
  467. }
  468. }
  469. .list-item {
  470. background-color: #fff;
  471. margin-top: #{-2rpx};
  472. image {
  473. width: 100%;
  474. }
  475. .loading {
  476. padding: #{75rpx} 0;
  477. background-color: #fff;
  478. .loading-img {
  479. height: #{160rpx};
  480. width: #{160rpx};
  481. }
  482. .loading-text {
  483. font-size: #{28rpx};
  484. color: #999999;
  485. margin-top: #{28rpx};
  486. }
  487. }
  488. .line {
  489. height: #{72rpx};
  490. }
  491. .choose {
  492. height: #{80rpx};
  493. font-size: #{28rpx};
  494. color: #353535;
  495. background-color: #f7f7f7;
  496. padding-bottom: #{16rpx};
  497. .choose-item {
  498. width: #{120rpx};
  499. height: #{64rpx};
  500. line-height: #{64rpx};
  501. text-align: center;
  502. border: #{2rpx} solid #446dfd;
  503. background-color: #ffffff;
  504. color: #446dfd;
  505. margin-left: #{16rpx};
  506. }
  507. }
  508. .txt {
  509. padding: #{24rpx};
  510. }
  511. }
  512. .list-item:first-of-type {
  513. border-top: 0;
  514. margin-top: 0;
  515. }
  516. .list-item:last-of-type {
  517. border-bottom: 0;
  518. }
  519. .add {
  520. position: fixed;
  521. bottom: 0;
  522. left: 0;
  523. height: #{120rpx};
  524. width: 100%;
  525. z-index: 15;
  526. background-color: #fff;
  527. view {
  528. width: #{702rpx};
  529. line-height: #{80rpx};
  530. height: #{80rpx};
  531. margin: #{20rpx} auto;
  532. border-radius: #{40rpx};
  533. background-color: #446dfd;
  534. color: #fff;
  535. font-size: #{32rpx};
  536. text-align: center;
  537. }
  538. }
  539. .add.iphone_x {
  540. height: #{170rpx};
  541. padding-bottom: #{50rpx};
  542. }
  543. .placeholder {
  544. height: #{120rpx};
  545. }
  546. .placeholder.iphone_x {
  547. height: #{170rpx};
  548. }
  549. </style>