routine.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. * 获取用户信息
  42. */
  43. getUserInfo() {
  44. let that = this,
  45. code = this.getUserCode();
  46. return new Promise((resolve, reject) => {
  47. uni.getUserInfo({
  48. lang: 'zh_CN',
  49. success(user) {
  50. if (code) user.code = code;
  51. resolve({
  52. userInfo: user,
  53. islogin: false
  54. });
  55. },
  56. fail(res) {
  57. reject(res);
  58. }
  59. })
  60. })
  61. }
  62. /**
  63. * 新版小程序获取用户信息 2021 4.13微信小程序开始正式启用
  64. */
  65. getUserProfile(code) {
  66. return new Promise((resolve, reject) => {
  67. uni.getUserProfile({
  68. lang: 'zh_CN',
  69. desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
  70. success(user) {
  71. if (code) user.code = code;
  72. resolve({
  73. userInfo: user,
  74. islogin: false
  75. });
  76. },
  77. fail(res) {
  78. reject(res);
  79. }
  80. })
  81. })
  82. }
  83. /**
  84. * 获取用户信息
  85. */
  86. authorize() {
  87. let that = this;
  88. return new Promise((resolve, reject) => {
  89. if (checkLogin())
  90. return resolve({
  91. userInfo: Cache.get(USER_INFO, true),
  92. islogin: true,
  93. });
  94. uni.authorize({
  95. scope: that.scopeUserInfo,
  96. success() {
  97. resolve({
  98. islogin: false
  99. });
  100. },
  101. fail(res) {
  102. reject(res);
  103. }
  104. })
  105. })
  106. }
  107. async getCode() {
  108. let provider = await this.getProvider();
  109. return new Promise((resolve, reject) => {
  110. // if(Cache.has(STATE_R_KEY)){
  111. // return resolve(Cache.get(STATE_R_KEY));
  112. // }
  113. uni.login({
  114. provider: provider,
  115. success(res) {
  116. if (res.code) Cache.set(STATE_R_KEY, res.code, 10800);
  117. return resolve(res.code);
  118. },
  119. fail() {
  120. return reject(null);
  121. }
  122. })
  123. })
  124. }
  125. /**
  126. * 获取服务供应商
  127. */
  128. getProvider() {
  129. return new Promise((resolve, reject) => {
  130. uni.getProvider({
  131. service: 'oauth',
  132. success(res) {
  133. resolve(res.provider);
  134. },
  135. fail() {
  136. resolve(false);
  137. }
  138. });
  139. });
  140. }
  141. /**
  142. * 是否授权
  143. */
  144. isAuth() {
  145. let that = this;
  146. return new Promise((resolve, reject) => {
  147. uni.getSetting({
  148. success(res) {
  149. if (!res.authSetting[that.scopeUserInfo]) {
  150. resolve(true)
  151. } else {
  152. resolve(true);
  153. }
  154. },
  155. fail() {
  156. resolve(false);
  157. }
  158. });
  159. });
  160. }
  161. /**
  162. * 小程序比较版本信息
  163. * @param v1 当前版本
  164. * @param v2 进行比较的版本
  165. * @return boolen
  166. *
  167. */
  168. compareVersion(v1, v2) {
  169. v1 = v1.split('.')
  170. v2 = v2.split('.')
  171. const len = Math.max(v1.length, v2.length)
  172. while (v1.length < len) {
  173. v1.push('0')
  174. }
  175. while (v2.length < len) {
  176. v2.push('0')
  177. }
  178. for (let i = 0; i < len; i++) {
  179. const num1 = parseInt(v1[i])
  180. const num2 = parseInt(v2[i])
  181. if (num1 > num2) {
  182. return 1
  183. } else if (num1 < num2) {
  184. return -1
  185. }
  186. }
  187. return 0
  188. }
  189. authUserInfo(data) {
  190. return new Promise((resolve, reject) => {
  191. routineLogin(data).then(res => {
  192. if (res.data.key !== undefined && res.data.key) {} else {
  193. store.commit('UPDATE_USERINFO', res.data.userInfo);
  194. store.commit('SETUID', res.data.userInfo.uid);
  195. Cache.set(USER_INFO, res.data.userInfo);
  196. }
  197. return resolve(res);
  198. }).catch(res => {
  199. return reject(res);
  200. })
  201. })
  202. }
  203. }
  204. export default new Routine();