index.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. inputPhone: '',
  20. avatarUrl: `${CDN_URL}images/default.png`,
  21. CDN_URL
  22. },
  23. /**
  24. * 组件的方法列表
  25. */
  26. methods: {
  27. bindKeyInput(e) {
  28. console.log(e.detail.value);
  29. this.setData({
  30. inputNickName: e.detail.value
  31. })
  32. },
  33. bindKeyPhone(e) {
  34. this.setData({
  35. inputPhone: e.detail.value
  36. })
  37. },
  38. onChooseAvatar(e) {
  39. const {
  40. avatarUrl
  41. } = e.detail
  42. this.setData({
  43. avatarUrl,
  44. })
  45. },
  46. onCancel() {
  47. this.triggerEvent('onCancel')
  48. },
  49. onComfirm() {
  50. this.updateUserInfo(() => {
  51. this.triggerEvent('onComfirm')
  52. })
  53. },
  54. onChooseAvatar(e) {
  55. const {
  56. avatarUrl
  57. } = e.detail
  58. console.log(e);
  59. let loginSessionKey = wx.getStorageSync("token");
  60. wx.uploadFile({
  61. filePath: avatarUrl,
  62. name: 'file',
  63. url: API_BASE_URL + 'api/cms/wxUser/upload',
  64. header: {
  65. token: loginSessionKey
  66. },
  67. formData: {
  68. type: 'img'
  69. },
  70. success: (res) => {
  71. let respon = JSON.parse(res.data)
  72. this.setData({
  73. avatarUrl: API_BASE_URL + (API_BASE_URL.length > 10 ? '' : '') + respon.data.filePath
  74. })
  75. },
  76. fail: function (res) {
  77. console.log(res); //发送失败回调,可以在这里了解失败原因
  78. }
  79. })
  80. },
  81. updateUserInfo(cb = () => { }) {
  82. if (!this.data.avatarUrl || !this.data.inputNickName.trim() || !this.data.inputPhone.trim()||this.data.inputPhone.length<11) {
  83. wx.showToast({
  84. title: '请补充完整信息~',
  85. icon: 'error'
  86. })
  87. return
  88. }
  89. fetchutil.post(`api/cms/wxUser/update`, {
  90. "avatarUrl": this.data.avatarUrl,
  91. "nickName": this.data.inputNickName,
  92. "phone": this.data.inputPhone,
  93. }, {}).then((res) => {
  94. app.globalData.userInfo = res.data;
  95. cb()
  96. }).catch(() => { })
  97. },
  98. }
  99. })