Authorize.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <template>
  2. <view>
  3. <view class='mask' v-if='isShowAuth && code' @click='close'></view>
  4. <view class='Popup' v-if='isShowAuth && code' :style="'top:'+top+'px;'">
  5. <view class="logo-auth">
  6. <image class="image" :src='routine_logo' mode="aspectFit"></image>
  7. </view>
  8. <!--#ifdef H5-->
  9. <text v-if="isWeixin" class='title'>授权提醒</text>
  10. <text v-else class='title'>{{title}}</text>
  11. <!--#endif-->
  12. <!--#ifndef H5-->
  13. <text class='title'>{{title}}</text>
  14. <!--#endif-->
  15. <!--#ifdef H5-->
  16. <text v-if="isWeixin" class='tip'>请授权头像等信息,以便为您提供更好的服务!</text>
  17. <text v-else class='tip'>{{info}}</text>
  18. <!--#endif-->
  19. <!--#ifndef H5-->
  20. <text class='tip'>{{info}}</text>
  21. <!--#endif-->
  22. <view class='bottom flex'>
  23. <text class='item' @click='close'>随便逛逛</text>
  24. <!-- #ifdef MP -->
  25. <button class="item grant" hover-class="none"
  26. @tap="getUserProfile"><text class="text">去授权</text></button>
  27. <!-- #endif -->
  28. <!-- #ifdef APP-PLUS -->
  29. <button class="item grant" @tap="toWecahtAuth">
  30. <text class="text">去登录</text>
  31. </button>
  32. <!-- #endif -->
  33. <!-- #ifdef H5 -->
  34. <button class="item grant" @tap="toWecahtAuth">
  35. <text v-if="isWeixin" class="text">去授权</text>
  36. <text v-else class="text">去登录</text>
  37. </button>
  38. <!-- #endif -->
  39. </view>
  40. </view>
  41. <!-- #ifdef MP -->
  42. <editUserModal :isShow="editModal" @closeEdit="closeEdit" @editSuccess="editSuccess"></editUserModal>
  43. <!-- #endif -->
  44. </view>
  45. </template>
  46. <script>
  47. // +----------------------------------------------------------------------
  48. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  49. // +----------------------------------------------------------------------
  50. // | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
  51. // +----------------------------------------------------------------------
  52. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  53. // +----------------------------------------------------------------------
  54. // | Author: CRMEB Team <admin@crmeb.com>
  55. // +----------------------------------------------------------------------
  56. const app = getApp();
  57. import Cache from '../utils/cache';
  58. import {
  59. getLogo, commonAuth
  60. } from '../api/public';
  61. import { LOGO_URL, USER_INFO, EXPIRES_TIME} from '../config/cache';
  62. import { mapGetters } from 'vuex';
  63. import Routine from '../libs/routine';
  64. import { configMap } from '@/utils/index';
  65. import Auth from '../libs/wechat';
  66. import { toLogin } from '../libs/login';
  67. // #ifdef MP
  68. import editUserModal from '@/components/eidtUserModal/index.vue'
  69. // #endif
  70. export default {
  71. name: 'Authorize',
  72. props: {
  73. isAuto: {
  74. type: Boolean,
  75. default: true
  76. },
  77. isGoIndex: {
  78. type: Boolean,
  79. default: true
  80. },
  81. isShowAuth: {
  82. type: Boolean,
  83. default: false
  84. }
  85. },
  86. components: {
  87. // #ifdef MP
  88. editUserModal
  89. // #endif
  90. },
  91. data() {
  92. return {
  93. title: '用户登录',
  94. info: '请登录,将为您提供更好的服务!',
  95. //#ifdef H5
  96. isWeixin: this.$wechat.isWeixin(),
  97. //#endif
  98. //#ifdef MP
  99. title: '授权提醒',
  100. info: '请授权头像等信息,以便为您提供更好的服务!',
  101. //#endif
  102. canUseGetUserProfile: false,
  103. code: null,
  104. top: 0,
  105. mp_is_new: this.$Cache.get('MP_VERSION_ISNEW') || false,
  106. editModal: false, // 编辑头像信息
  107. }
  108. },
  109. computed: {
  110. ...mapGetters(['isLogin', 'userInfo', 'viewColor']),
  111. ...configMap(['routine_logo'])
  112. },
  113. watch: {
  114. isLogin(n) {
  115. n === true && this.$emit('onLoadFun', this.userInfo);
  116. },
  117. isShowAuth(n) {
  118. this.getCode(this.isShowAuth)
  119. }
  120. },
  121. created() {
  122. this.top = uni.getSystemInfoSync().windowHeight/2-70
  123. if (wx.getUserProfile) {
  124. this.canUseGetUserProfile = true
  125. }
  126. this.setAuthStatus();
  127. this.getCode(this.isShowAuth)
  128. },
  129. methods: {
  130. // #ifdef MP
  131. editSuccess() {
  132. this.editModal = false
  133. this.$emit('onLoadFun', this.userInfo);
  134. },
  135. closeEdit() {
  136. this.editModal = false
  137. },
  138. // #endif
  139. setAuthStatus() {
  140. //#ifdef MP
  141. Routine.authorize().then(res => {
  142. if (res.islogin === false)
  143. this.$emit('onLoadFun', this.userInfo);
  144. }).catch(res => {
  145. if (this.isAuto)
  146. this.$emit('authColse', true);
  147. })
  148. //#endif
  149. },
  150. getCode(n){
  151. // #ifdef MP
  152. if (n) {
  153. uni.showLoading({
  154. title: '正在登录中'
  155. });
  156. Routine.getCode().then(code => {
  157. uni.hideLoading();
  158. this.code = code;
  159. }).catch(e => {
  160. uni.hideLoading();
  161. uni.showToast({
  162. title: '登录失败',
  163. duration: 2000
  164. });
  165. })
  166. } else {
  167. this.code = null;
  168. }
  169. // #endif
  170. // #ifndef MP
  171. if(n){
  172. this.code = 1;
  173. }
  174. // #endif
  175. },
  176. toWecahtAuth(){
  177. toLogin(true);
  178. },
  179. getUserProfile() {
  180. let self = this;
  181. Routine.getUserProfile()
  182. .then(res => {
  183. let userInfo = res.userInfo;
  184. userInfo.code = this.code;
  185. userInfo.spread = app.globalData.spid; //获取推广人ID
  186. userInfo.spread_code = app.globalData.code; //获取推广人分享二维码ID
  187. commonAuth({
  188. auth: {
  189. type:'routine',
  190. auth: userInfo
  191. }
  192. }).then(res=>{
  193. if(res.data.status == 200){
  194. let time = res.data.result.expires_time - Cache.time();
  195. self.$store.commit('UPDATE_USERINFO', res.data.result.user);
  196. self.$store.commit('LOGIN', {token:res.data.result.token, time:time});
  197. self.$store.commit('SETUID', res.data.result.user.uid);
  198. Cache.set(EXPIRES_TIME,res.data.result.expires_time,time);
  199. Cache.set(USER_INFO,res.data.result.user,time);
  200. this.$emit('onLoadFun', res.data.result.user);
  201. if(res.data.result.user.isNew && this.mp_is_new){
  202. this.editModal = true;
  203. }
  204. }else{
  205. uni.setStorageSync('auth_token',res.data.result.key);
  206. return uni.navigateTo({
  207. url:'/pages/users/login/index'
  208. })
  209. }
  210. }).catch(res => {
  211. uni.hideLoading();
  212. uni.showToast({
  213. title: res.message,
  214. icon: 'none',
  215. duration: 2000,
  216. });
  217. });
  218. })
  219. .catch(res => {
  220. uni.hideLoading();
  221. });
  222. },
  223. close() {
  224. let pages = getCurrentPages(),
  225. currPage = pages[pages.length - 1];
  226. this.$emit('authColse', false);
  227. // if (this.isGoIndex) {
  228. // uni.switchTab({
  229. // url: '/pages/index/index'
  230. // });
  231. // } else {
  232. // this.$emit('authColse', false);
  233. // }
  234. },
  235. }
  236. }
  237. </script>
  238. <style scoped lang='scss'>
  239. .Popup {
  240. flex: 1;
  241. align-items: center;
  242. justify-content: center;
  243. width: 500rpx;
  244. background-color: #fff;
  245. position: fixed;
  246. top: 500rpx;
  247. left: 125rpx;
  248. z-index: 1000;
  249. }
  250. .Popup {
  251. .logo-auth {
  252. z-index: -1;
  253. position: absolute;
  254. left: 50%;
  255. top: 0%;
  256. transform: translate(-50%, -50%);
  257. width: 150rpx;
  258. height: 150rpx;
  259. display: flex;
  260. align-items: center;
  261. justify-content: center;
  262. border: 8rpx solid #fff;
  263. border-radius: 50%;
  264. background: #fff;
  265. }
  266. .image {
  267. height: 42rpx;
  268. margin-top: -54rpx;
  269. }
  270. }
  271. .Popup .title {
  272. font-size: 28rpx;
  273. color: #000;
  274. text-align: center;
  275. margin-top: 30rpx;
  276. align-items: center;
  277. justify-content: center;
  278. width: 500rpx;
  279. display: flex;
  280. }
  281. .Popup .tip {
  282. font-size: 22rpx;
  283. color: #555;
  284. padding: 0 24rpx;
  285. margin-top: 25rpx;
  286. display: flex;
  287. align-items: center;
  288. justify-content: center;
  289. }
  290. .Popup .bottom .item {
  291. width: 250rpx;
  292. height: 80rpx;
  293. background-color: #eeeeee;
  294. text-align: center;
  295. line-height: 80rpx;
  296. margin-top: 54rpx;
  297. font-size: 24rpx;
  298. color: #666;
  299. .text{
  300. font-size: 24rpx;
  301. color: #666;
  302. }
  303. }
  304. .Popup .bottom .item.on {
  305. width: 500rpx;
  306. }
  307. .flex {
  308. display: flex;
  309. flex-direction: row;
  310. }
  311. .Popup .bottom .item.grant {
  312. font-weight: bold;
  313. background-color: #E93323;
  314. /* background-color: var(--view-theme); */
  315. border-radius: 0;
  316. padding: 0;
  317. .text{
  318. font-size: 28rpx;
  319. color: #fff;
  320. }
  321. }
  322. .mask {
  323. position: fixed;
  324. top: 0;
  325. right: 0;
  326. left: 0;
  327. bottom: 0;
  328. background-color: rgba(0, 0, 0, 0.65);
  329. z-index: 99;
  330. }
  331. </style>