Authorize.vue 5.9 KB

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