login.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // +----------------------------------------------------------------------
  2. // | likeshop开源商城系统
  3. // +----------------------------------------------------------------------
  4. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  5. // | gitee下载:https://gitee.com/likeshop_gitee
  6. // | github下载:https://github.com/likeshop-github
  7. // | 访问官网:https://www.likeshop.cn
  8. // | 访问社区:https://home.likeshop.cn
  9. // | 访问手册:http://doc.likeshop.cn
  10. // | 微信公众号:likeshop技术社区
  11. // | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
  12. // | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
  13. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  14. // | likeshop团队版权所有并拥有最终解释权
  15. // +----------------------------------------------------------------------
  16. // | author: likeshop.cn.team
  17. // +----------------------------------------------------------------------
  18. import {
  19. silentLogin
  20. } from '@/api/app';
  21. import {
  22. isWeixinClient,
  23. currentPage,
  24. trottle
  25. } from './tools'
  26. import store from '@/store'
  27. import Cache from './cache'
  28. import {
  29. BACK_URL,
  30. INVITE_CODE
  31. } from '@/config/cachekey'
  32. import {bindSuperior} from '@/api/user'
  33. import wechath5 from './wechath5'
  34. import {router} from '@/router'
  35. // 获取登录凭证(code)
  36. export function getWxCode() {
  37. return new Promise((resolve, reject) => {
  38. uni.login({
  39. success(res) {
  40. resolve(res.code);
  41. },
  42. fail(res) {
  43. reject(res);
  44. }
  45. });
  46. });
  47. }
  48. //小程序获取用户信息
  49. export function getUserProfile() {
  50. return new Promise((resolve, reject) => {
  51. uni.getUserProfile({
  52. desc: '获取用户信息,完善用户资料 ',
  53. success: (res) => {
  54. resolve(res);
  55. },
  56. fail(res) {}
  57. })
  58. })
  59. }
  60. //小程序静默授权
  61. export async function wxMnpLogin() {
  62. const code = await getWxCode()
  63. const {
  64. code: loginCode,
  65. data: loginData
  66. } = await silentLogin({
  67. code
  68. })
  69. const {
  70. options,
  71. onLoad,
  72. onShow,
  73. route
  74. } = currentPage()
  75. if (loginCode != 1) return
  76. if (loginData.token) {
  77. store.commit('login', loginData)
  78. // 绑定邀请码
  79. const inviteCode = Cache.get(INVITE_CODE)
  80. if (inviteCode) {
  81. bindSuperior({code: inviteCode})
  82. Cache.remove(INVITE_CODE)
  83. }
  84. // 刷新页面
  85. onLoad && onLoad(options)
  86. onShow && onShow()
  87. }
  88. }
  89. export const toLogin = trottle(_toLogin, 1000)
  90. function _toLogin() {
  91. //#ifdef MP-WEIXIN
  92. wxMnpLogin()
  93. // #endif
  94. //#ifndef MP-WEIXIN
  95. const {currentRoute} = router
  96. if(currentRoute.meta.auth) {
  97. router.push('/pages/login/login')
  98. }
  99. // #endif
  100. }