const app = getApp() import SparkMD5 from 'spark-md5' import tool from "../../../assets/js/toolClass.js"; import FormData from '../../../utils/formdata/index.js'; Page({ /** * 页面的初始数据 */ data: { userInfo: {}, defaultAvatarUrl: 'https://cdn-we-retail.ym.tencent.com/miniapp/usercenter/icon-user-center-avatar@2x.png', userInfoBox: false, editType: '', userInfoForm: { icon: '', nickName: '', phone: '', email: '', captcha: '', code: '', }, phoneError: false, emailError: false, imgCode: '', countDown: 0, }, /** * 生命周期函数--监听页面加载 */ onLoad() { if (wx.getStorageSync(app.config.tokenKey)) { this.getUserInfo() } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, // 获取登录用户身份 getUserInfo() { app.MG.identity.getCurrentAppUser().then(res => { // 用户信息优先级:教师认证 > 微信 > 学生(注册时默认) if (res) { let defaultUser = {}; let nickNameData = res.infoList.find((item) => item.type == 'nickName') let teacherRole = res.roleLinks.find((item) => item.role.refCode == 'teacher') let teacherInfos = res.infoList.find((item) => item.type == 'teacherInfo') let secretData = res.secretList.find(i => i.type == 'LoginNameAndPassword') let WeChatInfo = res.infoList.find((item) => item.type === "WeChat"); let phoneInfo = res.secretList.find((item) => item.type == 'MobilePhone') let emailInfo = res.secretList.find((item) => item.type == 'EMail') if (teacherRole && teacherInfos) { defaultUser = { ...teacherInfos, nickName: nickNameData ? JSON.parse(nickNameData.data).nickName : WeChatInfo ? WeChatInfo.name : '', icon: nickNameData ? JSON.parse(nickNameData.data).icon : WeChatInfo ? WeChatInfo.icon : '', userId: res.userId, role: 'Teacher', roleId: teacherRole.role.id, phoneNumber: phoneInfo?.credential, Email: emailInfo ? emailInfo.credential : JSON.parse(teacherInfos.data).email } } else if (WeChatInfo) { defaultUser = { ...WeChatInfo, nickName: nickNameData && JSON.parse(nickNameData.data).nickName ? JSON.parse(nickNameData.data).nickName : WeChatInfo.name, icon: nickNameData && JSON.parse(nickNameData.data).icon ? JSON.parse(nickNameData.data).icon : WeChatInfo.icon, userId: res.userId, phoneNumber: phoneInfo?.credential, Email: emailInfo?.credential } } else if (secretData) { defaultUser = { nickName: nickNameData ? JSON.parse(nickNameData.data).nickName : secretData.credential, icon: JSON.parse(nickNameData.data).icon ? JSON.parse(nickNameData.data).icon : '', userId: res.userId, phoneNumber: phoneInfo?.credential, Email: emailInfo?.credential } } this.setData({ userInfo: defaultUser }) } }); }, //修改用户类型 editUserInfo(e) { let { info } = e.currentTarget.dataset if (!this.data.userInfoBox) { this.setData({ userInfoBox: true, editType: info, "userInfoForm.captcha": "", "userInfoForm.code": "", "userInfoForm.nickName": '' }); } this.getImgCapcha() }, onVisibleChange(e) { this.setData({ userInfoBox: e.detail.visible, countDown: 0, }); }, editIconInfo(e) { var that = this; wx.chooseMedia({ count: 1, // 默认9 sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有 sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有 success: function (res) { wx.getFileSystemManager().readFile({ filePath: res.tempFiles[0].tempFilePath, //选择图片返回的相对路径 // encoding: 'binary', //编码格式 success: ress => { //成功的回调 let spark = new SparkMD5.ArrayBuffer(); spark.append(ress.data); let md5 = spark.end(false); let formData = new FormData(); formData.append('Md5', md5); formData.append('FileName', md5); formData.append('FileType', res.tempFiles[0].fileType); formData.appendFile("file", res.tempFiles[0].tempFilePath); const data = formData.getData(); let _token = wx.getStorageSync(app.config.tokenKey); let header = {}; if (_token == null) { header["Authorization"] = `Basic ${Base64.encode(website.clientId + ":" + website.clientSecret)}`; } else { header["Authorization"] = `Bearer ` + _token; } new Promise((resolve, reject) => { wx.request({ url: app.config.requestCtx + '/file/api/ApiUpload', method: 'POST', header: { 'content-type': data.contentType, ...header }, data: data.buffer, success(res) { if (res.statusCode == 200) { resolve(res.data); if (res.data) { that.setData({ 'userInfoForm.icon': app.config.requestCtx + `/file/GetPreViewImage?md5=` + md5, }); let userTypeInfo = { "requests": [{ data: JSON.stringify({ nickName: that.data.userInfo.nickName, icon: that.data.userInfoForm.icon }), name: '用户昵称头像', type: 'nickName' }] } app.MG.identity.setAppUserInfo(userTypeInfo).then((res) => { if (res) { wx.showToast({ title: "修改成功", icon: 'success', duration: 1000, }) that.getUserInfo() that.setData({ userInfoBox: false, }); } }) } } else { reject('运行时错误,请稍后再试'); } } }) }) } }) } }); }, onNameInput(e) { this.setData({ "userInfoForm.nickName": e.detail.value, }); }, //输入手机号 onPhoneInput(e) { const { phoneError } = this.data; if (e.detail.value) { const isPhoneNumber = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(e.detail.value); if (phoneError === isPhoneNumber) { this.setData({ phoneError: !isPhoneNumber, "userInfoForm.phone": e.detail.value, }); } } else { this.setData({ phoneError: false, }); } }, onEmailInput(e) { const { emailError } = this.data; if (e.detail.value) { const isPhoneNumber = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/.test(e.detail.value); if (emailError === isPhoneNumber) { this.setData({ emailError: !isPhoneNumber, }); } this.setData({ "userInfoForm.email": e.detail.value, }); } else { this.setData({ emailError: false, }); } }, getImgCapcha() { app.MG.identity.getImgCode().then((res) => { this.setData({ imgCode: 'data:image/png;base64,' + res, }); }) }, //图形验证码 onCaptchaInput(e) { this.setData({ "userInfoForm.captcha": e.detail.value, }); }, //短信邮箱验证码 onCodeInput(e) { this.setData({ "userInfoForm.code": e.detail.value, }); }, getPhoneCode() { if (this.data.userInfoForm.phone && this.data.userInfoForm.captcha) { app.MG.identity .getPhoneCode({ phoneNumber: this.data.userInfoForm.phone, imageCaptcha: this.data.userInfoForm.captcha, appRefCode: app.config.appRefCode }) .then((res) => { if (res == '验证码发送成功') { wx.showToast({ title: res, icon: 'none', duration: 1000 }) // 开启短信验证倒计时 this.getSecond(60) } else { wx.showToast({ title: res, icon: 'none', duration: 1000 }) this.getImgCapcha() } }) } else { wx.showToast({ title: '请填写手机号或图形验证码', icon: 'none', duration: 1000 }) } }, getSecond(time) { let timer = null if (!timer) { this.setData({ countDown: time, }); let conut = this.data.countDown; timer = setInterval(() => { conut-- this.setData({ countDown: conut, }); if (this.data.countDow == 0) { clearInterval(timer) timer = null } }, 1000) } }, getEmailCode() { if (this.data.userInfoForm.email && this.data.userInfoForm.captcha) { app.MG.identity .getEmailCode({ sendEmail: this.data.userInfoForm.email, captcha: this.data.userInfoForm.captcha, appRefCode: app.config.appRefCode }) .then((res) => { if (res == true) { wx.showToast({ title: '验证码已发送', icon: 'none', duration: 1000 }) } else { wx.showToast({ title: '邮箱验证码发送失败', icon: 'none', duration: 1000 }) this.getImgCapcha() } }) } else { wx.showToast({ title: '请填写邮箱或图形验证码', icon: 'none', duration: 1000 }) } }, confirmInfo() { if (this.data.editType == 'nickName') { if (!this.data.userInfoForm.nickName) { wx.showToast({ title: '请输入昵称', icon: 'none', duration: 1000 }) return false; } let userTypeInfo = { "requests": [{ data: JSON.stringify({ nickName: this.data.userInfoForm.nickName, icon: this.data.userInfo.icon }), name: '用户昵称头像', type: 'nickName' }] } app.MG.identity.setAppUserInfo(userTypeInfo).then((res) => { if (res) { wx.showToast({ title: "修改成功", icon: 'success', duration: 1000, }) this.getUserInfo() this.setData({ userInfoBox: false, }); } }) } else if (this.data.editType == 'phone') { if (!this.data.userInfoForm.phone) { wx.showToast({ title: '请输入手机号', icon: 'none', duration: 1000 }) return false; } if (!this.data.userInfoForm.code) { wx.showToast({ title: '请输入验证码', icon: 'none', duration: 1000 }) return false; } let query = { phoneNumber: this.data.userInfoForm.phone, phoneCaptcha: this.data.userInfoForm.code } app.MG.identity.userSetPhoneNumber(query).then((res) => { if (res == '验证码过期或错误') { wx.showToast({ title: res + ',请稍后重试', icon: 'none', duration: 1000 }) this.setData({ countDown: 0, }); } else if (res == '此手机号码已被其它账号绑定') { wx.showToast({ title: res + ',请更换其他手机号。', icon: 'none', duration: 1000 }) this.setData({ countDown: 0, }); } else { wx.showToast({ title: res, icon: 'none', duration: 1000 }) this.setData({ userInfoBox: false, }); this.getUserInfo() } }) } else if (this.data.editType == 'email') { if (!this.data.userInfoForm.email) { wx.showToast({ title: '请输入邮箱', icon: 'none', duration: 1000 }) return false; } if (!this.data.userInfoForm.code) { wx.showToast({ title: '请输入验证码', icon: 'none', duration: 1000 }) return false; } let query = { eMail: this.data.userInfoForm.email, captcha: this.data.userInfoForm.code } app.MG.identity.bindingEmail(query).then((res) => { if (res == '验证码过期') { wx.showToast({ title: res + ',请稍后重试。', icon: 'none', duration: 1000 }) } else if (res == '此邮箱已被其它账号绑定') { wx.showToast({ title: res + ',请更换其他邮箱。', icon: 'none', duration: 1000 }) } else if (res == '验证码无效') { wx.showToast({ title: res, icon: 'none', duration: 1000 }) } else { wx.showToast({ title: res, icon: 'none', duration: 1000 }) this.setData({ userInfoBox: false, }); this.getUserInfo() } }) } }, onCloseProtocol() { this.setData({ userInfoBox: false, }); }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { } })