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.

337 lines
6.8 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. return
  108. let that = this;
  109. // wx.chooseL
  110. // ocation({
  111. // success: function (res) {
  112. // that.setData({
  113. // address: res.name + '(' + res.address + ')',
  114. // addressStr: JSON.stringify(res)
  115. // })
  116. // },
  117. // fail: function (error) {
  118. // console.log(error)
  119. // },
  120. // complete: function (data) {
  121. // }
  122. // })
  123. },
  124. goSetChildren() {
  125. wx.navigateTo({
  126. url: '/pages/edit/edit?type=1',
  127. })
  128. },
  129. goSetAdress() {
  130. wx.navigateTo({
  131. url: '/pages/siteUser/siteUser',
  132. })
  133. },
  134. formSubmit(e) {
  135. const that = this
  136. let birthdate = null
  137. if (that.data.dateFlag == 2 && that.data.date) {
  138. birthdate = new Date(that.data.date).getTime();
  139. }
  140. const data = {
  141. avatarUrl: that.data.avatarUrl,
  142. nickName: e.detail.value.nickname.trim(),
  143. name: e.detail.value.username || "und",
  144. sex: 1 * that.data.gender + 1,
  145. birthdate: birthdate,
  146. height: e.detail.value.height ? e.detail.value.height : that.data.height,
  147. weight: e.detail.value.weight ? e.detail.value.weight : that.data.weight,
  148. address: that.data.addressStr || null,
  149. }
  150. console.log(data, 'data');
  151. if (!data.nickName) {
  152. wx.showToast({
  153. title: '请输入昵称',
  154. icon: 'error',
  155. duration: 2000
  156. })
  157. }
  158. // else if (!data.name) {
  159. // wx.showToast({
  160. // title: '请输入真实姓名',
  161. // icon: 'error',
  162. // duration: 2000
  163. // })
  164. // }
  165. else if (data.nickName.length > 16) {
  166. wx.showToast({
  167. title: '昵称上限为16字符',
  168. icon: 'none',
  169. duration: 2000
  170. })
  171. } else {
  172. Http.post({
  173. url: "/user/updateUserInfo",
  174. data,
  175. }).then(res => {
  176. if (res.code == 200) {
  177. wx.showToast({
  178. title: "信息提交成功",
  179. icon: 'success',
  180. duration: 1500,
  181. })
  182. setTimeout(() => {
  183. wx.navigateBack()
  184. }, 1500)
  185. }
  186. }).catch(err => {
  187. console.log(err);
  188. wx.showToast({
  189. title: err.message || "提交失败",
  190. icon: 'none',
  191. duration: 1500,
  192. })
  193. })
  194. }
  195. },
  196. goCanelAccount() {
  197. const that = this
  198. wx.showModal({
  199. title: '提示',
  200. content: '注销后请重新进入小程序',
  201. success(res) {
  202. if (res.confirm) {
  203. console.log('用户点击确定')
  204. that.goCancelUser()
  205. } else if (res.cancel) {
  206. console.log('用户点击取消')
  207. }
  208. }
  209. })
  210. },
  211. goCancelUser() {
  212. Http.get({
  213. url: "/user/cancelUser"
  214. }).then(res => {
  215. if (res.code == 200) {
  216. wx.showToast({
  217. title: "注销成功",
  218. icon: 'success',
  219. duration: 1500,
  220. })
  221. wx.exitMiniProgram()
  222. }
  223. }).catch(err => {
  224. console.log(err);
  225. wx.showToast({
  226. title: err.message || "注销失败",
  227. icon: 'none',
  228. duration: 1500,
  229. })
  230. })
  231. },
  232. /**
  233. * 生命周期函数--监听页面加载
  234. */
  235. onLoad(options) {
  236. if (app.globalData.token) {
  237. this.getUserInfo(app.globalData.token)
  238. }
  239. this.getType()
  240. wx.onThemeChange((result) => {
  241. this.setData({
  242. theme: result.theme
  243. })
  244. })
  245. },
  246. /**
  247. * 生命周期函数--监听页面初次渲染完成
  248. */
  249. onReady() {
  250. },
  251. /**
  252. * 生命周期函数--监听页面显示
  253. */
  254. onShow() {
  255. },
  256. /**
  257. * 生命周期函数--监听页面隐藏
  258. */
  259. onHide() {
  260. },
  261. /**
  262. * 生命周期函数--监听页面卸载
  263. */
  264. onUnload() {
  265. },
  266. /**
  267. * 页面相关事件处理函数--监听用户下拉动作
  268. */
  269. onPullDownRefresh() {
  270. },
  271. /**
  272. * 页面上拉触底事件的处理函数
  273. */
  274. onReachBottom() {
  275. },
  276. /**
  277. * 用户点击右上角分享
  278. */
  279. // onShareAppMessage() {
  280. // },
  281. onChooseAvatar(e) {
  282. const that = this
  283. const { avatarUrl } = e.detail
  284. wx.uploadFile({
  285. url: config.url + config.api.fileUpload,
  286. header: {
  287. 'token': app.globalData.token
  288. },
  289. filePath: avatarUrl,
  290. name: 'file',
  291. success: function (res) {
  292. that.setData({
  293. avatarUrl: JSON.parse(res.data).data.url
  294. })
  295. console.log(that.data.avatarUrl);
  296. }
  297. })
  298. },
  299. })