basic.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 user_id = arr[0];
  145. let school_id = arr[1];
  146. const aee = await app.$post(`/utbj`, { user_id: user_id, school_id: school_id }, 'race')
  147. console.log(aee);
  148. }
  149. })
  150. },
  151. /**
  152. * 生命周期函数--监听页面加载
  153. */
  154. onLoad: async function (options) {
  155. const that = this;
  156. //验证规则函数
  157. that.initValidate();
  158. // 查询其他信息
  159. await that.searchOther();
  160. // 监听用户是否登录
  161. await that.watchLogin();
  162. },
  163. searchOther: async function () {
  164. const that = this;
  165. let arr;
  166. arr = await app.$get(`/dict`, { code: 'gender' });
  167. if (arr.errcode == '0' && arr.total > 0) {
  168. let list = arr.data[0].list;
  169. that.setData({ genderList: list })
  170. }
  171. },
  172. // 监听用户是否登录
  173. watchLogin: async function () {
  174. const that = this;
  175. wx.getStorage({
  176. key: 'user',
  177. success: async res => {
  178. that.setData({ user: res.data })
  179. const arr = await app.$get(`/user/${res.data.id}`);
  180. if (arr.errcode == '0') {
  181. let gender = that.data.genderList.find(i => i.value == arr.data.gender);
  182. if (gender) arr.data.zhGender = gender.label;
  183. that.setData({ form: arr.data })
  184. }
  185. },
  186. fail: res => {
  187. wx.redirectTo({ url: '/pages/index/index', })
  188. }
  189. })
  190. wx.getStorage({
  191. key: 'raceuser',
  192. success: async res => { that.setData({ raceuser: res.data }) },
  193. fail: res => { }
  194. })
  195. },
  196. /**
  197. * 生命周期函数--监听页面初次渲染完成
  198. */
  199. onReady: function () {
  200. },
  201. /**
  202. * 生命周期函数--监听页面显示
  203. */
  204. onShow: function () {
  205. },
  206. /**
  207. * 生命周期函数--监听页面隐藏
  208. */
  209. onHide: function () {
  210. },
  211. /**
  212. * 生命周期函数--监听页面卸载
  213. */
  214. onUnload: function () {
  215. },
  216. /**
  217. * 页面相关事件处理函数--监听用户下拉动作
  218. */
  219. onPullDownRefresh: function () {
  220. },
  221. /**
  222. * 页面上拉触底事件的处理函数
  223. */
  224. onReachBottom: function () {
  225. },
  226. /**
  227. * 用户点击右上角分享
  228. */
  229. onShareAppMessage: function () {
  230. }
  231. })