mine.js 6.3 KB

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