u-qrcode.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <template>
  2. <view class="u-qrcode" @longpress="longpress">
  3. <view class="u-qrcode__content" @click="preview">
  4. <!-- #ifndef APP-NVUE -->
  5. <canvas
  6. class="u-qrcode__canvas"
  7. :id="cid"
  8. :canvas-id="cid"
  9. type="2d"
  10. :style="{ width: size + unit, height: size + unit }" />
  11. <!-- #endif -->
  12. <!-- #ifdef APP-NVUE -->
  13. <gcanvas class="u-qrcode__canvas" ref="gcanvess"
  14. :style="{ width: size + unit, height: size + unit }">
  15. </gcanvas>
  16. <!-- #endif -->
  17. <view v-if="showLoading && loading" class="u-qrcode__loading"
  18. :style="{ width: size + unit, height: size + unit }">
  19. <up-loading-icon vertical :text="loadingText" textSize="14px"></up-loading-icon>
  20. </view>
  21. </view>
  22. </view>
  23. </template>
  24. <script>
  25. import QRCode from "./qrcode.js"
  26. // #ifdef APP-NVUE
  27. // https://github.com/dcloudio/NvueCanvasDemo/blob/master/README.md
  28. import {
  29. enable,
  30. WeexBridge
  31. } from '../../libs/util/gcanvas/index.js';
  32. // #endif
  33. let qrcode
  34. export default {
  35. name: "u-qrcode",
  36. props: {
  37. cid: {
  38. type: String,
  39. default: 'u-qrcode-canvas' + Math.random().toString()
  40. },
  41. size: {
  42. type: Number,
  43. default: 200
  44. },
  45. unit: {
  46. type: String,
  47. default: 'px'
  48. },
  49. show: {
  50. type: Boolean,
  51. default: true
  52. },
  53. val: {
  54. type: String,
  55. default: ''
  56. },
  57. background: {
  58. type: String,
  59. default: '#ffffff'
  60. },
  61. foreground: {
  62. type: String,
  63. default: '#000000'
  64. },
  65. pdground: {
  66. type: String,
  67. default: '#000000'
  68. },
  69. icon: {
  70. type: String,
  71. default: ''
  72. },
  73. iconSize: {
  74. type: Number,
  75. default: 40
  76. },
  77. lv: {
  78. type: Number,
  79. default: 3
  80. },
  81. onval: {
  82. type: Boolean,
  83. default: true
  84. },
  85. loadMake: {
  86. type: Boolean,
  87. default: true
  88. },
  89. usingComponents: {
  90. type: Boolean,
  91. default: true
  92. },
  93. showLoading: {
  94. type: Boolean,
  95. default: true
  96. },
  97. loadingText: {
  98. type: String,
  99. default: '生成中'
  100. },
  101. allowPreview: {
  102. type: Boolean,
  103. default: false
  104. },
  105. },
  106. emits: ['result', 'longpress'],
  107. data() {
  108. return {
  109. loading: false,
  110. result: '',
  111. popupShow: false,
  112. list: [
  113. {
  114. name: '保存二维码',
  115. }
  116. ],
  117. ganvas: null,
  118. context: '',
  119. canvasObj: {}
  120. }
  121. },
  122. mounted(){
  123. // #ifdef APP-NVUE
  124. /*获取元素引用*/
  125. this.ganvas = this.$refs["gcanvess"]
  126. /*通过元素引用获取canvas对象*/
  127. this.canvasObj = enable(this.ganvas, {
  128. bridge: WeexBridge
  129. })
  130. /*获取绘图所需的上下文,目前不支持3d*/
  131. this.context = this.canvasObj.getContext('2d')
  132. // #endif
  133. if (this.loadMake) {
  134. if (!this._empty(this.val)) {
  135. setTimeout(() => {
  136. setTimeout(()=>{
  137. this._makeCode()
  138. })
  139. }, 0);
  140. }
  141. }
  142. },
  143. methods: {
  144. _makeCode() {
  145. let that = this
  146. if (!this._empty(this.val)) {
  147. // #ifndef APP-NVUE
  148. this.loading = true
  149. // #endif
  150. qrcode = new QRCode({
  151. context: that, // 上下文环境
  152. canvasId: that.cid, // canvas-id
  153. nvueContext: that.context,
  154. usingComponents: that.usingComponents, // 是否是自定义组件
  155. showLoading: false, // 是否显示loading
  156. loadingText: that.loadingText, // loading文字
  157. text: that.val, // 生成内容
  158. size: that.size, // 二维码大小
  159. background: that.background, // 背景色
  160. foreground: that.foreground, // 前景色
  161. pdground: that.pdground, // 定位角点颜色
  162. correctLevel: that.lv, // 容错级别
  163. image: that.icon, // 二维码图标
  164. imageSize: that.iconSize,// 二维码图标大小
  165. that,
  166. cbResult: function (res) { // 生成二维码的回调
  167. that._result(res)
  168. },
  169. });
  170. } else {
  171. uni.showToast({
  172. title: '二维码内容不能为空',
  173. icon: 'none',
  174. duration: 2000
  175. });
  176. }
  177. },
  178. _clearCode() {
  179. this._result('')
  180. qrcode.clear()
  181. },
  182. _saveCode() {
  183. let that = this;
  184. if (this.result != "") {
  185. uni.saveImageToPhotosAlbum({
  186. filePath: that.result,
  187. success: function () {
  188. uni.showToast({
  189. title: '二维码保存成功',
  190. icon: 'success',
  191. duration: 2000
  192. });
  193. }
  194. });
  195. }
  196. },
  197. preview(e) {
  198. // 预览图片
  199. // console.log(this.result)
  200. if (this.allowPreview) {
  201. uni.previewImage({
  202. urls: [this.result],
  203. longPressActions: {
  204. itemList: ['保存二维码图片'],
  205. success: function(data) {
  206. // console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
  207. switch (data.tapIndex) {
  208. case 0:
  209. that._saveCode();
  210. break;
  211. }
  212. },
  213. fail: function(err) {
  214. console.log(err.errMsg);
  215. }
  216. }
  217. });
  218. }
  219. this.$emit('preview', {
  220. url: this.result
  221. }, e)
  222. },
  223. longpress() {
  224. this.$emit('longpress', this.result)
  225. },
  226. selectClick(index) {
  227. switch (index) {
  228. case 0:
  229. alert('保存二维码')
  230. this._saveCode();
  231. break;
  232. }
  233. },
  234. _result(res) {
  235. this.loading = false;
  236. this.result = res;
  237. this.$emit('result', res);
  238. },
  239. _empty(v) {
  240. let tp = typeof v,
  241. rt = false;
  242. if (tp == "number" && String(v) == "") {
  243. rt = true
  244. } else if (tp == "undefined") {
  245. rt = true
  246. } else if (tp == "object") {
  247. if (JSON.stringify(v) == "{}" || JSON.stringify(v) == "[]" || v == null) rt = true
  248. } else if (tp == "string") {
  249. if (v == "" || v == "undefined" || v == "null" || v == "{}" || v == "[]") rt = true
  250. } else if (tp == "function") {
  251. rt = false
  252. }
  253. return rt
  254. },
  255. },
  256. watch: {
  257. size: function (n, o) {
  258. if (n != o && !this._empty(n)) {
  259. this.cSize = n
  260. if (!this._empty(this.val)) {
  261. setTimeout(() => {
  262. this._makeCode()
  263. }, 100);
  264. }
  265. }
  266. },
  267. val: function (n, o) {
  268. if (this.onval) {
  269. if (n != o && !this._empty(n)) {
  270. setTimeout(() => {
  271. this._makeCode()
  272. }, 0);
  273. }
  274. }
  275. }
  276. },
  277. computed: {
  278. }
  279. }
  280. </script>
  281. <style lang="scss" scoped>
  282. .u-qrcode {
  283. &__loading {
  284. display: flex;
  285. justify-content: center;
  286. align-items: center;
  287. background-color: #f7f7f7;
  288. position: absolute;
  289. top: 0;
  290. bottom: 0;
  291. left: 0;
  292. right: 0;
  293. }
  294. /* #ifdef MP-TOUTIAO */
  295. /**字节小程序在编译时会出现一个 [hidde]:{ display: none !important; } 这个样式
  296. * 会导致canvas 隐藏掉 没有找到具体原因先这样处理
  297. */
  298. &__canvas {
  299. display: block !important;
  300. }
  301. /* #endif */
  302. &__content {
  303. position: relative;
  304. &__canvas {
  305. position: fixed;
  306. top: -99999rpx;
  307. left: -99999rpx;
  308. z-index: -99999;
  309. }
  310. }
  311. }
  312. </style>