mine.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. if (!this.data.sessionId) {
  169. wx.navigateTo({
  170. url: `/pages/login/login`
  171. })
  172. return
  173. }
  174. wx.navigateTo({
  175. url: `/pages/${page}/${page}`
  176. })
  177. },
  178. call() {
  179. wx.makePhoneCall({
  180. phoneNumber: this.data.kefu
  181. })
  182. },
  183. showLogout() {
  184. this.setData({
  185. booLogout: true
  186. })
  187. },
  188. hideLogout() {
  189. this.setData({
  190. booLogout: false
  191. })
  192. },
  193. async confirmLogout() {
  194. this.setData({
  195. booLock: true
  196. })
  197. try {
  198. const { status, msg } = await userLoginOut()
  199. if (status) {
  200. this.hideLogout()
  201. wx.removeStorageSync(sessionStorageKey)
  202. // 更新用户信息
  203. app.globalData.userInfo = {}
  204. wx.reLaunch({
  205. url: '/pages/mine/mine'
  206. })
  207. } else {
  208. wx.showToast({
  209. title: msg,
  210. icon: 'none'
  211. })
  212. }
  213. } catch (e) {}
  214. this.setData({
  215. booLock: false
  216. })
  217. },
  218. // 参考 https://www.cnblogs.com/onesea/p/15005037.html
  219. async handleSubscribeMessage(e) {
  220. const { page } = e.currentTarget.dataset
  221. const that = this
  222. if (!this.data.sessionId) {
  223. wx.navigateTo({
  224. url: `/pages/login/login`
  225. })
  226. return
  227. }
  228. try {
  229. const { errMsg, subscriptionsSetting } = await wx.getSetting({ withSubscriptions: true })
  230. if (errMsg === 'getSetting:ok') {
  231. // 用户打开了订阅消息总开关
  232. if (subscriptionsSetting.mainSwitch) {
  233. // 用户同意总是保持是否推送消息的选择, 这里表示以后不会再拉起推送消息的授权
  234. if (subscriptionsSetting.itemSettings != null) {
  235. const moIdState = subscriptionsSetting.itemSettings[tmplIds]
  236. if (moIdState === 'accept') {
  237. // 接受了消息推送
  238. wx.navigateTo({
  239. url: `/pages/${page}/${page}`
  240. })
  241. } else if (moIdState === 'reject') {
  242. // 拒绝消息推送
  243. wx.navigateTo({
  244. url: `/pages/${page}/${page}`
  245. })
  246. } else if (moIdState === 'ban') {
  247. // 已被后台封禁
  248. wx.navigateTo({
  249. url: `/pages/${page}/${page}`
  250. })
  251. } else {
  252. that.handleSubscribeMessageBridge(tmplIds, page)
  253. }
  254. } else {
  255. that.handleSubscribeMessageBridge(tmplIds, page)
  256. }
  257. } else {
  258. // 订阅消息未开启
  259. that.handleSubscribeMessageBridge(tmplIds, page)
  260. }
  261. }
  262. } catch (err) {}
  263. },
  264. handleSubscribeMessageBridge(tmplIds, page) {
  265. // 当用户没有点击 ’总是保持以上选择,不再询问‘ 按钮。那每次执到这都会拉起授权弹窗
  266. wx.showModal({
  267. title: '提示',
  268. content: '请授权开通服务通知',
  269. showCancel: true,
  270. success: function (res) {
  271. if (res.confirm) {
  272. wx.requestSubscribeMessage({
  273. tmplIds: [tmplIds],
  274. success(res) {
  275. wx.navigateTo({
  276. url: `/pages/${page}/${page}`
  277. })
  278. },
  279. fail(err) {
  280. wx.navigateTo({
  281. url: `/pages/${page}/${page}`
  282. })
  283. }
  284. })
  285. }
  286. }
  287. })
  288. },
  289. handleFormItemClick(e) {
  290. const { formkey } = e.currentTarget.dataset
  291. if ((formkey === 'user_head_img_url' || formkey === 'user_nickname') && !this.data.sessionId) {
  292. wx.navigateTo({
  293. url: `/pages/login/login`
  294. })
  295. }
  296. }
  297. })