index.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. // nova-tourism/pages/my/sharing/index.js
  2. let {
  3. statusBarHeight,
  4. screenHeight
  5. } = wx.getSystemInfoSync()
  6. let custom = wx.getMenuButtonBoundingClientRect()
  7. let customHeight = custom.height
  8. let Parse = getApp().Parse;
  9. let company = getApp().globalData.company
  10. Page({
  11. /**
  12. * 页面的初始数据
  13. */
  14. data: {
  15. navHeight: 0, //导航高度_px
  16. canv_sty: {
  17. w: 0,
  18. h: 0,
  19. },
  20. canv_bg: 'https://file-cloud.fmode.cn/EbxZUK5lBI/20250416/pj1b4v042627175.png',
  21. canv_qr: '',
  22. canv_avatar: 'https://file-cloud.fmode.cn/EbxZUK5lBI/20250411/frvr1u080411965.jpg?imageView2/1/w/200/h/200',
  23. canv_name: '微信用户',
  24. canv_uid: '',
  25. canv_tag: '云仙客',
  26. canvas: null, //画布-下载所需
  27. noBind: false, //不绑定关系
  28. qrCodeUrl: '', //二维码原文
  29. },
  30. /**
  31. * 生命周期函数--监听页面加载
  32. */
  33. onLoad(options) {
  34. let navHeight = statusBarHeight + customHeight + 10
  35. let canv_sty = {
  36. w: screenHeight * 0.38 - 2,
  37. h: screenHeight * 0.60
  38. }
  39. this.setData({
  40. navHeight,
  41. canv_sty
  42. })
  43. console.log(options);
  44. let {
  45. path,
  46. noBind,
  47. route, //扫码携带路径
  48. uid
  49. } = options
  50. this.setData({
  51. path,
  52. noBind,
  53. route,
  54. uid
  55. });
  56. let qrCodeUrl = `https://pwa.fmode.cn/gomini/${path}/?invite=${uid}`;
  57. if (this.data.noBind) {
  58. qrCodeUrl = `https://pwa.fmode.cn/gomini/${path}/`;
  59. }
  60. if (route) {
  61. if (this.data.noBind) {
  62. qrCodeUrl = qrCodeUrl + `?route=${route}`
  63. } else {
  64. qrCodeUrl = qrCodeUrl + `&route=${route}`
  65. }
  66. }
  67. console.log(qrCodeUrl);
  68. this.setData({
  69. qrCodeUrl
  70. })
  71. this.refersh()
  72. },
  73. async refersh() {
  74. let {
  75. uid,
  76. canv_avatar,
  77. canv_name,
  78. canv_uid,
  79. canv_tag,
  80. } = this.data
  81. if (!uid) {
  82. wx.showToast({
  83. title: 'UID错误',
  84. icon: 'error'
  85. })
  86. return
  87. }
  88. let query = new Parse.Query('_User')
  89. let u = await query.get(uid)
  90. canv_avatar = u?.get('avatar') || 'https://file-cloud.fmode.cn/EbxZUK5lBI/20250411/frvr1u080411965.jpg?imageView2/1/w/200/h/200'
  91. canv_name = u?.get('nickname') || '微信用户'
  92. canv_uid = uid
  93. canv_tag = '云仙客'
  94. if (u?.get('type') == 'shop-admin') {
  95. let s_query = new Parse.Query('ShopStore')
  96. s_query.equalTo('company', company);
  97. s_query.equalTo('user', uid);
  98. s_query.equalTo('isVerified', true);
  99. s_query.notEqualTo('isDeleted', true);
  100. let s = await s_query.first()
  101. if (!s?.id) {
  102. wx.showToast({
  103. title: '暂无营业店铺',
  104. icon: 'error'
  105. })
  106. return
  107. }
  108. canv_avatar = s?.get('cover') || 'https://file-cloud.fmode.cn/EbxZUK5lBI/20250411/frvr1u080411965.jpg?imageView2/1/w/200/h/200'
  109. canv_name = u?.get('username') || '微信用户'
  110. canv_tag = '民宿主'
  111. }
  112. this.setData({
  113. canv_avatar,
  114. canv_name,
  115. canv_uid,
  116. canv_tag,
  117. })
  118. this.getQr()
  119. },
  120. /**获取二维码 */
  121. async getQr() {
  122. let {
  123. qrCodeUrl
  124. } = this.data
  125. let that = this;
  126. wx.request({
  127. url: "https://server.fmode.cn/api/common/qrcode",
  128. data: {
  129. qrCode: qrCodeUrl,
  130. darkColor: "#000",
  131. lightColor: "#fff"
  132. },
  133. async success(res) {
  134. console.log(res)
  135. if (res.data.code == 1) {
  136. that.setData({
  137. canv_qr: res?.data.data
  138. })
  139. that.draw()
  140. } else {
  141. wx.showToast({
  142. title: '请先删除小程序重新授权后重试',
  143. icon: 'none'
  144. })
  145. }
  146. },
  147. });
  148. },
  149. /**复制链接 */
  150. onCopy() {
  151. let {
  152. uid
  153. } = this.data
  154. if (!uid) {
  155. wx.showToast({
  156. title: 'UID为空,请退出重试',
  157. icon: 'none'
  158. })
  159. return
  160. }
  161. let url = 'https://server.fmode.cn/api/wxapp/wechat/urlLink'
  162. let query = `invite=${uid}`
  163. if (this.data.noBind) {
  164. query = ``
  165. }
  166. wx.request({
  167. url: url,
  168. data: {
  169. company: company,
  170. path: 'index',
  171. query: query,
  172. },
  173. header: {
  174. 'content-type': 'application/json'
  175. },
  176. method: 'POST',
  177. dataType: 'json',
  178. responseType: 'text',
  179. success: (result) => {
  180. console.log(result);
  181. if (result?.data?.url_link) {
  182. let path = result.data.url_link
  183. wx.setClipboardData({
  184. data: path,
  185. success: function (res) {
  186. wx.getClipboardData({
  187. success: function (res) {
  188. wx.showToast({
  189. title: '复制成功',
  190. })
  191. }
  192. })
  193. }
  194. })
  195. return
  196. }
  197. wx.showToast({
  198. title: '复制错误',
  199. icon: 'error',
  200. image: '',
  201. duration: 1500,
  202. mask: false,
  203. });
  204. },
  205. fail: () => {},
  206. complete: () => {}
  207. });
  208. },
  209. /**保存图片 */
  210. saveCanvas() {
  211. let {
  212. canvas
  213. } = this.data
  214. let that = this
  215. wx.canvasToTempFilePath({
  216. canvas: canvas,
  217. success(res) {
  218. that.setData({
  219. saveimage: res.tempFilePath
  220. })
  221. wx.saveImageToPhotosAlbum({
  222. filePath: res.tempFilePath,
  223. success: (res) => {
  224. wx.showToast({
  225. title: '已下载图片',
  226. icon: 'none'
  227. })
  228. },
  229. fail: (err) => {
  230. // console.log('err',err)
  231. }
  232. })
  233. },
  234. fail: (err) => {
  235. console.log(err)
  236. }
  237. })
  238. },
  239. /**绘制canvas */
  240. draw() {
  241. let {
  242. canv_bg,
  243. canv_qr,
  244. canv_avatar,
  245. canv_name,
  246. canv_uid,
  247. canv_tag,
  248. } = this.data
  249. let that = this
  250. wx.createSelectorQuery()
  251. .select('#myCanvas')
  252. .fields({
  253. node: true,
  254. size: true
  255. })
  256. .exec((res) => {
  257. const canvas = res[0].node
  258. const ctx = canvas.getContext('2d')
  259. const width = res[0].width
  260. const height = res[0].height
  261. // 初始化画布大小
  262. const dpr = wx.getWindowInfo().pixelRatio
  263. canvas.width = width * dpr
  264. canvas.height = height * dpr
  265. ctx.scale(dpr, dpr)
  266. ctx.clearRect(0, 0, width, height)
  267. //========背景=====
  268. let bg = canvas.createImage()
  269. bg.onload = () => {
  270. ctx.drawImage(bg, 0, 0, width, height)
  271. //========头像=========
  272. let x = width * 0.06
  273. let y = width * 0.06
  274. let w = width * 0.25
  275. let h = width * 0.25
  276. let avatar = canvas.createImage();
  277. avatar.onload = () => {
  278. ctx.save();
  279. let r = 10
  280. ctx.beginPath()
  281. ctx.moveTo(x + r, y)
  282. ctx.arcTo(x + w, y, x + w, y + h, r)
  283. ctx.arcTo(x + w, y + h, x, y + h, r)
  284. ctx.arcTo(x, y + h, x, y, r)
  285. ctx.arcTo(x, y, x + w, y, r)
  286. ctx.closePath()
  287. ctx.clip()
  288. ctx.drawImage(avatar, x, y, w, h)
  289. ctx.restore()
  290. ctx.draw()
  291. }
  292. avatar.src = canv_avatar
  293. //========文字========
  294. // ctx.fillStyle = '#4F9AF7';
  295. ctx.font = `${h*0.25}px bold Arial`;
  296. ctx.fillText(canv_name, x + w + 8, y + (h * 0.36));
  297. ctx.font = `${h*0.15}px Arial`;
  298. ctx.fillText(`UID:${canv_uid}`, x + w + 8, y + (h * 0.63));
  299. // 标签
  300. let tag = canvas.createImage()
  301. tag.onload = () => {
  302. ctx.drawImage(tag, x + w + 8, y + (h * 0.7), h * 0.36+6, h * 0.21)
  303. ctx.fillStyle = '#fff';
  304. ctx.font = `${h*0.12}px bold Arial`;
  305. ctx.fillText(canv_tag, x + w + 8+3, y + (h * 0.85));
  306. }
  307. tag.src = 'https://file-cloud.fmode.cn/EbxZUK5lBI/20250418/f1d8n1042110554.png'
  308. //二维码
  309. let qr = canvas.createImage()
  310. qr.onload = () => {
  311. ctx.drawImage(qr, width * 0.25, height * 0.5, width * 0.5, width * 0.5)
  312. }
  313. qr.src = canv_qr
  314. }
  315. bg.src = canv_bg
  316. that.setData({
  317. canvas
  318. })
  319. })
  320. },
  321. /**
  322. * 生命周期函数--监听页面初次渲染完成
  323. */
  324. onReady() {
  325. },
  326. /**
  327. * 生命周期函数--监听页面显示
  328. */
  329. onShow() {
  330. },
  331. /**
  332. * 生命周期函数--监听页面隐藏
  333. */
  334. onHide() {
  335. },
  336. /**
  337. * 生命周期函数--监听页面卸载
  338. */
  339. onUnload() {
  340. },
  341. /**
  342. * 页面相关事件处理函数--监听用户下拉动作
  343. */
  344. onPullDownRefresh() {
  345. },
  346. /**
  347. * 页面上拉触底事件的处理函数
  348. */
  349. onReachBottom() {
  350. },
  351. /**
  352. * 用户点击右上角分享
  353. */
  354. onShareAppMessage() {
  355. let {
  356. uid
  357. } = this.data
  358. let path = `index?invite=${uid}`
  359. if (this.data.noBind) {
  360. path = 'index'
  361. }
  362. if (this.data.route) {
  363. if (this.data.noBind) {
  364. qrCodeUrl = qrCodeUrl + `?route=${this.data.route}`
  365. } else {
  366. qrCodeUrl = qrCodeUrl + `&route=${this.data.route}`
  367. }
  368. }
  369. return {
  370. title: '邀请好友',
  371. path: path,
  372. imageUrl: '',
  373. }
  374. }
  375. })