mine.js 7.7 KB

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