information.js 6.0 KB

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