mine.js 7.9 KB

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