u-qrcode.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. 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: function () {
  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. this._makeCode()
  137. }, 0);
  138. }
  139. }
  140. },
  141. methods: {
  142. _makeCode() {
  143. let that = this
  144. if (!this._empty(this.val)) {
  145. // #ifndef APP-NVUE
  146. this.loading = true
  147. // #endif
  148. qrcode = new QRCode({
  149. context: that, // 上下文环境
  150. canvasId: that.cid, // canvas-id
  151. nvueContext: that.context,
  152. usingComponents: that.usingComponents, // 是否是自定义组件
  153. showLoading: false, // 是否显示loading
  154. loadingText: that.loadingText, // loading文字
  155. text: that.val, // 生成内容
  156. size: that.size, // 二维码大小
  157. background: that.background, // 背景色
  158. foreground: that.foreground, // 前景色
  159. pdground: that.pdground, // 定位角点颜色
  160. correctLevel: that.lv, // 容错级别
  161. image: that.icon, // 二维码图标
  162. imageSize: that.iconSize,// 二维码图标大小
  163. cbResult: function (res) { // 生成二维码的回调
  164. that._result(res)
  165. },
  166. });
  167. } else {
  168. uni.showToast({
  169. title: '二维码内容不能为空',
  170. icon: 'none',
  171. duration: 2000
  172. });
  173. }
  174. },
  175. _clearCode() {
  176. this._result('')
  177. qrcode.clear()
  178. },
  179. _saveCode() {
  180. let that = this;
  181. if (this.result != "") {
  182. uni.saveImageToPhotosAlbum({
  183. filePath: that.result,
  184. success: function () {
  185. uni.showToast({
  186. title: '二维码保存成功',
  187. icon: 'success',
  188. duration: 2000
  189. });
  190. }
  191. });
  192. }
  193. },
  194. preview(e) {
  195. // 预览图片
  196. // console.log(this.result)
  197. if (this.allowPreview) {
  198. uni.previewImage({
  199. urls: [this.result],
  200. longPressActions: {
  201. itemList: ['保存二维码图片'],
  202. success: function(data) {
  203. // console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
  204. switch (data.tapIndex) {
  205. case 0:
  206. that._saveCode();
  207. break;
  208. }
  209. },
  210. fail: function(err) {
  211. console.log(err.errMsg);
  212. }
  213. }
  214. });
  215. }
  216. this.$emit('preview', {
  217. url: this.result
  218. }, e)
  219. },
  220. longpress() {
  221. this.$emit('longpress', this.result)
  222. },
  223. selectClick(index) {
  224. switch (index) {
  225. case 0:
  226. alert('保存二维码')
  227. this._saveCode();
  228. break;
  229. }
  230. },
  231. _result(res) {
  232. this.loading = false;
  233. this.result = res;
  234. this.$emit('result', res);
  235. },
  236. _empty(v) {
  237. let tp = typeof v,
  238. rt = false;
  239. if (tp == "number" && String(v) == "") {
  240. rt = true
  241. } else if (tp == "undefined") {
  242. rt = true
  243. } else if (tp == "object") {
  244. if (JSON.stringify(v) == "{}" || JSON.stringify(v) == "[]" || v == null) rt = true
  245. } else if (tp == "string") {
  246. if (v == "" || v == "undefined" || v == "null" || v == "{}" || v == "[]") rt = true
  247. } else if (tp == "function") {
  248. rt = false
  249. }
  250. return rt
  251. }
  252. },
  253. watch: {
  254. size: function (n, o) {
  255. if (n != o && !this._empty(n)) {
  256. this.cSize = n
  257. if (!this._empty(this.val)) {
  258. setTimeout(() => {
  259. this._makeCode()
  260. }, 100);
  261. }
  262. }
  263. },
  264. val: function (n, o) {
  265. if (this.onval) {
  266. if (n != o && !this._empty(n)) {
  267. setTimeout(() => {
  268. this._makeCode()
  269. }, 0);
  270. }
  271. }
  272. }
  273. },
  274. computed: {
  275. }
  276. }
  277. </script>
  278. <style lang="scss" scoped>
  279. .u-qrcode {
  280. &__loading {
  281. display: flex;
  282. justify-content: center;
  283. align-items: center;
  284. background-color: #f7f7f7;
  285. position: absolute;
  286. top: 0;
  287. bottom: 0;
  288. left: 0;
  289. right: 0;
  290. }
  291. &__content {
  292. position: relative;
  293. &__canvas {
  294. position: fixed;
  295. top: -99999rpx;
  296. left: -99999rpx;
  297. z-index: -99999;
  298. }
  299. }
  300. }
  301. </style>