basic.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. const app = getApp()
  2. import WxValidate from '../../utils/wxValidate'
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. frameStyle: { useTop: true, name: '个人信息', leftArrow: true, useBar: false },
  9. form: { icon: [] },
  10. // 性别
  11. genderList: [],
  12. // 账号绑定信息
  13. bindForm: {}
  14. },
  15. initValidate() {
  16. const rules = { icon: { required: true }, name: { required: true }, gender: { required: true }, phone: { required: true, tel: true } }
  17. // 验证字段的提示信息,若不传则调用默认的信息
  18. const messages = { icon: { required: '请选择头像', }, name: { required: '请输入用户姓名', }, gender: { required: '请选择性别', }, phone: { required: '请输入手机号', } };
  19. this.WxValidate = new WxValidate(rules, messages)
  20. },
  21. // 返回
  22. back: function () {
  23. wx.navigateBack({ delta: 1 })
  24. },
  25. imgUpl: function (e) {
  26. const that = this;
  27. let data = that.data.form.icon;
  28. data.push(e.detail)
  29. that.setData({ 'form.icon': data })
  30. },
  31. // 删除图片
  32. imgDel: function (e) {
  33. const that = this;
  34. let list = that.data.form.icon;
  35. let arr = list.filter((i, index) => index != e.detail.index)
  36. that.setData({ 'form.icon': arr })
  37. },
  38. // 选择性别
  39. genderChange: function (e) {
  40. const that = this;
  41. let data = that.data.genderList[e.detail.value];
  42. if (data) {
  43. that.setData({ 'form.gender': data.value });
  44. that.setData({ 'form.zhGender': data.label })
  45. }
  46. },
  47. typeChange: function (e) {
  48. const { value } = e.detail;
  49. if (value == '0') wx.showToast({ title: `如进行身份切换,请在维护信息成功后退出登录,系统会自动进行身份置换!`, icon: 'none', duration: 2000 })
  50. },
  51. // 提交保存
  52. onSubmit: async function (e) {
  53. const that = this;
  54. const params = e.detail.value;
  55. const form = that.data.form;
  56. params.icon = form.icon;
  57. if (!this.WxValidate.checkForm(params)) {
  58. const error = this.WxValidate.errorList[0];
  59. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  60. return false
  61. } else {
  62. const arr = await app.$post(`/user/${form._id}`, params);
  63. if (arr.errcode == '0') { wx.showToast({ title: `维护信息完成`, icon: 'success', duration: 2000 }); that.back(); }
  64. else wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 })
  65. }
  66. },
  67. // 账号绑定
  68. scanCode: function () {
  69. const that = this;
  70. wx.showModal({
  71. title: '提示',
  72. content: '你确定要使用此微信绑定账号吗?',
  73. async success(res) {
  74. if (res.confirm) {
  75. // 账号绑定
  76. that.scanCode1();
  77. }
  78. }
  79. })
  80. },
  81. // 账号绑定
  82. scanCode1: function () {
  83. const that = this;
  84. wx.scanCode({
  85. async success(res) {
  86. let arr = res.result.split('&&');
  87. let id = arr[0];
  88. let type = arr[1];
  89. // 绑定账号
  90. that.searchBind(id, type);
  91. }
  92. })
  93. },
  94. // 绑定账号
  95. searchBind: function (id, type) {
  96. wx.getStorage({
  97. key: 'user',
  98. success: async res => {
  99. // 学校
  100. let arr;
  101. let aee;
  102. if (type == '1') { arr = await app.$post(`/school/${id}`, { user_id: res.data._id }); aee = await app.$post(`/user/${res.data._id}`, { type: type }); }
  103. else if (type == '2') { arr = await app.$post(`/coach/${id}`, { user_id: res.data._id }); aee = await app.$post(`/user/${res.data._id}`, { type: type }); }
  104. else if (type == '3') { arr = await app.$post(`/student/${id}`, { user_id: res.data._id }); aee = await app.$post(`/user/${res.data._id}`, { type: type }); }
  105. if (arr.errcode == '0' && aee.errcode == '0') {
  106. wx.showModal({
  107. title: '提示',
  108. content: '绑定账号成功,请退出登录',
  109. async success(res) {
  110. if (res.confirm) { wx.clearStorage(); wx.redirectTo({ url: '/pages/index/index' }) }
  111. }
  112. })
  113. } else { wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 }); wx.showToast({ title: `${aee.errmsg}`, icon: 'error', duration: 2000 }) }
  114. },
  115. fail: res => {
  116. wx.redirectTo({ url: '/pages/index/index', })
  117. }
  118. })
  119. },
  120. /**
  121. * 生命周期函数--监听页面加载
  122. */
  123. onLoad: async function (options) {
  124. const that = this;
  125. //验证规则函数
  126. that.initValidate();
  127. // 查询其他信息
  128. await that.searchOther();
  129. // 监听用户是否登录
  130. await that.watchLogin();
  131. },
  132. searchOther: async function () {
  133. const that = this;
  134. let arr;
  135. arr = await app.$get(`/dict`, { code: 'gender' });
  136. if (arr.errcode == '0' && arr.total > 0) {
  137. let list = arr.data[0].list;
  138. that.setData({ genderList: list })
  139. }
  140. },
  141. // 监听用户是否登录
  142. watchLogin: async function () {
  143. const that = this;
  144. wx.getStorage({
  145. key: 'user',
  146. success: async res => {
  147. const arr = await app.$get(`/user/${res.data.id}`);
  148. if (arr.errcode == '0') {
  149. let gender = that.data.genderList.find(i => i.value == arr.data.gender);
  150. if (gender) arr.data.zhGender = gender.label;
  151. that.setData({ form: arr.data })
  152. }
  153. },
  154. fail: res => {
  155. wx.redirectTo({ url: '/pages/index/index', })
  156. }
  157. })
  158. },
  159. /**
  160. * 生命周期函数--监听页面初次渲染完成
  161. */
  162. onReady: function () {
  163. },
  164. /**
  165. * 生命周期函数--监听页面显示
  166. */
  167. onShow: function () {
  168. },
  169. /**
  170. * 生命周期函数--监听页面隐藏
  171. */
  172. onHide: function () {
  173. },
  174. /**
  175. * 生命周期函数--监听页面卸载
  176. */
  177. onUnload: function () {
  178. },
  179. /**
  180. * 页面相关事件处理函数--监听用户下拉动作
  181. */
  182. onPullDownRefresh: function () {
  183. },
  184. /**
  185. * 页面上拉触底事件的处理函数
  186. */
  187. onReachBottom: function () {
  188. },
  189. /**
  190. * 用户点击右上角分享
  191. */
  192. onShareAppMessage: function () {
  193. }
  194. })