index.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // pages/login/login.js
  2. import WxValidate from '../../utils/wxValidate'
  3. const app = getApp()
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. showModal: false,
  10. // 主体高度
  11. infoHeight: '',
  12. frameStyle: { useTop: true, name: '创建团队', leftArrow: true, useBar: false },
  13. form: {
  14. type: ['乒乓球', '足球', '篮球'],
  15. objectType: [{ id: 0, name: '乒乓球' }, { id: 1, name: '足球' }, { id: 2, name: '篮球' },],
  16. create_time: '2020-02-02',
  17. create_id: '',
  18. create_user: '',
  19. },
  20. //成员
  21. members: [],
  22. index: 0,
  23. //选择成员列表
  24. item: [],
  25. // 上传图片
  26. fileList: [],
  27. },
  28. //验证必填项
  29. initValidate() {
  30. const rules = { name: { required: true }, type: { required: true }, create_user: { required: true }, }
  31. // 验证字段的提示信息,若不传则调用默认的信息
  32. const messages = { name: { required: '请输入团队名称' }, type: { required: '请输入团队类型' }, create_user: { required: '请输入团队创建人名称' }, };
  33. this.WxValidate = new WxValidate(rules, messages)
  34. },
  35. back: function () {
  36. wx.navigateBack({ url: '/pages/me/index' })
  37. },
  38. //上传图片
  39. imgUpload: function (e) {
  40. const that = this;
  41. let data = that.data.fileList;
  42. data.push(e.detail)
  43. that.setData({ fileList: data })
  44. },
  45. //删除图片
  46. imgDel: function (e) {
  47. const that = this;
  48. let data = that.data.fileList;
  49. let arr = data.filter((i, index) => index != e.detail.index)
  50. that.setData({ fileList: arr })
  51. },
  52. //点击确定关闭弹窗
  53. determine: function (e) {
  54. this.setData({ showModal: false })
  55. },
  56. //显示对话框
  57. clickme: function () {
  58. this.setData({
  59. showModal: true
  60. })
  61. },
  62. preventTouchMove: function () {
  63. },
  64. //关闭弹窗
  65. go: function () {
  66. this.setData({
  67. showModal: false
  68. })
  69. },
  70. //选择队员
  71. checkboxChange: function (e) {
  72. const that = this;
  73. let data = e.detail.value;
  74. let item = that.data.item;
  75. let members = [];
  76. for (const val of data) {
  77. let arr = item.find((i) => i._id == val);
  78. if (arr) members.push({ id: arr._id, nickname: arr.nickname, icon: arr.icon })
  79. }
  80. that.setData({ members: members })
  81. },
  82. //选择
  83. bindPickerChange: function (e) {
  84. this.setData({
  85. index: e.detail.value
  86. })
  87. },
  88. bindDateChange: function (e) {
  89. this.setData({
  90. ['form.create_time']: e.detail.value
  91. })
  92. },
  93. //点击减号删除
  94. delList: function (e) {
  95. var id = e.currentTarget.dataset.id;
  96. var members = this.data.members;
  97. for (var i = 0; i < members.length; i++) {
  98. if (members[i].id == id) {
  99. members.splice(i, 1)
  100. }
  101. }
  102. this.setData({ members: members })
  103. },
  104. //提交
  105. formSubmit: function (e) {
  106. const value = e.detail.value;
  107. if (!this.WxValidate.checkForm(value)) {
  108. const error = this.WxValidate.errorList[0];
  109. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  110. return false
  111. } else {
  112. value.logo = this.data.fileList;
  113. value.members = this.data.members;
  114. value.create_id = this.data.form.create_id;
  115. wx.request({
  116. url: `${app.globalData.publicUrl}/courtAdmin/api/team`, //接口地址
  117. method: "post",//请求方法
  118. //请求参数
  119. data: value,
  120. header: {},
  121. success: res => {
  122. if (res.data.errcode == 0) {
  123. wx.showToast({
  124. title: '创建团队成功',
  125. icon: 'success',
  126. duration: 2000//延迟两秒
  127. })
  128. return wx.redirectTo({ url: '/pages/me/index' })// 跳转页面
  129. } else {
  130. wx.showToast({
  131. title: '创建团队失败',
  132. icon: 'error',
  133. duration: 2000
  134. })
  135. }
  136. },
  137. error: err => {
  138. }
  139. })
  140. }
  141. },
  142. /**
  143. * 生命周期函数--监听页面加载
  144. */
  145. onLoad: function (options) {
  146. //验证规则函数
  147. this.initValidate()
  148. // 计算高度
  149. this.searchHeight()
  150. // 监听用户是否登录
  151. this.watchLogin();
  152. },
  153. // 计算高度
  154. searchHeight: function () {
  155. let frameStyle = this.data.frameStyle;
  156. let client = app.globalData.client;
  157. let infoHeight = client.windowHeight;
  158. // 是否去掉状态栏
  159. if (frameStyle.useTop) infoHeight = infoHeight - (client.statusBarHeight + client.getMenu.height + (client.getMenu.top - client.statusBarHeight) * 2);
  160. // 是否减去底部菜单
  161. if (frameStyle.useBar) infoHeight = infoHeight - 50;
  162. if (infoHeight) this.setData({ infoHeight: infoHeight })
  163. },
  164. // 监听用户是否登录
  165. watchLogin: function () {
  166. const that = this;
  167. wx.getStorage({
  168. key: 'token',
  169. success: res => {
  170. //数据请求
  171. wx.request({
  172. url: `${app.globalData.publicUrl}/courtAdmin/api/user`, //接口地址
  173. method: "get",
  174. data: {},
  175. header: {},
  176. success: res => {
  177. that.setData({ item: res.data.data })
  178. },
  179. error: err => {
  180. }
  181. })
  182. wx.request({
  183. url: `${app.globalData.publicUrl}/courtAdmin/api/user/${res.data.id}`, //接口地址
  184. method: "get",//请求方法
  185. data: {},//请求参数
  186. header: {},
  187. success: res => {
  188. that.setData({ ['form.create_user']: res.data.data.nickname, ['form.create_id']: res.data.data.id })
  189. },
  190. error: err => {
  191. }
  192. })
  193. },
  194. fail: res => {
  195. wx.redirectTo({ url: '/pages/login/index', })
  196. }
  197. })
  198. },
  199. /**
  200. * 生命周期函数--监听页面初次渲染完成
  201. */
  202. onReady: function () {
  203. },
  204. /**
  205. * 生命周期函数--监听页面显示
  206. */
  207. onShow: function () {
  208. },
  209. /**
  210. * 生命周期函数--监听页面隐藏
  211. */
  212. onHide: function () {
  213. },
  214. /**
  215. * 生命周期函数--监听页面卸载
  216. */
  217. onUnload: function () {
  218. },
  219. /**
  220. * 页面相关事件处理函数--监听用户下拉动作
  221. */
  222. onPullDownRefresh: function () {
  223. },
  224. /**
  225. * 页面上拉触底事件的处理函数
  226. */
  227. onReachBottom: function () {
  228. },
  229. /**
  230. * 用户点击右上角分享
  231. */
  232. onShareAppMessage: function () {
  233. }
  234. })