routine.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // +----------------------------------------------------------------------
  2. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  3. // +----------------------------------------------------------------------
  4. // | Copyright (c) 2016~2023 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. /**
  196. * 小程序比较版本信息
  197. * @param v1 当前版本
  198. * @param v2 进行比较的版本
  199. * @return boolen
  200. *
  201. */
  202. compareVersion(v1, v2) {
  203. v1 = v1.split('.')
  204. v2 = v2.split('.')
  205. const len = Math.max(v1.length, v2.length)
  206. while (v1.length < len) {
  207. v1.push('0')
  208. }
  209. while (v2.length < len) {
  210. v2.push('0')
  211. }
  212. for (let i = 0; i < len; i++) {
  213. const num1 = parseInt(v1[i])
  214. const num2 = parseInt(v2[i])
  215. if (num1 > num2) {
  216. return 1
  217. } else if (num1 < num2) {
  218. return -1
  219. }
  220. }
  221. return 0
  222. }
  223. authUserInfo(data) {
  224. return new Promise((resolve, reject) => {
  225. routineLogin(data).then(res => {
  226. if (res.data.key !== undefined && res.data.key) {} else {
  227. store.commit('UPDATE_USERINFO', res.data.userInfo);
  228. store.commit('SETUID', res.data.userInfo.uid);
  229. Cache.set(USER_INFO, res.data.userInfo);
  230. }
  231. return resolve(res);
  232. }).catch(res => {
  233. return reject(res);
  234. })
  235. })
  236. }
  237. }
  238. export default new Routine();