const uploadJS = require('../../mixin/upload.js') const { updateUserInfo, userLoginOut } = require('../../api/common') const { sessionStorageKey } = require('../../api/request') const app = getApp() const tmplIds = 'focfKhWON52XWaBNTkZAbohmKJkXMiexbvGa5XGoVwE' Page({ /** * 页面的初始数据 */ data: { form: { user_head_img_url: '', user_nickname: '', user_phone: '' }, user_head_img_url: [], user_nickname: '', isAutoFocus: false, booNickname: false, booLogout: false, booLock: false, sessionId: '', userInfo: { // 'shop_status': 0, // 状态(0审核中1审核通过2审核失败) // 'user_shop_id': 0 //店铺ID(0代表无店铺) }, kefu: '' }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { app.fetchUserDataCallback = () => { this.setUserInfo() this.setTabBarList() } app.fetchSystemConfigCallback = () => { this.setKefu() } this.init() }, async bindCallBack() { this.init() }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { this.setUserInfo() this.setTabBarList() this.setKefu() }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, onPullDownRefresh() { app.fetchUserData() const timer = setTimeout(() => { clearTimeout(timer) wx.stopPullDownRefresh() }, 1000) }, async init() { // 更新用户信息 app.fetchUserDataCallback = () => { const userInfo = app.globalData.userInfo let temp = { userInfo } for (let key in userInfo) { let value = userInfo[key] if ((Array.isArray(value) && value.length >= 1) || (Object.prototype.toString.call(value) === '[object Object]') || (typeof value === 'string' && value) || typeof value === 'number') { if (this.data.hasOwnProperty(key)) { if (key === 'user_head_img_url') { value = [ { url: value, formkey: key } ] } temp[`${key}`] = value } if (this.data.form.hasOwnProperty(key)) { temp[`form.${key}`] = value } } } this.setData(temp) } await app.fetchUserData() }, ...uploadJS, // 图片上传成功回调 uploadCallBack(res) { const temp = res.map(item => { return { 'url': item.url, 'formkey': item.formkey } }) let formkey = '' if (temp.length > 0) { formkey = temp[0].formkey } if (formkey === 'user_head_img_url') { this._updateUserInfo(formkey, temp[0].url) } }, // 有昵称时:编辑昵称 editNickname() { this.setData({ booNickname: true }) setTimeout(() => { this.setData({ isAutoFocus: true }) }, 500) }, bindInput(e) { this.setData({ user_nickname: e.detail.value.trim() }) }, // 失去焦点:编辑昵称 bindblur() { const user_nickname = this.data.user_nickname if (user_nickname.length < 1) { this.setData({ booNickname: false }) return } this._updateUserInfo('user_nickname', user_nickname) }, async _updateUserInfo(key, val) { try { const { status, msg } = await updateUserInfo({ [key]: val }) if (status) { this.init() } else { wx.showToast({ title: msg, icon: 'none' }) } } catch (e) {} if (key === 'user_nickname') { this.setData({ booNickname: false }) } }, jump(e) { const { page } = e.currentTarget.dataset if (!this.data.sessionId) { wx.navigateTo({ url: `/pages/login/login` }) return } wx.navigateTo({ url: `/pages/${page}/${page}` }) }, call() { const { kefu } = this.data if (!kefu) { return } wx.makePhoneCall({ phoneNumber: kefu }) }, showLogout() { this.setData({ booLogout: true }) }, hideLogout() { this.setData({ booLogout: false }) }, async confirmLogout() { this.setData({ booLock: true }) try { const { status, msg } = await userLoginOut() if (status) { this.hideLogout() wx.removeStorageSync(sessionStorageKey) // 更新用户信息 app.globalData.userInfo = {} wx.reLaunch({ url: '/pages/mine/mine' }) } else { wx.showToast({ title: msg, icon: 'none' }) } } catch (e) {} this.setData({ booLock: false }) }, // 参考 https://www.cnblogs.com/onesea/p/15005037.html async handleSubscribeMessage(e) { const { page } = e.currentTarget.dataset const that = this if (!this.data.sessionId) { wx.navigateTo({ url: `/pages/login/login` }) return } try { const { errMsg, subscriptionsSetting } = await wx.getSetting({ withSubscriptions: true }) if (errMsg === 'getSetting:ok') { // 用户打开了订阅消息总开关 if (subscriptionsSetting.mainSwitch) { // 用户同意总是保持是否推送消息的选择, 这里表示以后不会再拉起推送消息的授权 if (subscriptionsSetting.itemSettings != null) { const moIdState = subscriptionsSetting.itemSettings[tmplIds] if (moIdState === 'accept') { // 接受了消息推送 wx.navigateTo({ url: `/pages/${page}/${page}` }) } else if (moIdState === 'reject') { // 拒绝消息推送 wx.navigateTo({ url: `/pages/${page}/${page}` }) } else if (moIdState === 'ban') { // 已被后台封禁 wx.navigateTo({ url: `/pages/${page}/${page}` }) } else { that.handleSubscribeMessageBridge(tmplIds, page) } } else { that.handleSubscribeMessageBridge(tmplIds, page) } } else { // 订阅消息未开启 that.handleSubscribeMessageBridge(tmplIds, page) } } } catch (err) {} }, handleSubscribeMessageBridge(tmplIds, page) { // 当用户没有点击 ’总是保持以上选择,不再询问‘ 按钮。那每次执到这都会拉起授权弹窗 wx.showModal({ title: '提示', content: '请授权开通服务通知', showCancel: true, success: function (res) { if (res.confirm) { wx.requestSubscribeMessage({ tmplIds: [tmplIds], success(res) { wx.navigateTo({ url: `/pages/${page}/${page}` }) }, fail(err) { wx.navigateTo({ url: `/pages/${page}/${page}` }) } }) } } }) }, handleFormItemClick(e) { const { formkey } = e.currentTarget.dataset if ((formkey === 'user_head_img_url' || formkey === 'user_nickname') && !this.data.sessionId) { wx.navigateTo({ url: `/pages/login/login` }) } }, setKefu() { const { kefu_phone } = app.globalData.objSystemConfig this.setData({ kefu: kefu_phone || '' }) }, setUserInfo() { this.setData({ sessionId: wx.getStorageSync(sessionStorageKey), userInfo: app.globalData.userInfo }) }, setTabBarList() { const { shop_status } = app.globalData.userInfo this.getTabBar().init() if (shop_status === 1 && this.getTabBar().data.list.findIndex(item => item.pagePath === 'pages/news/news') === -1) { this.getTabBar().setData({ list: app.globalData.tabBarList }) } } })