123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- // pages/login/login.js
- const app = getApp()
- const $request = require('../../utils/request.js');
- Page({
-
- /**
- * 页面的初始数据
- */
- data: {
-
- userInfo: null,
- // 是否获取到了用户信息
- hasUserInfo: false,
- },
-
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function (options) {
- const userInfo = wx.getStorageSync('userInfo') || null;
- if (app.globalData.userInfo || userInfo) {
- this.setData({
- userInfo
- })
- }
- },
-
- // 获取手机号授权
- getPhoneNumber(e) {
- console.log(e);
- if (!!e.detail.iv && !!e.detail.encryptedData) {
- const iv = e.detail.iv
- const encryptedData = e.detail.encryptedData
- this.appletLogin(iv, encryptedData)
- } else {
- wx.showToast({
- title: '请允许授权',
- icon: "none",
- duration: 3000
- })
- }
- },
- // 保存用户信息
- appletLogin(iv, encryptedData) {
- wx.showLoading({
- title: '登录中...',
- mask: true
- })
- let { nickName, gender, country, city, province, avatarUrl } = this.data.userInfo;
- $request.get('/com/appletLogin.action', {
- // openid: app.globalData.openid,
- iv: iv,
- encryptedData: encryptedData,
- sessionKey: app.globalData.sessionKey,
- loginState: "需求对对碰",
- avatarUrl,
- gender,
- nickName,
- country,
- province,
- city
- }).then(res => {
- console.log(res, '保存用户信息返回的Res')
- this.getUserSession()
- }).catch(error => {
- console.log(error, 'error appletLogin')
- })
- },
- // 获取用户信息弹框
- onCheckboxChange() {
- if (!this.data.hasUserInfo) {
- if (!this.data.userInfo) {
- this.getUserProfile()
- } else {
- this.setData({
- hasUserInfo: true
- })
- }
- } else {
- this.setData({
- hasUserInfo: false
- })
- }
- },
- // 获取用户信息
- getUserProfile(e) {
- // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认
- // 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
- wx.getUserProfile({
- desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
- lang:"zh_CN",
- success: (res) => {
- this.setData({
- userInfo: res.userInfo,
- hasUserInfo: true
- })
- wx.setStorageSync('userInfo', res.userInfo)
- },
- fail: () => {
- wx.showToast({
- title: '请允许授权',
- icon: "none",
- duration: 3000
- })
- }
- })
- },
- // 保存个人信息后拿到新数据
- getUserSession() {
- let that = this
- $request.post('/script/getSession.action', {}).then(res => {
- wx.hideLoading()
- console.log(res, '返回的res')
- app.globalData.customerId = res.data.customer.customerId;
- app.globalData.customer = res.data.customer;
- app.globalData.businessCommunicationCustomer = res.data.businessCommunicationCustomer;
- let pages = getCurrentPages();
- let Page = pages[pages.length - 1];//当前页
- let prevPage = pages[pages.length - 2]; //上一个页面
- let info = prevPage.data //取上页data里的数据也可以修改
- // 如果是上一页是我的页面
- if (prevPage.route == "pages/myModule/index") {
- let businessCommunicationCustomer = res.data.businessCommunicationCustomer;
- if (res.data.customer.paidByMonth) {
- businessCommunicationCustomer.nickName = res.data.customer.nickName;
- businessCommunicationCustomer.isPartner = true;
- } else {
- businessCommunicationCustomer.isPartner = false;
- }
- prevPage.setData({
- user: businessCommunicationCustomer,
- isLogin:true
- })
- wx.setStorageSync('businessCommunicationCustomer', businessCommunicationCustomer)
- wx.navigateBack()
- } else {
- wx.navigateBack()
- }
- // let tempListData = info.listData;
- // tempListData.forEach(el => {
- // if (el.businessCommunicationDemandId == businessCommunicationDemandId) {
- // el.info = Page.data.detailValue
- // }
- // })
- // prevPage.setData({ listData: tempListData })//设置数据
- // wx.navigateBack()
- }).catch(error => {
- console.log(error, "获取error")
- })
- },
- })
|