receiveCode.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <template>
  2. <view v-if="isShow">
  3. <view class="con">
  4. <view class="tits ddflex"><image src="../static/images/bag.png"></image>二维码收款</view>
  5. <view class="code-box">
  6. <view class="tips">无需添加好友,扫二维码向我付钱</view>
  7. <image :src="codeUrl" class="code"></image>
  8. <view class="save" @click="saveImage()">保存收款码</view>
  9. </view>
  10. <navigator url="/payment/receiveList/receiveList" hover-class="none" class="skjl ddflex">
  11. <image src="../static/images/zhang.png" class="ico"></image>
  12. <view class="fflex">收款记录</view>
  13. <image src="../static/images/crico1.png" class="rico"></image>
  14. </navigator>
  15. </view>
  16. <canvas canvas-id="invite" class="canvas" style="width:1080px;height:1466px;"></canvas>
  17. </view>
  18. </template>
  19. <script>
  20. const req = require('../../utils/request.js');
  21. const api = require('../../utils/api.js');
  22. const util = require('../../utils/util.js');
  23. const app = getApp();
  24. export default {
  25. data() {
  26. return {
  27. isShow: false,
  28. codeUrl: '',
  29. merchant: '',
  30. title: ''
  31. };
  32. },
  33. async onLoad(options) {
  34. let userInfo = req.getStorage('userInfo');
  35. if(userInfo.listMerchant && userInfo.listMerchant.length > 0){
  36. this.merchant = userInfo.listMerchant[0]
  37. }
  38. await this.getConfig()
  39. await this.getDetail();
  40. await this.generatePoster();
  41. },
  42. onShow() {
  43. },
  44. methods: {
  45. getConfig() {
  46. var _this = this;
  47. return new Promise((resolve, reject) => {
  48. req.getRequest('/api/config', {}, function(res) {
  49. _this.title = res.CONFIG_PROJECT_TITLE;
  50. resolve();
  51. });
  52. })
  53. },
  54. getDetail() {
  55. let id = '';
  56. if(this.merchant&&this.merchant.id){
  57. id = this.merchant.id
  58. }
  59. return new Promise((resolve, reject) => {
  60. req.getRequest('/api/QrCode/collectionQr', {merchantId: id}, res => {
  61. this.isShow = true;
  62. this.codeUrl = res;
  63. resolve();
  64. });
  65. })
  66. },
  67. generatePoster() {
  68. let that = this;
  69. that.generate(imgUrl => {
  70. that.setData({
  71. imgUrl: imgUrl
  72. });
  73. });
  74. },
  75. isAuth(fun) {
  76. if (!uni.saveImageToPhotosAlbum) {
  77. uni.showModal({
  78. title: '提示',
  79. content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
  80. });
  81. return;
  82. }
  83. ; //可以通过 wx.getSetting 先查询一下用户是否授权了 "scope.writePhotosAlbum" 这个 scope
  84. // #ifndef H5
  85. uni.getSetting({
  86. success(res) {
  87. // console.log(res);
  88. if (!res.authSetting['scope.writePhotosAlbum']) {
  89. // 接口调用询问
  90. uni.authorize({
  91. scope: 'scope.writePhotosAlbum',
  92. success() {
  93. // console.log("2-授权《保存图片》权限成功");
  94. fun(true);
  95. },
  96. fail() {
  97. fun(false);
  98. }
  99. });
  100. } else {
  101. // 拒绝授权
  102. fun(true);
  103. }
  104. },
  105. fail(res) {
  106. fun(false);
  107. }
  108. });
  109. // #endif
  110. },
  111. saveImage() {
  112. let that = this;
  113. that.isAuth(success => {
  114. if (success) {
  115. // console.log("图片地址=====" + that.imgUrl);
  116. uni.saveImageToPhotosAlbum({
  117. filePath: that.imgUrl,
  118. success(res) {
  119. uni.showToast({
  120. title: '图片保存成功',
  121. icon: 'none',
  122. success() {},
  123. fail() {
  124. uni.showToast({
  125. title: '图片保存失败'
  126. });
  127. }
  128. });
  129. }
  130. });
  131. }
  132. });
  133. },
  134. getImageInfo(url) {
  135. return new Promise((resolve, reject) => {
  136. if (!url) {
  137. resolve();
  138. return false;
  139. }
  140. uni.getImageInfo({
  141. src: url,
  142. success: resolve,
  143. fail: reject
  144. });
  145. });
  146. },
  147. generate(success) {
  148. // 获取商品图片
  149. let userInfo = req.getStorage('userInfo');
  150. const avatarPromise = this.getImageInfo(userInfo.avatar); // 获取小程序码图
  151. const codePromise = this.getImageInfo(this.codeUrl);
  152. console.log('codePromise')
  153. let that = this;
  154. Promise.all([avatarPromise,codePromise]).then(([avatar,code]) => {
  155. // 创建绘图上下文
  156. const ctx = uni.createCanvasContext('invite', this); // 绘制背景,填充满整个canvas画布
  157. let width = 1080,
  158. height = 1466; //默认背景
  159. ctx.setFillStyle('#00a3ff');
  160. ctx.fillRect(0, 0, width, height);
  161. ctx.save();
  162. that.roundRect(ctx, 30, 200, 1020, 1180, 10);
  163. ctx.restore();
  164. //白色背景
  165. ctx.drawImage(code.path, 235, 515, 605, 689);
  166. ctx.textAlign = 'center'
  167. ctx.fillStyle = '#333';
  168. ctx.font = "60px PingFang SC";
  169. ctx.fillText(that.title, 540, 400); //logo
  170. ctx.fillStyle = '#666';
  171. ctx.font = "42px PingFang SC";
  172. ctx.fillText(that.merchant.title ? that.merchant.title + ' ' + userInfo.nickName : userInfo.nickName, 540, 470); //logo
  173. ctx.fillStyle = '#000';
  174. ctx.font = "40px PingFang SC font";
  175. ctx.fillText('扫码付款', 540, 1300); //logo
  176. ctx.save();
  177. ctx.beginPath();
  178. ctx.arc(540, 200, 100, 0, 2 * Math.PI, true); //画一个圆形裁剪区域
  179. ctx.clip(); //裁剪
  180. ctx.drawImage(avatar && avatar.path ? avatar.path : "/static/pages/images/logo.png", 440, 100, 200, 200); //绘制图片
  181. // 完成作画
  182. ctx.draw(false, function() {
  183. uni.canvasToTempFilePath({
  184. canvasId: 'invite',
  185. success: function(res) {
  186. success.call(this, res.tempFilePath);
  187. },
  188. fail: function(res) {
  189. // console.log(res);
  190. }
  191. }, that);
  192. });
  193. });
  194. },
  195. /**
  196. *
  197. * @param {CanvasContext} ctx canvas上下文
  198. * @param {number} x 圆角矩形选区的左上角 x坐标
  199. * @param {number} y 圆角矩形选区的左上角 y坐标
  200. * @param {number} w 圆角矩形选区的宽度
  201. * @param {number} h 圆角矩形选区的高度
  202. * @param {number} r 圆角的半径
  203. */
  204. roundRect: function(ctx, x, y, w, h, r) {
  205. // 开始绘制
  206. ctx.beginPath(); // 因为边缘描边存在锯齿,最好指定使用 transparent 填充
  207. // 这里是使用 fill 还是 stroke都可以,二选一即可
  208. ctx.setFillStyle('white'); // ctx.setStrokeStyle('transparent')
  209. // 左上角
  210. ctx.arc(x + r, y + r, r, Math.PI, Math.PI * 1.5); // border-top
  211. ctx.moveTo(x + r, y);
  212. ctx.lineTo(x + w - r, y);
  213. ctx.lineTo(x + w, y + r); // 右上角
  214. ctx.arc(x + w - r, y + r, r, Math.PI * 1.5, Math.PI * 2); // border-right
  215. ctx.lineTo(x + w, y + h - r);
  216. ctx.lineTo(x + w - r, y + h); // 右下角
  217. ctx.arc(x + w - r, y + h - r, r, 0, Math.PI * 0.5); // border-bottom
  218. ctx.lineTo(x + r, y + h);
  219. ctx.lineTo(x, y + h - r); // 左下角
  220. ctx.arc(x + r, y + h - r, r, Math.PI * 0.5, Math.PI); // border-left
  221. ctx.lineTo(x, y + r);
  222. ctx.lineTo(x + r, y); // 这里是使用 fill 还是 stroke都可以,二选一即可,但是需要与上面对应
  223. ctx.fill(); // ctx.stroke()
  224. ctx.closePath(); // 剪切
  225. ctx.clip();
  226. },
  227. // saveImg() {
  228. // uni.getImageInfo({
  229. // src: this.codeUrl,
  230. // success: function (res) {
  231. // console.log('success==' + JSON.stringify(res))
  232. // uni.saveImageToPhotosAlbum({
  233. // filePath: res.path,
  234. // success: function() {
  235. // uni.showToast({
  236. // title: '图片保存成功',
  237. // icon: 'none',
  238. // duration: 2200
  239. // });
  240. // },
  241. // fail: function(suc) {
  242. // uni.showToast({
  243. // title: '图片保存失败',
  244. // icon: 'none',
  245. // duration: 2200
  246. // });
  247. // }
  248. // });
  249. // },
  250. // fail(res){
  251. // console.log('fail==' + JSON.stringify(res))
  252. // }
  253. // });
  254. // },
  255. }
  256. };
  257. </script>
  258. <style>
  259. @import "./receiveCode.css";
  260. </style>