const { api_wx_login } = require("../../utils/api.js"); const { net } = require("../../utils/net.js"); const util = require("../../utils/util.js"); Page({ data: { showState: "1", tel: "400-898-6659", name: "Hooli Partner", // 小程序名称 buttonWidth: 110, // 按钮宽度,单位px isSupportButtonApi: true, // agreePrivacyAuthorization api是否在当前版本可用 isAgreeUserAgreement: false, // 是否同意用户协议&隐私政策 }, pageData: { wxCode: "", }, onLoad(options) { // 判断 agreePrivacyAuthorization 是否在当前版本可用 if (wx.canIUse("button.open-type.agreePrivacyAuthorization")) { // 计算按钮内容区域宽度 this.getButtonContentWidth(); // 展示隐私协议授权按钮 this.setData({ isSupportButtonApi: true, }); } else { // 不展示隐私协议授权按钮,向低版本兼容 this.setData({ isSupportButtonApi: false, }); } let userInfo = util.getStorageSync("userInfo"); if (userInfo && userInfo.token) { wx.reLaunch({ url: "/pages/home/home", }); } else { this.setData({ showState: "2", }); wx.login({ success: (res) => { this.pageData.wxCode = res.code; }, }); } }, onReady() { wx.setNavigationBarTitle({ title: "登录", }); }, noLogin() { wx.reLaunch({ url: "/pages/home/home", }); }, // 用户未同意隐私协议之前点击登录 tapNoAgreeUserAgreement() { wx.showToast({ title: "请阅读并同意隐私协议", icon: "none", duration: 2000, }); }, // 计算Button按钮内容区域宽度 getButtonContentWidth() { wx.nextTick(() => { // wx.createSelectorQuery(); const query = wx.createSelectorQuery().in(this); const self = this; query .select("#buttonContent") .boundingClientRect((data) => { // 获取当前选择的节点的宽度 if (data && data.width) { self.setData({ buttonWidth: data.width + 20, }); } }) .exec(); }); }, // 查看用户协议 checkUserAgreement() { // 打开隐私协议页面 if (wx.openPrivacyContract) { wx.openPrivacyContract({ success: (e) => {}, // 打开成功 fail: (e) => { // 打开失败 wx.showToast({ title: "打开失败,请重试", icon: "none", duration: 2000, }); }, }); } }, // 点击用户协议区域按钮 handleClickUserAgreementButton(e) { this.setData({ isAgreeUserAgreement: !this.data.isAgreeUserAgreement, }); }, // 点击用户协议区域 handleClickUserAgreement(e) { if (!this.data.isSupportButtonApi) { this.setData({ isAgreeUserAgreement: !this.data.isAgreeUserAgreement, }); } }, getPhoneNumber(e) { let { encryptedData, iv } = e.detail; if (!encryptedData || !iv) { return false; } let goNext = () => { wx.login({ success: (res) => { let { code } = res; if (!code) { return false; } this.wxLogin(code, encryptedData, iv); }, }); }; wx.checkSession({ success: () => { if (this.pageData.wxCode) { this.wxLogin(this.pageData.wxCode, encryptedData, iv); } else { goNext(); } }, fail: () => { goNext(); }, }); }, wxLogin(code, encryptedData, iv) { let params = {}; params.url = api_wx_login; params.data = { code, encryptedData, iv, }; net.req(params, true).then( (res) => { let { data } = res; if (data && data.token) { wx.setStorageSync("userInfo", data); wx.reLaunch({ url: "/pages/home/home", }); return false; } wx.showModal({ title: "提示", content: `抱歉,您暂无登录权限,合作请咨询${this.data.tel}`, showCancel: false, }); }, (e) => { wx.showModal({ title: "提示", content: (e && e.msg) || `抱歉,您暂无登录权限,合作请咨询${this.data.tel}`, showCancel: false, }); } ); }, handleMobileLogin() { if (this.data.isAgreeUserAgreement) { // 同意隐私协议,跳转到手机号登录页面 wx.navigateTo({ url: "/pages/loginmobile/loginmobile", }); } else { // 不同意弹出提示 wx.showToast({ title: "请阅读并同意隐私协议", icon: "none", duration: 2000, }); } }, handleMakePhone() { let { tel } = this.data; let systemInfo = util.getStorageSync("systemInfo"); if (!systemInfo) { systemInfo = wx.getSystemInfoSync(); if (systemInfo) { wx.setStorageSync("systemInfo", systemInfo); } } let { system } = systemInfo; if (system.toLowerCase().indexOf("ios") !== -1) { wx.makePhoneCall({ phoneNumber: tel, success() {}, fail() {}, complete() {}, }); } else { wx.showActionSheet({ itemList: [tel], success(res) { if (res.tapIndex === 0) { wx.makePhoneCall({ phoneNumber: tel, success() {}, fail() {}, complete() {}, }); } }, }); } }, });