index.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <template>
  2. <view class="share-bg-box" @longpress="saveCode">
  3. <image class="logo" :src="sharePicture"></image>
  4. <view class="poster-info-box">
  5. <view class="user-box">
  6. <image :src="userHead" mode=""></image>
  7. <view class="test">
  8. <view style="font-size: 14px;">{{userName}}</view>
  9. <view style="color: #999;">为您分享好物,享受生活</view>
  10. </view>
  11. </view>
  12. <view class="code-box">
  13. <image :src="codeUrl" mode="aspectFit"></image>
  14. </view>
  15. </view>
  16. <button type="default" class="saveBtn">长按保存到相册</button>
  17. <!-- <view class="code-box">
  18. <image :src="codeUrl" mode="aspectFit" ></image>
  19. </view> -->
  20. <!-- <view class="share-page-title">
  21. <view class="first-title">长按图片保存为二维码海报</view>
  22. <view class="second-title">快去分享给好友扫码加入吧~</view>
  23. </view> -->
  24. </view>
  25. </template>
  26. <script>
  27. var md5 = require('../../../common/md5.js');
  28. export default {
  29. data() {
  30. return {
  31. inviteId: getApp().globalData.user_id,
  32. codeUrl: '',
  33. sharePicture:getApp().globalData.shareImg,
  34. userName:getApp().globalData.user_name,
  35. userHead:getApp().globalData.user_headUrl,
  36. };
  37. },
  38. onLoad(options) {
  39. // this.sharePicture = getApp().globalData.shareImg;
  40. this.getCodeRequest();
  41. },
  42. onShow() {
  43. //this.getUserTimes();
  44. },
  45. onShareAppMessage() {
  46. return {
  47. title: '阿拉灯神丁',
  48. path: '/pages/index/index?inviteId=' + getApp().globalData.user_id
  49. };
  50. },
  51. methods: {
  52. getCodeRequest() {
  53. let that = this;
  54. uni.showLoading({
  55. title: '生成中'
  56. });
  57. uni.request({
  58. url: getApp().globalData.shareUrl, //需要设置为全局
  59. method: 'POST',
  60. header: {
  61. 'content-type': 'application/x-www-form-urlencoded'
  62. },
  63. data: {
  64. method: 'creatQrcode',
  65. timestamp: getApp().globalData.globalTimestamp, //Date.now()
  66. uid: getApp().globalData.user_id,
  67. sign: md5('creatQrcode' + getApp().globalData.globalTimestamp)
  68. },
  69. success: res => {
  70. uni.hideLoading();
  71. if (res.data.code === 200) {
  72. that.codeUrl = res.data.msg;
  73. } else {
  74. uni.showToast({
  75. title: res.data.msg,
  76. icon: 'none'
  77. });
  78. }
  79. }
  80. });
  81. },
  82. saveCode() {
  83. const that = this;
  84. uni.getSetting({
  85. success(res) {
  86. if (!res.authSetting['scope.writePhotosAlbum']) {
  87. uni.authorize({
  88. scope: 'scope.writePhotosAlbum',
  89. success() {
  90. //这里是用户同意授权后的回调
  91. that.saveImgToLocal();
  92. },
  93. fail() {
  94. //这里是用户拒绝授权后的回调
  95. uni.showModal({
  96. title: '相册授权',
  97. content: '点击确定重新获取授权。',
  98. success: function(res) {
  99. if (res.confirm) {
  100. uni.openSetting({
  101. success: res => {
  102. console.log('授权成功');
  103. }
  104. });
  105. }
  106. }
  107. });
  108. }
  109. });
  110. } else {
  111. //用户已经授权过了
  112. that.saveImgToLocal();
  113. }
  114. }
  115. });
  116. },
  117. saveImgToLocal() {
  118. let that = this;
  119. uni.showModal({
  120. title: '提示',
  121. content: '确定保存到相册吗',
  122. success: function(res) {
  123. if (res.confirm) {
  124. uni.downloadFile({
  125. url: that.codeUrl, //图片地址
  126. success: res => {
  127. if (res.statusCode === 200) {
  128. uni.saveImageToPhotosAlbum({
  129. filePath: res.tempFilePath,
  130. success: function() {
  131. uni.showToast({
  132. title: '保存成功',
  133. icon: 'none'
  134. });
  135. },
  136. fail: function() {
  137. uni.showToast({
  138. title: '保存失败',
  139. icon: 'none'
  140. });
  141. }
  142. });
  143. }
  144. }
  145. });
  146. } else if (res.cancel) {
  147. return false;
  148. }
  149. }
  150. });
  151. }
  152. }
  153. };
  154. </script>
  155. <style>
  156. .logo {
  157. height:70%;
  158. width: 100%;
  159. }
  160. .poster-info-box {
  161. width: 100%;
  162. height: 20%;
  163. background: #fff;
  164. display: flex;
  165. justify-content: space-around;
  166. align-items: center;
  167. }
  168. .share-bg-box {
  169. width: 100%;
  170. height: 100%;
  171. display: flex;
  172. flex-direction: column;
  173. align-items: center;
  174. justify-content: flex-start;
  175. }
  176. .user-box {
  177. display: flex;
  178. font-size: 13px;
  179. /* justify-content: space-around; */
  180. width: 70%;
  181. align-items: center;
  182. }
  183. .user-box image {
  184. height: 100rpx;
  185. width: 100rpx;
  186. border-radius: 50%;
  187. margin-right: 5%;
  188. }
  189. .code-box {
  190. width: 80px;
  191. height:80px;
  192. }
  193. .code-box image {
  194. width: 100%;
  195. height: 100%;
  196. }
  197. .share-page-title {
  198. text-align: center;
  199. padding-top: 10%;
  200. }
  201. .first-title {
  202. color: #fff;
  203. font-size: 44rpx;
  204. font-weight: bold;
  205. }
  206. .second-title {
  207. color: orange;
  208. font-size: 36upx;
  209. font-weight: 550;
  210. }
  211. .test{
  212. background: transparent;
  213. position: relative;
  214. border: 1px solid #ccc;
  215. /* border-radius: 5px; */
  216. padding: 10px;
  217. /* background: #9eea6a; */
  218. font-size: 28rpx;
  219. border-top-right-radius: 20rpx;
  220. border-bottom-left-radius: 20rpx;
  221. border-bottom-right-radius: 20rpx;
  222. }
  223. .saveBtn {
  224. width: 60%;
  225. margin-top: 3%;
  226. background-color: #27BCEF!important;
  227. color: #fff!important;
  228. font-size: 16px;
  229. }
  230. </style>