goods-share.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. <template>
  2. <view class="goods">
  3. <view class="hint flexS">
  4. <image src="../../static/imgs/shop/goods_hint.png" class="hint_icon"></image>
  5. <text>点击图片可查看商品详情</text>
  6. </view>
  7. <!-- <view class="stock_box flexB" @click="skipStock">
  8. <view class="flexS">
  9. <view class="stock_icon_box flexC">
  10. <image src="../../static/imgs/shop/stock_icon.png" class="stock_icon"></image>
  11. </view>
  12. <view class="stock_con">
  13. <view>库存管理</view>
  14. <text>方便及时查库存、改库存</text>
  15. </view>
  16. </view>
  17. <image src="../../static/imgs/shop/arrow.png" class="arrow"></image>
  18. </view> -->
  19. <view class="goods_box" v-for="(item, idx) in goods" :key="item.id">
  20. <view class="goods_info flexS">
  21. <image :src="item.img" class="goods_img" @click="skipDetail(item.id)"></image>
  22. <view class="goods_con" @click.stop="checkGoods(item, idx)">
  23. <view class="goods_name">{{ item.name }}</view>
  24. <view class="one_year" style="width: 120rpx;">3条装</view>
  25. <view class="goods_price flexB">
  26. <view class="flexS">
  27. <view class="big_price">
  28. <text>¥</text>
  29. <text>{{ item.price }}</text>
  30. </view>
  31. <view class="save_box">VIP可省{{ Number(item.price) - Number(item.vip_price) || 0 }}</view>
  32. </view>
  33. <view class="check_icon">
  34. <text class="iconfont iconxuanzhong" v-if="item.checked"></text>
  35. <text class="iconfont iconweixuanzhong" v-else></text>
  36. </view>
  37. </view>
  38. </view>
  39. </view>
  40. </view>
  41. <view class="share_btn flexCC" @click="shareNow">立即分享</view>
  42. <view class="share_show" v-if="shareShow">
  43. <view class="share_con flexCC">
  44. <text class="iconfont iconguanbi" @click="shareShow = false"></text>
  45. <view class="canvas">
  46. <view class="canvas">
  47. <canvas canvas-id="canvas" style="width: 100%;height:100%;" id="canvas"><img :src="poster"
  48. class="poster_img" mode="widthFix" @click="preview" /></canvas>
  49. </view>
  50. </view>
  51. <view class="save_img">点击图片-长按-发送给朋友</view>
  52. </view>
  53. </view>
  54. <view class="qrimg">
  55. <tki-qrcode ref="qrcode" :val="code.val" :size="code.size" :icon="code.icon" :iconSize="code.iconsize"
  56. :onval="code.onval" :loadMake="code.loadMake" :showLoading="code.showLoading"
  57. :loadingText="code.loadingText" @result="drawPoster" />
  58. </view>
  59. </view>
  60. </template>
  61. <script>
  62. import tkiQrcode from '@/components/tki-qrcode/tki-qrcode.vue';
  63. import {
  64. getSku
  65. } from '@/apis/shop.js';
  66. export default {
  67. data() {
  68. return {
  69. goods: '', //商品信息
  70. store: '', //店铺信息
  71. shareShow: false,
  72. poster: '',
  73. code: {
  74. val: '',
  75. size: 200,
  76. icon: '',
  77. iconsize: 25,
  78. onval: true,
  79. loadMake: '',
  80. showLoading: true,
  81. loadingText: '二维码生成中...'
  82. },
  83. codeImg: '',
  84. shareGoods: {}, //分享海报上的图片
  85. canvasWidth: '',
  86. canvasHeight: '',
  87. model: '',
  88. qr_code: '', //微信二维码
  89. store_img: '' //店铺头像
  90. };
  91. },
  92. onLoad() {
  93. this.getGoods();
  94. uni.getSystemInfo({
  95. success: res => {
  96. this.model = res.model;
  97. this.canvasWidth = res.windowWidth / 375;
  98. this.canvasHeight = res.windowHeight;
  99. }
  100. });
  101. },
  102. onShow() {
  103. this.store = uni.getStorageSync('store');
  104. this.qr_code = uni.getStorageSync('qrCode');
  105. if (this.store) {
  106. uni.getImageInfo({
  107. src: this.store.img,
  108. success: res => {
  109. this.store_img = res.path
  110. }
  111. })
  112. }
  113. },
  114. methods: {
  115. preview() {
  116. uni.previewImage({
  117. current: this.poster,
  118. urls: [this.poster]
  119. });
  120. },
  121. //跳转到库存管理
  122. skipStock() {
  123. uni.navigateTo({
  124. url: '../stock-manage/stock-manage'
  125. })
  126. },
  127. //跳转到商品详情
  128. skipDetail(id) {
  129. uni.navigateTo({
  130. url: '../goods-detail/goods-detail?goods_id=' + id
  131. });
  132. },
  133. //小计数量
  134. subNum(data) {
  135. let subNum = 0;
  136. data.sku.map(i => {
  137. subNum += i.num;
  138. });
  139. return subNum;
  140. },
  141. //立即分享
  142. shareNow() {
  143. if (!this.qr_code) {
  144. uni.showModal({
  145. content: '为了方便客户加你微信咨询购买,请先上传微信二维码',
  146. showCancel: false,
  147. success: res => {
  148. if (res.confirm) {
  149. uni.navigateTo({
  150. url: '../add-qrcode/add-qrcode'
  151. });
  152. }
  153. }
  154. });
  155. return false;
  156. }
  157. let arr = [];
  158. let ids = [];
  159. let list = this.goods;
  160. list.map(i => {
  161. if (i.checked) {
  162. arr.push(i);
  163. ids.push(i.id);
  164. }
  165. });
  166. if (ids.length == 0) {
  167. uni.showModal({
  168. content: '请先选择商品',
  169. showCancel: false
  170. });
  171. return false;
  172. }
  173. let goods_id = ids.join(',');
  174. this.code.val =
  175. `${this.$config.share_url}#pages/index/index?store_id=${this.store.id}&goods_id=${goods_id}`;
  176. this.$refs.qrcode._makeCode();
  177. this.shareShow = true;
  178. },
  179. dataURLtoFile(dataurl, filename = 'file') {
  180. let arr = dataurl.split(',');
  181. let mime = arr[0].match(/:(.*?);/)[1];
  182. let suffix = mime.split('/')[1];
  183. let bstr = atob(arr[1]);
  184. let n = bstr.length;
  185. let u8arr = new Uint8Array(n);
  186. while (n--) {
  187. u8arr[n] = bstr.charCodeAt(n);
  188. }
  189. return new File([u8arr], `${filename}.${suffix}`, {
  190. type: mime
  191. });
  192. },
  193. blobToDataURI(blob, callback) {
  194. var reader = new FileReader();
  195. reader.readAsDataURL(blob);
  196. reader.onload = function(e) {
  197. callback(e.target.result);
  198. };
  199. },
  200. /*生成canvas图片*/
  201. drawPoster(codeImg) {
  202. let that = this;
  203. let store = this.store;
  204. uni.getImageInfo({
  205. src: 'https://api.wd.cliu.cc/shop_poster.jpg',
  206. success: res => {
  207. uni.showLoading({
  208. title: '图片加载中...'
  209. });
  210. let shopName = store.name.length > 9 ? store.name.slice(0, 9) + '...的店铺' : store.name +
  211. '的店铺';
  212. let ctx = uni.createCanvasContext('canvas');
  213. let rpx = that.canvasWidth;
  214. ctx.drawImage(res.path, 0, 0, 270 * rpx, 480 * rpx);
  215. ctx.font = 'normal bold 16px pingfang';
  216. ctx.textAlign = 'center';
  217. ctx.fillText(shopName, uni.upx2px(270), uni.upx2px(390));
  218. ctx.drawImage(codeImg, uni.upx2px(110), uni.upx2px(420), uni.upx2px(320), uni.upx2px(
  219. 320));
  220. ctx.font = 'noraml bold 14px pingfang';
  221. ctx.textAlign = 'center';
  222. ctx.setFillStyle('#060001');
  223. ctx.fillText('微信-扫一扫-进店铺', uni.upx2px(270), uni.upx2px(790));
  224. ctx.beginPath();
  225. ctx.arc(uni.upx2px(100) / 2 + uni.upx2px(222), uni.upx2px(100) / 2 + uni.upx2px(240),
  226. uni.upx2px(100) / 2,
  227. 0, Math.PI * 2, false); // 画一个圆形裁剪区域
  228. ctx.clip(); // 裁剪
  229. ctx.drawImage(this.store_img, uni.upx2px(222), uni.upx2px(240), uni.upx2px(100), uni
  230. .upx2px(
  231. 100));
  232. ctx.restore(); //恢复之前保存的绘图上下文
  233. setTimeout(res => {
  234. ctx.draw(false, () => {
  235. uni.canvasToTempFilePath({
  236. width: 270 * rpx,
  237. height: 480 * rpx,
  238. canvasId: 'canvas',
  239. success: res => {
  240. if (this.model == 'iPhone') {
  241. this.poster = this.dataURLtoFile(res
  242. .tempFilePath).name;
  243. } else {
  244. let url = res.tempFilePath.replace(
  245. /\. +/g, '');
  246. this.poster = url.replace(/[\r\n]/g,
  247. '');
  248. }
  249. }
  250. });
  251. });
  252. uni.hideLoading();
  253. }, 2000);
  254. }
  255. })
  256. },
  257. //获取商品
  258. getGoods() {
  259. uni.showLoading({
  260. title: '加载中....'
  261. });
  262. getSku({
  263. type: 2
  264. })
  265. .then(res => {
  266. if (res.code == 200) {
  267. let list = res.data;
  268. list.map(i => {
  269. this.$set(i, 'checked', false);
  270. });
  271. this.goods = list;
  272. }
  273. uni.hideLoading();
  274. })
  275. .catch(err => {
  276. uni.hideLoading();
  277. });
  278. },
  279. checkGoods(item, idx) {
  280. this.goods[idx].checked = !this.goods[idx].checked;
  281. }
  282. }
  283. };
  284. </script>
  285. <style>
  286. page {
  287. width: 100vw;
  288. overflow-x: hidden;
  289. min-height: 100%;
  290. background: #f9f9fb;
  291. }
  292. </style>
  293. <style lang="scss" scoped>
  294. .goods {
  295. display: flex;
  296. flex-direction: column;
  297. position: relative;
  298. .hint {
  299. width: 100%;
  300. height: 88rpx;
  301. background: #fcf7c0;
  302. padding-left: 30rpx;
  303. .hint_icon {
  304. width: 56rpx;
  305. height: 56rpx;
  306. margin-right: 10rpx;
  307. }
  308. text {
  309. font-size: 32rpx;
  310. color: #ff6400;
  311. }
  312. }
  313. .stock_box {
  314. width: 690rpx;
  315. height: 160rpx;
  316. background: linear-gradient(267deg, #F1292C 0%, #D2118B 100%);
  317. border-radius: 24rpx;
  318. margin: 20rpx auto 0;
  319. padding: 0 30rpx;
  320. box-sizing: border-box;
  321. .stock_icon_box {
  322. width: 80rpx;
  323. height: 80rpx;
  324. background: linear-gradient(180deg, #FFA600 0%, #FCBC05 100%);
  325. border-radius: 8rpx;
  326. margin-right: 20rpx;
  327. .stock_icon {
  328. width: 56rpx;
  329. height: 54rpx;
  330. }
  331. }
  332. .stock_con {
  333. text,
  334. view {
  335. color: #fff;
  336. }
  337. view {
  338. font-size: 32rpx;
  339. }
  340. text {
  341. font-size: 28rpx;
  342. }
  343. }
  344. .arrow {
  345. width: 36rpx;
  346. height: 40rpx;
  347. }
  348. }
  349. .goods_box {
  350. margin-top: 30rpx;
  351. .goods_info {
  352. padding: 10rpx;
  353. width: 100%;
  354. min-height: 236rpx;
  355. background: #fff;
  356. .check_icon {
  357. .iconfont {
  358. font-size: 55rpx;
  359. margin-right: 30rpx;
  360. }
  361. .iconweixuanzhong {
  362. color: #999;
  363. }
  364. }
  365. .goods_img {
  366. width: 220rpx;
  367. height: 176rpx;
  368. border-radius: 24rpx;
  369. flex-shrink: 0;
  370. margin: 0 20rpx;
  371. }
  372. .goods_con {
  373. flex: 1;
  374. .goods_name {
  375. font-size: 30rpx;
  376. font-weight: 600;
  377. }
  378. .goods_price {
  379. .price {
  380. color: $base-color;
  381. font-weight: 600;
  382. font-size: 36rpx;
  383. }
  384. .vip {
  385. font-size: 28rpx;
  386. font-weight: bold;
  387. }
  388. .vip_img {
  389. width: 88rpx;
  390. height: 36rpx;
  391. vertical-align: -6rpx;
  392. margin-right: 10rpx;
  393. }
  394. }
  395. }
  396. }
  397. }
  398. .share_btn {
  399. width: 100%;
  400. height: 100rpx;
  401. background: $base-line-bg;
  402. position: fixed;
  403. bottom: 0;
  404. left: 0;
  405. z-index: 999;
  406. color: #fff;
  407. font-size: 32rpx;
  408. }
  409. .share_show {
  410. width: 100%;
  411. height: 100vh;
  412. position: fixed;
  413. top: 0;
  414. left: 0;
  415. background-color: rgba(0, 0, 0, 0.7);
  416. z-index: 9999;
  417. .share_con {
  418. width: 80%;
  419. margin: 0 auto;
  420. height: 100%;
  421. z-index: 999999;
  422. .canvas {
  423. width: 540rpx;
  424. height: 960rpx;
  425. position: relative;
  426. #canvas {
  427. width: 540rpx;
  428. height: 960rpx;
  429. position: absolute;
  430. top: 0;
  431. left: 0;
  432. }
  433. .poster_img {
  434. -webkit-touch-callout: default;
  435. width: 100%;
  436. height: 100%;
  437. display: block;
  438. }
  439. img[src=''],
  440. img:not([src]) {
  441. opacity: 0;
  442. }
  443. }
  444. .save_img {
  445. color: #fff;
  446. width: 100%;
  447. text-align: center;
  448. font-size: 40rpx;
  449. margin-top: 20rpx;
  450. }
  451. .iconfont {
  452. position: absolute;
  453. top: 4vh;
  454. right: 25rpx;
  455. color: #dedede;
  456. font-size: 60rpx;
  457. z-index: 999;
  458. }
  459. }
  460. .sub_btn {
  461. margin-top: 76rpx;
  462. }
  463. }
  464. .qrimg {
  465. opacity: 0;
  466. }
  467. }
  468. </style>