279 lines
8.5 KiB
JavaScript
279 lines
8.5 KiB
JavaScript
// pages/login/login.js
|
|
import UserService from '../../net/api/userApi';
|
|
const Cache = require('../../utils/storage');
|
|
const app = getApp()
|
|
Page({
|
|
|
|
/**
|
|
* 页面的初始数据
|
|
*/
|
|
data: {
|
|
imgAssets: app.globalData.imgAssetsUrl,
|
|
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 => {
|
|
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',
|
|
})
|
|
}
|
|
})
|
|
.catch(err => {
|
|
// 发生未知错误,需要提醒重新进入小程序
|
|
wx.hideLoading()
|
|
_self.doShowExit('LoginApi fail')
|
|
})
|
|
} else {
|
|
// 发生未知错误,需要提醒重新进入小程序
|
|
_self.doShowExit('jsCode is null')
|
|
}
|
|
},
|
|
})
|
|
},
|
|
//显示退出弹窗
|
|
doShowExit(type) {
|
|
wx.showModal({
|
|
title: '提示',
|
|
content: `应用需授权才能运行,因未授权将退出.重新打开完成授权,就能继续体验.(${type})`,
|
|
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) {
|
|
this.setData({
|
|
isChecked: e.detail.value === '1'
|
|
});
|
|
},
|
|
//获取手机号
|
|
doGetPhoneNumber(e) {
|
|
const _self = this
|
|
if (e.detail.errMsg === 'getPhoneNumber:ok') {
|
|
// 用户同意授权
|
|
const {
|
|
code
|
|
} = e.detail;
|
|
//通过后台获取手机号
|
|
wx.showLoading({
|
|
title: '登录中...',
|
|
})
|
|
var data = {
|
|
"code": code,
|
|
"openid": _self.data.openId
|
|
}
|
|
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.doGetMineContact()
|
|
} else {
|
|
//获取客服编号
|
|
wx.switchTab({
|
|
url: '/pages/index/index',
|
|
})
|
|
}
|
|
} else {
|
|
_self.doShowExit('Token is null')
|
|
}
|
|
}, err => {
|
|
console.log('注册错误', err);
|
|
// 绑定失败重新
|
|
wx.hideLoading();
|
|
_self.doShowExit('Register fail');
|
|
})
|
|
} else {
|
|
// 用户拒绝授权
|
|
console.log('用户拒绝授权');
|
|
_self.doShowExit('Phone Authorization failure')
|
|
}
|
|
},
|
|
//判断是否需要创建联系人
|
|
doGetMineContact() {
|
|
const _self = this
|
|
wx.showLoading({
|
|
title: '加载中...',
|
|
})
|
|
const data = {
|
|
page: 1,
|
|
rows: 2
|
|
}
|
|
UserService.doGetMineContactList(data)
|
|
.then(res => {
|
|
wx.hideLoading()
|
|
if (res.rows && res.rows.length <= 0) {
|
|
//需要创建
|
|
_self.doUpdateUserInfo('')
|
|
_self.doGetCsaNo()
|
|
} else {
|
|
//无需创建
|
|
wx.switchTab({
|
|
url: '/pages/index/index',
|
|
})
|
|
}
|
|
})
|
|
.catch(err => {
|
|
wx.hideLoading()
|
|
_self.doShowExit('Contact is Null')
|
|
})
|
|
},
|
|
//创建用户
|
|
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('CsaNo is Null')
|
|
})
|
|
},
|
|
//联系人姓名
|
|
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('Create contact fail')
|
|
})
|
|
},
|
|
//同意协议
|
|
doChangeAgree() {
|
|
this.setData({
|
|
isAgree: !this.data.isAgree
|
|
})
|
|
},
|
|
onUnload() {
|
|
console.log('login卸载')
|
|
},
|
|
}) |