C端小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

288 lines
5.7 KiB

  1. let app = getApp();
  2. const Http = require("../../utils/HttpBasics");
  3. const util = require("../../utils/util.js");
  4. const bgColor = require("../../utils/bgColor.js")
  5. const config = require("../../config/config");
  6. const navigationBarHeight = (getApp().statusBarHeight + 44) + 'px'
  7. const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
  8. Page({
  9. /**
  10. * 页面的初始数据
  11. */
  12. data: {
  13. navigationBarHeight,
  14. avatarUrl: defaultAvatarUrl,
  15. nickName: '',
  16. username: '',
  17. date: '1988-03-12',
  18. dateEnd: '',
  19. dateFlag: 1,
  20. gender: '',
  21. sexArry: ['男', '女'],
  22. sexFlag: 1,
  23. height: '',
  24. weight: '',
  25. theme: wx.getSystemInfoSync().theme,
  26. typeLsit: {}
  27. },
  28. /**
  29. * 获取全局样式
  30. */
  31. getType() {
  32. Http.get({
  33. url: config.api.setType,
  34. data: {
  35. mouldType: app.globalData.mouldType
  36. }
  37. }).then(res => {
  38. const {
  39. code,
  40. data
  41. } = res
  42. if (code == 200) {
  43. app.globalData.typeLsit = data
  44. this.setData({
  45. typeLsit: data,
  46. })
  47. }
  48. })
  49. },
  50. getUserInfo(token) {
  51. let that = this
  52. Http.get({
  53. url: config.api.getScore,
  54. data: {
  55. token: token
  56. }
  57. }).then(res => {
  58. console.log(res, 'res')
  59. let avatarUrl = null
  60. if (res.data.avatarUrl.length > 1) {
  61. avatarUrl = res.data.avatarUrl
  62. } else {
  63. avatarUrl = defaultAvatarUrl
  64. }
  65. let gender = null
  66. let sexFlag = null
  67. if (1 * res.data.sex) {
  68. gender = res.data.sex - 1
  69. sexFlag = 2
  70. } else {
  71. gender = res.data.sex
  72. sexFlag = 1
  73. }
  74. let adress = null
  75. if (res.data.address) {
  76. adress = JSON.parse(res.data.address)
  77. }
  78. this.setData({
  79. avatarUrl: avatarUrl,
  80. nickName: res.data.nickName,
  81. username: res.data.name,
  82. gender: gender,
  83. sexFlag: sexFlag,
  84. date: util.fmtDate(parseInt(res.data.birthdate)),
  85. dateFlag: res.data.birthdate ? 2 : 1,
  86. height: res.data.height,
  87. weight: res.data.weight,
  88. address: adress ? adress.name + '(' + adress.address + ')' : ''
  89. })
  90. }).catch(err => {
  91. console.log(err, 'err');
  92. })
  93. },
  94. bindDateChange(e) {
  95. this.setData({
  96. date: e.detail.value,
  97. dateFlag: 2
  98. })
  99. },
  100. bindGenderChange(e) {
  101. this.setData({
  102. gender: e.detail.value,
  103. sexFlag: 2
  104. })
  105. },
  106. setAddress() {
  107. let that = this;
  108. wx.chooseLocation({
  109. success: function (res) {
  110. that.setData({
  111. address: res.name + '(' + res.address + ')',
  112. addressStr: JSON.stringify(res)
  113. })
  114. },
  115. fail: function (error) {
  116. console.log(error)
  117. },
  118. complete: function (data) {
  119. }
  120. })
  121. },
  122. goSetChildren() {
  123. wx.navigateTo({
  124. url: '/pages/edit/edit?type=1',
  125. })
  126. },
  127. goSetAdress() {
  128. wx.navigateTo({
  129. url: '/pages/siteUser/siteUser',
  130. })
  131. },
  132. formSubmit(e) {
  133. const that = this
  134. let birthdate = null
  135. if (that.data.dateFlag == 2 && that.data.date) {
  136. birthdate = new Date(that.data.date).getTime();
  137. }
  138. const data = {
  139. avatarUrl: that.data.avatarUrl,
  140. nickName: e.detail.value.nickname.trim(),
  141. name: e.detail.value.username,
  142. sex: 1 * that.data.gender + 1,
  143. birthdate: birthdate,
  144. height: e.detail.value.height ? e.detail.value.height : that.data.height,
  145. weight: e.detail.value.weight ? e.detail.value.weight : that.data.weight,
  146. address: that.data.addressStr || null,
  147. }
  148. console.log(data, 'data');
  149. if (!data.nickName) {
  150. wx.showToast({
  151. title: '请输入昵称',
  152. icon: 'error',
  153. duration: 2000
  154. })
  155. } else if (!data.name) {
  156. wx.showToast({
  157. title: '请输入真实姓名',
  158. icon: 'error',
  159. duration: 2000
  160. })
  161. } else if (data.nickName.length > 16) {
  162. wx.showToast({
  163. title: '昵称上限为16字符',
  164. icon: 'none',
  165. duration: 2000
  166. })
  167. } else {
  168. Http.post({
  169. url: "/user/updateUserInfo",
  170. data,
  171. }).then(res => {
  172. if (res.code == 200) {
  173. wx.showToast({
  174. title: "信息提交成功",
  175. icon: 'success',
  176. duration: 1500,
  177. })
  178. setTimeout(() => {
  179. wx.navigateBack()
  180. }, 1500)
  181. }
  182. }).catch(err => {
  183. console.log(err);
  184. })
  185. }
  186. },
  187. /**
  188. * 生命周期函数--监听页面加载
  189. */
  190. onLoad(options) {
  191. if (app.globalData.token) {
  192. this.getUserInfo(app.globalData.token)
  193. }
  194. this.getType()
  195. wx.onThemeChange((result) => {
  196. this.setData({
  197. theme: result.theme
  198. })
  199. })
  200. },
  201. /**
  202. * 生命周期函数--监听页面初次渲染完成
  203. */
  204. onReady() {
  205. },
  206. /**
  207. * 生命周期函数--监听页面显示
  208. */
  209. onShow() {
  210. },
  211. /**
  212. * 生命周期函数--监听页面隐藏
  213. */
  214. onHide() {
  215. },
  216. /**
  217. * 生命周期函数--监听页面卸载
  218. */
  219. onUnload() {
  220. },
  221. /**
  222. * 页面相关事件处理函数--监听用户下拉动作
  223. */
  224. onPullDownRefresh() {
  225. },
  226. /**
  227. * 页面上拉触底事件的处理函数
  228. */
  229. onReachBottom() {
  230. },
  231. /**
  232. * 用户点击右上角分享
  233. */
  234. // onShareAppMessage() {
  235. // },
  236. onChooseAvatar(e) {
  237. const that = this
  238. const { avatarUrl } = e.detail
  239. wx.uploadFile({
  240. url: config.url + config.api.fileUpload,
  241. header: {
  242. 'token': app.globalData.token
  243. },
  244. filePath: avatarUrl,
  245. name: 'file',
  246. success: function (res) {
  247. that.setData({
  248. avatarUrl: JSON.parse(res.data).data.url
  249. })
  250. console.log(that.data.avatarUrl);
  251. }
  252. })
  253. },
  254. })