basic.js 7.9 KB

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