routine.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // +----------------------------------------------------------------------
  2. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  3. // +----------------------------------------------------------------------
  4. // | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
  5. // +----------------------------------------------------------------------
  6. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  7. // +----------------------------------------------------------------------
  8. // | Author: CRMEB Team <admin@crmeb.com>
  9. // +----------------------------------------------------------------------
  10. import store from '../store';
  11. import {
  12. checkLogin
  13. } from './login';
  14. import {
  15. login,
  16. routineLogin,
  17. silenceAuth
  18. } from '../api/public';
  19. import Cache from '../utils/cache';
  20. import {
  21. STATE_R_KEY,
  22. USER_INFO,
  23. EXPIRES_TIME,
  24. LOGIN_STATUS
  25. } from './../config/cache';
  26. import {
  27. mapGetters
  28. } from "vuex";
  29. class Routine {
  30. constructor() {
  31. this.scopeUserInfo = 'scope.userInfo';
  32. }
  33. async getUserCode() {
  34. let isAuth = await this.isAuth(),
  35. code = '';
  36. if (isAuth)
  37. code = await this.getCode();
  38. return code;
  39. }
  40. // 小程序静默授权
  41. silenceAuth(code) {
  42. const app = getApp();
  43. let that = this;
  44. let spread = app.globalData.spid ? app.globalData.spid : '';
  45. return new Promise((resolve, reject) => {
  46. silenceAuth({
  47. code: code,
  48. spread_spid: spread,
  49. spread_code: app.globalData.code
  50. })
  51. .then(res => {
  52. if (res.data && res.data.token !== undefined) {
  53. uni.hideLoading();
  54. let time = res.data.expires_time - Math.round(new Date() / 1000);
  55. store.commit('LOGIN', {
  56. token: res.data.token,
  57. time: time
  58. });
  59. store.commit('SETUID', res.data.userInfo.uid);
  60. store.commit('UPDATE_USERINFO', res.data.userInfo);
  61. resolve(res)
  62. } else {
  63. reject()
  64. uni.navigateTo({
  65. url: '/pages/users/wechat_login/index'
  66. })
  67. }
  68. })
  69. .catch(err => {
  70. reject(err)
  71. });
  72. })
  73. }
  74. /**
  75. * 获取用户信息
  76. */
  77. getUserInfo() {
  78. let that = this,
  79. code = this.getUserCode();
  80. return new Promise((resolve, reject) => {
  81. uni.getUserInfo({
  82. lang: 'zh_CN',
  83. success(user) {
  84. if (code) user.code = code;
  85. resolve({
  86. userInfo: user,
  87. islogin: false
  88. });
  89. },
  90. fail(res) {
  91. reject(res);
  92. }
  93. })
  94. })
  95. }
  96. /**
  97. * 新版小程序获取用户信息 2021 4.13微信小程序开始正式启用
  98. */
  99. getUserProfile(code) {
  100. return new Promise((resolve, reject) => {
  101. uni.getUserProfile({
  102. lang: 'zh_CN',
  103. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  104. success(user) {
  105. if (code) user.code = code;
  106. resolve({
  107. userInfo: user,
  108. islogin: false
  109. });
  110. },
  111. fail(res) {
  112. reject(res);
  113. }
  114. })
  115. })
  116. }
  117. /**
  118. * 获取用户信息
  119. */
  120. authorize() {
  121. let that = this;
  122. return new Promise((resolve, reject) => {
  123. if (checkLogin())
  124. return resolve({
  125. userInfo: Cache.get(USER_INFO, true),
  126. islogin: true,
  127. });
  128. uni.authorize({
  129. scope: that.scopeUserInfo,
  130. success() {
  131. resolve({
  132. islogin: false
  133. });
  134. },
  135. fail(res) {
  136. reject(res);
  137. }
  138. })
  139. })
  140. }
  141. async getCode() {
  142. let provider = await this.getProvider();
  143. return new Promise((resolve, reject) => {
  144. // if(Cache.has(STATE_R_KEY)){
  145. // return resolve(Cache.get(STATE_R_KEY));
  146. // }
  147. uni.login({
  148. provider: provider,
  149. success(res) {
  150. if (res.code) Cache.set(STATE_R_KEY, res.code, 10800);
  151. return resolve(res.code);
  152. },
  153. fail() {
  154. return reject(null);
  155. }
  156. })
  157. })
  158. }
  159. /**
  160. * 获取服务供应商
  161. */
  162. getProvider() {
  163. return new Promise((resolve, reject) => {
  164. uni.getProvider({
  165. service: 'oauth',
  166. success(res) {
  167. resolve(res.provider);
  168. },
  169. fail() {
  170. resolve(false);
  171. }
  172. });
  173. });
  174. }
  175. /**
  176. * 是否授权
  177. */
  178. isAuth() {
  179. let that = this;
  180. return new Promise((resolve, reject) => {
  181. uni.getSetting({
  182. success(res) {
  183. if (!res.authSetting[that.scopeUserInfo]) {
  184. resolve(true)
  185. } else {
  186. resolve(true);
  187. }
  188. },
  189. fail() {
  190. resolve(false);
  191. }
  192. });
  193. });
  194. }
  195. authUserInfo(data) {
  196. return new Promise((resolve, reject) => {
  197. routineLogin(data).then(res => {
  198. if (res.data.key !== undefined && res.data.key) {} else {
  199. store.commit('UPDATE_USERINFO', res.data.userInfo);
  200. store.commit('SETUID', res.data.userInfo.uid);
  201. Cache.set(USER_INFO, res.data.userInfo);
  202. }
  203. return resolve(res);
  204. }).catch(res => {
  205. return reject(res);
  206. })
  207. })
  208. }
  209. }
  210. export default new Routine();