createFile.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // pages/createFile/createFile.js
  2. import { createDocument } from "../../api/document";
  3. import { userUploadHeadImg } from '../../api/upload'
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. choseAvatar: false,
  10. choseAvaSrc: '',
  11. username: '',
  12. sexIndex: '0',
  13. relateIndex: 1,
  14. pickerText: '请选择',
  15. relativeText: '请选择',
  16. relativeRange: ['本人','父母', '子女', '配偶', '朋友', '祖孙', '兄妹', '其他'] // 关系 关系 1.本人 2.父母 3.子女 4.配偶 5.朋友 6.祖孙、7.兄妹、8.其他
  17. },
  18. bindUserNameInput (e) {
  19. this.setData({
  20. username: e.detail.value
  21. })
  22. },
  23. /**
  24. * 保存
  25. */
  26. handleSave() {
  27. if (this.data.choseAvaSrc == '') {
  28. wx.showToast({
  29. title: '请选择头像',
  30. icon: 'error'
  31. })
  32. return
  33. }
  34. if (this.data.username == '') {
  35. wx.showToast({
  36. title: '请输入姓名',
  37. icon: 'error'
  38. })
  39. return
  40. }
  41. if (this.data.pickerText == '请选择') {
  42. wx.showToast({
  43. title: '请选择生日',
  44. icon: 'error'
  45. })
  46. return
  47. }
  48. if (this.data.relativeText == '请选择') {
  49. wx.showToast({
  50. title: '请选择关系',
  51. icon: 'error'
  52. })
  53. return
  54. }
  55. var data = {
  56. realName: this.data.username,
  57. sex: this.data.sexIndex == 0 ? true : false,
  58. birthday: this.data.pickerText,
  59. relationship: this.data.relateIndex,
  60. headImg: this.data.choseAvaSrc
  61. }
  62. console.log(data);
  63. wx.showLoading({
  64. title: '加载中...',
  65. mask: true
  66. })
  67. createDocument(data).then(res => {
  68. wx.hideLoading()
  69. wx.showToast({
  70. title: '保存成功',
  71. icon: 'success'
  72. })
  73. wx.navigateBack()
  74. }).catch(e => {
  75. wx.hideLoading()
  76. wx.showModal({
  77. content: e,
  78. confirmColor: '#333',
  79. showCancel: false
  80. })
  81. })
  82. },
  83. /**
  84. * 选择关系
  85. */
  86. bindReaPickerChange(e) {
  87. console.log(e);
  88. var value = this.data.relativeRange[e.detail.value]
  89. var selsectKey = Number(e.detail.value) + 1
  90. console.log(selsectKey);
  91. this.setData({
  92. relativeText: value,
  93. relateIndex: selsectKey
  94. })
  95. },
  96. /**
  97. * 用户选择生日
  98. */
  99. bindPickerChange(e) {
  100. this.setData({
  101. pickerText: e.detail.value
  102. })
  103. },
  104. /**
  105. * 切换性别
  106. */
  107. handleSex(e) {
  108. console.log(e.currentTarget.dataset.index);
  109. this.setData({
  110. sexIndex: e.currentTarget.dataset.index
  111. })
  112. },
  113. /**
  114. * 用户选择头像
  115. */
  116. handleChoseAvatar() {
  117. var that = this
  118. wx.chooseMedia({
  119. count: 1,
  120. mediaType: ['image'],
  121. sourceType: ['album'],
  122. success (res) {
  123. console.log(res);
  124. // tempFilePath可以作为 img 标签的 src 属性显示图片
  125. const tempFilePaths = res.tempFiles[0].tempFilePath
  126. that.uploadImg(tempFilePaths)
  127. }
  128. })
  129. },
  130. // 上传用户头像到服务器
  131. uploadImg (filePath) {
  132. var that = this
  133. var data = {
  134. filePath: filePath
  135. }
  136. wx.showLoading({
  137. title: '上传中...',
  138. mask: true
  139. })
  140. userUploadHeadImg(data).then(res => {
  141. wx.hideLoading()
  142. var response = JSON.parse(res)
  143. if (response.code != '000') {
  144. wx.showToast({
  145. title: '头像上传失败',
  146. icon: 'error'
  147. })
  148. } else {
  149. that.setData({
  150. choseAvatar: true,
  151. choseAvaSrc: response.data
  152. })
  153. }
  154. }).catch(e => {
  155. wx.hideLoading()
  156. wx.showModal({
  157. content: e,
  158. confirmColor: '#333',
  159. showCancel: false
  160. })
  161. })
  162. }
  163. })