index.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. import request from '@/helpers/request'
  2. import {
  3. Button,
  4. closeToast,
  5. Icon,
  6. Image,
  7. showFailToast,
  8. showLoadingToast,
  9. showSuccessToast,
  10. showToast
  11. } from 'vant'
  12. import { defineComponent, onMounted, reactive } from 'vue'
  13. import { useRoute } from 'vue-router'
  14. import styles from './index.module.less'
  15. import { postMessage, promisefiyPostMessage } from '@/helpers/native-message'
  16. import html2canvas from 'html2canvas'
  17. import { state as baseState } from '@/state'
  18. import OQrcode from '@/components/o-qrcode'
  19. import iconImage from './images/icon-image.png'
  20. import iconWeichat from './images/icon-weichat.png'
  21. import teacherTopBg from './images/teacher-top_bg.png'
  22. import manageTopBg from './images/manage-top_bg.png'
  23. import orchestraTopBg from './images/orchestra-top_bg.png'
  24. import defaultLogo from '@/common/images/logo@2x.png'
  25. export default defineComponent({
  26. name: 'save-share-image',
  27. setup() {
  28. const route = useRoute()
  29. const state = reactive({
  30. type: route.query.type as any,
  31. paramValue: 2,
  32. schoolName: '',
  33. schoolId: '',
  34. url: null as any,
  35. schoolLogo: '',
  36. loading: false
  37. })
  38. const imgs = reactive({
  39. saveLoading: false,
  40. image: null as any,
  41. shareLoading: false
  42. })
  43. const onSaveImg = async () => {
  44. // 判断是否在保存中...
  45. if (imgs.saveLoading) {
  46. return
  47. }
  48. imgs.saveLoading = true
  49. // 判断是否已经生成图片
  50. if (imgs.image) {
  51. saveImg()
  52. } else {
  53. const container: any = document.getElementById(`preview-container`)
  54. html2canvas(container, {
  55. allowTaint: true,
  56. useCORS: true,
  57. backgroundColor: null
  58. })
  59. .then(async (canvas) => {
  60. const url = canvas.toDataURL('image/png')
  61. imgs.image = url
  62. saveImg()
  63. })
  64. .catch(() => {
  65. closeToast()
  66. imgs.saveLoading = false
  67. })
  68. }
  69. }
  70. const onShare = () => {
  71. if (imgs.shareLoading) {
  72. return
  73. }
  74. imgs.shareLoading = true
  75. if (imgs.image) {
  76. openShare()
  77. } else {
  78. const container: any = document.getElementById(`preview-container`)
  79. html2canvas(container, {
  80. allowTaint: true,
  81. useCORS: true,
  82. backgroundColor: null
  83. })
  84. .then(async (canvas) => {
  85. const url = canvas.toDataURL('image/png')
  86. imgs.image = url
  87. openShare()
  88. })
  89. .catch(() => {
  90. closeToast()
  91. imgs.shareLoading = false
  92. })
  93. }
  94. }
  95. const openShare = () => {
  96. const image = imgs.image
  97. setTimeout(() => {
  98. imgs.shareLoading = false
  99. }, 100)
  100. if (image) {
  101. postMessage(
  102. {
  103. api: 'shareTripartite',
  104. content: {
  105. title: '',
  106. desc: '',
  107. image,
  108. video: '',
  109. type: 'image',
  110. // button: ['copy']
  111. shareType: 'wechat'
  112. }
  113. },
  114. (res: any) => {
  115. if (res && res.content) {
  116. showToast(res.content.message || (res.content.status ? '分享成功' : '分享失败'))
  117. }
  118. }
  119. )
  120. }
  121. }
  122. const saveImg = async () => {
  123. showLoadingToast({ message: '图片生成中...', forbidClick: true })
  124. setTimeout(() => {
  125. imgs.saveLoading = false
  126. }, 100)
  127. const res = await promisefiyPostMessage({
  128. api: 'savePicture',
  129. content: {
  130. base64: imgs.image
  131. }
  132. })
  133. if (res?.content?.status === 'success') {
  134. showSuccessToast('保存成功')
  135. } else {
  136. showFailToast('保存失败')
  137. }
  138. }
  139. // 获取当前用户所在的学校
  140. const getDetail = async () => {
  141. try {
  142. const schoolId = (baseState.user.data.schoolInfos || [])
  143. .map((item) => {
  144. return item.id
  145. })
  146. .join(',')
  147. const res = await request.get(`/api-school/school/detail/${schoolId}`, {})
  148. state.schoolName = res.data.name
  149. state.schoolId = res.data.id
  150. state.schoolLogo = res.data.logo
  151. ? res.data.logo + '@base@tag=imgScale&w=570?t=' + +new Date()
  152. : ''
  153. // 生成二维码
  154. if (state.type === 'teacher') {
  155. state.url =
  156. location.origin +
  157. '/orchestra-school/#/companion-teacher-register?id=' +
  158. res.data.id +
  159. '&name=' +
  160. res.data.name +
  161. '&t=' +
  162. +new Date()
  163. } else if (state.type === 'manage') {
  164. state.url =
  165. location.origin +
  166. '/orchestra-school/#/manage-teacher-register?id=' +
  167. res.data.id +
  168. '&name=' +
  169. res.data.name +
  170. '&t=' +
  171. +new Date()
  172. }
  173. } catch {
  174. //
  175. }
  176. }
  177. // 获取当前用户所在的学校
  178. const getOrchestraDetail = async () => {
  179. try {
  180. const res = await request.get('/api-school/orchestra/detail/' + route.query.id)
  181. state.schoolName = res.data.name
  182. state.schoolId = res.data.id
  183. state.schoolLogo = res.data.schoolLogo + '@base@tag=imgScale&w=570?t=' + +new Date()
  184. // 生成二维码
  185. state.url = window.location.origin + '/orchestra-student/#/preApply?id=' + route.query.id
  186. } catch {
  187. //
  188. }
  189. }
  190. onMounted(async () => {
  191. if (state.type === 'teacher') {
  192. document.title = '乐团伴学指导注册'
  193. } else if (state.type === 'manage') {
  194. document.title = '乐团管理老师注册'
  195. } else if (state.type === 'orchestra') {
  196. document.title = '乐团报名'
  197. }
  198. try {
  199. const { data } = await request.get('/api-school/open/paramConfig/queryByParamName', {
  200. requestType: 'form',
  201. params: {
  202. paramName: 'qr_code_expire_hours'
  203. }
  204. })
  205. state.paramValue = data.paramValue
  206. } catch {
  207. //
  208. }
  209. if (state.type === 'orchestra') {
  210. getOrchestraDetail()
  211. } else {
  212. getDetail()
  213. }
  214. })
  215. return () => (
  216. <div class={[styles.saveShareImage]}>
  217. {state.type === 'teacher' && <Image src={teacherTopBg} class={styles.topImage} />}
  218. {state.type === 'manage' && <Image src={manageTopBg} class={styles.topImage} />}
  219. {state.type === 'orchestra' && <Image src={orchestraTopBg} class={styles.topImage} />}
  220. <div
  221. class={[styles.shareContaienr, state.type === 'orchestra' && styles.orchestraContainer]}
  222. >
  223. {state.type !== 'orchestra' ? (
  224. <>
  225. <img
  226. class={[styles.schoolLogo]}
  227. src={state.schoolLogo || defaultLogo}
  228. crossorigin="anonymous"
  229. style={{
  230. objectFit: 'cover'
  231. }}
  232. />
  233. <div class={styles.schoolName}>{state.schoolName}</div>
  234. <div class={styles.shareType}>
  235. 邀请您成为
  236. <span>
  237. {state.type === 'teacher' && '乐团伴学指导'}
  238. {state.type === 'manage' && '乐团管理老师'}
  239. </span>
  240. </div>
  241. </>
  242. ) : (
  243. <>
  244. <div class={styles.schoolName}>乐团报名</div>
  245. <div class={styles.shareType}>{state.schoolName}</div>
  246. </>
  247. )}
  248. <div class={styles.qrcodeSection}>
  249. <OQrcode text={state.url} logoSize={'small'} size={'100%'} />
  250. </div>
  251. <div class={styles.memo}>扫描上方二维码完成资料填写</div>
  252. {state.type !== 'orchestra' && (
  253. <div class={styles.endTime}>
  254. 二维码将在<span>{state.paramValue}小时后</span>失效,请及时登记
  255. </div>
  256. )}
  257. </div>
  258. <div class={styles.btnGroup}>
  259. <Button class={styles.btn} round block onClick={onSaveImg}>
  260. <Icon name={iconImage} class={styles.icon} />
  261. 保存图片
  262. </Button>
  263. <Button class={styles.btn} round block onClick={onShare}>
  264. <Icon name={iconWeichat} class={styles.icon} />
  265. 分享到微信
  266. </Button>
  267. </div>
  268. {!state.loading && (
  269. <div class={[styles.saveShareImage, styles.previewSection]} id="preview-container">
  270. {state.type === 'teacher' && <Image src={teacherTopBg} class={styles.topImage} />}
  271. {state.type === 'manage' && <Image src={manageTopBg} class={styles.topImage} />}
  272. {state.type === 'orchestra' && <Image src={orchestraTopBg} class={styles.topImage} />}
  273. <div
  274. class={[
  275. styles.shareContaienr,
  276. state.type === 'orchestra' && styles.orchestraContainer
  277. ]}
  278. >
  279. {state.type !== 'orchestra' ? (
  280. <>
  281. <img
  282. class={[styles.schoolLogo]}
  283. src={state.schoolLogo || defaultLogo}
  284. crossorigin="anonymous"
  285. style={{
  286. objectFit: 'cover'
  287. }}
  288. />
  289. <div class={styles.schoolName}>{state.schoolName}</div>
  290. <div class={styles.shareType}>
  291. 邀请您成为
  292. <span>
  293. {state.type === 'teacher' && '乐团伴学指导'}
  294. {state.type === 'manage' && '乐团管理老师'}
  295. </span>
  296. </div>
  297. </>
  298. ) : (
  299. <>
  300. <div class={styles.schoolName}>乐团报名</div>
  301. <div class={styles.shareType}>{state.schoolName}</div>
  302. </>
  303. )}
  304. <div class={styles.qrcodeSection}>
  305. <OQrcode text={state.url} logoSize={'small'} size={'100%'} />
  306. </div>
  307. <div class={styles.memo}>扫描上方二维码完成资料填写</div>
  308. {state.type !== 'orchestra' && (
  309. <div class={styles.endTime}>
  310. 二维码将在<span>{state.paramValue}小时后</span>失效,请及时登记
  311. </div>
  312. )}
  313. </div>
  314. </div>
  315. )}
  316. </div>
  317. )
  318. }
  319. })