Authorize.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <template>
  2. <view>
  3. <view class="Popup" v-if="isShowAuth">
  4. <image :src="logoUrl"></image>
  5. <view class="title">授权提醒</view>
  6. <view class="tip">请授权头像等信息,以便为您提供更好的服务</view>
  7. <view class="bottom flex">
  8. <view class="item" @click="close">随便逛逛</view>
  9. <!-- #ifdef APP-PLUS -->
  10. <button class="item grant" @click="setUserInfo">去授权</button>
  11. <!-- #endif -->
  12. <!-- #ifdef MP -->
  13. <button class="item grant" type="primary" open-type="getPhoneNumber" lang="zh_CN" @getphonenumber="setUserInfo">去授权</button>
  14. <!-- #endif -->
  15. </view>
  16. </view>
  17. <view class="mask" v-if="isShowAuth" @click="close"></view>
  18. </view>
  19. </template>
  20. <script>
  21. const app = getApp();
  22. import Cache from '../utils/cache';
  23. import { getLogo, silenceAuth, getUserPhone } from '../api/public';
  24. import { LOGO_URL, EXPIRES_TIME, USER_INFO, STATE_R_KEY } from '../config/cache';
  25. import { mapGetters } from 'vuex';
  26. import Routine from '../libs/routine';
  27. import store from '../store';
  28. export default {
  29. name: 'Authorize',
  30. props: {
  31. isAuto: {
  32. type: Boolean,
  33. default: true
  34. },
  35. isGoIndex: {
  36. type: Boolean,
  37. default: true
  38. },
  39. isShowAuth: {
  40. type: Boolean,
  41. default: false
  42. }
  43. },
  44. data() {
  45. return {
  46. logoUrl: '',
  47. authKey: ''
  48. };
  49. },
  50. computed: mapGetters(['isLogin', 'userInfo']),
  51. watch: {
  52. isLogin(n) {
  53. n === true && this.$emit('onLoadFun', this.userInfo);
  54. }
  55. },
  56. mounted() {
  57. this.getLogoUrl();
  58. let that = this;
  59. if (!this.isLogin && !Cache.has(STATE_R_KEY)) {
  60. wx.login({
  61. success(res) {
  62. Cache.set(STATE_R_KEY, res.code, 10800);
  63. let spread = app.globalData.spid ? app.globalData.spid : '';
  64. // silenceAuth({ code: res.code, spread: spread, spid: app.globalData.code })
  65. // .then(res => {
  66. // if (res.data.key !== undefined && res.data.key) {
  67. // that.authKey = res.data.key;
  68. // } else {
  69. // app.globalData.code = 0;
  70. // let time = res.data.expires_time - Cache.time();
  71. // // store.commit('UPDATE_USERINFO', res.data.userInfo);
  72. // store.commit('LOGIN', { token: res.data.token, time: time });
  73. // // store.commit('SETUID', res.data.userInfo.uid);
  74. // // Cache.set(EXPIRES_TIME,res.data.expires_time,time);
  75. // // Cache.set(USER_INFO,res.data.userInfo,time);
  76. // }
  77. // })
  78. // .catch(res => {
  79. // });
  80. }
  81. });
  82. } else {
  83. this.setAuthStatus();
  84. }
  85. },
  86. methods: {
  87. setAuthStatus() {
  88. Routine.authorize()
  89. .then(res => {
  90. if (res.islogin === false) this.setUserInfo();
  91. else this.$emit('onLoadFun', this.userInfo);
  92. })
  93. .catch(res => {
  94. if (this.isAuto) this.$emit('authColse', true);
  95. });
  96. },
  97. getUserInfo(code) {
  98. Routine.getUserInfo()
  99. .then(res => {
  100. let userInfo = res.userInfo;
  101. userInfo.code = code;
  102. userInfo.spread_spid = app.globalData.spid; //获取推广人ID
  103. userInfo.spread_code = app.globalData.code; //获取推广人分享二维码ID
  104. Routine.authUserInfo(userInfo)
  105. .then(res => {
  106. uni.hideLoading();
  107. this.$emit('authColse', false);
  108. this.$emit('onLoadFun', this.userInfo);
  109. })
  110. .catch(res => {
  111. uni.hideLoading();
  112. uni.showToast({
  113. title: res.msg,
  114. icon: 'none',
  115. duration: 2000
  116. });
  117. });
  118. })
  119. .catch(res => {
  120. uni.hideLoading();
  121. });
  122. },
  123. getUserPhoneNumber(encryptedData, iv, code) {
  124. getUserPhone({
  125. encryptedData: encryptedData,
  126. iv: iv,
  127. code: code,
  128. spid: app.globalData.spid,
  129. spread: app.globalData.code
  130. })
  131. .then(res => {
  132. let time = res.data.expires_time - this.$Cache.time();
  133. this.$store.commit('LOGIN', {
  134. token: res.data.token,
  135. time: time
  136. });
  137. this.$emit('authColse', false);
  138. this.$emit('onLoadFun', res.data.userInfo);
  139. uni.hideLoading();
  140. })
  141. .catch(res => {
  142. uni.hideLoading();
  143. });
  144. },
  145. setUserInfo(e) {
  146. uni.showLoading({ title: '正在登录中' });
  147. Routine.getCode()
  148. .then(code => {
  149. this.getUserPhoneNumber(e.detail.encryptedData, e.detail.iv, code);
  150. })
  151. .catch(res => {
  152. uni.hideLoading();
  153. });
  154. },
  155. getLogoUrl() {
  156. let that = this;
  157. if (Cache.has(LOGO_URL)) {
  158. this.logoUrl = Cache.get(LOGO_URL);
  159. return;
  160. }
  161. getLogo().then(res => {
  162. that.logoUrl = res.data.logo_url;
  163. Cache.set(LOGO_URL, that.logoUrl);
  164. });
  165. },
  166. close() {
  167. let pages = getCurrentPages(),
  168. currPage = pages[pages.length - 1];
  169. if (this.isGoIndex) {
  170. uni.navigateTo({ url: '/pages/index/index' });
  171. } else {
  172. this.$emit('authColse', false);
  173. }
  174. // if (currPage && currPage.isShowAuth != undefined){
  175. // currPage.isShowAuth = true;
  176. // }
  177. }
  178. }
  179. };
  180. </script>
  181. <style scoped lang="scss">
  182. .Popup {
  183. width: 500rpx;
  184. background-color: #fff;
  185. position: fixed;
  186. top: 50%;
  187. left: 50%;
  188. margin-left: -250rpx;
  189. transform: translateY(-50%);
  190. z-index: 320;
  191. }
  192. .Popup image {
  193. width: 150rpx;
  194. height: 150rpx;
  195. margin: -67rpx auto 0 auto;
  196. display: block;
  197. border: 8rpx solid #fff;
  198. border-radius: 50%;
  199. }
  200. .Popup .title {
  201. font-size: 28rpx;
  202. color: #000;
  203. text-align: center;
  204. margin-top: 30rpx;
  205. }
  206. .Popup .tip {
  207. font-size: 22rpx;
  208. color: #555;
  209. padding: 0 24rpx;
  210. margin-top: 25rpx;
  211. }
  212. .Popup .bottom .item {
  213. width: 50%;
  214. height: 80rpx;
  215. background-color: #eeeeee;
  216. text-align: center;
  217. line-height: 80rpx;
  218. font-size: 24rpx;
  219. color: #666;
  220. margin-top: 54rpx;
  221. }
  222. .Popup .bottom .item.on {
  223. width: 100%;
  224. }
  225. .flex {
  226. display: flex;
  227. }
  228. .Popup .bottom .item.grant {
  229. font-size: 28rpx;
  230. color: #fff;
  231. font-weight: bold;
  232. background-color: var(--view-theme);
  233. border-radius: 0;
  234. padding: 0;
  235. }
  236. .mask {
  237. position: fixed;
  238. top: 0;
  239. right: 0;
  240. left: 0;
  241. bottom: 0;
  242. background-color: rgba(0, 0, 0, 0.65);
  243. z-index: 310;
  244. }
  245. </style>