speakerInvitation.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. <template>
  2. <view class="container">
  3. <image class="bg" src="https://ysrw-1395926010.cos.ap-chengdu.myqcloud.com/image/bg_invite.png" mode="aspectFill"></image>
  4. <!-- 状态栏占位(微信小程序原生适配) -->
  5. <view class="status-bar" :style="{ height: statusBarHeight + 'px' }"></view>
  6. <view class="return">
  7. <image class="return-img" @click="goBack" src="https://ysrw-1395926010.cos.ap-chengdu.myqcloud.com/image/back_white.png" mode="aspectFill"></image>
  8. </view>
  9. <!-- 核心内容区:居中布局 -->
  10. <view class="main-content">
  11. <!-- 页面大标题:讲者邀请 -->
  12. <view class="page-title">
  13. <image class="title-img" src="https://ysrw-1395926010.cos.ap-chengdu.myqcloud.com/image/img_title.png" mode="aspectFill"></image>
  14. <text class="title-txt">讲者邀请</text>
  15. <image class="title-img rotate180" src="https://ysrw-1395926010.cos.ap-chengdu.myqcloud.com/image/img_title.png" mode="aspectFill"></image>
  16. </view>
  17. <!-- 邀请卡片:白色背景+圆角+轻微阴影(1:1匹配UI) -->
  18. <view class="invite-card">
  19. <!-- 邀请人信息区 -->
  20. <view class="inviter-section">
  21. <image class="inviter-avatar" src="https://ysrw-1395926010.cos.ap-chengdu.myqcloud.com/image/my_heads_icon.png" mode="aspectFill"></image>
  22. <text class="inviter-name">王小明</text>
  23. <text class="invite-desc">邀请你成为讲者</text>
  24. </view>
  25. <!-- 二维码区域:白色边框+圆角 -->
  26. <view class="qrcode-box">
  27. <image class="qrcode-img" :src="qscode" @longpress="saveQrcode"></image>
  28. </view>
  29. <!-- 扫码提示文字 -->
  30. <view class="scan-tip">扫一扫,注册一咻学术</view>
  31. </view>
  32. </view>
  33. </view>
  34. </template>
  35. <script>
  36. import { inviteDoctor } from '@/api/speaker.js'
  37. export default {
  38. data() {
  39. return {
  40. // 仅获取微信小程序状态栏高度,无多余逻辑
  41. statusBarHeight: uni.getSystemInfoSync().statusBarHeight,
  42. userInfo: {},
  43. qscode:''
  44. };
  45. },
  46. onLoad() {
  47. this.userInfo=uni.getStorageSync('userInfo')||{};
  48. console.log('userInfo:', this.userInfo);
  49. console.log('userInfo.userId:', this.userInfo.userId);
  50. console.log('userInfo.companyId:', this.userInfo.companyId);
  51. this.inviteDoctor();
  52. },
  53. methods: {
  54. goBack() {
  55. uni.navigateBack({
  56. delta: 1
  57. });
  58. },
  59. saveQrcode() {
  60. if (!this.qscode) {
  61. uni.showToast({
  62. icon: 'none',
  63. title: '二维码未加载'
  64. });
  65. return;
  66. }
  67. uni.showLoading({
  68. title: '保存中...'
  69. });
  70. // 如果是base64格式的图片,需要先转换为临时文件
  71. if (this.qscode.startsWith('data:image')) {
  72. const base64Data = this.qscode.replace(/^data:image\/\w+;base64,/, '');
  73. const filePath = `${wx.env.USER_DATA_PATH}/qrcode_${Date.now()}.png`;
  74. const fs = wx.getFileSystemManager();
  75. try {
  76. fs.writeFileSync(filePath, base64Data, 'base64');
  77. uni.saveImageToPhotosAlbum({
  78. filePath: filePath,
  79. success: () => {
  80. uni.hideLoading();
  81. uni.showToast({
  82. icon: 'success',
  83. title: '保存成功'
  84. });
  85. },
  86. fail: (err) => {
  87. uni.hideLoading();
  88. if (err.errMsg.includes('auth')) {
  89. uni.showModal({
  90. title: '提示',
  91. content: '需要您授权保存图片到相册',
  92. success: (modalRes) => {
  93. if (modalRes.confirm) {
  94. uni.openSetting({
  95. success: (settingRes) => {
  96. if (settingRes.authSetting['scope.writePhotosAlbum']) {
  97. this.saveQrcode();
  98. }
  99. }
  100. });
  101. }
  102. }
  103. });
  104. } else {
  105. uni.showToast({
  106. icon: 'none',
  107. title: '保存失败'
  108. });
  109. }
  110. }
  111. });
  112. } catch (e) {
  113. uni.hideLoading();
  114. uni.showToast({
  115. icon: 'none',
  116. title: '保存失败'
  117. });
  118. }
  119. } else {
  120. // 如果是网络图片URL
  121. uni.downloadFile({
  122. url: this.qscode,
  123. success: (res) => {
  124. if (res.statusCode === 200) {
  125. uni.saveImageToPhotosAlbum({
  126. filePath: res.tempFilePath,
  127. success: () => {
  128. uni.hideLoading();
  129. uni.showToast({
  130. icon: 'success',
  131. title: '保存成功'
  132. });
  133. },
  134. fail: (err) => {
  135. uni.hideLoading();
  136. if (err.errMsg.includes('auth')) {
  137. uni.showModal({
  138. title: '提示',
  139. content: '需要您授权保存图片到相册',
  140. success: (modalRes) => {
  141. if (modalRes.confirm) {
  142. uni.openSetting({
  143. success: (settingRes) => {
  144. if (settingRes.authSetting['scope.writePhotosAlbum']) {
  145. this.saveQrcode();
  146. }
  147. }
  148. });
  149. }
  150. }
  151. });
  152. } else {
  153. uni.showToast({
  154. icon: 'none',
  155. title: '保存失败'
  156. });
  157. }
  158. }
  159. });
  160. }
  161. },
  162. fail: () => {
  163. uni.hideLoading();
  164. uni.showToast({
  165. icon: 'none',
  166. title: '下载失败'
  167. });
  168. }
  169. });
  170. }
  171. },
  172. inviteDoctor() {
  173. console.log('开始调用inviteDoctor');
  174. console.log('userId:', this.userInfo.userId);
  175. console.log('companyId:', this.userInfo.companyId);
  176. // 检查参数是否完整
  177. if (!this.userInfo.userId || !this.userInfo.companyId) {
  178. console.error('参数不完整:', { userId: this.userInfo.userId, companyId: this.userInfo.companyId });
  179. uni.showToast({
  180. icon: 'none',
  181. title: '用户信息不完整'
  182. });
  183. return;
  184. }
  185. uni.showLoading({
  186. title: '生成二维码中...'
  187. });
  188. console.log('调用API前的userId:', this.userInfo.userId);
  189. console.log('调用API前的companyId:', this.userInfo.companyId);
  190. inviteDoctor(this.userInfo.userId, this.userInfo.companyId).then(res => {
  191. uni.hideLoading();
  192. // console.log('inviteDoctor返回数据类型:', typeof res);
  193. console.log('inviteDoctor返回数据:', res);
  194. console.log('res是否为null/undefined:', res === null || res === undefined);
  195. // 处理各种可能的返回格式
  196. try {
  197. // 情况1:返回base64字符串
  198. if (typeof res === 'string') {
  199. console.log('处理直接返回的字符串,长度:', res.length);
  200. if (res.startsWith('data:image')) {
  201. console.log('处理直接返回的base64字符串');
  202. this.qscode = res;
  203. } else {
  204. // 尝试直接使用字符串作为图片URL
  205. console.log('处理返回的图片URL');
  206. this.qscode = res;
  207. }
  208. }
  209. // 情况2:标准API返回格式
  210. else if (res && typeof res === 'object') {
  211. console.log('处理对象类型返回值');
  212. console.log('res对象的keys:', Object.keys(res));
  213. // 子情况1:data为base64字符串
  214. if (typeof res.data === 'string') {
  215. console.log('处理API返回的base64字符串,长度:', res.data.length);
  216. if (res.data.startsWith('data:image')) {
  217. this.qscode = res.data;
  218. } else {
  219. // 尝试直接使用字符串作为图片URL
  220. this.qscode = res.data;
  221. }
  222. }
  223. // 子情况2:data为ArrayBuffer
  224. else if (res.data) {
  225. console.log('处理API返回的data,类型:', typeof res.data);
  226. try {
  227. const base64 = uni.arrayBufferToBase64(res.data);
  228. this.qscode = 'data:image/png;base64,' + base64;
  229. console.log('ArrayBuffer转换成功:', this.qscode.substring(0, 50) + '...');
  230. } catch (e) {
  231. console.error('ArrayBuffer转换失败:', e);
  232. uni.showToast({
  233. icon: 'none',
  234. title: '二维码转换失败'
  235. });
  236. }
  237. }
  238. // 子情况3:直接在响应对象中包含二维码信息
  239. else if (res.qrcode || res.qrCode) {
  240. console.log('处理API返回的qrcode字段');
  241. this.qscode = res.qrcode || res.qrCode;
  242. }
  243. // 子情况4:检查其他可能的字段
  244. else {
  245. console.log('检查其他可能的字段');
  246. // 尝试直接使用res作为图片URL
  247. this.qscode = JSON.stringify(res);
  248. console.log('尝试使用JSON字符串作为二维码:', this.qscode);
  249. }
  250. }
  251. // 情况3:直接返回ArrayBuffer
  252. else if (res) {
  253. console.log('处理直接返回的非对象非字符串数据');
  254. try {
  255. const base64 = uni.arrayBufferToBase64(res);
  256. this.qscode = 'data:image/png;base64,' + base64;
  257. console.log('ArrayBuffer转换成功:', this.qscode.substring(0, 50) + '...');
  258. } catch (e) {
  259. console.error('ArrayBuffer转换失败:', e);
  260. uni.showToast({
  261. icon: 'none',
  262. title: '二维码转换失败'
  263. });
  264. }
  265. }
  266. else {
  267. console.error('无法识别的返回格式:', res);
  268. uni.showToast({
  269. icon: 'none',
  270. title: '二维码格式错误'
  271. });
  272. }
  273. // 确保二维码值已设置
  274. if (this.qscode) {
  275. console.log('二维码设置成功:', this.qscode.substring(0, 100) + (this.qscode.length > 100 ? '...' : ''));
  276. // 强制组件更新,确保图片显示
  277. this.$forceUpdate();
  278. console.log('强制组件更新后');
  279. // 模拟延迟后再次更新,确保图片加载
  280. setTimeout(() => {
  281. console.log('延迟更新后qscode:', this.qscode);
  282. this.$forceUpdate();
  283. }, 100);
  284. }
  285. } catch (e) {
  286. console.error('二维码处理失败:', e);
  287. uni.showToast({
  288. icon: 'none',
  289. title: '二维码处理失败'
  290. });
  291. }
  292. }).catch(err => {
  293. uni.hideLoading();
  294. console.error('获取二维码失败:', err);
  295. uni.showToast({
  296. icon: 'none',
  297. title: '获取二维码失败'
  298. });
  299. });
  300. }
  301. }
  302. };
  303. </script>
  304. <style lang="scss" scoped>
  305. /* 小程序页面全局重置:消除默认样式 */
  306. page {
  307. margin: 0;
  308. padding: 0;
  309. background-color: #F8F7F5;
  310. font-family: "PingFang SC", "Microsoft YaHei", sans-serif;
  311. }
  312. /* 根容器:全屏+垂直布局 */
  313. .container {
  314. min-height: 100vh;
  315. display: flex;
  316. flex-direction: column;
  317. background-color: #F8F7F5;
  318. position: relative;
  319. .bg {
  320. position: absolute;
  321. top: 0;
  322. width: 100%;
  323. height: 100%;
  324. }
  325. }
  326. /* 状态栏占位:透明背景,仅占高度 */
  327. .status-bar {
  328. width: 100%;
  329. background-color: transparent;
  330. }
  331. .return {
  332. position: relative;
  333. height: 88rpx;
  334. z-index: 2;
  335. line-height: 88rpx;
  336. .return-img {
  337. width: 40rpx;
  338. height: 40rpx;
  339. margin-left: 32rpx;
  340. }
  341. }
  342. /* 核心内容区:水平居中+顶部间距 */
  343. .main-content {
  344. width: 100%;
  345. display: flex;
  346. flex-direction: column;
  347. align-items: center;
  348. padding: 52rpx 74rpx 0;
  349. box-sizing: border-box;
  350. position: relative;
  351. z-index: 2;
  352. /* 页面大标题:讲者邀请(匹配UI字体/大小/颜色) */
  353. .page-title {
  354. font-weight: 600;
  355. font-size: 48rpx;
  356. color: #FFFFFF;
  357. margin-bottom: 124rpx;
  358. display: flex;
  359. align-items: center;
  360. .title-img {
  361. width: 134rpx;
  362. height: 12rpx;
  363. }
  364. .title-txt {
  365. margin: 0 20rpx;
  366. }
  367. .rotate180 {
  368. transform: rotate(180deg);
  369. }
  370. }
  371. /* 邀请卡片:白色背景+圆角+轻微阴影(1:1匹配) */
  372. .invite-card {
  373. width: 100%;
  374. background-color: #FFFFFF;
  375. border-radius: 32rpx;
  376. box-shadow: 0rpx 16rpx 28rpx 0rpx rgba(45, 109, 199, 0.37);
  377. padding: 60rpx 40rpx 80rpx;
  378. position: relative;
  379. box-sizing: border-box;
  380. /* 邀请人信息区:头像+文字横向布局 */
  381. .inviter-section {
  382. position: absolute;
  383. top: -72rpx;
  384. left: 50%;
  385. transform: translateX(-50%);
  386. display: flex;
  387. flex-direction: column;
  388. justify-content: center;
  389. align-items: center;
  390. .inviter-avatar {
  391. width: 144rpx;
  392. height: 144rpx;
  393. border-radius: 50%;
  394. margin-bottom: 18rpx;
  395. }
  396. /* 邀请人姓名 */
  397. .inviter-name {
  398. color: #000000;
  399. font-weight: 600;
  400. font-size: 36rpx;
  401. margin-bottom: 36rpx;
  402. }
  403. /* 邀请文案 */
  404. .invite-desc {
  405. font-size: 32rpx;
  406. color: #666666;
  407. }
  408. }
  409. .qrcode-box {
  410. width: 372rpx;
  411. height: 372rpx;
  412. background-color: #FFFFFF;
  413. display: flex;
  414. align-items: center;
  415. justify-content: center;
  416. margin: 200rpx auto 40rpx;
  417. .qrcode-img {
  418. width: 372rpx;
  419. height: 372rpx;
  420. }
  421. }
  422. /* 扫码提示文字:居中+浅灰色 */
  423. .scan-tip {
  424. text-align: center;
  425. font-size: 32rpx;
  426. color: #333333;
  427. }
  428. }
  429. }
  430. </style>