Authorize.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <template>
  2. <view :style="viewColor">
  3. <view class='Popup' v-if='isShowAuth && code && showsq'>
  4. <!-- <view class="logo-auth">
  5. <image :src='routine_logo' mode="aspectFit"></image>
  6. </view> -->
  7. <view class='title'>登录</view>
  8. <view class='tip'>您未登录,是否马上登录?</view>
  9. <view class='bottom flex'>
  10. <view class='item' @click='close'>随便逛逛</view>
  11. <!-- #ifdef MP -->
  12. <!-- <button class="item grant" hover-class="none"
  13. @tap="getUserProfile">登录</button> -->
  14. <button class="item grant" hover-class="none" open-type="getPhoneNumber"
  15. @getphonenumber="getPhoneNumber">登录</button>
  16. <!-- #endif -->
  17. <!-- #ifdef H5 || APP-PLUS -->
  18. <button class="item grant"
  19. @tap="toWecahtAuth">登录</button>
  20. <!-- #endif -->
  21. </view>
  22. </view>
  23. <!-- #ifndef MP-WEIXIN -->
  24. <view class='mask' v-if='isShowAuth && code' @click='close'></view>
  25. <!-- #endif -->
  26. <!-- #ifdef MP-WEIXIN -->
  27. <view class='mask' v-if='isShowAuth && code && showsq' @click='close'></view>
  28. <!-- #endif -->
  29. </view>
  30. </template>
  31. <script>
  32. // +----------------------------------------------------------------------
  33. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  34. // +----------------------------------------------------------------------
  35. // | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
  36. // +----------------------------------------------------------------------
  37. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  38. // +----------------------------------------------------------------------
  39. // | Author: CRMEB Team <admin@crmeb.com>
  40. // +----------------------------------------------------------------------
  41. const app = getApp();
  42. import Cache from '../utils/cache';
  43. import {
  44. getLogo, commonAuth
  45. } from '../api/public';
  46. import { LOGO_URL, USER_INFO, EXPIRES_TIME} from '../config/cache';
  47. import { mapGetters } from 'vuex';
  48. import Routine from '../libs/routine';
  49. import { configMap } from '@/utils/index';
  50. import Auth from '../libs/wechat';
  51. import { toLogin } from '../libs/login';
  52. export default {
  53. name: 'Authorize',
  54. props: {
  55. isAuto: {
  56. type: Boolean,
  57. default: true
  58. },
  59. isGoIndex: {
  60. type: Boolean,
  61. default: true
  62. },
  63. isShowAuth: {
  64. type: Boolean,
  65. default: false
  66. }
  67. },
  68. data() {
  69. return {
  70. canUseGetUserProfile: false,
  71. code: null,
  72. showsq: true,
  73. isclick: false,
  74. iv1: '',
  75. encryptedData1: '',
  76. }
  77. },
  78. computed: {
  79. ...mapGetters(['isLogin', 'userInfo', 'viewColor']),
  80. ...configMap(['routine_logo'])
  81. },
  82. watch: {
  83. isLogin(n) {
  84. n === true && this.$emit('onLoadFun', this.userInfo);
  85. },
  86. isShowAuth(n) {
  87. this.getCode(this.isShowAuth)
  88. }
  89. },
  90. created() {
  91. if (wx.getUserProfile) {
  92. this.canUseGetUserProfile = true
  93. }
  94. this.setAuthStatus();
  95. this.getCode(this.isShowAuth)
  96. },
  97. methods: {
  98. //
  99. getPhoneNumber(e) {
  100. let self = this
  101. let obj = this
  102. console.log(e, 'e+++++++++++++++')
  103. //用户同意授权
  104. if (e.detail.errMsg == "getPhoneNumber:ok") {
  105. let userInfo = {}
  106. userInfo.iv = e.detail.iv
  107. userInfo.encryptedData = e.detail.encryptedData
  108. userInfo.code = this.code;
  109. userInfo.spread = app.globalData.spid; //获取推广人ID
  110. userInfo.spread_code = app.globalData.code; //获取推广人分享二维码ID
  111. console.log(userInfo,'userInfo+++++')
  112. commonAuth({
  113. auth: {
  114. type:'routine',
  115. auth: userInfo
  116. }
  117. }).then(res=>{
  118. if(res.data.status == 200){
  119. let time = res.data.result.expires_time - Cache.time();
  120. self.$store.commit('UPDATE_USERINFO', res.data.result.user);
  121. self.$store.commit('LOGIN', {token:res.data.result.token, time:time});
  122. self.$store.commit('SETUID', res.data.result.user.uid);
  123. Cache.set(EXPIRES_TIME,res.data.result.expires_time,time);
  124. Cache.set(USER_INFO,res.data.result.user,time);
  125. this.$emit('onLoadFun', res.data.result.user);
  126. }else{
  127. uni.setStorageSync('auth_token',res.data.result.key);
  128. return uni.navigateTo({
  129. url:'/pages/users/login/index'
  130. })
  131. }
  132. }).catch(res => {
  133. uni.hideLoading();
  134. uni.showToast({
  135. title: res.message,
  136. icon: 'none',
  137. duration: 2000,
  138. });
  139. });
  140. } else {
  141. obj.isclick = false
  142. uni.showToast({
  143. title: '您拒绝了请求,不能正常使用小程序',
  144. icon: 'error',
  145. duration: 2000
  146. });
  147. return;
  148. }
  149. },
  150. setAuthStatus() {
  151. //#ifdef MP
  152. Routine.authorize().then(res => {
  153. if (res.islogin === false)
  154. this.$emit('onLoadFun', this.userInfo);
  155. }).catch(res => {
  156. if (this.isAuto)
  157. this.$emit('authColse', true);
  158. })
  159. //#endif
  160. },
  161. getCode(n){
  162. // #ifdef MP
  163. if (n) {
  164. uni.showLoading({
  165. title: '正在登录中'
  166. });
  167. Routine.getCode().then(code => {
  168. uni.hideLoading();
  169. this.code = code;
  170. }).catch(e => {
  171. uni.hideLoading();
  172. uni.showToast({
  173. title: '登录失败',
  174. duration: 2000
  175. });
  176. })
  177. } else {
  178. this.code = null;
  179. }
  180. // #endif
  181. // #ifndef MP
  182. if(n){
  183. this.code = 1;
  184. }
  185. // #endif
  186. },
  187. toWecahtAuth(){
  188. toLogin(true);
  189. },
  190. getUserProfile() {
  191. let self = this;
  192. Routine.getUserProfile()
  193. .then(res => {
  194. let userInfo = res.userInfo;
  195. userInfo.code = this.code;
  196. userInfo.spread = app.globalData.spid; //获取推广人ID
  197. userInfo.spread_code = app.globalData.code; //获取推广人分享二维码ID
  198. commonAuth({
  199. auth: {
  200. type:'routine',
  201. auth: userInfo
  202. }
  203. }).then(res=>{
  204. if(res.data.status == 200){
  205. let time = res.data.result.expires_time - Cache.time();
  206. self.$store.commit('UPDATE_USERINFO', res.data.result.user);
  207. self.$store.commit('LOGIN', {token:res.data.result.token, time:time});
  208. self.$store.commit('SETUID', res.data.result.user.uid);
  209. Cache.set(EXPIRES_TIME,res.data.result.expires_time,time);
  210. Cache.set(USER_INFO,res.data.result.user,time);
  211. this.$emit('onLoadFun', res.data.result.user);
  212. }else{
  213. uni.setStorageSync('auth_token',res.data.result.key);
  214. return uni.navigateTo({
  215. url:'/pages/users/login/index'
  216. })
  217. }
  218. }).catch(res => {
  219. uni.hideLoading();
  220. uni.showToast({
  221. title: res.message,
  222. icon: 'none',
  223. duration: 2000,
  224. });
  225. });
  226. })
  227. .catch(res => {
  228. uni.hideLoading();
  229. });
  230. },
  231. close() {
  232. let pages = getCurrentPages(),
  233. currPage = pages[pages.length - 1];
  234. this.$emit('authColse', false);
  235. // if (this.isGoIndex) {
  236. // uni.switchTab({
  237. // url: '/pages/index/index'
  238. // });
  239. // } else {
  240. // this.$emit('authColse', false);
  241. // }
  242. },
  243. // 用户确认授权
  244. userInfoData() {
  245. let that = this
  246. if (that.isclick) {
  247. return
  248. }
  249. that.isclick = true
  250. wx.getUserProfile({
  251. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  252. success: res => {
  253. console.log(res, 'that.userInfo+++++++++++++++++++')
  254. that.userInfo = res;
  255. uni.showLoading({
  256. title: '授权中',
  257. mask: true
  258. });
  259. that.loadMp();
  260. },
  261. fail: err => {
  262. that.isclick = false
  263. uni.showToast({
  264. title: '您拒绝了请求,不能正常使用小程序',
  265. icon: 'error',
  266. duration: 2000
  267. });
  268. return;
  269. }
  270. });
  271. // this.userInfo = e;
  272. // console.log(e,'用户确认授权')
  273. // this.loadMp()
  274. },
  275. }
  276. }
  277. </script>
  278. <style scoped lang='scss'>
  279. .Popup {
  280. width: 500rpx;
  281. background-color: #fff;
  282. position: fixed;
  283. top: 50%;
  284. left: 50%;
  285. margin-left: -250rpx;
  286. transform: translateY(-50%);
  287. z-index: 1000;
  288. }
  289. .Popup {
  290. .logo-auth {
  291. z-index: -1;
  292. position: absolute;
  293. left: 50%;
  294. top: 0%;
  295. transform: translate(-50%, -50%);
  296. width: 150rpx;
  297. height: 150rpx;
  298. display: flex;
  299. align-items: center;
  300. justify-content: center;
  301. border: 8rpx solid #fff;
  302. border-radius: 50%;
  303. background: #fff;
  304. }
  305. image {
  306. height: 42rpx;
  307. margin-top: -54rpx;
  308. }
  309. }
  310. .Popup .title {
  311. font-size: 28rpx;
  312. color: #000;
  313. text-align: center;
  314. margin-top: 30rpx
  315. }
  316. .Popup .tip {
  317. font-size: 22rpx;
  318. color: #555;
  319. padding: 0 24rpx;
  320. margin-top: 25rpx;
  321. }
  322. .Popup .bottom .item {
  323. width: 50%;
  324. height: 80rpx;
  325. background-color: #eeeeee;
  326. text-align: center;
  327. line-height: 80rpx;
  328. font-size: 24rpx;
  329. color: #666;
  330. margin-top: 54rpx;
  331. }
  332. .Popup .bottom .item.on {
  333. width: 100%
  334. }
  335. .flex {
  336. display: flex;
  337. }
  338. .Popup .bottom .item.grant {
  339. font-size: 28rpx;
  340. color: #fff;
  341. font-weight: bold;
  342. background-color: var(--view-theme);
  343. border-radius: 0;
  344. padding: 0;
  345. }
  346. .mask {
  347. position: fixed;
  348. top: 0;
  349. right: 0;
  350. left: 0;
  351. bottom: 0;
  352. background-color: rgba(0, 0, 0, 0.65);
  353. z-index: 999;
  354. }
  355. .popupsq-wrap {
  356. width: 550rpx;
  357. height: 450rpx;
  358. background-color: #fff;
  359. border-radius: 20rpx;
  360. padding: 0 30rpx;
  361. .btn {
  362. width: 240rpx;
  363. height: 90rpx;
  364. text-align: center;
  365. line-height: 90rpx;
  366. font-size: 30rpx;
  367. border-radius: 45rpx;
  368. font-weight: bold;
  369. }
  370. .qx {
  371. color: #000;
  372. border: #f0f0f0 1px solid;
  373. }
  374. .qr {
  375. color: #fff;
  376. background-color: #637bef;
  377. }
  378. }
  379. </style>