basic.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. onlyFromCamera: true,
  86. async success(res) {
  87. let arr = res.result.split('&&');
  88. let id = arr[0];
  89. let type = arr[1];
  90. // 绑定账号
  91. that.searchBind(id, type);
  92. }
  93. })
  94. },
  95. // 绑定账号
  96. searchBind: function (id, type) {
  97. wx.getStorage({
  98. key: 'user',
  99. success: async res => {
  100. // 学校
  101. let arr;
  102. let aee;
  103. if (type == '1') { arr = await app.$post(`/school/${id}`, { user_id: res.data._id }); aee = await app.$post(`/user/${res.data._id}`, { type: type }); };
  104. if (arr.errcode == '0' && aee.errcode == '0') {
  105. wx.showModal({
  106. title: '提示',
  107. content: '绑定账号成功,请退出登录',
  108. async success(res) {
  109. if (res.confirm) { wx.clearStorage(); wx.redirectTo({ url: '/pages/index/index' }) }
  110. }
  111. })
  112. } else { wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 }); wx.showToast({ title: `${aee.errmsg}`, icon: 'error', duration: 2000 }) }
  113. },
  114. fail: res => {
  115. wx.redirectTo({ url: '/pages/index/index', })
  116. }
  117. })
  118. },
  119. /**
  120. * 生命周期函数--监听页面加载
  121. */
  122. onLoad: function (options) {
  123. const that = this;
  124. //验证规则函数
  125. that.initValidate();
  126. // 查询其他信息
  127. that.searchOther();
  128. // 监听用户是否登录
  129. that.watchLogin();
  130. },
  131. searchOther: async function () {
  132. const that = this;
  133. let arr;
  134. arr = await app.$get(`/dict`, { code: 'gender' });
  135. if (arr.errcode == '0' && arr.total > 0) {
  136. let list = arr.data[0].list;
  137. that.setData({ genderList: list })
  138. }
  139. },
  140. // 监听用户是否登录
  141. watchLogin: async function () {
  142. const that = this;
  143. wx.getStorage({
  144. key: 'user',
  145. success: async res => {
  146. const arr = await app.$get(`/user/${res.data.id}`);
  147. if (arr.errcode == '0') {
  148. let gender = that.data.genderList.find(i => i.value == arr.data.gender);
  149. if (gender) arr.data.zhGender = gender.label;
  150. that.setData({ form: arr.data })
  151. }
  152. },
  153. fail: res => {
  154. wx.redirectTo({ url: '/pages/index/index', })
  155. }
  156. })
  157. },
  158. /**
  159. * 生命周期函数--监听页面初次渲染完成
  160. */
  161. onReady: function () {
  162. },
  163. /**
  164. * 生命周期函数--监听页面显示
  165. */
  166. onShow: function () {
  167. },
  168. /**
  169. * 生命周期函数--监听页面隐藏
  170. */
  171. onHide: function () {
  172. },
  173. /**
  174. * 生命周期函数--监听页面卸载
  175. */
  176. onUnload: function () {
  177. },
  178. /**
  179. * 页面相关事件处理函数--监听用户下拉动作
  180. */
  181. onPullDownRefresh: function () {
  182. },
  183. /**
  184. * 页面上拉触底事件的处理函数
  185. */
  186. onReachBottom: function () {
  187. },
  188. /**
  189. * 用户点击右上角分享
  190. */
  191. onShareAppMessage: function () {
  192. }
  193. })