index.js 7.9 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. 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. ['form.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. imgUpload: function (e) {
  86. const that = this;
  87. let data = that.data.fileList;
  88. data.push(e.detail)
  89. that.setData({ fileList: data })
  90. },
  91. //删除图片
  92. imgDel: function (e) {
  93. const that = this;
  94. let data = that.data.fileList;
  95. let arr = data.filter((i, index) => index != e.detail.index)
  96. that.setData({ fileList: arr })
  97. },
  98. /**
  99. * 生命周期函数--监听页面加载
  100. */
  101. onLoad: function (options) {
  102. // 计算高度
  103. this.searchHeight()
  104. // 监听用户是否登录
  105. this.watchLogin();
  106. //验证规则函数
  107. this.initValidate()
  108. },
  109. //验证必填项
  110. initValidate() {
  111. const rules = { name: { required: true }, type: { required: true }, create_user: { required: true }, }
  112. // 验证字段的提示信息,若不传则调用默认的信息
  113. const messages = { name: { required: '请输入团队名称' }, type: { required: '请输入团队类型' }, create_user: { required: '请输入团队创建人名称' }, };
  114. this.WxValidate = new WxValidate(rules, messages)
  115. },
  116. // 监听用户是否登录
  117. watchLogin: function () {
  118. const that = this;
  119. wx.getStorage({
  120. key: 'token',
  121. success: res => {
  122. //数据请求
  123. wx.request({
  124. url: `${app.globalData.publicUrl}/courtAdmin/api/user`, //接口地址
  125. method: "get",
  126. data: {},
  127. header: {},
  128. success: res => {
  129. that.setData({ item: res.data.data })
  130. },
  131. error: err => {
  132. console.log(err);
  133. }
  134. })
  135. wx.request({
  136. url: `${app.globalData.publicUrl}/courtAdmin/api/user/${res.data.id}`, //接口地址
  137. method: "get",//请求方法
  138. data: {},//请求参数
  139. header: {},
  140. success: res => {
  141. console.log(res);
  142. that.setData({ ['form.create_user']: res.data.data.nickname, ['form.create_id']: res.data.data.id })
  143. console.log(res.data.data.nickname);
  144. },
  145. error: err => {
  146. console.log(err);
  147. }
  148. })
  149. },
  150. fail: res => {
  151. wx.redirectTo({ url: '/pages/login/index', })
  152. }
  153. })
  154. },
  155. //提交
  156. formSubmit: function (e) {
  157. const value = e.detail.value;
  158. if (!this.WxValidate.checkForm(value)) {
  159. const error = this.WxValidate.errorList[0];
  160. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  161. return false
  162. } else {
  163. value.logo = this.data.fileList;
  164. value.members = this.data.members;
  165. value.create_id = this.data.form.create_id;
  166. wx.getStorage({
  167. key: 'token',
  168. success: function (res) {
  169. wx.request({
  170. url: `${app.globalData.publicUrl}/courtAdmin/api/team`, //接口地址
  171. method: "post",//请求方法
  172. //请求参数
  173. data: value,
  174. header: {},
  175. success: res => {
  176. console.log(res);
  177. if (res.data.errcode == 0) {
  178. wx.showToast({
  179. title: '创建团队成功',
  180. icon: 'success',
  181. duration: 2000//延迟两秒
  182. })
  183. } else {
  184. wx.showToast({
  185. title: '创建团队失败',
  186. icon: 'error',
  187. duration: 2000
  188. })
  189. }
  190. },
  191. error: err => {
  192. console.log(err);
  193. }
  194. })
  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. })