| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- var tcm = require('../../../../utils/tcm.js')
- var COUNTRY_OPTIONS = [
- { value: '', label: '请选择' },
- { value: 'china', label: '中国' },
- { value: 'russia', label: '俄罗斯' },
- { value: 'kazakhstan', label: '哈萨克斯坦' },
- { value: 'mongolia', label: '蒙古' },
- { value: 'japan', label: '日本' },
- { value: 'korea', label: '韩国' },
- { value: 'usa', label: '美国' },
- { value: 'uk', label: '英国' },
- { value: 'germany', label: '德国' },
- { value: 'france', label: '法国' },
- { value: 'other', label: '其他' }
- ]
- var RACE_OPTIONS = [
- { value: '', label: '请选择' },
- { value: 'asian', label: '亚裔' },
- { value: 'caucasian', label: '白人' },
- { value: 'african', label: '非裔' },
- { value: 'hispanic', label: '拉丁裔' },
- { value: 'mixed', label: '混血' },
- { value: 'other', label: '其他' }
- ]
- var OCCUPATION_OPTIONS = [
- { value: '', label: '请选择' },
- { value: 'sedentary_office', label: '久坐办公(文员/行政/财务等)' },
- { value: 'it_programmer', label: 'IT/程序员' },
- { value: 'driver', label: '司机(出租车/货运/公交等)' },
- { value: 'teacher', label: '教师/培训师' },
- { value: 'medical_staff', label: '医护人员' },
- { value: 'service_industry', label: '服务行业(餐饮/零售/酒店等)' },
- { value: 'heavy_labor', label: '重体力劳动(建筑/搬运/制造等)' },
- { value: 'outdoor_worker', label: '户外工作者(农业/快递/外卖等)' },
- { value: 'student', label: '学生' },
- { value: 'homemaker', label: '家庭主妇/夫' },
- { value: 'retired', label: '退休人员' },
- { value: 'other', label: '其他' }
- ]
- function buildHeightOptions() {
- var list = []
- for (var i = 100; i <= 220; i++) {
- list.push({
- value: String(i),
- label: i + ' cm'
- })
- }
- return list
- }
- function buildWeightOptions() {
- var list = []
- for (var i = 60; i <= 400; i++) {
- var num = i / 2
- list.push({
- value: num.toFixed(1),
- label: num.toFixed(1) + ' kg'
- })
- }
- return list
- }
- var HEIGHT_OPTIONS = buildHeightOptions()
- var TARGET_WEIGHT_OPTIONS = buildWeightOptions()
- function findIndexByValue(list, value) {
- for (var i = 0; i < list.length; i++) {
- if (String(list[i].value) === String(value)) {
- return i
- }
- }
- return 0
- }
- function findLabelByValue(list, value) {
- var index = findIndexByValue(list, value)
- return list[index] ? list[index].label : '请选择'
- }
- function formatBirthday(value) {
- if (!value) {
- return '1999-01-01'
- }
- return String(value).split('T')[0]
- }
- function calcAge(birthday) {
- if (!birthday) {
- return 25
- }
- var date = new Date(birthday)
- if (isNaN(date.getTime())) {
- return 25
- }
- var now = new Date()
- var age = now.getFullYear() - date.getFullYear()
- var monthDiff = now.getMonth() - date.getMonth()
- var dayDiff = now.getDate() - date.getDate()
- if (monthDiff < 0 || (monthDiff === 0 && dayDiff < 0)) {
- age--
- }
- if (age < 1) {
- age = 1
- }
- return age
- }
- Page({
- data: {
- isEditMode: false,
- saving: false,
- sexIndex: 0,
- sexOptions: ['男', '女'],
- countryOptions: COUNTRY_OPTIONS,
- raceOptions: RACE_OPTIONS,
- occupationOptions: OCCUPATION_OPTIONS,
- heightOptions: HEIGHT_OPTIONS,
- targetWeightOptions: TARGET_WEIGHT_OPTIONS,
- countryIndex: 0,
- raceIndex: 0,
- occupationIndex: 0,
- heightIndex: findIndexByValue(HEIGHT_OPTIONS, '170'),
- targetWeightIndex: findIndexByValue(TARGET_WEIGHT_OPTIONS, '65.0'),
- form: {
- ID: null,
- active: true,
- avatar: '',
- birthday: '1999-01-01',
- deviceId: 0,
- height: '170',
- nickName: '',
- sex: 0,
- targetWeight: '65.0',
- wxUserId: 0,
- country: '',
- race: '',
- occupation: '',
- createdAt: '',
- updatedAt: ''
- }
- },
- onLoad: function (options) {
- if (options && options.mode === 'edit') {
- var user = wx.getStorageSync('tcmEditScaleUser')
- if (user && user.ID) {
- this.applyUserData(user)
- }
- }
- },
- applyUserData: function (user) {
- var birthday = formatBirthday(user.birthday || user.Birthday)
- var height = String(user.height || 170)
- var targetWeight = String(user.targetWeight || '65.0')
- var country = user.country || ''
- var race = user.race || ''
- var occupation = user.occupation || ''
- var sex = Number(user.sex || 0)
- this.setData({
- isEditMode: true,
- sexIndex: sex,
- countryIndex: findIndexByValue(COUNTRY_OPTIONS, country),
- raceIndex: findIndexByValue(RACE_OPTIONS, race),
- occupationIndex: findIndexByValue(OCCUPATION_OPTIONS, occupation),
- heightIndex: findIndexByValue(HEIGHT_OPTIONS, height),
- targetWeightIndex: findIndexByValue(TARGET_WEIGHT_OPTIONS, targetWeight),
- form: {
- ID: user.ID,
- active: user.active !== false,
- avatar: user.avatar || '',
- birthday: birthday,
- deviceId: user.deviceId || 0,
- height: height,
- nickName: user.nickName || '',
- sex: sex,
- targetWeight: targetWeight,
- wxUserId: user.wxUserId || 0,
- country: country,
- race: race,
- occupation: occupation,
- createdAt: user.createdAt || user.CreatedAt || '',
- updatedAt: user.updatedAt || user.UpdatedAt || ''
- }
- })
- },
- updateNickName: function (e) {
- this.setData({
- 'form.nickName': e.detail.value
- })
- },
- updateBirthday: function (e) {
- this.setData({
- 'form.birthday': e.detail.value
- })
- },
- changeSex: function (e) {
- var index = Number(e.detail.value)
- this.setData({
- sexIndex: index,
- 'form.sex': index
- })
- },
- changeHeight: function (e) {
- var index = Number(e.detail.value)
- this.setData({
- heightIndex: index,
- 'form.height': this.data.heightOptions[index].value
- })
- },
- changeTargetWeight: function (e) {
- var index = Number(e.detail.value)
- this.setData({
- targetWeightIndex: index,
- 'form.targetWeight': this.data.targetWeightOptions[index].value
- })
- },
- changeCountry: function (e) {
- var index = Number(e.detail.value)
- this.setData({
- countryIndex: index,
- 'form.country': this.data.countryOptions[index].value
- })
- },
- changeRace: function (e) {
- var index = Number(e.detail.value)
- this.setData({
- raceIndex: index,
- 'form.race': this.data.raceOptions[index].value
- })
- },
- changeOccupation: function (e) {
- var index = Number(e.detail.value)
- this.setData({
- occupationIndex: index,
- 'form.occupation': this.data.occupationOptions[index].value
- })
- },
- submitForm: function () {
- var that = this
- var form = that.data.form
- var height = Number(form.height)
- var targetWeight = Number(form.targetWeight)
- if (!form.nickName) {
- wx.showToast({
- title: '请输入昵称',
- icon: 'none'
- })
- return
- }
- if (form.nickName.length > 20) {
- wx.showToast({
- title: '昵称不能超过20个字符',
- icon: 'none'
- })
- return
- }
- if (!height || height < 100 || height > 220) {
- wx.showToast({
- title: '请选择正确身高',
- icon: 'none'
- })
- return
- }
- if (targetWeight && (targetWeight < 30 || targetWeight > 200)) {
- wx.showToast({
- title: '请选择正确目标体重',
- icon: 'none'
- })
- return
- }
- if (that.data.saving) {
- return
- }
- var now = new Date().toISOString()
- var birthday = form.birthday
- var payload = {
- ID: that.data.isEditMode ? form.ID : null,
- active: true,
- age: String(calcAge(birthday)),
- avatar: form.avatar || '',
- birthday: birthday,
- createdAt: form.createdAt || now,
- deviceId: form.deviceId || 0,
- height: String(height),
- nickName: form.nickName,
- sex: Number(form.sex),
- targetWeight: form.targetWeight || '65.0',
- updatedAt: now,
- wxUserId: form.wxUserId || 0,
- country: form.country || '',
- race: form.race || '',
- occupation: form.occupation || ''
- }
- that.setData({
- saving: true
- })
- var submitPromise = that.data.isEditMode ? tcm.updateScaleUser(payload) : tcm.createScaleUser(payload)
- submitPromise.then(function () {
- wx.setStorageSync('tcmUserChanged', true)
- wx.showToast({
- title: that.data.isEditMode ? '更新成功' : '创建成功',
- icon: 'success'
- })
- setTimeout(function () {
- wx.navigateBack()
- }, 300)
- }).catch(function (err) {
- that.setData({
- saving: false
- })
- wx.showToast({
- title: err && err.message ? err.message : '保存失败',
- icon: 'none'
- })
- })
- },
- getCountryLabel: function () {
- return findLabelByValue(COUNTRY_OPTIONS, this.data.form.country)
- },
- getRaceLabel: function () {
- return findLabelByValue(RACE_OPTIONS, this.data.form.race)
- },
- getOccupationLabel: function () {
- return findLabelByValue(OCCUPATION_OPTIONS, this.data.form.occupation)
- }
- })
|