ts_aimz/pages/login/login.js
2025-04-09 11:31:24 +08:00

256 lines
7.8 KiB
JavaScript

// pages/login/login.js
import UserService from '../../net/api/userApi';
const Cache = require('../../utils/storage');
Page({
/**
* 页面的初始数据
*/
data: {
isChecked: false,
hintText: "获取电话号码",
openId: '',
isShowHalfScreenDialog: false,
phone: '',
csaNo: '',
isAgree: false, //是否同意协议
contactName: '', //联系人姓名
errorHint: '',
showError: false,
successHint: '',
showSuccess: false,
infoHint: '',
showInfo: false,
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
this.doLogin();
},
//登录
doLogin() {
const _self = this
wx.showLoading({
title: '请稍后...',
})
wx.login({
success: (res) => {
if (res.code) {
var data = {
'jsCode': res.code
}
UserService.doLogin(data)
.then(res => {
console.log(res)
wx.hideLoading()
_self.setData({
openId: res.openid
})
//1未绑定 0绑定
if (res.isNew == 1) {
_self.setData({
infoHint: '为了给您带来更便捷、个性化的服务,需要您提供手机号,完成注册流程,请点击授权登录',
showInfo: true
})
} else {
//保存token
Cache.set("token", res.accessToken)
//绑定成功
wx.switchTab({
url: '/pages/index/index',
})
}
}, err => {
//TODO 发生未知错误,需要提醒重新进入小程序
console.log('doLogin')
console.log(err)
wx.hideLoading()
_self.doShowExit()
})
} else {
//TODO 发生未知错误,需要提醒重新进入小程序
console.log('code null')
_self.doShowExit()
}
},
})
},
//显示退出弹窗
doShowExit() {
wx.showModal({
title: '提示',
content: '应用需授权才能运行,因未授权将退出.重新打开完成授权,就能继续体验.',
showCancel: false,
complete: (res) => {
if (res.confirm) {
wx.exitMiniProgram()
}
}
})
},
//用户服务协议
showServiceDeal() {
const id = '68eee8f5-33d3-4246-aeee-a33956677101'
wx.navigateTo({
url: '/pages/treaty/rule/rule?id=' + id,
})
},
//隐私条款
showPrivacyDeal() {
const id = '93679af4-e264-4d1c-bd49-538028daa95d'
wx.navigateTo({
url: '/pages/treaty/rule/rule?id=' + id,
})
},
onChange(e) {
console.log(e)
this.setData({
isChecked: e.detail.value === '1'
});
},
//获取手机号
doGetPhoneNumber(e) {
const _self = this
if (e.detail.errMsg === 'getPhoneNumber:ok') {
// 用户同意授权
const {
code
} = e.detail;
console.log(code)
//通过后台获取手机号
wx.showLoading({
title: '登录中...',
})
var data = {
"code": code,
"openid": _self.data.openId
}
console.log(data)
UserService.doRegister(data)
.then(res => {
wx.hideLoading()
if (res.accessToken) {
Cache.set("token", res.accessToken);
//创建所属人
if (res.phone && res.phone != '') {
_self.setData({
phone: res.phone
})
_self.doUpdateUserInfo('')
_self.doGetCsaNo()
} else {
//获取客服编号
wx.switchTab({
url: '/pages/index/index',
})
}
} else {
_self.doShowExit()
}
}, err => {
console.log(err)
//TODO 绑定失败重新
wx.hideLoading()
_self.doShowExit()
})
} else {
// 用户拒绝授权
console.log('用户拒绝授权');
_self.doShowExit()
}
},
//创建用户
doUpdateUserInfo(name) {
const _self = this
const data = {
userInfoName: name != '' ? name : '新用户',
contactPhone: _self.data.phone,
idCardType: 'ID_CARD',
userInfoType: 'PERSONAL',
}
UserService.doUpdateUserInfo(data)
.then(res => {
console.log(`更新用户信息${res}`)
}, err => {
console.log(`更新用户信息错误${err}`)
})
},
//获取客服编号
doGetCsaNo() {
const _self = this
wx.showLoading({
title: '加载中...',
})
UserService.doGetCsaNo()
.then(res => {
wx.hideLoading()
_self.setData({
csaNo: res.csaNo
})
_self.setData({
isShowHalfScreenDialog: true
})
}, err => {
wx.hideLoading()
_self.doShowExit()
console.log(err)
})
},
//联系人姓名
inputContact(e) {
this.setData({
contactName: e.detail.value
})
},
//创建版权所属人
doCreateOwn() {
//判断名称是否为空
const _self = this
if (_self.data.contactName == '') {
_self.setData({
errorHint: '请输入联系人姓名',
showError: true
})
return
}
//去创建
const data = {
"company": "",
"csaNo": _self.data.csaNo,
"name": _self.data.contactName,
"phone": _self.data.phone
}
wx.showLoading({
title: '创建中...',
})
_self.doUpdateUserInfo(_self.data.contactName)
UserService.doCreateContact(data)
.then(res => {
wx.hideLoading()
wx.showToast({
title: '创建成功',
icon: 'success',
success: () => {
wx.switchTab({
url: '/pages/index/index',
})
}
})
}, err => {
wx.hideLoading()
_self.doShowExit()
})
},
//同意协议
doChangeAgree() {
this.setData({
isAgree: !this.data.isAgree
})
console.log(this.data.isAgree)
},
onUnload() {
console.log('login卸载')
},
})