mine.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. const uploadJS = require('../../mixin/upload.js')
  2. const { updateUserInfo, userLoginOut } = require('../../api/common')
  3. const { sessionStorageKey } = require('../../api/request')
  4. const app = getApp()
  5. const tmplIds = 'focfKhWON52XWaBNTkZAbohmKJkXMiexbvGa5XGoVwE'
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. form: {
  12. user_head_img_url: '',
  13. user_nickname: '',
  14. user_phone: ''
  15. },
  16. user_head_img_url: [],
  17. user_nickname: '',
  18. isAutoFocus: false,
  19. booNickname: false,
  20. booLogout: false,
  21. booLock: false,
  22. sessionId: '',
  23. userInfo: {
  24. // 'shop_status': 0, // 状态(0审核中1审核通过2审核失败)
  25. // 'user_shop_id': 0 //店铺ID(0代表无店铺)
  26. },
  27. kefu: '18551725991' // todo 客服电话
  28. },
  29. /**
  30. * 生命周期函数--监听页面加载
  31. */
  32. onLoad(options) {
  33. this.init()
  34. },
  35. async bindCallBack() {
  36. this.init()
  37. },
  38. /**
  39. * 生命周期函数--监听页面初次渲染完成
  40. */
  41. onReady() {
  42. },
  43. /**
  44. * 生命周期函数--监听页面显示
  45. */
  46. onShow() {
  47. const { shop_status } = app.globalData.userInfo
  48. this.getTabBar().init()
  49. this.setData({
  50. sessionId: wx.getStorageSync(sessionStorageKey),
  51. userInfo: app.globalData.userInfo
  52. })
  53. if (shop_status === 1 && this.getTabBar().data.list.findIndex(item => item.pagePath === 'pages/news/news') === -1) {
  54. this.getTabBar().setData({ list: app.globalData.tabBarList })
  55. }
  56. },
  57. /**
  58. * 生命周期函数--监听页面隐藏
  59. */
  60. onHide() {
  61. },
  62. /**
  63. * 生命周期函数--监听页面卸载
  64. */
  65. onUnload() {
  66. },
  67. onPullDownRefresh() {
  68. app.fetchUserData()
  69. const timer = setTimeout(() => {
  70. clearTimeout(timer)
  71. wx.stopPullDownRefresh()
  72. }, 1000)
  73. },
  74. async init() {
  75. // 更新用户信息
  76. app.fetchUserDataCallback = () => {
  77. const userInfo = app.globalData.userInfo
  78. let temp = {
  79. userInfo
  80. }
  81. for (let key in userInfo) {
  82. let value = userInfo[key]
  83. if ((Array.isArray(value) && value.length >= 1) || (Object.prototype.toString.call(value) === '[object Object]') || (typeof value === 'string' && value) || typeof value === 'number') {
  84. if (this.data.hasOwnProperty(key)) {
  85. if (key === 'user_head_img_url') {
  86. value = [
  87. {
  88. url: value,
  89. formkey: key
  90. }
  91. ]
  92. }
  93. temp[`${key}`] = value
  94. }
  95. if (this.data.form.hasOwnProperty(key)) {
  96. temp[`form.${key}`] = value
  97. }
  98. }
  99. }
  100. this.setData(temp)
  101. }
  102. await app.fetchUserData()
  103. },
  104. ...uploadJS,
  105. // 图片上传成功回调
  106. uploadCallBack(res) {
  107. const temp = res.map(item => {
  108. return {
  109. 'url': item.url,
  110. 'formkey': item.formkey
  111. }
  112. })
  113. let formkey = ''
  114. if (temp.length > 0) {
  115. formkey = temp[0].formkey
  116. }
  117. if (formkey === 'user_head_img_url') {
  118. this._updateUserInfo(formkey, temp[0].url)
  119. }
  120. },
  121. // 有昵称时:编辑昵称
  122. editNickname() {
  123. this.setData({
  124. booNickname: true
  125. })
  126. setTimeout(() => {
  127. this.setData({
  128. isAutoFocus: true
  129. })
  130. }, 500)
  131. },
  132. bindInput(e) {
  133. this.setData({
  134. user_nickname: e.detail.value.trim()
  135. })
  136. },
  137. // 失去焦点:编辑昵称
  138. bindblur() {
  139. const user_nickname = this.data.user_nickname
  140. if (user_nickname.length < 1) {
  141. this.setData({
  142. booNickname: false
  143. })
  144. return
  145. }
  146. this._updateUserInfo('user_nickname', user_nickname)
  147. },
  148. async _updateUserInfo(key, val) {
  149. try {
  150. const { status, msg } = await updateUserInfo({ [key]: val })
  151. if (status) {
  152. this.init()
  153. } else {
  154. wx.showToast({
  155. title: msg,
  156. icon: 'none'
  157. })
  158. }
  159. } catch (e) {}
  160. if (key === 'user_nickname') {
  161. this.setData({
  162. booNickname: false
  163. })
  164. }
  165. },
  166. jump(e) {
  167. const { page } = e.currentTarget.dataset
  168. wx.navigateTo({
  169. url: `/pages/${page}/${page}`
  170. })
  171. },
  172. call() {
  173. wx.makePhoneCall({
  174. phoneNumber: this.data.kefu
  175. })
  176. },
  177. showLogout() {
  178. this.setData({
  179. booLogout: true
  180. })
  181. },
  182. hideLogout() {
  183. this.setData({
  184. booLogout: false
  185. })
  186. },
  187. async confirmLogout() {
  188. this.setData({
  189. booLock: true
  190. })
  191. try {
  192. const { status, msg } = await userLoginOut()
  193. if (status) {
  194. this.hideLogout()
  195. wx.removeStorageSync(sessionStorageKey)
  196. // 更新用户信息
  197. app.globalData.userInfo = {}
  198. wx.reLaunch({
  199. url: '/pages/mine/mine'
  200. })
  201. } else {
  202. wx.showToast({
  203. title: msg,
  204. icon: 'none'
  205. })
  206. }
  207. } catch (e) {}
  208. this.setData({
  209. booLock: false
  210. })
  211. },
  212. // 参考 https://www.cnblogs.com/onesea/p/15005037.html
  213. async handleSubscribeMessage(e) {
  214. const { page } = e.currentTarget.dataset
  215. const that = this
  216. try {
  217. const { errMsg, subscriptionsSetting } = await wx.getSetting({ withSubscriptions: true })
  218. if (errMsg === 'getSetting:ok') {
  219. // 用户打开了订阅消息总开关
  220. if (subscriptionsSetting.mainSwitch) {
  221. // 用户同意总是保持是否推送消息的选择, 这里表示以后不会再拉起推送消息的授权
  222. if (subscriptionsSetting.itemSettings != null) {
  223. const moIdState = subscriptionsSetting.itemSettings[tmplIds]
  224. if (moIdState === 'accept') {
  225. // 接受了消息推送
  226. wx.navigateTo({
  227. url: `/pages/${page}/${page}`
  228. })
  229. } else if (moIdState === 'reject') {
  230. // 拒绝消息推送
  231. wx.navigateTo({
  232. url: `/pages/${page}/${page}`
  233. })
  234. } else if (moIdState === 'ban') {
  235. // 已被后台封禁
  236. wx.navigateTo({
  237. url: `/pages/${page}/${page}`
  238. })
  239. } else {
  240. that.handleSubscribeMessageBridge(tmplIds, page)
  241. }
  242. } else {
  243. that.handleSubscribeMessageBridge(tmplIds, page)
  244. }
  245. } else {
  246. // 订阅消息未开启
  247. that.handleSubscribeMessageBridge(tmplIds, page)
  248. }
  249. }
  250. } catch (err) {}
  251. },
  252. handleSubscribeMessageBridge(tmplIds, page) {
  253. // 当用户没有点击 ’总是保持以上选择,不再询问‘ 按钮。那每次执到这都会拉起授权弹窗
  254. wx.showModal({
  255. title: '提示',
  256. content: '请授权开通服务通知',
  257. showCancel: true,
  258. success: function (res) {
  259. if (res.confirm) {
  260. wx.requestSubscribeMessage({
  261. tmplIds: [tmplIds],
  262. success(res) {
  263. wx.navigateTo({
  264. url: `/pages/${page}/${page}`
  265. })
  266. },
  267. fail(err) {
  268. wx.navigateTo({
  269. url: `/pages/${page}/${page}`
  270. })
  271. }
  272. })
  273. }
  274. }
  275. })
  276. }
  277. })