index.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // pages/login/login.js
  2. import WxValidate from '../../utils/wxValidate'
  3. const app = getApp()
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. // 主体高度
  10. infoHeight: '',
  11. frameStyle: { useTop: true, name: '创建团队', leftArrow: true, useBar: false },
  12. form: {
  13. type: ['乒乓球', '足球', '篮球'],
  14. objectType: [{ id: 0, name: '乒乓球' }, { id: 1, name: '足球' }, { id: 2, name: '篮球' },],
  15. },
  16. lists: [{ id: '1', menbers: '' }],
  17. menbers:[],
  18. index: 0,
  19. // 上传图片
  20. fileList: [],
  21. },
  22. //选择
  23. bindPickerChange: function (e) {
  24. console.log('picker发送选择改变,携带值为', e.detail.value)
  25. this.setData({
  26. index: e.detail.value
  27. })
  28. },
  29. back: function () {
  30. wx.navigateBack({ url: '/pages/me/index' })
  31. },
  32. //点击加号添加
  33. addList: function () {
  34. var lists = this.data.lists;
  35. var newData = [];
  36. lists.push(newData);//实质是添加lists数组内容,使for循环多一次
  37. this.setData({
  38. lists: lists,
  39. })
  40. },
  41. //点击减号删除
  42. delList: function () {
  43. var lists = this.data.lists;
  44. lists.pop(); //实质是删除lists数组内容,使for循环少一次
  45. this.setData({
  46. lists: lists,
  47. })
  48. },
  49. //提交
  50. formSubmit: function (e) {
  51. const value = e.detail.value;
  52. const that = this;
  53. if (!this.WxValidate.checkForm(value)) {
  54. const error = this.WxValidate.errorList[0];
  55. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  56. return false
  57. } else {
  58. value.icon = this.data.fileList;
  59. wx.getStorage({
  60. key: 'token',
  61. success: function (res) {
  62. wx.request({
  63. url: `${app.globalData.publicUrl}/courtAdmin/api/team`, //接口地址
  64. method: "post",//请求方法
  65. data: value,//请求参数
  66. header: {},
  67. success: res => {
  68. // var menbers = JSON.parse(res.data.menbers)
  69. console.log(res);
  70. if (res.data.errcode == 0) {
  71. wx.showToast({
  72. title: '创建团队成功',
  73. icon: 'success',
  74. duration: 2000//延迟两秒
  75. })
  76. } else {
  77. wx.showToast({
  78. title: '创建团队失败',
  79. icon: 'error',
  80. duration: 2000
  81. })
  82. }
  83. },
  84. error: err => {
  85. console.log(err);
  86. }
  87. })
  88. }
  89. })
  90. }
  91. },
  92. //上传图片
  93. afterRead: function (event) {
  94. const { file } = event.detail;
  95. // 当设置 mutiple 为 true 时, file 为数组格式,否则为对象格式app.globalData.fileUrl:
  96. console.log(file);
  97. wx.uploadFile({
  98. url: `${app.globalData.imageUrl}/files/court/elimg/upload`,
  99. filePath: file.url,
  100. name: 'file',
  101. formData: {},
  102. success: (res) => {
  103. console.log(res);
  104. if (res.statusCode == '200') {
  105. let data = [{ name: JSON.parse(res.data).name, url: `${app.globalData.imageUrl}` + JSON.parse(res.data).uri }]
  106. // const { fileList = [] } = this.data;
  107. // fileList.push({ ...file, url: res.data });
  108. that.setData({ fileList: data });
  109. } else {
  110. wx.showToast({ title: `${res.data.errmsg}`, icon: 'fail', duration: 2000 })
  111. }
  112. },
  113. });
  114. },
  115. //验证必填项
  116. initValidate() {
  117. const rules = { name: { required: true }, type: { required: true }, create_user: { required: true }, }
  118. // 验证字段的提示信息,若不传则调用默认的信息
  119. const messages = { name: { required: '请输入团队名称' }, type: { required: '请输入团队类型' }, create_user: { required: '请输入团队创建人名称' }, };
  120. this.WxValidate = new WxValidate(rules, messages)
  121. },
  122. /**
  123. * 生命周期函数--监听页面加载
  124. */
  125. onLoad: function (options) {
  126. // 计算高度
  127. this.searchHeight()
  128. // 监听用户是否登录
  129. this.watchLogin();
  130. //验证规则函数
  131. this.initValidate()
  132. },
  133. // 监听用户是否登录
  134. watchLogin: function () {
  135. const that = this;
  136. wx.getStorage({
  137. key: 'token',
  138. success: res => {
  139. //数据请求
  140. },
  141. fail: res => {
  142. wx.redirectTo({ url: '/pages/login/index', })
  143. }
  144. })
  145. },
  146. // 计算高度
  147. searchHeight: function () {
  148. let frameStyle = this.data.frameStyle;
  149. let client = app.globalData.client;
  150. // 减去状态栏
  151. let infoHeight = client.windowHeight - (client.statusBarHeight + client.getMenu.height + (client.getMenu.top - client.statusBarHeight) * 2);
  152. // 是否减去底部菜单
  153. if (frameStyle.useBar) infoHeight = infoHeight - 50;
  154. if (infoHeight) this.setData({ infoHeight: infoHeight })
  155. },
  156. /**
  157. * 生命周期函数--监听页面初次渲染完成
  158. */
  159. onReady: function () {
  160. },
  161. /**
  162. * 生命周期函数--监听页面显示
  163. */
  164. onShow: function () {
  165. },
  166. /**
  167. * 生命周期函数--监听页面隐藏
  168. */
  169. onHide: function () {
  170. },
  171. /**
  172. * 生命周期函数--监听页面卸载
  173. */
  174. onUnload: function () {
  175. },
  176. /**
  177. * 页面相关事件处理函数--监听用户下拉动作
  178. */
  179. onPullDownRefresh: function () {
  180. },
  181. /**
  182. * 页面上拉触底事件的处理函数
  183. */
  184. onReachBottom: function () {
  185. },
  186. /**
  187. * 用户点击右上角分享
  188. */
  189. onShareAppMessage: function () {
  190. }
  191. })