sharePoster.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // +----------------------------------------------------------------------
  2. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  3. // +----------------------------------------------------------------------
  4. // | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
  5. // +----------------------------------------------------------------------
  6. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  7. // +----------------------------------------------------------------------
  8. // | Author: CRMEB Team <admin@crmeb.com>
  9. // +----------------------------------------------------------------------
  10. import {
  11. imageBase64
  12. } from "@/api/public";
  13. import {
  14. getProductCode, // 普通商品小程序code
  15. } from "@/api/store.js";
  16. import {
  17. scombinationCode, // 拼团code
  18. seckillCode // 秒杀
  19. } from '@/api/activity.js';
  20. import i18n from '../utils/lang.js';
  21. let sysHeight = uni.getSystemInfoSync().statusBarHeight + 'px';
  22. export const sharePoster = {
  23. data() {
  24. return {
  25. //二维码参数
  26. codeShow: false,
  27. cid: '1',
  28. codeVal: "", // 要生成的二维码值
  29. size: 200, // 二维码大小
  30. unit: 'upx', // 单位
  31. background: '#FFF', // 背景色
  32. foreground: '#000', // 前景色
  33. pdground: '#000', // 角标色
  34. codeIcon: '', // 二维码图标
  35. iconsize: 40, // 二维码图标大小
  36. lv: 3, // 二维码容错级别 , 一般不用设置,默认就行
  37. onval: true, // val值变化时自动重新生成二维码
  38. loadMake: true, // 组件加载完成后自动生成二维码
  39. base64Show: 0,
  40. shareQrcode: 0,
  41. followCode: '',
  42. selectSku: {},
  43. currentPage: false,
  44. sysHeight: sysHeight,
  45. isShow: 0,
  46. storeImageBase64: ''
  47. };
  48. },
  49. methods: {
  50. qrR(res) {
  51. // #ifdef H5
  52. if (!this.$wechat.isWeixin() || this.shareQrcode != '1') {
  53. this.PromotionCode = res;
  54. this.followCode = ''
  55. }
  56. // #endif
  57. // #ifdef APP-PLUS
  58. this.PromotionCode = res;
  59. // #endif
  60. },
  61. getImageBase64() {
  62. let that = this;
  63. imageBase64(that.storeImage, this.storeInfo.wechat_code)
  64. .then((res) => {
  65. that.storeImageBase64 = res.data.image;
  66. if (this.storeInfo.wechat_code) {
  67. that.PromotionCode = res.data.code;
  68. }
  69. })
  70. .catch(() => {});
  71. },
  72. initPoster(arr2) {
  73. let that = this;
  74. uni.getImageInfo({
  75. src: that.PromotionCode,
  76. success() {
  77. if (arr2[2] == "") {
  78. //海报二维码不存在则从新下载
  79. that.downloadFilePromotionCode(function(
  80. msgPromotionCode) {
  81. arr2[2] = msgPromotionCode;
  82. if (arr2[2] == "")
  83. return that.$util.Tips({
  84. title: i18n.t(
  85. `海报二维码生成失败`
  86. ),
  87. });
  88. that.$util.PosterCanvas(
  89. arr2,
  90. that.storeInfo.store_name,
  91. that.storeInfo.price,
  92. that.storeInfo.ot_price,
  93. function(tempFilePath) {
  94. that.$set(that,
  95. "posterImage",
  96. tempFilePath);
  97. that.$set(that,
  98. "posterImageStatus",
  99. true);
  100. that.$set(that,
  101. "canvasStatus",
  102. false);
  103. that.$set(that,
  104. "actionSheetHidden",
  105. !that
  106. .actionSheetHidden
  107. );
  108. }
  109. );
  110. });
  111. } else {
  112. //生成推广海报
  113. that.$nextTick(e => {
  114. that.$util.PosterCanvas(
  115. arr2,
  116. that.storeInfo.store_name,
  117. that.storeInfo.price,
  118. that.storeInfo.ot_price,
  119. function(tempFilePath) {
  120. that.$set(that,
  121. "posterImage",
  122. tempFilePath);
  123. that.$set(that,
  124. "posterImageStatus",
  125. true);
  126. that.$set(that,
  127. "canvasStatus",
  128. false);
  129. that.$set(that,
  130. "actionSheetHidden",
  131. !that
  132. .actionSheetHidden
  133. );
  134. }
  135. );
  136. })
  137. }
  138. },
  139. fail: function(res) {
  140. // #ifdef H5
  141. return that.$util.Tips({
  142. title: res,
  143. });
  144. // #endif
  145. // #ifdef MP
  146. return that.$util.Tips({
  147. title: i18n.t(`正在下载海报,请稍后再试`),
  148. });
  149. // #endif
  150. },
  151. });
  152. },
  153. /**
  154. * 生成海报
  155. */
  156. async goPoster(type) {
  157. let that = this;
  158. that.posters = false;
  159. that.$set(that, "canvasStatus", true);
  160. let arr2
  161. // #ifdef MP
  162. let met = type === 'scombination' ? scombinationCode(that.id) : type === 'seckill' ? seckillCode(
  163. that
  164. .id) : getProductCode(that.id)
  165. met.then((res) => {
  166. uni.downloadFile({
  167. url: that.setDomain(res.data.code),
  168. success: function(res) {
  169. that.$set(that, "isDown", false);
  170. that.$set(that, "PromotionCode", res.tempFilePath)
  171. if (typeof successFn == "function")
  172. successFn && successFn(res.tempFilePath);
  173. arr2 = [that.posterbackgd, that.storeImage, that.PromotionCode];
  174. that.initPoster(arr2)
  175. },
  176. fail: function() {
  177. that.$set(that, "isDown", false);
  178. that.$set(that, "PromotionCode", "");
  179. },
  180. });
  181. })
  182. .catch((err) => {
  183. that.$set(that, "isDown", false);
  184. that.$set(that, "PromotionCode", "");
  185. return that.$util.Tips({
  186. title: err,
  187. });
  188. });
  189. // #endif
  190. // #ifdef H5 || APP-PLUS
  191. arr2 = [that.posterbackgd, that.storeImageBase64, that.PromotionCode];
  192. console.log(arr2)
  193. if (!that.storeImageBase64)
  194. return that.$util.Tips({
  195. title: i18n.t(`正在下载海报,请稍后再试`),
  196. });
  197. that.initPoster(arr2)
  198. // #endif
  199. },
  200. //替换安全域名
  201. setDomain(url) {
  202. url = url ? url.toString() : "";
  203. //本地调试打开,生产请注销
  204. if (url.indexOf("https://") > -1) return url;
  205. else return url.replace("http://", "https://");
  206. },
  207. //获取海报产品图
  208. downloadFilestoreImage() {
  209. let that = this;
  210. uni.downloadFile({
  211. url: that.setDomain(that.storeInfo.image),
  212. success: function(res) {
  213. that.storeImage = res.tempFilePath;
  214. that.storeImageBase64 = res.tempFilePath;
  215. },
  216. fail: function() {
  217. return that.$util.Tips({
  218. title: "",
  219. });
  220. that.storeImage = "";
  221. },
  222. });
  223. },
  224. /**
  225. * 获取产品分销二维码
  226. * @param function successFn 下载完成回调
  227. *
  228. */
  229. downloadFilePromotionCode(successFn) {
  230. let that = this;
  231. // #ifdef MP
  232. getProductCode(that.id)
  233. .then((res) => {
  234. uni.downloadFile({
  235. url: that.setDomain(res.data.code),
  236. success: function(res) {
  237. that.$set(that, "isDown", false);
  238. that.$set(that, "PromotionCode", res.tempFilePath)
  239. if (typeof successFn == "function")
  240. successFn && successFn(res.tempFilePath);
  241. },
  242. fail: function() {
  243. that.$set(that, "isDown", false);
  244. that.$set(that, "PromotionCode", "");
  245. },
  246. });
  247. })
  248. .catch((err) => {
  249. that.$set(that, "isDown", false);
  250. that.$set(that, "PromotionCode", "");
  251. return that.$util.Tips({
  252. title: err,
  253. });
  254. });
  255. // #endif
  256. // #ifdef APP-PLUS
  257. uni.downloadFile({
  258. url: that.setDomain(that.PromotionCode),
  259. success: function(res) {
  260. that.$set(that, "isDown", false);
  261. if (typeof successFn == "function")
  262. successFn && successFn(res.tempFilePath);
  263. else that.$set(that, "PromotionCode", res.tempFilePath);
  264. },
  265. fail: function() {
  266. that.$set(that, "isDown", false);
  267. that.$set(that, "PromotionCode", "");
  268. },
  269. });
  270. // #endif
  271. },
  272. }
  273. };