goods-detail.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. <template>
  2. <view class="app-layout">
  3. <app-layout>
  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 v-if="textDialog" class="textarea">
  55. <textarea focus maxlength="-1" @confirm="toList" show-confirm-bar 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. <!-- <editor id="editor" class="ql-container text-cyan radius" show-img-toolbar="true" @ready="onEditorReady" @input='contentChange' :read-only='false'></editor> -->
  68. </app-layout>
  69. </view>
  70. </template>
  71. <script>
  72. import { mapState } from "vuex";
  73. export default {
  74. data() {
  75. return {
  76. tempFilePaths: '',
  77. imgheight: 0,
  78. menu: false,
  79. txt: null,
  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. theme: state => state.mallConfig.theme,
  94. userInfo: state => state.user.info,
  95. adminImg: state => state.mallConfig.__wxapp_img.app_admin,
  96. })
  97. },
  98. methods: {
  99. onEditorReady() {
  100. let that = this;
  101. uni.createSelectorQuery().select('#editor').context((res) => {
  102. console.log(res)
  103. that.editorCtx =res.context;
  104. let content={
  105. html:that.list
  106. }
  107. that.editorCtx.setContents(content);//设置富文本编辑器的内容
  108. }).exec()
  109. },
  110. contentChange(e){
  111. console.log(e.detail)
  112. },
  113. toList() {
  114. this.textDialog = false;
  115. let item = {type:'text',text: this.txt}
  116. if(this.reEdit) {
  117. this.list.splice(this.index,1,item)
  118. this.reEdit = false;
  119. }else {
  120. this.list.splice(this.index,0,item)
  121. }
  122. },
  123. editText(index,item) {
  124. this.txt = item.text;
  125. this.reEdit = true;
  126. this.textDialog = true;
  127. this.index = index;
  128. },
  129. move(type,index,item) {
  130. this.list.splice(index,1);
  131. if(type == 1) {
  132. this.list.splice(index + 1,0,item);
  133. }else {
  134. this.list.splice(index - 1,0,item);
  135. }
  136. },
  137. toRemove(index) {
  138. this.txt = null;
  139. this.is_delete = true;
  140. this.index = index;
  141. },
  142. cancel() {
  143. this.is_delete = false;
  144. },
  145. remove() {
  146. this.is_delete = false;
  147. this.list.splice(this.index,1);
  148. },
  149. showMenu(index) {
  150. if(index > -1) {
  151. this.index = index + 1;
  152. this.menu = true;
  153. this.txt = '';
  154. this.toToggle = false;
  155. }else {
  156. if(this.txt.length > 0) {
  157. this.toList();
  158. }else {
  159. this.textDialog = false;
  160. }
  161. this.menu = false;
  162. }
  163. },
  164. toggle(index) {
  165. this.index = index;
  166. this.toToggle = true;
  167. this.menu = true;
  168. },
  169. addLine() {
  170. let item = {type:'line'}
  171. this.list.splice(this.index,0,item)
  172. this.menu = false;
  173. },
  174. showText() {
  175. this.textDialog = !this.textDialog;
  176. this.menu = false;
  177. },
  178. add(sourceType) {
  179. let that = this;
  180. this.menu = false;
  181. uni.chooseImage({
  182. count: 1,
  183. sizeType: ['original', 'compressed'],
  184. sourceType: [sourceType],
  185. success(res) {
  186. // tempFilePath可以作为img标签的src属性显示图片
  187. const tempFilePaths = res.tempFilePaths;
  188. let list = that.list;
  189. tempFilePaths.forEach(function (row, index) {
  190. // uni.showLoading({
  191. // title: '上传中...'
  192. // });
  193. let para = {
  194. pic_url: '',
  195. type: 'img',
  196. id: 0
  197. }
  198. that.loadingImg = true;
  199. if(that.toToggle) {
  200. console.log(1)
  201. list.splice(that.index,1,para)
  202. }else {
  203. console.log(2)
  204. list.splice(that.index,0,para)
  205. }
  206. that.list = list;
  207. let fileName = '';
  208. // #ifdef MP-BAIDU
  209. fileName = row.substr(row.lastIndexOf('/') + 1);
  210. // #endif
  211. uni.uploadFile({
  212. url: that.$api.upload.file,
  213. filePath: row,
  214. name: 'file',
  215. fileType: 'image',
  216. formData: {
  217. file: row,
  218. file_name: fileName
  219. },
  220. success(res) {
  221. // #ifndef MP-BAIDU
  222. const imgInfo = JSON.parse(res.data);
  223. // #endif
  224. // #ifdef MP-BAIDU
  225. const imgInfo = res.data;
  226. // #endif
  227. if (imgInfo.code == 0) {
  228. for(let i in list) {
  229. if(list[i].type == 'img' && list[i].pic_url == '') {
  230. list[i].pic_url = imgInfo.data.url;
  231. list[i].id = imgInfo.data.id;
  232. }
  233. }
  234. }
  235. that.list = list;
  236. if (index == tempFilePaths.length - 1) {
  237. that.loadingImg = false;
  238. uni.hideLoading();
  239. }
  240. }
  241. })
  242. })
  243. }
  244. })
  245. },
  246. imageLoad(index,e) {
  247. let $height = e.detail.height; //获取图片真实高度
  248. let $width = e.detail.width; //获取图片真实宽度
  249. let height = $height * (750 / $width);
  250. this.list[index].height = height + 'rpx';
  251. this.$forceUpdate();
  252. },
  253. clear(index) {
  254. this.img.splice(index,1)
  255. uni.setStorage({
  256. key: "goods_detail",
  257. data: this.list
  258. })
  259. },
  260. save() {
  261. if(!this.loadingImg) {
  262. uni.setStorage({
  263. key: "goods_detail",
  264. data: this.list
  265. })
  266. setTimeout(function() {
  267. uni.navigateBack();
  268. }, 500)
  269. }else {
  270. uni.showToast({
  271. title: '上传图片中,请稍后...',
  272. icon: 'none',
  273. duration: 1000
  274. });
  275. }
  276. }
  277. },
  278. onLoad(options) {
  279. let that = this;
  280. uni.getStorage({
  281. key: 'goods_detail',
  282. success(res) {
  283. if(res.data) {
  284. that.list = res.data
  285. }
  286. }
  287. })
  288. uni.getSystemInfo({
  289. success: function (res) {
  290. 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) {
  291. that.iphone_x = true;
  292. }
  293. }
  294. })
  295. }
  296. }
  297. </script>
  298. <style scoped lang="scss">
  299. #editor {
  300. width: 100%;
  301. height: 100%;
  302. background-color: #fff;
  303. }
  304. .app-layout {
  305. position: absolute;
  306. top: 0;
  307. left: 0;
  308. height: 100%;
  309. width: 100%;
  310. background: #fff;
  311. }
  312. .app-layout /deep/ .app-layout {
  313. background: #FFFFFF;
  314. }
  315. .show {
  316. margin-top: #{220rpx};
  317. width: 100%;
  318. text-align: center;
  319. color: #353535;
  320. font-size: #{32rpx};
  321. image {
  322. width: #{310rpx};
  323. height: #{310rpx};
  324. margin-bottom: #{90rpx};
  325. }
  326. }
  327. .dialog {
  328. background-color: rgba(0,0,0,.3);
  329. position: fixed;
  330. top: 0;
  331. left: 0;
  332. width: 100%;
  333. height: 100%;
  334. z-index: 20;
  335. .menu {
  336. height: #{420rpx};
  337. width: 100%;
  338. position: fixed;
  339. bottom: 0;
  340. left: 0;
  341. z-index: 21;
  342. background-color: #fff;
  343. border-top-left-radius: #{16rpx};
  344. border-top-right-radius: #{16rpx};
  345. padding-top: #{68rpx};
  346. .toggle-placeholder {
  347. height: #{194rpx};
  348. width: #{134rpx};
  349. }
  350. .menu-list {
  351. margin-left: #{23rpx};
  352. .menu-item {
  353. margin: 0 #{28rpx};
  354. width: #{120rpx};
  355. text-align: center;
  356. color: #353535;
  357. font-size: #{28rpx};
  358. image {
  359. height: #{120rpx};
  360. width: #{120rpx};
  361. margin-bottom: #{20rpx};
  362. }
  363. }
  364. }
  365. .close {
  366. width: #{30rpx};
  367. height: #{30rpx};
  368. margin: #{88rpx} auto 0;
  369. display: block;
  370. }
  371. }
  372. .textarea {
  373. position: fixed;
  374. top: #{25rpx};
  375. left: #{24rpx};
  376. z-index: 50;
  377. width: #{702rpx};
  378. height: #{300rpx};
  379. background-color: #fff;
  380. border-radius: #{16rpx};
  381. padding: #{24rpx} #{28rpx};
  382. color: #353535;
  383. font-size: #{28rpx};
  384. textarea {
  385. width: 100%;
  386. height: 100%;
  387. }
  388. }
  389. .dialog-item {
  390. padding-top: #{35rpx};
  391. position: fixed;
  392. top: 30%;
  393. left: 0;
  394. right: 0;
  395. margin: 0 auto;
  396. width: #{620rpx};
  397. border-radius: #{16rpx};
  398. background-color: #fff;
  399. .dialog-title {
  400. font-size: #{32rpx};
  401. color: #353535;
  402. margin-bottom: #{6rpx};
  403. text-align: center;
  404. }
  405. .dialog-txt {
  406. margin: #{40rpx} auto;
  407. text-align: center;
  408. font-size: #{32rpx};
  409. color: #353535;
  410. }
  411. .btn-area {
  412. height: #{88rpx};
  413. position: relative;
  414. border-top: #{1rpx} solid #e2e2e2;
  415. .line {
  416. height: #{32rpx};
  417. width: #{1rpx};
  418. background-color: #e2e2e2;
  419. position: absolute;
  420. top: #{28rpx};
  421. left: 0;
  422. right: 0;
  423. margin: 0 auto;
  424. }
  425. .submit-btn {
  426. height: #{88rpx};
  427. line-height: #{88rpx};
  428. font-size: #{32rpx};
  429. color: #666;
  430. width: #{310rpx};
  431. text-align: center;
  432. }
  433. .submit-btn.be-submit {
  434. color: #446dfd;
  435. }
  436. }
  437. }
  438. }
  439. .list-item {
  440. background-color: #fff;
  441. margin-top: #{-2rpx};
  442. image {
  443. width: 100%;
  444. }
  445. .loading {
  446. padding: #{75rpx} 0;
  447. background-color: #fff;
  448. .loading-img {
  449. height: #{160rpx};
  450. width: #{160rpx};
  451. }
  452. .loading-text {
  453. font-size: #{28rpx};
  454. color: #999999;
  455. margin-top: #{28rpx};
  456. }
  457. }
  458. .line {
  459. height: #{72rpx};
  460. }
  461. .choose {
  462. height: #{80rpx};
  463. font-size: #{28rpx};
  464. color: #353535;
  465. background-color: #f7f7f7;
  466. padding-bottom: #{16rpx};
  467. .choose-item {
  468. width: #{120rpx};
  469. height: #{64rpx};
  470. line-height: #{64rpx};
  471. text-align: center;
  472. border: #{2rpx} solid #446dfd;
  473. background-color: #ffffff;
  474. color: #446dfd;
  475. margin-left: #{16rpx};
  476. }
  477. }
  478. .txt {
  479. padding: #{24rpx};
  480. }
  481. }
  482. .list-item:first-of-type {
  483. border-top: 0;
  484. margin-top: 0;
  485. }
  486. .list-item:last-of-type {
  487. border-bottom: 0;
  488. }
  489. .add {
  490. position: fixed;
  491. bottom: 0;
  492. left: 0;
  493. height: #{120rpx};
  494. width: 100%;
  495. z-index: 15;
  496. background-color: #fff;
  497. view {
  498. width: #{702rpx};
  499. line-height: #{80rpx};
  500. height: #{80rpx};
  501. margin: #{20rpx} auto;
  502. border-radius: #{40rpx};
  503. background-color: #446dfd;
  504. color: #fff;
  505. font-size: #{32rpx};
  506. text-align: center;
  507. }
  508. }
  509. .add.iphone_x {
  510. height: #{170rpx};
  511. padding-bottom: #{50rpx};
  512. }
  513. .placeholder {
  514. height: #{120rpx};
  515. }
  516. .placeholder.iphone_x {
  517. height: #{170rpx};
  518. }
  519. </style>