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