index.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // components/authorize/index.ts
  2. import fetchutil from '../../utils/http'
  3. var {
  4. CDN_URL,
  5. API_BASE_URL
  6. } = require('../../utils/util')
  7. const app = getApp()
  8. Component({
  9. /**
  10. * 组件的属性列表
  11. */
  12. properties: {
  13. },
  14. /**
  15. * 组件的初始数据
  16. */
  17. data: {
  18. inputNickName: '',
  19. avatarUrl: `${CDN_URL}images/default.png`,
  20. CDN_URL
  21. },
  22. /**
  23. * 组件的方法列表
  24. */
  25. methods: {
  26. bindKeyInput(e) {
  27. console.log(e.detail.value);
  28. this.setData({
  29. inputNickName: e.detail.value
  30. })
  31. },
  32. onChooseAvatar(e) {
  33. const {
  34. avatarUrl
  35. } = e.detail
  36. this.setData({
  37. avatarUrl,
  38. })
  39. },
  40. onCancel() {
  41. this.triggerEvent('onCancel')
  42. },
  43. onComfirm() {
  44. this.updateUserInfo(() => {
  45. this.triggerEvent('onComfirm')
  46. })
  47. },
  48. onChooseAvatar(e) {
  49. const {
  50. avatarUrl
  51. } = e.detail
  52. console.log(e);
  53. let loginSessionKey = wx.getStorageSync("token");
  54. wx.uploadFile({
  55. filePath: avatarUrl,
  56. name: 'file',
  57. url: API_BASE_URL + 'api/cms/wxUser/upload',
  58. header: {
  59. token: loginSessionKey
  60. },
  61. formData: {
  62. type: 'img'
  63. },
  64. success: (res) => {
  65. let respon = JSON.parse(res.data)
  66. this.setData({
  67. avatarUrl: API_BASE_URL + (API_BASE_URL.length > 10 ? 'api' : '') + respon.data.filePath
  68. })
  69. },
  70. fail: function (res) {
  71. console.log(res); //发送失败回调,可以在这里了解失败原因
  72. }
  73. })
  74. },
  75. updateUserInfo(cb = () => {}) {
  76. if (!this.data.avatarUrl || !this.data.inputNickName.trim()) {
  77. wx.showToast({
  78. title: '请补充完整信息~',
  79. icon: 'error'
  80. })
  81. return
  82. }
  83. fetchutil.post(`api/cms/wxUser/update`, {
  84. "avatarUrl": this.data.avatarUrl,
  85. "nickName": this.data.inputNickName,
  86. }, {}).then((res) => {
  87. app.globalData.userInfo = res.data;
  88. cb()
  89. }).catch(() => {})
  90. },
  91. }
  92. })