information.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // parents/pages/information/information.js
  2. import { routers, viewImage, sharePage, isFn, getGlobalVal } from '../../../utils/util.js'
  3. import { GetVipInfo, UploadFile, SaveVipInfo, GetMineInfo } from '../../../utils/api.js'
  4. const { navigateTo, redirectTo, navigateBack } = routers()
  5. const { globalData, hasLibraryService, hasVipService } = getApp()
  6. const { baseImgUrl, qrcodeInvalidToastText, noVipListToastText} = globalData
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. baseImgUrl,
  13. isLoaded: 0,
  14. vip: {
  15. imgUrl: '',
  16. childName: '',
  17. sex: '',
  18. grade: '',
  19. school: '',
  20. parentName: '',
  21. phone: '',
  22. parentRole: '',
  23. libId:''
  24. },
  25. isSchool:false,
  26. gradeArray: ['一年级', '二年级', '三年级', '四年级', '五年级', '六年级', '七年级', '八年级', '九年级'],
  27. gradeIndex: 0,
  28. sexArray: ['男', '女'],
  29. sexIndex: 0,
  30. parentRoleArray: ['爸爸', '妈妈', '爷爷', '奶奶', '外公', '外婆', '其他'],
  31. parentRoleIndex: 0,
  32. footerArray: [
  33. {
  34. type: 1,
  35. text: '取消'
  36. },
  37. {
  38. type: 2,
  39. text: '保存'
  40. }
  41. ]
  42. },
  43. /**
  44. * 生命周期函数--监听页面加载
  45. */
  46. onLoad: function (options) {
  47. if (options.zindex!=3){
  48. options.vipId = getGlobalVal('vipId', `vipId_${getGlobalVal('userId')}`) || ''
  49. }
  50. console.log(options)
  51. const {vip}=this.data
  52. vip.school = options.name || ''
  53. vip.sex = options.sex || ''
  54. vip.phone = options.phone || ''
  55. vip.parentName = options.parentName || ''
  56. vip.parentRole = options.parentRole || ''
  57. vip.grade = options.grade || ''
  58. vip.childName = options.childName || ''
  59. vip.imgUrl = options.imgUrl || ''
  60. vip.vipId = options.vipId
  61. this.setData({ options, vip, isLoaded: 1})
  62. if (!options.isSchool){
  63. this.getData()
  64. }
  65. },
  66. /**
  67. * 生命周期函数--监听页面初次渲染完成
  68. */
  69. onReady: function () {
  70. const obj = { navigateTo, redirectTo, navigateBack, viewImage }
  71. for (const i in obj) {
  72. this[i] = obj[i]
  73. }
  74. },
  75. /**
  76. * 生命周期函数--监听页面显示
  77. */
  78. onShow: function () {
  79. },
  80. /**
  81. * 生命周期函数--监听页面隐藏
  82. */
  83. onHide: function () {
  84. },
  85. /**
  86. * 生命周期函数--监听页面卸载
  87. */
  88. onUnload: function () {
  89. },
  90. /**
  91. * 页面相关事件处理函数--监听用户下拉动作
  92. */
  93. onPullDownRefresh: function () {
  94. },
  95. /**
  96. * 页面上拉触底事件的处理函数
  97. */
  98. onReachBottom: function () {
  99. },
  100. /**
  101. * 用户点击右上角分享
  102. */
  103. onShareAppMessage: function () {
  104. if (isFn(sharePage)) return sharePage()
  105. },
  106. /**
  107. * 获取个人资料
  108. */
  109. getData: function (cb) {
  110. const continuousFn = { fn: this.getData, param: { ...arguments } }
  111. if (!hasVipService()) {
  112. cb && cb()
  113. return
  114. }
  115. const { vipId } = this.data.options
  116. GetVipInfo({ data: { vipId }, continuousFn }).then(res => {
  117. this.setData({ vip: { ...res.data } })
  118. cb && cb(res)
  119. }).catch(res => {
  120. cb && cb(res)
  121. })
  122. },
  123. /**
  124. * 上传头像
  125. */
  126. uploadImgCtl: function () {
  127. const continuousFn = { fn: this.uploadImgCtl, param: { ...arguments } }
  128. const {vip}=this.data;
  129. wx.chooseImage({
  130. count: 1,
  131. success: res => {
  132. UploadFile({ file: res.tempFilePaths, continuousFn }).then(res => {
  133. vip.imgUrl = res[0]
  134. this.setData({ vip})
  135. })
  136. }
  137. })
  138. },
  139. /**
  140. * 保存输入值
  141. */
  142. inputCtl: function (e) {
  143. const { value } = e.detail
  144. const { type } = e.currentTarget.dataset
  145. const { vip } = this.data
  146. vip[type] = value
  147. this.setData({ vip })
  148. },
  149. /**
  150. * 改变年级
  151. */
  152. pickerGradeCtl: function (e) {
  153. const { value } = e.detail
  154. const { gradeArray, gradeIndex,vip } = this.data
  155. if (value == gradeIndex) {
  156. return
  157. }
  158. vip.grade = gradeArray[value]
  159. this.setData({ vip, gradeIndex: value })
  160. },
  161. /**
  162. * 改变性别
  163. */
  164. pickerSexCtl: function (e) {
  165. const { value } = e.detail
  166. const { sexArray, sexIndex,vip } = this.data
  167. vip.sex = ~~value + 1
  168. this.setData({ vip, sexIndex: value })
  169. },
  170. /**
  171. * 改变角色
  172. */
  173. pickerRoleCtl: function (e) {
  174. console.log(e)
  175. const { value } = e.detail
  176. const { parentRoleArray, parentRoleIndex } = this.data
  177. this.setData({ 'vip.parentRole': parentRoleArray[~~value], parentRoleIndex: value })
  178. console.log(parentRoleArray[~~value])
  179. },
  180. /**
  181. * 获取会员卡列表
  182. */
  183. getVipList: function () {
  184. const continuousFn = { fn: this.getVipList, param: { ...arguments } }
  185. const userId = getGlobalVal('userId')
  186. GetMineInfo({ continuousFn }).then(res => {
  187. const { vipList = null } = res.data
  188. if (vipList.length > 0) {
  189. wx.setStorageSync(`vipList_${userId}`, vipList)
  190. } else {
  191. wx.showToast({
  192. title: noVipListToastText,
  193. icon: 'none'
  194. })
  195. }
  196. })
  197. },
  198. /**
  199. * 提交按钮
  200. */
  201. submitCtl: function (e) {
  202. const continuousFn = { fn: this.submitCtl, param: { ...arguments } }
  203. const { type } = e.currentTarget.dataset
  204. const { vip, options } = this.data
  205. if (options.zindex == 3) {
  206. vip.libId = options.libId || ''
  207. }
  208. if (type == 1) {
  209. wx.navigateBack({
  210. delta: 1
  211. })
  212. } else if (type == 2) {
  213. if (!vip['childName']) {
  214. wx.showToast({
  215. title: '请填写完姓名',
  216. icon: 'none'
  217. })
  218. return
  219. }
  220. SaveVipInfo({ data: { ...vip }, continuousFn }).then(res => {
  221. if (options.zindex == 3){
  222. this.getVipList()
  223. wx.showToast({
  224. title: '添加成功',
  225. })
  226. }else{
  227. wx.showToast({
  228. title: '保存成功',
  229. })
  230. }
  231. setTimeout(() => {
  232. navigateBack()
  233. }, 2000)
  234. })
  235. }
  236. }
  237. })