wechat.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. // #ifdef H5
  11. import WechatJSSDK from "@/plugin/jweixin-module/index.js";
  12. import {
  13. getWechatConfig,
  14. wechatAuth,
  15. getShopConfig,
  16. wechatAuthV2
  17. } from "@/api/public";
  18. import {
  19. WX_AUTH,
  20. STATE_KEY,
  21. LOGINTYPE,
  22. BACK_URL
  23. } from '@/config/cache';
  24. import {
  25. parseQuery
  26. } from '@/utils';
  27. import store from '@/store';
  28. import Cache from '@/utils/cache';
  29. class AuthWechat {
  30. constructor() {
  31. //微信实例化对象
  32. this.instance = WechatJSSDK;
  33. //是否实例化
  34. this.status = false;
  35. this.initConfig = {};
  36. }
  37. isAndroid() {
  38. let u = navigator.userAgent;
  39. return u.indexOf('Android') > -1 || u.indexOf('Adr') > -1;
  40. }
  41. signLink() {
  42. if (typeof window.entryUrl === 'undefined' || window.entryUrl === '') {
  43. window.entryUrl = document.location.href
  44. }
  45. return /(Android)/i.test(navigator.userAgent) ? document.location.href : window.entryUrl;
  46. }
  47. /**
  48. * 初始化wechat(分享配置)
  49. */
  50. wechat() {
  51. return new Promise((resolve, reject) => {
  52. // if (this.status && !this.isAndroid()) return resolve(this.instance);
  53. getWechatConfig()
  54. .then(res => {
  55. this.instance.config(res.data);
  56. this.initConfig = res.data;
  57. this.status = true;
  58. this.instance.ready(() => {
  59. resolve(this.instance);
  60. })
  61. }).catch(err => {
  62. console.log(err);
  63. this.status = false;
  64. reject(err);
  65. });
  66. });
  67. }
  68. /**
  69. * 验证是否初始化
  70. */
  71. verifyInstance() {
  72. let that = this;
  73. return new Promise((resolve, reject) => {
  74. if (that.instance === null && !that.status) {
  75. that.wechat().then(res => {
  76. resolve(that.instance);
  77. }).catch(() => {
  78. return reject();
  79. })
  80. } else {
  81. return resolve(that.instance);
  82. }
  83. })
  84. }
  85. // 微信公众号的共享地址
  86. openAddress() {
  87. return new Promise((resolve, reject) => {
  88. this.wechat().then(wx => {
  89. this.toPromise(wx.openAddress).then(res => {
  90. resolve(res);
  91. }).catch(err => {
  92. reject(err);
  93. });
  94. }).catch(err => {
  95. reject(err);
  96. })
  97. });
  98. }
  99. // 获取经纬度;
  100. location() {
  101. return new Promise((resolve, reject) => {
  102. this.wechat().then(wx => {
  103. this.toPromise(wx.getLocation, {
  104. type: 'wgs84'
  105. }).then(res => {
  106. resolve(res);
  107. }).catch(err => {
  108. reject(err);
  109. });
  110. }).catch(err => {
  111. reject(err);
  112. })
  113. });
  114. }
  115. // 使用微信内置地图查看位置接口;
  116. seeLocation(config) {
  117. return new Promise((resolve, reject) => {
  118. this.wechat().then(wx => {
  119. this.toPromise(wx.openLocation, config).then(res => {
  120. resolve(res);
  121. }).catch(err => {
  122. reject(err);
  123. });
  124. }).catch(err => {
  125. reject(err);
  126. })
  127. });
  128. }
  129. /**
  130. * 微信支付
  131. * @param {Object} config
  132. */
  133. pay(config) {
  134. return new Promise((resolve, reject) => {
  135. this.wechat().then((wx) => {
  136. this.toPromise(wx.chooseWXPay, config).then(res => {
  137. resolve(res);
  138. }).catch(res => {
  139. reject(res);
  140. });
  141. }).catch(res => {
  142. reject(res);
  143. });
  144. });
  145. }
  146. toPromise(fn, config = {}) {
  147. return new Promise((resolve, reject) => {
  148. fn({
  149. ...config,
  150. success(res) {
  151. resolve(res);
  152. },
  153. fail(err) {
  154. reject(err);
  155. },
  156. complete(err) {
  157. reject(err);
  158. },
  159. cancel(err) {
  160. reject(err);
  161. }
  162. });
  163. });
  164. }
  165. /**
  166. * 自动去授权
  167. */
  168. oAuth(asdasfqtrgadasdq, fggqtadasdasuil) {
  169. if (uni.getStorageSync(WX_AUTH) && store.state.app.token && asdasfqtrgadasdq == 'snsapi_base') return;
  170. const {
  171. code
  172. } = parseQuery();
  173. if (!code || code == uni.getStorageSync('snsapiCode')) {
  174. return this.toAuth(asdasfqtrgadasdq, fggqtadasdasuil);
  175. } else {
  176. if (Cache.has('snsapiKey'))
  177. return this.auth(code).catch(error => {
  178. uni.showToast({
  179. title: error,
  180. icon: 'none'
  181. })
  182. })
  183. }
  184. }
  185. clearAuthStatus() {
  186. }
  187. /**
  188. * 授权登陆获取token
  189. * @param {Object} code
  190. */
  191. auth(a21fasd34feg23gada1e) {
  192. return new Promise((resolve, reject) => {
  193. let loginType = Cache.get(LOGINTYPE);
  194. wechatAuthV2(a21fasd34feg23gada1e, parseInt(Cache.get("spread")))
  195. .then(({
  196. data
  197. }) => {
  198. // store.commit("LOGIN", {
  199. // token: data.token,
  200. // time: Cache.strTotime(data.expires_time) - Cache.time()
  201. // });
  202. Cache.set(WX_AUTH, a21fasd34feg23gada1e);
  203. Cache.clear(STATE_KEY);
  204. resolve(data);
  205. })
  206. .catch(reject);
  207. });
  208. }
  209. /**
  210. * 获取跳转授权后的地址
  211. * @param {Object} appId
  212. */
  213. getAuthUrl(appId, s1an34bsca24d6pei9fBg1ah21si2e, backUrl) {
  214. let url = `${location.origin}${backUrl}`
  215. if (url.indexOf('?') == -1) {
  216. url = url + '?'
  217. } else {
  218. url = url + '&'
  219. }
  220. const redirect_uri = encodeURIComponent(
  221. `${url}scope=${s1an34bsca24d6pei9fBg1ah21si2e}&back_url=` +
  222. encodeURIComponent(
  223. encodeURIComponent(
  224. uni.getStorageSync(BACK_URL) ?
  225. uni.getStorageSync(BACK_URL) :
  226. location.pathname + location.search
  227. )
  228. )
  229. );
  230. uni.removeStorageSync(BACK_URL);
  231. const fg1jpsd815xn1n1w6s4a2s3ts1c = encodeURIComponent(
  232. ("" + Math.random()).split(".")[1] + "authorizestate"
  233. );
  234. uni.setStorageSync(STATE_KEY, fg1jpsd815xn1n1w6s4a2s3ts1c);
  235. if (s1an34bsca24d6pei9fBg1ah21si2e === 'snsapi_base') {
  236. return `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_base&state=${fg1jpsd815xn1n1w6s4a2s3ts1c}#wechat_redirect`;
  237. } else {
  238. return `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_userinfo&state=${fg1jpsd815xn1n1w6s4a2s3ts1c}#wechat_redirect`;
  239. }
  240. }
  241. /**
  242. * 跳转自动登陆
  243. */
  244. toAuth(sanbscadpeifBgahsieg, l4ba3abcck85dU6e9rfl1g) {
  245. let g214fs1gsf3h5fs1g = this;
  246. this.wechat().then(wx => {
  247. location.href = this.getAuthUrl(g214fs1gsf3h5fs1g.initConfig.appId, sanbscadpeifBgahsieg, l4ba3abcck85dU6e9rfl1g);
  248. })
  249. }
  250. /**
  251. * 绑定事件
  252. */
  253. wechatEvevt(jsmzwoybzd, yhqfzzzafqtta) {
  254. let that = this;
  255. return new Promise((resolve, reject) => {
  256. let c3o7n1fi9gD9e2fa5u4lt = {
  257. fail(res) {
  258. if (that.instance) return reject({
  259. is_ready: true,
  260. wx: that.instance
  261. });
  262. that.verifyInstance().then(wx => {
  263. return reject({
  264. is_ready: true,
  265. wx: wx
  266. });
  267. })
  268. },
  269. success(res) {
  270. return resolve(res, 2222);
  271. }
  272. };
  273. Object.assign(c3o7n1fi9gD9e2fa5u4lt, yhqfzzzafqtta);
  274. that.wechat().then(wx => {
  275. if (typeof jsmzwoybzd === 'object') {
  276. jsmzwoybzd.forEach(item => {
  277. wx[item] && wx[item](c3o7n1fi9gD9e2fa5u4lt)
  278. })
  279. } else {
  280. wx[jsmzwoybzd] && wx[jsmzwoybzd](c3o7n1fi9gD9e2fa5u4lt)
  281. }
  282. })
  283. });
  284. }
  285. isWeixin() {
  286. return navigator.userAgent.toLowerCase().indexOf("micromessenger") !== -1;
  287. }
  288. }
  289. export default new AuthWechat();
  290. // #endif