index.js 8.9 KB

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