// pages/login/login.js import UserService from '../../net/api/userApi'; const Cache = require('../../utils/storage'); Page({ /** * 页面的初始数据 */ data: { isChecked: false, hintText: "获取电话号码", code: '', msg: '', errno: '', openId: '', isShowHalfScreenDialog: false, phone: '', csaNo: '', isAgree: false, //是否同意协议 contactName: '', //联系人姓名 }, /** * 生命周期函数--监听页面加载 */ 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) { wx.showToast({ title: '为了给您带来更便捷、个性化的服务,需要您提供手机号,完成注册流程,请点击授权登录', icon: 'none', duration: 3000, }) } else { //保存token Cache.set("token", res.accessToken) //绑定成功 wx.switchTab({ url: '/pages/index/index', }) } }, err => { //TODO 发生未知错误,需要提醒重新进入小程序 wx.hideLoading() _self.doShowExit() }) } else { //TODO 发生未知错误,需要提醒重新进入小程序 _self.doShowExit() } }, }) }, //显示退出弹窗 doShowExit() { wx.showModal({ title: '提示', content: '您未授权,请重新打开小程序,进行授权', showCancel: false, complete: (res) => { if (res.confirm) { wx.exitMiniProgram() } } }) }, //用户服务协议 showServiceDeal() { wx.navigateTo({ url: '/pages/treaty/service/service', }) }, //隐私条款 showPrivacyDeal() { wx.navigateTo({ url: '/pages/treaty/privacy/privacy', }) }, 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.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() } }, //获取客服编号 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 == '') { wx.showToast({ title: '请输入联系人姓名', icon: 'error' }) return } //去创建 const data = { "company": "", "csaNo": _self.data.csaNo, "name": _self.data.contactName, "phone": _self.data.phone } wx.showLoading({ title: '创建中...', }) 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卸载') }, })