u-qrcode.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <template>
  2. <view class="u-qrcode" @longpress="longpress">
  3. <view class="u-qrcode__content" @click="preview">
  4. <!-- #ifndef APP-NVUE -->
  5. <canvas class="u-qrcode__canvas"
  6. :id="cid" :canvas-id="cid" :style="{ width: size + unit, height: size + unit }" />
  7. <!-- #endif -->
  8. <!-- #ifdef APP-NVUE -->
  9. <gcanvas class="u-qrcode__canvas" ref="gcanvess"
  10. :style="{ width: size + unit, height: size + unit }">
  11. </gcanvas>
  12. <!-- #endif -->
  13. <view v-if="showLoading && loading" class="u-qrcode__loading"
  14. :style="{ width: size + unit, height: size + unit }">
  15. <up-loading-icon vertical :text="loadingText" textSize="14px"></up-loading-icon>
  16. </view>
  17. <!-- <image v-show="show" :src="result" :style="{ width: size + unit, height: size + unit }" /> -->
  18. </view>
  19. <!-- <up-action-sheet :actions="list" cancelText="取消"
  20. v-model:show="popupShow" @select="selectClick">
  21. </up-action-sheet> -->
  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. },
  102. emits: ['result', 'longpress'],
  103. data() {
  104. return {
  105. loading: false,
  106. result: '',
  107. popupShow: false,
  108. list: [
  109. {
  110. name: '保存二维码',
  111. }
  112. ],
  113. ganvas: null,
  114. context: '',
  115. canvasObj: {}
  116. }
  117. },
  118. mounted: function () {
  119. // #ifdef APP-NVUE
  120. /*获取元素引用*/
  121. this.ganvas = this.$refs["gcanvess"]
  122. /*通过元素引用获取canvas对象*/
  123. this.canvasObj = enable(this.ganvas, {
  124. bridge: WeexBridge
  125. })
  126. /*获取绘图所需的上下文,目前不支持3d*/
  127. this.context = this.canvasObj.getContext('2d')
  128. // #endif
  129. if (this.loadMake) {
  130. if (!this._empty(this.val)) {
  131. setTimeout(() => {
  132. this._makeCode()
  133. }, 0);
  134. }
  135. }
  136. },
  137. methods: {
  138. _makeCode() {
  139. let that = this
  140. if (!this._empty(this.val)) {
  141. // #ifndef APP-NVUE
  142. this.loading = true
  143. // #endif
  144. qrcode = new QRCode({
  145. context: that, // 上下文环境
  146. canvasId: that.cid, // canvas-id
  147. nvueContext: that.context,
  148. usingComponents: that.usingComponents, // 是否是自定义组件
  149. showLoading: false, // 是否显示loading
  150. loadingText: that.loadingText, // loading文字
  151. text: that.val, // 生成内容
  152. size: that.size, // 二维码大小
  153. background: that.background, // 背景色
  154. foreground: that.foreground, // 前景色
  155. pdground: that.pdground, // 定位角点颜色
  156. correctLevel: that.lv, // 容错级别
  157. image: that.icon, // 二维码图标
  158. imageSize: that.iconSize,// 二维码图标大小
  159. cbResult: function (res) { // 生成二维码的回调
  160. that._result(res)
  161. },
  162. });
  163. } else {
  164. uni.showToast({
  165. title: '二维码内容不能为空',
  166. icon: 'none',
  167. duration: 2000
  168. });
  169. }
  170. },
  171. _clearCode() {
  172. this._result('')
  173. qrcode.clear()
  174. },
  175. _saveCode() {
  176. let that = this;
  177. if (this.result != "") {
  178. uni.saveImageToPhotosAlbum({
  179. filePath: that.result,
  180. success: function () {
  181. uni.showToast({
  182. title: '二维码保存成功',
  183. icon: 'success',
  184. duration: 2000
  185. });
  186. }
  187. });
  188. }
  189. },
  190. preview() {
  191. // 预览图片
  192. // console.log(this.result)
  193. uni.previewImage({
  194. urls: [this.result],
  195. longPressActions: {
  196. itemList: ['保存二维码图片'],
  197. success: function(data) {
  198. // console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
  199. switch (data.tapIndex) {
  200. case 0:
  201. that._saveCode();
  202. break;
  203. }
  204. },
  205. fail: function(err) {
  206. console.log(err.errMsg);
  207. }
  208. }
  209. });
  210. },
  211. longpress() {
  212. this.$emit('longpress', this.result)
  213. },
  214. selectClick(index) {
  215. switch (index) {
  216. case 0:
  217. alert('保存二维码')
  218. this._saveCode();
  219. break;
  220. }
  221. },
  222. _result(res) {
  223. this.loading = false;
  224. this.result = res;
  225. this.$emit('result', res);
  226. },
  227. _empty(v) {
  228. let tp = typeof v,
  229. rt = false;
  230. if (tp == "number" && String(v) == "") {
  231. rt = true
  232. } else if (tp == "undefined") {
  233. rt = true
  234. } else if (tp == "object") {
  235. if (JSON.stringify(v) == "{}" || JSON.stringify(v) == "[]" || v == null) rt = true
  236. } else if (tp == "string") {
  237. if (v == "" || v == "undefined" || v == "null" || v == "{}" || v == "[]") rt = true
  238. } else if (tp == "function") {
  239. rt = false
  240. }
  241. return rt
  242. }
  243. },
  244. watch: {
  245. size: function (n, o) {
  246. if (n != o && !this._empty(n)) {
  247. this.cSize = n
  248. if (!this._empty(this.val)) {
  249. setTimeout(() => {
  250. this._makeCode()
  251. }, 100);
  252. }
  253. }
  254. },
  255. val: function (n, o) {
  256. if (this.onval) {
  257. if (n != o && !this._empty(n)) {
  258. setTimeout(() => {
  259. this._makeCode()
  260. }, 0);
  261. }
  262. }
  263. }
  264. },
  265. computed: {
  266. }
  267. }
  268. </script>
  269. <style lang="scss" scoped>
  270. .u-qrcode {
  271. &__loading {
  272. display: flex;
  273. justify-content: center;
  274. align-items: center;
  275. background-color: #f7f7f7;
  276. position: absolute;
  277. top: 0;
  278. bottom: 0;
  279. left: 0;
  280. right: 0;
  281. }
  282. &__content {
  283. position: relative;
  284. &__canvas {
  285. position: fixed;
  286. top: -99999rpx;
  287. left: -99999rpx;
  288. z-index: -99999;
  289. }
  290. }
  291. }
  292. </style>