ay-qrcode.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <template>
  2. <view :class="modal ? 'show-qrcode' : 'hide-qrcode'">
  3. <view class="box-qrcode" @longtap="longtapCode">
  4. <!-- style="width: 550px;height: 550px;" -->
  5. <canvas
  6. class="canvas-qrcode"
  7. :canvas-id="qrcode_id"
  8. style="outline: none"
  9. >
  10. </canvas>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. var qr_we = require("./qrcode_wx.js");
  16. const qrCode = require("./weapp-qrcode.js");
  17. export default {
  18. data() {
  19. return {
  20. isAndroid: false,
  21. show: true,
  22. imagePath: "",
  23. // qrcode_id: 'qrcode_id',
  24. marginLeft: 0,
  25. //一般的安卓app只需加30就能显示全
  26. //苹果app的不加就能显示全,加了就要弄margin-left
  27. //有些安卓app显示不全
  28. add_num: 30,
  29. add_num_key: "rectify_code_key",
  30. };
  31. },
  32. props: {
  33. modal: {
  34. type: Boolean,
  35. default: false,
  36. },
  37. url: {
  38. type: String,
  39. default: "",
  40. },
  41. height: {
  42. type: Number,
  43. default: 520,
  44. },
  45. width: {
  46. type: Number,
  47. default: 520,
  48. },
  49. themeColor: {
  50. type: String,
  51. default: "#333333",
  52. },
  53. qrcode_id: {
  54. type: String,
  55. default: "qrcode_id",
  56. },
  57. is_themeImg: {
  58. type: Boolean,
  59. default: false,
  60. },
  61. themeImg: {
  62. type: String,
  63. default:
  64. "https://cdn.pixabay.com/photo/2016/11/29/13/24/balloons-1869816__340.jpg",
  65. },
  66. h_w_img: {
  67. type: Number,
  68. default: 0,
  69. },
  70. },
  71. watch: {},
  72. created: function () {
  73. let that = this;
  74. try {
  75. //app苹果二维码不居中
  76. //#ifndef MP
  77. let isAndroid = false;
  78. const res = uni.getSystemInfoSync();
  79. if (res.platform == "android") {
  80. isAndroid = true;
  81. } else {
  82. isAndroid = false;
  83. }
  84. // if (!isAndroid) {
  85. // that.marginLeft = 46;
  86. // }
  87. that.isAndroid = isAndroid;
  88. try {
  89. const add_num = uni.getStorageSync(that.add_num_key);
  90. if (add_num) {
  91. that.add_num = add_num;
  92. }
  93. } catch (e) {
  94. // error
  95. }
  96. // #endif
  97. } catch (e) {
  98. // error
  99. }
  100. //#ifdef MP
  101. //that.marginLeft = 40;
  102. // #endif
  103. },
  104. methods: {
  105. hideQrcode() {
  106. this.$emit("hideQrcode");
  107. },
  108. // 二维码生成工具
  109. crtQrCode() {
  110. let that = this;
  111. //#ifndef MP
  112. new qrCode(that.qrcode_id, {
  113. text: this.url,
  114. width: that.width,
  115. height: that.height,
  116. colorDark: that.themeColor, //#333333
  117. colorLight: "#FFFFFF",
  118. correctLevel: qrCode.CorrectLevel.H,
  119. });
  120. // #endif
  121. //#ifdef MP
  122. that.createQrCode(
  123. this.url,
  124. that.qrcode_id,
  125. that.width,
  126. that.height,
  127. that.themeColor,
  128. that.is_themeImg,
  129. that.themeImg,
  130. that.h_w_img
  131. );
  132. // #endif
  133. //that.createQrCode(this.url, that.qrcode_id, that.width, that.height);
  134. },
  135. //#ifdef MP
  136. createQrCode: function (
  137. url,
  138. canvasId,
  139. cavW,
  140. cavH,
  141. cavColor,
  142. haveImg,
  143. imgurl,
  144. imgsize
  145. ) {
  146. //调用插件中的draw方法,绘制二维码图片
  147. qr_we.api.draw(
  148. url,
  149. canvasId,
  150. "205",
  151. "205",
  152. cavColor,
  153. haveImg,
  154. imgurl,
  155. imgsize,
  156. this,
  157. this.canvasToTempImage
  158. );
  159. // setTimeout(() => { this.canvasToTempImage();},100);
  160. },
  161. // #endif
  162. //获取临时缓存照片路径,存入data中
  163. canvasToTempImage: function () {
  164. var that = this;
  165. },
  166. saveImage: function () {
  167. var that = this;
  168. uni.canvasToTempFilePath(
  169. {
  170. canvasId: that.qrcode_id,
  171. success: function (res) {
  172. var tempFilePath = res.tempFilePath;
  173. console.log(tempFilePath);
  174. that.imagePath = tempFilePath;
  175. //保存到相册
  176. // uni.saveFile({
  177. // tempFilePath: tempFilePath,
  178. // success: function (res2) {
  179. // var savedFilePath = res2.savedFilePath;
  180. // }
  181. // });
  182. uni.saveImageToPhotosAlbum({
  183. filePath: tempFilePath,
  184. success: function (res3) {
  185. uni.showModal({
  186. title: "提示",
  187. content: "保存成功",
  188. confirmText: "确定",
  189. showCancel: false,
  190. confirmColor: "#33CCCC",
  191. success(res4) {},
  192. });
  193. },
  194. });
  195. },
  196. fail: function (res) {
  197. console.log(res);
  198. },
  199. },
  200. that
  201. );
  202. },
  203. //微信小程序支持:长按二维码,提示是否保存相册
  204. //安卓APP长按校正二维码
  205. longtapCode() {
  206. var that = this;
  207. //#ifndef MP
  208. uni.showModal({
  209. title: "校正二维码",
  210. content: "二维码是否异常",
  211. confirmText: "确定",
  212. confirmColor: "#33CCCC",
  213. success(res) {
  214. if (res.confirm) {
  215. that.rectify_code();
  216. }
  217. },
  218. });
  219. // #endif
  220. //#ifdef MP-WEIXIN
  221. uni.showModal({
  222. title: "提示",
  223. content: "是否保存到相册",
  224. confirmText: "确定",
  225. confirmColor: "#33CCCC",
  226. success(res) {
  227. if (res.confirm) {
  228. that.saveImage();
  229. }
  230. },
  231. });
  232. // #endif
  233. },
  234. //安卓有些手机不正常,长按可选择矫正
  235. rectify_code() {
  236. var that = this;
  237. let add_num = that.add_num;
  238. add_num += 30;
  239. that.add_num = add_num;
  240. that.crtQrCode(); //重新生成才会立即覆盖
  241. try {
  242. //第一次长按校正设置了就不用在设置
  243. uni.setStorage({
  244. key: that.add_num_key,
  245. data: add_num,
  246. success: function () {},
  247. });
  248. } catch (e) {
  249. // error
  250. }
  251. },
  252. },
  253. mounted() {},
  254. };
  255. </script>
  256. <style scoped lang="scss">
  257. // .qrcode-box {
  258. // position: fixed;
  259. // left: 0;
  260. // top: 0;
  261. // right: 0;
  262. // bottom: 0;
  263. // height: 100vh;
  264. // width: 100vw;
  265. // background-color: rgba(59, 59, 59, 0.6);
  266. // // opacity: 0.8;
  267. // text-align: center;
  268. // display: flex;
  269. // align-items: center;
  270. // display: none;
  271. // .qrcode-item {
  272. // flex: 1;
  273. // position: relative;
  274. // text-align: center;
  275. // .item-box {
  276. // width: 90%;
  277. // margin: auto;
  278. // display: inline-block;
  279. // margin-top: 30%;
  280. // padding-bottom: 30px;
  281. // // animation: show 0.7s;
  282. // .title {
  283. // font-size: 46px;
  284. // text-align: center;
  285. // margin-bottom: 24px;
  286. // }
  287. // .canvas {
  288. // margin: auto;
  289. // display: inline-block;
  290. // margin: auto;
  291. // }
  292. // background-color: #FFFFFF;
  293. // }
  294. // }
  295. // }
  296. .box-qrcode {
  297. text-align: center;
  298. position: relative;
  299. .box-img-qrcode {
  300. position: absolute;
  301. display: flex;
  302. flex-direction: column;
  303. justify-content: center;
  304. align-items: center;
  305. z-index: 2;
  306. image {
  307. width: 205px;
  308. height: 205px;
  309. }
  310. }
  311. }
  312. image {
  313. width: 60px;
  314. height: 60px;
  315. border-radius: 50%;
  316. }
  317. .canvas-qrcode {
  318. margin: auto;
  319. width: 205px;
  320. height: 205px;
  321. }
  322. .opacity-qrcode {
  323. opacity: 0;
  324. display: block;
  325. }
  326. .show-qrcode {
  327. display: block;
  328. // animation: fade 0.7s;
  329. // -moz-animation: fade 0.5s; /* Firefox */
  330. // -webkit-animation: fade 0.5s; /* Safari 和 Chrome */
  331. // -o-animation: fade 0.5s;
  332. }
  333. .hide-qrcode {
  334. // animation: hide 0.7s;
  335. }
  336. </style>