mine.js 6.8 KB

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