Authorize.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. console.log(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. console.log(res);
  64. Cache.set(STATE_R_KEY, res.code, 10800);
  65. let spread = app.globalData.spid ? app.globalData.spid : '';
  66. silenceAuth({ code: res.code, spread: spread, spid: app.globalData.code })
  67. .then(res => {
  68. if (res.data.key !== undefined && res.data.key) {
  69. that.authKey = res.data.key;
  70. } else {
  71. app.globalData.code = 0;
  72. let time = res.data.expires_time - Cache.time();
  73. // store.commit('UPDATE_USERINFO', res.data.userInfo);
  74. store.commit('LOGIN', { token: res.data.token, time: time });
  75. // store.commit('SETUID', res.data.userInfo.uid);
  76. // Cache.set(EXPIRES_TIME,res.data.expires_time,time);
  77. // Cache.set(USER_INFO,res.data.userInfo,time);
  78. }
  79. })
  80. .catch(res => {
  81. console.log(res);
  82. });
  83. }
  84. });
  85. } else {
  86. this.setAuthStatus();
  87. }
  88. },
  89. methods: {
  90. setAuthStatus() {
  91. Routine.authorize()
  92. .then(res => {
  93. if (res.islogin === false) this.setUserInfo();
  94. else this.$emit('onLoadFun', this.userInfo);
  95. })
  96. .catch(res => {
  97. if (this.isAuto) this.$emit('authColse', true);
  98. });
  99. },
  100. getUserInfo(code) {
  101. Routine.getUserInfo()
  102. .then(res => {
  103. let userInfo = res.userInfo;
  104. userInfo.code = code;
  105. userInfo.spread_spid = app.globalData.spid; //获取推广人ID
  106. userInfo.spread_code = app.globalData.code; //获取推广人分享二维码ID
  107. Routine.authUserInfo(userInfo)
  108. .then(res => {
  109. uni.hideLoading();
  110. this.$emit('authColse', false);
  111. this.$emit('onLoadFun', this.userInfo);
  112. })
  113. .catch(res => {
  114. uni.hideLoading();
  115. uni.showToast({
  116. title: res.msg,
  117. icon: 'none',
  118. duration: 2000
  119. });
  120. });
  121. })
  122. .catch(res => {
  123. uni.hideLoading();
  124. });
  125. },
  126. getUserPhoneNumber(encryptedData, iv, code) {
  127. getUserPhone({
  128. encryptedData: encryptedData,
  129. iv: iv,
  130. code: code,
  131. spid: app.globalData.spid,
  132. spread: app.globalData.code
  133. })
  134. .then(res => {
  135. let time = res.data.expires_time - this.$Cache.time();
  136. this.$store.commit('LOGIN', {
  137. token: res.data.token,
  138. time: time
  139. });
  140. this.$emit('authColse', false);
  141. this.$emit('onLoadFun', res.data.userInfo);
  142. uni.hideLoading();
  143. })
  144. .catch(res => {
  145. console.log(res);
  146. uni.hideLoading();
  147. });
  148. },
  149. setUserInfo(e) {
  150. uni.showLoading({ title: '正在登陆中' });
  151. Routine.getCode()
  152. .then(code => {
  153. this.getUserPhoneNumber(e.detail.encryptedData, e.detail.iv, code);
  154. })
  155. .catch(res => {
  156. uni.hideLoading();
  157. });
  158. },
  159. getLogoUrl() {
  160. let that = this;
  161. if (Cache.has(LOGO_URL)) {
  162. this.logoUrl = Cache.get(LOGO_URL);
  163. return;
  164. }
  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: #e93323;
  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>