import Vue from "vue"; export default { data() { return { configAll: { disable_send_province: [], }, //公共配置 login_show: false, login_content: "美美号源将获取您的头像丶昵称丶手机号等信息为您提供优质的服务.", chanId: "", //站点id mixin_code: "", //授权code mixin_type: "", //小程序区别 }; }, onLaunch() { console.log("onLaunch"); }, onShow() { this.login_content = "美美号源将登录账号为您提供优质的服务."; //#ifdef MP-TOUTIAO this.mixin_type = "TOUTIAO"; this.login_content = "美美号源将获取您的头像丶昵称等信息为您提供优质的服务."; tt.showShareMenu({ menus: ["share", "record"], success(res) { // console.log("已成功显示转发按钮", res); }, fail(err) { // console.log("showShareMenu 调用失败", err.errMsg); }, complete(res) { // console.log("showShareMenu 调用完成", res); }, }); //#endif //#ifdef H5 this.mixin_type = "H5"; //#endif // console.log(this.mixin_type) // 除了H5 小程序需要登录 if(this.mixin_type!='H5'){ if(!uni.getStorageSync('token')){ this.login_show=true; this.getcode() }else{ // 有token 判断是否过期 Vue.prototype.$http.userIndex() .then(res=>{ if(res.statusCode==401){ uni.removeStorageSync('token'); uni.showToast({ title: '登录失效,请重新进入小程序.', duration: 2000, icon: 'none' }); const pages = getCurrentPages() // 声明一个pages使用getCurrentPages方法 const curPage = pages[pages.length - 1] // 声明一个当前页面 curPage.onLoad(curPage.options) // 传入参数 curPage.onShow() curPage.onReady() } }) } //快手分享 if (this.mixin_type != "TOUTIAO") { ks.showShareMenu({ success(res) { // console.log("已成功显示转发按钮", res); }, }); } } }, onLoad(option) { // if(option.chanId){ // this.chanId = option.chanId // uni.setStorageSync('chanId', this.chanId); // } // if(uni.getStorageSync('chanId')){ // this.chanId = uni.getStorageSync('chanId') // } // // 公共配置 this.commonConfig(); }, onHide() { // console.log('onHide') // uni.removeStorageSync('chanId'); }, onUnload() { // console.log('onUnload') // uni.removeStorageSync('chanId'); }, methods: { // 用于处理请求腾讯api 跨域问题 jsonp(url, data) { return new Promise((resolve, reject) => { // 1.初始化url let dataString = url.indexOf("?") === -1 ? "?" : "&"; let callbackName = `jsonpCB_${Date.now()}`; url += `${dataString}callback=${callbackName}`; if (data) { // 2.有请求参数,依次添加到url for (let k in data) { url += `&${k}=${data[k]}`; } } let scriptNode = document.createElement("script"); scriptNode.src = url; // 3. callback window[callbackName] = (result) => { result ? resolve(result) : reject("没有返回数据"); delete window[callbackName]; document.body.removeChild(scriptNode); }; // 4. 异常情况 scriptNode.addEventListener( "error", () => { reject("接口返回数据失败"); delete window[callbackName]; document.body.removeChild(scriptNode); }, false ); // 5. 开始请求 document.body.appendChild(scriptNode); }); }, // 快手 授权获取手机号登录 decryptPhoneNumber(e) { console.log(e.detail); Vue.prototype.$http .KuaishouLogin({ code: this.mixin_code, encryptedData: e.detail.encryptedData, iv: e.detail.iv, }) .then((res) => { if (res.data.code == 1) { uni.setStorageSync("token", res.data.data.userinfo.token); this.login_show = false; const pages = getCurrentPages(); // 声明一个pages使用getCurrentPages方法 const curPage = pages[pages.length - 1]; // 声明一个当前页面 curPage.onLoad(curPage.options); // 传入参数 curPage.onShow(); } else { uni.showToast({ title: res.data.msg, duration: 2000, icon: "none", }); uni.removeStorageSync("token"); } }); }, // 取消授权按钮呢 login_cancel() { console.log("取消"); this.login_show = false; // 抖音直接退出小程序 //#ifdef MP-TOUTIAO tt.exitMiniProgram({ success() { console.log("exited"); }, }); //#endif }, // 确认按钮 login_confirm() { // 用户登录 this.userLogin(); }, commonConfig() { Vue.prototype.$http.commonConfig().then((res) => { if (res.data.code == 1) { this.configAll = res.data.data; console.log(this.configAll); } }); }, getcode() { console.log("getcode"); let this_ = this; // 获取openid uni.getProvider({ service: "oauth", copes: "auth_user", success: function (res) { console.log(res.provider[0]); uni.login({ provider: res.provider[0], //这里服务商名是字节跳动小程序 所以填toutiao success: function (loginRes) { console.log(1231, loginRes); this_.mixin_code = loginRes.code; }, }); }, }); }, // 抖音登录 userLogin() { let this_ = this; // tt.getUserProfile({ // success(res) { // console.log("getUserProfile 调用成功:", res.userInfo); // }, // fail(res) { // console.log("getUserProfile 调用失败", res); // }, // }); if (uni.getUserProfile) { console.log("getUserProfile") uni.getUserProfile({ withCredentials:true, success(res) { console.log("login") console.log(res) Vue.prototype.$http.dy_login({code:this_.mixin_code,encryptedData:res.encryptedData,iv:res.iv,}) .then(res=>{ if(res.data.code==1){ this_.login_show=false uni.setStorageSync('token', res.data.data.userinfo.token); const pages = getCurrentPages() // 声明一个pages使用getCurrentPages方法 const curPage = pages[pages.length - 1] // 声明一个当前页面 curPage.onLoad(curPage.options) // 传入参数 curPage.onShow() }else{ this_.login_show=false uni.showToast({ title: res.data.msg, duration: 2000, icon: 'none' }); uni.removeStorageSync('token'); } }) }, fail(res){ console.log(res) } }) } else{ console.log('getUserInfo') uni.getUserInfo({ withCredentials:true, success(res) { console.log("login") console.log(res) Vue.prototype.$http.dy_login({code:this_.mixin_code,encryptedData:res.encryptedData,iv:res.iv,}) .then(res=>{ if(res.data.code==1){ this_.login_show=false uni.setStorageSync('token', res.data.data.userinfo.token); const pages = getCurrentPages() // 声明一个pages使用getCurrentPages方法 const curPage = pages[pages.length - 1] // 声明一个当前页面 curPage.onLoad(curPage.options) // 传入参数 curPage.onShow() }else{ this_.login_show=false uni.showToast({ title: res.data.msg, duration: 2000, icon: 'none' }); uni.removeStorageSync('token'); } }) }, fail(res){ console.log(res) uni.showToast({ title: "请点击右上角更多中选择设置授权头像和昵称", duration: 2000, icon: 'none' }); uni.removeStorageSync('token'); } }) } }, // 用于处理请求腾讯api 跨域问题 jsonp(url, data) { return new Promise((resolve, reject) => { // 1.初始化url let dataString = url.indexOf("?") === -1 ? "?" : "&"; let callbackName = `jsonpCB_${Date.now()}`; url += `${dataString}callback=${callbackName}`; if (data) { // 2.有请求参数,依次添加到url for (let k in data) { url += `&${k}=${data[k]}`; } } let scriptNode = document.createElement("script"); scriptNode.src = url; // 3. callback window[callbackName] = (result) => { result ? resolve(result) : reject("没有返回数据"); delete window[callbackName]; document.body.removeChild(scriptNode); }; // 4. 异常情况 scriptNode.addEventListener( "error", () => { reject("接口返回数据失败"); delete window[callbackName]; document.body.removeChild(scriptNode); }, false ); // 5. 开始请求 document.body.appendChild(scriptNode); }); }, }, };