wechat.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * Wechat v1.0.0
  3. * @Class Wechat
  4. * @description shopro-wechat 1.0.0 wehcat第三方登录组件
  5. * @Author llidongtony
  6. * @Date 2020-02-19
  7. * @Email lidongtony@qq.com
  8. */
  9. import store from '@/store/index.js'
  10. // import {
  11. // API_URL
  12. // } from '@/env'
  13. // #ifdef H5
  14. function checkHttps(){
  15. let ishttps = 'https:' == window.location.protocol ? true : false;
  16. return ishttps
  17. }
  18. let apiUrl = 'https://fd.joisen.net.cn/';
  19. let pathArr = apiUrl.split('://');
  20. let ishttps = checkHttps();
  21. ishttps ? (pathArr[0] = 'https') : (pathArr[0] = 'http');
  22. apiUrl = pathArr.join('://');
  23. // #endif
  24. export default class Wechat {
  25. async login() {
  26. // #ifdef H5
  27. this.wxOfficialAccountLogin();
  28. // #endif
  29. }
  30. // #ifdef H5
  31. wxOfficialAccountLogin() {
  32. // let oUrl = window.location.href;
  33. let oUrl = encodeURIComponent(window.location.href);
  34. window.location.replace('https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + store.state.appid +
  35. `&redirect_uri=${encodeURIComponent(`${apiUrl}h5/authen/login.html`)}&response_type=code&scope=snsapi_userinfo&state=` +
  36. oUrl)
  37. throw 'stop';
  38. }
  39. //临时登录获取OpenId 不入库不绑定用户
  40. wxOfficialAccountBaseLogin() {
  41. let oUrl = encodeURIComponent(window.location.href);
  42. window.location.replace('https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + store.appid +
  43. `&redirect_uri=${encodeURIComponent(`${apiUrl}h5/authen/login.html`)}&response_type=code&scope=snsapi_base&state=` +
  44. oUrl)
  45. throw 'stop';
  46. }
  47. // #endif
  48. // 小程序更新
  49. checkMiniProgramUpdate() {
  50. if (uni.canIUse('getUpdateManager')) {
  51. const updateManager = uni.getUpdateManager()
  52. updateManager.onCheckForUpdate(function(res) {
  53. // 请求完新版本信息的回调
  54. if (res.hasUpdate) {
  55. updateManager.onUpdateReady(function() {
  56. uni.showModal({
  57. title: '更新提示',
  58. content: '新版本已经准备好,是否重启应用?',
  59. success: function(res) {
  60. console.log('success====', res)
  61. // res: {errMsg: "showModal: ok", cancel: false, confirm: true}
  62. if (res.confirm) {
  63. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  64. updateManager.applyUpdate()
  65. }
  66. }
  67. })
  68. })
  69. updateManager.onUpdateFailed(function() {
  70. // 新的版本下载失败
  71. uni.showModal({
  72. title: '已经有新版本了哟~',
  73. content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~'
  74. })
  75. })
  76. }
  77. })
  78. }
  79. }
  80. }