index.js 8.4 KB

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