index.js 7.8 KB

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