index.js 9.0 KB

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