mine.js 7.6 KB

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