user-form.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. var tcm = require('../../../../utils/tcm.js')
  2. var COUNTRY_OPTIONS = [
  3. { value: '', label: '请选择' },
  4. { value: 'china', label: '中国' },
  5. { value: 'russia', label: '俄罗斯' },
  6. { value: 'kazakhstan', label: '哈萨克斯坦' },
  7. { value: 'mongolia', label: '蒙古' },
  8. { value: 'japan', label: '日本' },
  9. { value: 'korea', label: '韩国' },
  10. { value: 'usa', label: '美国' },
  11. { value: 'uk', label: '英国' },
  12. { value: 'germany', label: '德国' },
  13. { value: 'france', label: '法国' },
  14. { value: 'other', label: '其他' }
  15. ]
  16. var RACE_OPTIONS = [
  17. { value: '', label: '请选择' },
  18. { value: 'asian', label: '亚裔' },
  19. { value: 'caucasian', label: '白人' },
  20. { value: 'african', label: '非裔' },
  21. { value: 'hispanic', label: '拉丁裔' },
  22. { value: 'mixed', label: '混血' },
  23. { value: 'other', label: '其他' }
  24. ]
  25. var OCCUPATION_OPTIONS = [
  26. { value: '', label: '请选择' },
  27. { value: 'sedentary_office', label: '久坐办公(文员/行政/财务等)' },
  28. { value: 'it_programmer', label: 'IT/程序员' },
  29. { value: 'driver', label: '司机(出租车/货运/公交等)' },
  30. { value: 'teacher', label: '教师/培训师' },
  31. { value: 'medical_staff', label: '医护人员' },
  32. { value: 'service_industry', label: '服务行业(餐饮/零售/酒店等)' },
  33. { value: 'heavy_labor', label: '重体力劳动(建筑/搬运/制造等)' },
  34. { value: 'outdoor_worker', label: '户外工作者(农业/快递/外卖等)' },
  35. { value: 'student', label: '学生' },
  36. { value: 'homemaker', label: '家庭主妇/夫' },
  37. { value: 'retired', label: '退休人员' },
  38. { value: 'other', label: '其他' }
  39. ]
  40. function buildHeightOptions() {
  41. var list = []
  42. for (var i = 100; i <= 220; i++) {
  43. list.push({
  44. value: String(i),
  45. label: i + ' cm'
  46. })
  47. }
  48. return list
  49. }
  50. function buildWeightOptions() {
  51. var list = []
  52. for (var i = 60; i <= 400; i++) {
  53. var num = i / 2
  54. list.push({
  55. value: num.toFixed(1),
  56. label: num.toFixed(1) + ' kg'
  57. })
  58. }
  59. return list
  60. }
  61. var HEIGHT_OPTIONS = buildHeightOptions()
  62. var TARGET_WEIGHT_OPTIONS = buildWeightOptions()
  63. function findIndexByValue(list, value) {
  64. for (var i = 0; i < list.length; i++) {
  65. if (String(list[i].value) === String(value)) {
  66. return i
  67. }
  68. }
  69. return 0
  70. }
  71. function findLabelByValue(list, value) {
  72. var index = findIndexByValue(list, value)
  73. return list[index] ? list[index].label : '请选择'
  74. }
  75. function formatBirthday(value) {
  76. if (!value) {
  77. return '1999-01-01'
  78. }
  79. return String(value).split('T')[0]
  80. }
  81. function calcAge(birthday) {
  82. if (!birthday) {
  83. return 25
  84. }
  85. var date = new Date(birthday)
  86. if (isNaN(date.getTime())) {
  87. return 25
  88. }
  89. var now = new Date()
  90. var age = now.getFullYear() - date.getFullYear()
  91. var monthDiff = now.getMonth() - date.getMonth()
  92. var dayDiff = now.getDate() - date.getDate()
  93. if (monthDiff < 0 || (monthDiff === 0 && dayDiff < 0)) {
  94. age--
  95. }
  96. if (age < 1) {
  97. age = 1
  98. }
  99. return age
  100. }
  101. Page({
  102. data: {
  103. isEditMode: false,
  104. saving: false,
  105. sexIndex: 0,
  106. sexOptions: ['男', '女'],
  107. countryOptions: COUNTRY_OPTIONS,
  108. raceOptions: RACE_OPTIONS,
  109. occupationOptions: OCCUPATION_OPTIONS,
  110. heightOptions: HEIGHT_OPTIONS,
  111. targetWeightOptions: TARGET_WEIGHT_OPTIONS,
  112. countryIndex: 0,
  113. raceIndex: 0,
  114. occupationIndex: 0,
  115. heightIndex: findIndexByValue(HEIGHT_OPTIONS, '170'),
  116. targetWeightIndex: findIndexByValue(TARGET_WEIGHT_OPTIONS, '65.0'),
  117. form: {
  118. ID: null,
  119. active: true,
  120. avatar: '',
  121. birthday: '1999-01-01',
  122. deviceId: 0,
  123. height: '170',
  124. nickName: '',
  125. sex: 0,
  126. targetWeight: '65.0',
  127. wxUserId: 0,
  128. country: '',
  129. race: '',
  130. occupation: '',
  131. createdAt: '',
  132. updatedAt: ''
  133. }
  134. },
  135. onLoad: function (options) {
  136. if (options && options.mode === 'edit') {
  137. var user = wx.getStorageSync('tcmEditScaleUser')
  138. if (user && user.ID) {
  139. this.applyUserData(user)
  140. }
  141. }
  142. },
  143. applyUserData: function (user) {
  144. var birthday = formatBirthday(user.birthday || user.Birthday)
  145. var height = String(user.height || 170)
  146. var targetWeight = String(user.targetWeight || '65.0')
  147. var country = user.country || ''
  148. var race = user.race || ''
  149. var occupation = user.occupation || ''
  150. var sex = Number(user.sex || 0)
  151. this.setData({
  152. isEditMode: true,
  153. sexIndex: sex,
  154. countryIndex: findIndexByValue(COUNTRY_OPTIONS, country),
  155. raceIndex: findIndexByValue(RACE_OPTIONS, race),
  156. occupationIndex: findIndexByValue(OCCUPATION_OPTIONS, occupation),
  157. heightIndex: findIndexByValue(HEIGHT_OPTIONS, height),
  158. targetWeightIndex: findIndexByValue(TARGET_WEIGHT_OPTIONS, targetWeight),
  159. form: {
  160. ID: user.ID,
  161. active: user.active !== false,
  162. avatar: user.avatar || '',
  163. birthday: birthday,
  164. deviceId: user.deviceId || 0,
  165. height: height,
  166. nickName: user.nickName || '',
  167. sex: sex,
  168. targetWeight: targetWeight,
  169. wxUserId: user.wxUserId || 0,
  170. country: country,
  171. race: race,
  172. occupation: occupation,
  173. createdAt: user.createdAt || user.CreatedAt || '',
  174. updatedAt: user.updatedAt || user.UpdatedAt || ''
  175. }
  176. })
  177. },
  178. updateNickName: function (e) {
  179. this.setData({
  180. 'form.nickName': e.detail.value
  181. })
  182. },
  183. updateBirthday: function (e) {
  184. this.setData({
  185. 'form.birthday': e.detail.value
  186. })
  187. },
  188. changeSex: function (e) {
  189. var index = Number(e.detail.value)
  190. this.setData({
  191. sexIndex: index,
  192. 'form.sex': index
  193. })
  194. },
  195. changeHeight: function (e) {
  196. var index = Number(e.detail.value)
  197. this.setData({
  198. heightIndex: index,
  199. 'form.height': this.data.heightOptions[index].value
  200. })
  201. },
  202. changeTargetWeight: function (e) {
  203. var index = Number(e.detail.value)
  204. this.setData({
  205. targetWeightIndex: index,
  206. 'form.targetWeight': this.data.targetWeightOptions[index].value
  207. })
  208. },
  209. changeCountry: function (e) {
  210. var index = Number(e.detail.value)
  211. this.setData({
  212. countryIndex: index,
  213. 'form.country': this.data.countryOptions[index].value
  214. })
  215. },
  216. changeRace: function (e) {
  217. var index = Number(e.detail.value)
  218. this.setData({
  219. raceIndex: index,
  220. 'form.race': this.data.raceOptions[index].value
  221. })
  222. },
  223. changeOccupation: function (e) {
  224. var index = Number(e.detail.value)
  225. this.setData({
  226. occupationIndex: index,
  227. 'form.occupation': this.data.occupationOptions[index].value
  228. })
  229. },
  230. submitForm: function () {
  231. var that = this
  232. var form = that.data.form
  233. var height = Number(form.height)
  234. var targetWeight = Number(form.targetWeight)
  235. if (!form.nickName) {
  236. wx.showToast({
  237. title: '请输入昵称',
  238. icon: 'none'
  239. })
  240. return
  241. }
  242. if (form.nickName.length > 20) {
  243. wx.showToast({
  244. title: '昵称不能超过20个字符',
  245. icon: 'none'
  246. })
  247. return
  248. }
  249. if (!height || height < 100 || height > 220) {
  250. wx.showToast({
  251. title: '请选择正确身高',
  252. icon: 'none'
  253. })
  254. return
  255. }
  256. if (targetWeight && (targetWeight < 30 || targetWeight > 200)) {
  257. wx.showToast({
  258. title: '请选择正确目标体重',
  259. icon: 'none'
  260. })
  261. return
  262. }
  263. if (that.data.saving) {
  264. return
  265. }
  266. var now = new Date().toISOString()
  267. var birthday = form.birthday
  268. var payload = {
  269. ID: that.data.isEditMode ? form.ID : null,
  270. active: true,
  271. age: String(calcAge(birthday)),
  272. avatar: form.avatar || '',
  273. birthday: birthday,
  274. createdAt: form.createdAt || now,
  275. deviceId: form.deviceId || 0,
  276. height: String(height),
  277. nickName: form.nickName,
  278. sex: Number(form.sex),
  279. targetWeight: form.targetWeight || '65.0',
  280. updatedAt: now,
  281. wxUserId: form.wxUserId || 0,
  282. country: form.country || '',
  283. race: form.race || '',
  284. occupation: form.occupation || ''
  285. }
  286. that.setData({
  287. saving: true
  288. })
  289. var submitPromise = that.data.isEditMode ? tcm.updateScaleUser(payload) : tcm.createScaleUser(payload)
  290. submitPromise.then(function () {
  291. wx.setStorageSync('tcmUserChanged', true)
  292. wx.showToast({
  293. title: that.data.isEditMode ? '更新成功' : '创建成功',
  294. icon: 'success'
  295. })
  296. setTimeout(function () {
  297. wx.navigateBack()
  298. }, 300)
  299. }).catch(function (err) {
  300. that.setData({
  301. saving: false
  302. })
  303. wx.showToast({
  304. title: err && err.message ? err.message : '保存失败',
  305. icon: 'none'
  306. })
  307. })
  308. },
  309. getCountryLabel: function () {
  310. return findLabelByValue(COUNTRY_OPTIONS, this.data.form.country)
  311. },
  312. getRaceLabel: function () {
  313. return findLabelByValue(RACE_OPTIONS, this.data.form.race)
  314. },
  315. getOccupationLabel: function () {
  316. return findLabelByValue(OCCUPATION_OPTIONS, this.data.form.occupation)
  317. }
  318. })