index.js 7.2 KB

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