createFile.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. from: '',
  10. choseAvatar: false,
  11. choseAvaSrc: '',
  12. username: '',
  13. sexIndex: '0',
  14. relateIndex: 1,
  15. pickerText: '请选择',
  16. relativeText: '请选择',
  17. relativeRange: ['本人','父母', '子女', '配偶', '朋友', '祖孙', '兄妹', '其他'] // 关系 关系 1.本人 2.父母 3.子女 4.配偶 5.朋友 6.祖孙、7.兄妹、8.其他
  18. },
  19. onLoad (options) {
  20. this.setData({
  21. from: options.from
  22. })
  23. },
  24. bindUserNameInput (e) {
  25. this.setData({
  26. username: e.detail.value
  27. })
  28. },
  29. /**
  30. * 保存
  31. */
  32. handleSave() {
  33. var that = this
  34. if (this.data.choseAvaSrc == '') {
  35. wx.showToast({
  36. title: '请选择头像',
  37. icon: 'error'
  38. })
  39. return
  40. }
  41. if (this.data.username == '') {
  42. wx.showToast({
  43. title: '请输入姓名',
  44. icon: 'error'
  45. })
  46. return
  47. }
  48. if (this.data.pickerText == '请选择') {
  49. wx.showToast({
  50. title: '请选择生日',
  51. icon: 'error'
  52. })
  53. return
  54. }
  55. if (this.data.relativeText == '请选择') {
  56. wx.showToast({
  57. title: '请选择关系',
  58. icon: 'error'
  59. })
  60. return
  61. }
  62. wx.showModal({
  63. title: '温馨提示',
  64. content: '新建健康档案时,请您如实填写信息,如填写不正确将会影响您的健康指标!',
  65. confirmColor: '#333',
  66. cancelColor: '#666',
  67. success (res) {
  68. if (res.confirm) {
  69. that.submitCreate()
  70. }
  71. }
  72. })
  73. },
  74. // 提交新建人员表单
  75. submitCreate () {
  76. var that = this
  77. var data = {
  78. realName: this.data.username,
  79. sex: this.data.sexIndex == 0 ? true : false,
  80. birthday: this.data.pickerText,
  81. relationship: this.data.relateIndex,
  82. headImg: this.data.choseAvaSrc
  83. }
  84. wx.showLoading({
  85. title: '加载中...',
  86. mask: true
  87. })
  88. createDocument(data).then(res => {
  89. wx.hideLoading()
  90. wx.showToast({
  91. title: '保存成功',
  92. icon: 'success'
  93. })
  94. var timer = setTimeout(() => {
  95. if (that.data.from == 'buy') {
  96. wx.navigateBack({
  97. delta: 2
  98. })
  99. } else {
  100. wx.navigateBack()
  101. }
  102. clearTimeout(timer)
  103. }, 1500);
  104. }).catch(e => {
  105. wx.hideLoading()
  106. wx.showModal({
  107. content: e,
  108. confirmColor: '#333',
  109. showCancel: false
  110. })
  111. })
  112. },
  113. /**
  114. * 选择关系
  115. */
  116. bindReaPickerChange(e) {
  117. console.log(e);
  118. var value = this.data.relativeRange[e.detail.value]
  119. var selsectKey = Number(e.detail.value) + 1
  120. console.log(selsectKey);
  121. this.setData({
  122. relativeText: value,
  123. relateIndex: selsectKey
  124. })
  125. },
  126. /**
  127. * 用户选择生日
  128. */
  129. bindPickerChange(e) {
  130. this.setData({
  131. pickerText: e.detail.value
  132. })
  133. },
  134. /**
  135. * 切换性别
  136. */
  137. handleSex(e) {
  138. console.log(e.currentTarget.dataset.index);
  139. this.setData({
  140. sexIndex: e.currentTarget.dataset.index
  141. })
  142. },
  143. /**
  144. * 用户选择头像
  145. */
  146. handleChoseAvatar() {
  147. var that = this
  148. wx.chooseMedia({
  149. count: 1,
  150. mediaType: ['image'],
  151. sourceType: ['album'],
  152. success (res) {
  153. console.log(res);
  154. // tempFilePath可以作为 img 标签的 src 属性显示图片
  155. const tempFilePaths = res.tempFiles[0].tempFilePath
  156. that.uploadImg(tempFilePaths)
  157. }
  158. })
  159. },
  160. // 上传用户头像到服务器
  161. uploadImg (filePath) {
  162. var that = this
  163. var data = {
  164. filePath: filePath
  165. }
  166. wx.showLoading({
  167. title: '上传中...',
  168. mask: true
  169. })
  170. userUploadHeadImg(data).then(res => {
  171. wx.hideLoading()
  172. var response = JSON.parse(res)
  173. if (response.code != '000') {
  174. wx.showToast({
  175. title: '头像上传失败',
  176. icon: 'error'
  177. })
  178. } else {
  179. that.setData({
  180. choseAvatar: true,
  181. choseAvaSrc: response.data
  182. })
  183. }
  184. }).catch(e => {
  185. wx.hideLoading()
  186. wx.showModal({
  187. content: e,
  188. confirmColor: '#333',
  189. showCancel: false
  190. })
  191. })
  192. }
  193. })