index.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. let Parse = getApp().Parse;
  2. let company = getApp().globalData.company
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. noBind:false //不绑定关系
  9. },
  10. /**
  11. * 生命周期函数--监听页面加载
  12. */
  13. onLoad: async function (options) {
  14. console.log(options);
  15. let {
  16. path,
  17. sx,
  18. sy,
  19. cColor,
  20. bColor,
  21. noBind,
  22. route //扫码携带路径
  23. } = options
  24. let userId = Parse.User.current().id
  25. let imgUrl = await this.loadPoster() || 'https://file-cloud.fmode.cn/L3xHgXbTHJ/20230814/vod1h4054437092.png'
  26. this.setData({
  27. imgUrl,
  28. path,
  29. sx,
  30. sy,
  31. noBind,
  32. route
  33. });
  34. const _this = this
  35. let qrCodeUrl = `https://pwa.fmode.cn/gomini/${path}/?invite=${userId}`;
  36. if(this.data.noBind){
  37. qrCodeUrl = `https://pwa.fmode.cn/gomini/${path}/`;
  38. }
  39. if(route){
  40. if(this.data.noBind){
  41. qrCodeUrl = qrCodeUrl+`?route=${route}`
  42. }else{
  43. qrCodeUrl = qrCodeUrl+`&route=${route}`
  44. }
  45. }
  46. console.log(qrCodeUrl);
  47. wx.request({
  48. url: "https://server.fmode.cn/api/common/qrcode",
  49. data: {
  50. qrCode: qrCodeUrl,
  51. darkColor: bColor || "#ffffff",
  52. lightColor: cColor || "#000000"
  53. },
  54. async success(res) {
  55. if (res.data.code == 1) {
  56. let that = this;
  57. //把base64文件转图片
  58. let fileSystemManager = wx.getFileSystemManager()
  59. fileSystemManager.writeFile({
  60. filePath: wx.env.USER_DATA_PATH + '/qr_code.png',
  61. data: res.data.data.slice(22),
  62. encoding: 'base64',
  63. success: res => {
  64. _this.setData({
  65. xcxcode: wx.env.USER_DATA_PATH + '/qr_code.png'
  66. })
  67. _this.drawOne()
  68. },
  69. fail(e) {
  70. console.log('文件保存失败', e)
  71. }
  72. })
  73. } else {
  74. wx.showToast({
  75. title: '请先删除小程序重新授权后重试',
  76. icon: 'none'
  77. })
  78. }
  79. },
  80. });
  81. },
  82. //获取海报图片
  83. async loadPoster() {
  84. let Poster = new Parse.Query('Banner')
  85. Poster.equalTo('company', company)
  86. Poster.equalTo('desc', 'invite')
  87. Poster.ascending('index')
  88. let poster = await Poster.first()
  89. if(poster?.id){
  90. return poster.get('image')
  91. }
  92. return
  93. },
  94. async drawOne() {
  95. let { sx, sy } = this.data
  96. let that = this
  97. let imgPath = that.data.imgUrl; //背景图片;
  98. let avatar = Parse.User.current()?.get('avatar')
  99. let balance = await this.getImageInfo(imgPath)
  100. let userAvatar = await this.getImageInfo(avatar)
  101. let xcxcode = that.data.xcxcode; //二维码
  102. const ctx = wx.createCanvasContext('myCanvas');
  103. //填充背景(图片的白色背景)
  104. ctx.setFillStyle('#ffffff');
  105. ctx.fillRect(0, 0, 375, 596); //坐标和宽高
  106. ctx.fillRect(0, 0, 338, 480); //坐标和宽高
  107. // 第一张图片(背景图片)
  108. if(company != 'UP2cStyjuk') ctx.drawImage(balance, 0, 0, 340, 540);
  109. userAvatar && ctx.drawImage(userAvatar, 126 , 100, 80, 80);
  110. //绘制二维码
  111. ctx.drawImage(xcxcode, sx ? sx : 70 , sy ? sy : 250, 200, 200);
  112. ctx.draw();
  113. },
  114. getImageInfo(url){
  115. return new Promise((result)=>{
  116. wx.getImageInfo({
  117. src: url, //服务器返回的图片地址
  118. success: function (res) {
  119. // res.path是网络图片的本地地址
  120. let Path = res.path;
  121. result(Path)
  122. },
  123. fail(err) {
  124. console.log(err);
  125. result()
  126. }
  127. });
  128. })
  129. },
  130. // 保存图片
  131. savetup: function () {
  132. let that = this;
  133. wx.canvasToTempFilePath({
  134. x: 0,
  135. y: 0,
  136. width: 750,
  137. height: 1334,
  138. destWidth: 750,
  139. destHeight: 1334,
  140. canvasId: 'myCanvas',
  141. success: function (result) {
  142. wx.getSetting({
  143. success(res) {
  144. if (!res.authSetting['scope.writePhotosAlbum']) {
  145. wx.authorize({
  146. scope: 'scope.writePhotosAlbum',
  147. success() {
  148. //这里是用户同意授权后的回调
  149. // that.saveImgToLocal();
  150. wx.saveImageToPhotosAlbum({
  151. filePath: result.tempFilePath,
  152. success(res) {
  153. wx.showModal({
  154. title: '图片保存成功!',
  155. content: '请将图片分享到朋友圈',
  156. showCancel: false,
  157. confirmText: '知道了',
  158. confirmColor: '#72B9C3',
  159. success: function (res) {
  160. if (res.confirm) {
  161. console.log('用户点击确定');
  162. that.setData({
  163. show: false
  164. })
  165. }
  166. }
  167. })
  168. }
  169. })
  170. },
  171. fail() { //这里是用户拒绝授权后的回调
  172. wx.showToast({
  173. title: '打开相册授权,才能保存二维码哦~',
  174. icon: 'none',
  175. duration: 2000
  176. })
  177. that.setData({
  178. openSettingBtnHidden: false
  179. })
  180. }
  181. })
  182. } else {
  183. //调取小程序当中获取图片
  184. console.log(result.tempFilePath);
  185. wx.saveImageToPhotosAlbum({
  186. filePath: result.tempFilePath,
  187. success(res) {
  188. wx.showModal({
  189. title: '图片保存成功!',
  190. content: '请将图片分享到朋友圈',
  191. showCancel: false,
  192. confirmText: '知道了',
  193. confirmColor: '#72B9C3',
  194. success: function (res) {
  195. if (res.confirm) {
  196. console.log('用户点击确定');
  197. that.setData({
  198. show: false
  199. })
  200. }
  201. }
  202. })
  203. }
  204. })
  205. }
  206. }
  207. })
  208. },
  209. fail: function (res) {
  210. console.log(res)
  211. }
  212. })
  213. },
  214. onCopy() {
  215. let id = Parse.User.current().id;
  216. let url = 'https://server.fmode.cn/api/wxapp/wechat/urlLink'
  217. let query = `invite=${id}`
  218. if(this.data.noBind) {
  219. query = ``
  220. }
  221. wx.request({
  222. url: url,
  223. data: {
  224. company: company,
  225. path: 'index',
  226. query: query,
  227. },
  228. header: {
  229. 'content-type': 'application/json'
  230. },
  231. method: 'POST',
  232. dataType: 'json',
  233. responseType: 'text',
  234. success: (result) => {
  235. console.log(result);
  236. if (result ?.data ?.url_link) {
  237. let path = result.data.url_link
  238. wx.setClipboardData({
  239. data: path,
  240. success: function (res) {
  241. wx.getClipboardData({
  242. success: function (res) {
  243. wx.showToast({
  244. title: '复制成功',
  245. })
  246. // console.log(res.data) // data
  247. }
  248. })
  249. }
  250. })
  251. return
  252. }
  253. wx.showToast({
  254. title: '复制错误',
  255. icon: 'error',
  256. image: '',
  257. duration: 1500,
  258. mask: false,
  259. });
  260. },
  261. fail: () => {},
  262. complete: () => {}
  263. });
  264. },
  265. onShareTimeline() {
  266. console.log(123)
  267. return {
  268. title: '发送朋友圈',
  269. query: '',
  270. }
  271. },
  272. /**
  273. * 生命周期函数--监听页面初次渲染完成
  274. */
  275. onReady: function () {
  276. },
  277. /**
  278. * 生命周期函数--监听页面显示
  279. */
  280. onShow: function () {
  281. },
  282. /**
  283. * 生命周期函数--监听页面隐藏
  284. */
  285. onHide: function () {
  286. },
  287. /**
  288. * 生命周期函数--监听页面卸载
  289. */
  290. onUnload: function () {
  291. },
  292. /**
  293. * 页面相关事件处理函数--监听用户下拉动作
  294. */
  295. onPullDownRefresh: function () {
  296. },
  297. /**
  298. * 页面上拉触底事件的处理函数
  299. */
  300. onReachBottom: function () {
  301. },
  302. /**
  303. * 用户点击右上角分享
  304. */
  305. onShareAppMessage: function () {
  306. let uid = Parse.User.current().id
  307. let path = `index?invite=${uid}`
  308. if(this.data.noBind) {
  309. path = 'index'
  310. }
  311. if(this.data.route){
  312. if(this.data.noBind){
  313. qrCodeUrl = qrCodeUrl+`?route=${this.data.route}`
  314. }else{
  315. qrCodeUrl = qrCodeUrl+`&route=${this.data.route}`
  316. }
  317. }
  318. return {
  319. title: '邀请好友',
  320. path: path,
  321. imageUrl: '',
  322. }
  323. }
  324. })