123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- 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 })
- }
- }
- })
|