uniapp打包公众号h5

1、app.vue
<script>
	export default {
		onLaunch: function() {
			var token = uni.getStorageSync('token');
			if (token) {
				this.$isResolve();
			} else {
				// 公众号授权登录
				this.getCode();
			}
		},
		methods: {
            // 公众号授权登录
			getCode() {
				var _this = this;
				let link = window.location.href;
				console.log("==获取当前页面的链接==")
				console.log(link);

				// 截取code
				var code = _this.getUrlCode().code;

				// 如果有code就去登录,获取token;没有的话,跳转微信授权链接去获取
				if (code) {
					// 使用登录接口用code获取token
					uni.$u.http.get('/api/xxx/login', {
						params: {
							code: code,
						}
					}).then(res => {
						console.log("==登录成功==");
						console.log(res);
						_this.$isResolve();

						// 缓存token
						uni.setStorageSync('token', res.data.token);
					})
				} else {
					let appid = _this.$appid; // 微信公众号id
					let uri = encodeURIComponent(link);
					let authURL =
						`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${uri}&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect`;
					window.location.href = authURL;
				}
			},

			// 截取url中的code
			getUrlCode() {
				var _this = this;
				var url = location.search;
				var theRequest = new Object();
				if (url.indexOf("?") != -1) {
					var str = url.substr(1);
					var strs = str.split("&");
					for (var i = 0; i < strs.length; i++) {
						theRequest[strs[i].split("=")[0]] = strs[i].split("=")[1];
					}
				}
				// console.log(theRequest);
				return theRequest;
			},
		}
	}
</script>
二、main.js
import Vue from 'vue'
import App from './App'

// 引入配置文件
import config from '@/config/config.js'
Vue.prototype.$baseUrl = config.baseUrl;
Vue.prototype.$appid = config.appid; // 开发者id

// 解决地图跨域
import {VueJsonp} from 'vue-jsonp'
Vue.use(VueJsonp)

// 回调
Vue.prototype.$onLaunched = new Promise(resolve => {
    Vue.prototype.$isResolve = resolve;
})

Vue.config.productionTip = false
App.mpType = 'app'

const app = new Vue({
	...App
})

app.$mount()

三、h5跨域问题

打开manifest.json文件,左侧菜单,最下面的【源码视图】。

"h5" : {
        "publicPath" : "./",
        "devServer" : {
            "https" : true,
            "disableHostCheck" : true,
            "port" : 8080,
            "proxy" : {
                "/" : {
                    "target" : "网站域名",
                    "changeOrigin" : true,
                    "secure" : false,
                    "pathRewrite" : {
                        "^/" : ""
                    }
                }
            }
        },
        "sdkConfigs" : {
            "maps" : {
                "qqmap" : {
                    "key" : "腾讯地图key"
                }
            }
        },
        "template" : "",
        "router" : {
            "mode" : "hash",
            "base" : "/h5"
        },
        "domain" : "网站域名",
        "title" : "网站标题",
        "optimization" : {
            "treeShaking" : {
                "enable" : false
            }
        }
    },

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

 康 

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值