index.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. // pages/add/index.js
  2. import util from '../../utils/util'
  3. var dateTimePicker = require('../../utils/dateTimePicker');
  4. const QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
  5. const qqmapsdk = new QQMapWX({
  6. key: '36IBZ-VI53O-FJHW2-SV2NP-USSAE-GFBUS'
  7. })
  8. const app = getApp();
  9. Page({
  10. /**
  11. * 页面的初始数据
  12. */
  13. data: {
  14. form: {
  15. key1: '', //活动名称
  16. key2: '', //活动地点
  17. key3: '', //活动开始时间
  18. key4: '', //活动结束时间
  19. key5: '', //活动描述
  20. money: 0,
  21. num: '',
  22. key7: ''
  23. },
  24. isShowForm: {
  25. key1: false,
  26. key2: false,
  27. key5: false
  28. },
  29. inputFocus: {
  30. key1: false,
  31. key2: false,
  32. key5: false
  33. },
  34. date: '2018-10-01',
  35. time: '12:00',
  36. dateTimeArray1: null,
  37. dateTime1: null,
  38. dateTimeArray7: null,
  39. dateTime7: null,
  40. dateTimeArray2: null,
  41. dateTime2: null,
  42. startYear: null,
  43. endYear: null,
  44. BASEIMGURL: '', //图片域名
  45. imgList: [],
  46. userInfo: {},
  47. bigImgList: [],
  48. //编辑
  49. actid: '',
  50. actiddetail: {},
  51. switch1Checked: false
  52. },
  53. switch1Change(e) {
  54. this.setData({
  55. switch1Checked: e.detail.value
  56. })
  57. },
  58. async getPhoneNumber(e) {
  59. const that = this;
  60. if (e.detail.errMsg == "getPhoneNumber:ok") {
  61. let res = await app.globalData.api.bindphone({
  62. "openid": app.globalData.userInfo.openid,
  63. "encryptedData": e.detail.encryptedData,
  64. "iv": e.detail.iv,
  65. "invitor": app.globalData.shareOpenId
  66. });
  67. app.globalData.userInfo.phone = res.data.phone;
  68. this.setData({
  69. userInfo: {
  70. ...this.data.userInfo,
  71. phone: res.data.phone
  72. }
  73. })
  74. that.createBtn();
  75. } else {
  76. wx.showToast({
  77. title: '请授权您的手机号!',
  78. icon: 'none'
  79. })
  80. }
  81. },
  82. async isOpen(privateObj) {
  83. let imgObj = {
  84. img: this.data.imgList,
  85. bigImg: this.data.bigImgList
  86. }
  87. let params = {
  88. "title": this.data.form.key1,
  89. "location": this.data.form.key2,
  90. "starttime": this.data.form.key3,
  91. "endtime": this.data.form.key7,
  92. "deadline": this.data.form.key4,
  93. "capacity": this.data.form.num ? this.data.form.num - 0 : 0,
  94. "content": this.data.form.key5.split('\n').join('&hc'),
  95. "images": JSON.stringify(imgObj),
  96. "attendfee": this.data.form.money ? (this.data.form.money - 0).toFixed(2) : 0,
  97. "organizer": app.globalData.userInfo.phone,
  98. "private": privateObj.private
  99. };
  100. try {
  101. let lngArr = await this.getLng();
  102. params = Object.assign({}, params, lngArr);
  103. } catch (error) {
  104. console.log(error)
  105. }
  106. if (this.data.actid) {
  107. params.actid = this.data.actid;
  108. let res = await app.globalData.api.activity_update(params);
  109. if (res.data == "OK") {
  110. util.myShowToast('修改成功!')
  111. setTimeout(() => {
  112. wx.reLaunch({
  113. url: '/pages/my/index?update=1',
  114. })
  115. }, 1500)
  116. }
  117. } else {
  118. let res = await app.globalData.api.createActivity(params);
  119. if (res.data == "OK") {
  120. util.myShowToast('创建成功!')
  121. setTimeout(() => {
  122. wx.reLaunch({
  123. url: '/pages/index/index?update=1',
  124. })
  125. }, 1500)
  126. }
  127. }
  128. },
  129. datetab(date1, date2) {
  130. var oDate1 = new Date(date1);
  131. var oDate2 = new Date(date2);
  132. if (oDate1.getTime() > oDate2.getTime()) {
  133. return true; //date1 > date2
  134. } else {
  135. return false;
  136. }
  137. },
  138. createBtn() {
  139. let reg = /[^\u0020-\u007E\u00A0-\u00BE\u2E80-\uA4CF\uF900-\uFAFF\uFE30-\uFE4F\uFF00-\uFFEF\u0080-\u009F\u2000-\u201f\u2026\u2022\u20ac\r\n]/g;
  140. if (!this.data.form.key1) return util.myShowToast('请输入活动名称!');
  141. if (this.data.form.key1.length > 30 || this.data.form.key1.length < 2) return util.myShowToast('活动名称应为2-30字!');
  142. if (this.data.form.key1.match(reg)) return util.myShowToast('活动名称不能包含表情!');
  143. if (!this.data.form.key2) return util.myShowToast('请输入活动地点!');
  144. if (this.data.form.key2.length > 30 || this.data.form.key2.length < 2) return util.myShowToast('活动地点应为2-30字!');
  145. if (this.data.form.key2.match(reg)) return util.myShowToast('活动地点不能包含表情!');
  146. if (!this.data.form.key3) return util.myShowToast('请输入活动开始时间!');
  147. // if (!this.data.form.key7) return util.myShowToast('请输入活动结束时间!');
  148. if(this.data.form.key7){
  149. if(this.datetab(this.data.form.key3,this.data.form.key7)) return util.myShowToast('活动开始时间不能大于结束时间!');
  150. }
  151. if (!this.data.form.key5) return util.myShowToast('请输入活动描述!');
  152. if (this.data.form.key5.length > 300 || this.data.form.key5.length < 2) return util.myShowToast('活动描述应为2-300字!');
  153. if (this.data.form.key5.match(reg)) return util.myShowToast('活动描述不能包含表情!');
  154. let reg1 = /^\d+(\.\d+)?$/;
  155. if (this.data.form.money && !reg1.test(this.data.form.money)) return util.myShowToast('请输入正确的金额!');
  156. let reg2 = /^[1-9]\d*$/;
  157. // if(!this.data.form.num) return util.myShowToast('请输入正确的报名人数!');
  158. // if(this.data.form.num < 2) return util.myShowToast('报名人数不应小于2人!');
  159. // if(this.data.form.num > 9999) return util.myShowToast('报名人数不应大于9999人!');
  160. if (this.data.form.num && !reg2.test(this.data.form.num)) return util.myShowToast('请输入正确的报名人数!');
  161. if (!this.data.imgList.length) return util.myShowToast('至少上传一张图片!');
  162. this.isOpen({
  163. private: this.data.switch1Checked ? 1 : 0 //不公开
  164. });
  165. // wx.showModal({
  166. // title: '提示',
  167. // content: '是否公开您发起的活动?',
  168. // cancelText: '否',
  169. // confirmText: '是',
  170. // success (res) {
  171. // if (res.confirm) {
  172. // that.isOpen({
  173. // private: 0 //不公开
  174. // });
  175. // } else if (res.cancel) {
  176. // that.isOpen({
  177. // private: 1
  178. // });
  179. // }
  180. // }
  181. // })
  182. },
  183. getLng() {
  184. return new Promise((resolve, reject) => {
  185. qqmapsdk.geocoder({
  186. //获取表单传入地址
  187. address: this.data.form.key2, //地址参数,例:固定地址,address: '北京市海淀区彩和坊路海淀西大街74号'
  188. complete: (res) => {
  189. if (res.message == "query ok") {
  190. //根据地址解析在地图上标记解析地址位置
  191. resolve({
  192. latitude: res.result.location.lat,
  193. longitude: res.result.location.lng
  194. })
  195. } else {
  196. resolve({
  197. latitude: 0,
  198. longitude: 0
  199. })
  200. }
  201. }
  202. })
  203. })
  204. },
  205. //删除图片
  206. removeImg(e) {
  207. const that = this;
  208. wx.showModal({
  209. title: '提示',
  210. content: '确定删除图片?',
  211. success(res) {
  212. if (res.confirm) {
  213. // let item = e.currentTarget.dataset.item;
  214. // let index = e.currentTarget.dataset.index;
  215. // console.log(index);
  216. // let newImgList = that.data.imgList.splice(index,1);
  217. // let newBigImglist = that.data.bigImgList.splice(index,1);
  218. // let newImgList = that.data.imgList.filter(v => {
  219. // return item != v;
  220. // });
  221. that.setData({
  222. imgList: [],
  223. bigImgList: []
  224. })
  225. } else if (res.cancel) {
  226. console.log('用户点击取消')
  227. }
  228. }
  229. });
  230. },
  231. updateImg() {
  232. const that = this;
  233. wx.chooseImage({
  234. count: 1, // 默认9
  235. sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有
  236. sourceType: ['album', 'camera'],
  237. success: function (res) {
  238. var tempFilePaths = res.tempFilePaths;
  239. const tempFilesSize = res.tempFiles[0].size;
  240. const src = res.tempFilePaths[0];
  241. wx.showLoading();
  242. if (tempFilesSize > 2000000) { //图片小于或者等于1M时 可以执行获取图片
  243. return wx.showToast({
  244. title: '上传图片不能大于2M!', //标题
  245. icon: 'none' //图标 none不使用图标,详情看官方文档
  246. })
  247. }
  248. wx.uploadFile({
  249. url: 'https://hw.cirray.cn/api/upload_image', //仅为示例,非真实的接口地址
  250. filePath: tempFilePaths[0],
  251. name: 'file',
  252. formData: {},
  253. success: function (res) {
  254. wx.navigateTo({
  255. url: '../upload/index?src=' + src + '&originimg=' + res.data
  256. })
  257. wx.hideLoading()
  258. },
  259. fail(e) {
  260. // wx.showToast({
  261. // title: '上传失败,请稍后重试!',
  262. // icon: 'none'
  263. // })
  264. wx.hideLoading()
  265. }
  266. })
  267. }
  268. })
  269. },
  270. back() {
  271. wx.navigateBack()
  272. },
  273. inputVal(e) {
  274. let value = e.detail.value;
  275. let key = e.currentTarget.dataset.key;
  276. let newKey = 'form.' + key;
  277. this.setData({
  278. [newKey]: value
  279. })
  280. },
  281. inputBlur(e) {
  282. let key = e.currentTarget.dataset.key;
  283. if (this.data.form[key]) {
  284. let newKey = 'isShowForm.' + key;
  285. this.setData({
  286. [newKey]: true
  287. })
  288. }
  289. },
  290. showInput(e) {
  291. let key = e.currentTarget.dataset.key;
  292. if (this.data.actid && key == 'key1') return;
  293. if (this.data.isShowForm[key]) {
  294. let newKey = 'isShowForm.' + key;
  295. let newInputFocus = Object.assign({
  296. key1: false,
  297. key2: false
  298. }, {
  299. [key]: true
  300. });
  301. this.setData({
  302. [newKey]: false,
  303. inputFocus: newInputFocus
  304. });
  305. }
  306. },
  307. changeDate(e) {
  308. this.setData({
  309. date: e.detail.value
  310. });
  311. },
  312. changeTime(e) {
  313. this.setData({
  314. time: e.detail.value
  315. });
  316. },
  317. changeDateTime2(e) {
  318. let dateTime = e.detail.value;
  319. let dateTimeArray = this.data.dateTimeArray2;
  320. this.setData({
  321. dateTime2: dateTime
  322. });
  323. let aaa1 = dateTime[0];
  324. let aaa2 = dateTime[1];
  325. let aaa3 = dateTime[2];
  326. let aaa4 = dateTime[3];
  327. let aaa5 = dateTime[4];
  328. let time1 = dateTimeArray[0][aaa1];
  329. let time2 = dateTimeArray[1][aaa2];
  330. let time3 = dateTimeArray[2][aaa3];
  331. let time4 = dateTimeArray[3][aaa4];
  332. let time5 = dateTimeArray[4][aaa5];
  333. let time = time1 + '-' + time2 + '-' + time3 + ' ' + time4 + ':' + time5;
  334. let key = e.currentTarget.dataset.key;
  335. let newKey = 'form.' + key;
  336. this.setData({
  337. [newKey]: time
  338. })
  339. },
  340. changeDateTime1(e) {
  341. let dateTime = e.detail.value;
  342. let dateTimeArray = this.data.dateTimeArray1;
  343. this.setData({
  344. dateTime1: dateTime
  345. });
  346. let aaa1 = dateTime[0];
  347. let aaa2 = dateTime[1];
  348. let aaa3 = dateTime[2];
  349. let aaa4 = dateTime[3];
  350. let aaa5 = dateTime[4];
  351. let time1 = dateTimeArray[0][aaa1];
  352. let time2 = dateTimeArray[1][aaa2];
  353. let time3 = dateTimeArray[2][aaa3];
  354. let time4 = dateTimeArray[3][aaa4];
  355. let time5 = dateTimeArray[4][aaa5];
  356. let time = time1 + '-' + time2 + '-' + time3 + ' ' + time4 + ':' + time5;
  357. let key = e.currentTarget.dataset.key;
  358. let newKey = 'form.' + key;
  359. this.setData({
  360. [newKey]: time
  361. })
  362. },
  363. changeDateTime7(e) {
  364. let dateTime = e.detail.value;
  365. let dateTimeArray = this.data.dateTimeArray7;
  366. this.setData({
  367. dateTime7: dateTime
  368. });
  369. let aaa1 = dateTime[0];
  370. let aaa2 = dateTime[1];
  371. let aaa3 = dateTime[2];
  372. let aaa4 = dateTime[3];
  373. let aaa5 = dateTime[4];
  374. let time1 = dateTimeArray[0][aaa1];
  375. let time2 = dateTimeArray[1][aaa2];
  376. let time3 = dateTimeArray[2][aaa3];
  377. let time4 = dateTimeArray[3][aaa4];
  378. let time5 = dateTimeArray[4][aaa5];
  379. let time = time1 + '-' + time2 + '-' + time3 + ' ' + time4 + ':' + time5;
  380. let key = e.currentTarget.dataset.key;
  381. let newKey = 'form.' + key;
  382. this.setData({
  383. [newKey]: time
  384. })
  385. },
  386. changeDateTimeColumn2(e) {
  387. var arr = this.data.dateTime2,
  388. dateArr = this.data.dateTimeArray2;
  389. arr[e.detail.column] = e.detail.value;
  390. dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
  391. this.setData({
  392. dateTimeArray2: dateArr,
  393. dateTime2: arr
  394. });
  395. },
  396. changeDateTimeColumn1(e) {
  397. var arr = this.data.dateTime1,
  398. dateArr = this.data.dateTimeArray1;
  399. arr[e.detail.column] = e.detail.value;
  400. dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
  401. this.setData({
  402. dateTimeArray1: dateArr,
  403. dateTime1: arr
  404. });
  405. },
  406. changeDateTimeColumn1(e) {
  407. var arr = this.data.dateTime7,
  408. dateArr = this.data.dateTimeArray7;
  409. arr[e.detail.column] = e.detail.value;
  410. dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
  411. this.setData({
  412. dateTimeArray7: dateArr,
  413. dateTime7: arr
  414. });
  415. },
  416. resetData(data) {
  417. console.log(data, 333)
  418. let form = this.data.form;
  419. let isShowForm = this.data.isShowForm;
  420. form.key1 = data.title;
  421. form.key2 = data.location;
  422. form.key3 = data.starttime.substr(0, 16);
  423. form.key4 = data.endtime.substr(0, 16);
  424. form.key5 = data.content && (data.content.split('&hc').join('\n'));
  425. form.num = data.capacity - 0;
  426. form.money = data.attendfee - 0;
  427. isShowForm.key1 = true;
  428. isShowForm.key2 = true;
  429. isShowForm.key5 = true;
  430. let imgList1 = JSON.parse(data.images);
  431. // 获取完整的年月日 时分秒,以及默认显示的数组
  432. var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear, data.starttime);
  433. var obj2 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear, data.endtime);
  434. // 精确到分的处理,将数组的秒去掉
  435. obj1.dateTimeArray.pop();
  436. obj1.dateTime.pop();
  437. obj2.dateTimeArray.pop();
  438. obj2.dateTime.pop();
  439. this.setData({
  440. form,
  441. isShowForm,
  442. imgList: imgList1.img,
  443. bigImgList: imgList1.bigImg,
  444. actiddetail: data,
  445. dateTime2: obj2.dateTime,
  446. dateTimeArray2: obj2.dateTimeArray,
  447. dateTimeArray1: obj1.dateTimeArray,
  448. dateTime1: obj1.dateTime,
  449. switch1Checked: data.private ? true : false
  450. })
  451. },
  452. /**
  453. * 生命周期函数--监听页面加载
  454. */
  455. onLoad: function (options) {
  456. if (options.actid) {
  457. this.setData({
  458. actid: options.actid
  459. });
  460. app.globalData.api.activity_id(options.actid).then(res => {
  461. this.resetData(res.data);
  462. })
  463. }
  464. // 获取完整的年月日 时分秒,以及默认显示的数组
  465. var obj1 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
  466. var obj2 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
  467. var obj7 = dateTimePicker.dateTimePicker(this.data.startYear, this.data.endYear);
  468. // 精确到分的处理,将数组的秒去掉
  469. var lastArray = obj1.dateTimeArray.pop();
  470. var lastTime = obj1.dateTime.pop();
  471. obj2.dateTimeArray.pop();
  472. obj2.dateTime.pop();
  473. obj7.dateTimeArray.pop();
  474. obj7.dateTime.pop();
  475. this.setData({
  476. dateTime2: obj2.dateTime,
  477. dateTimeArray2: obj2.dateTimeArray,
  478. dateTimeArray1: obj1.dateTimeArray,
  479. dateTimeArray7: obj7.dateTimeArray,
  480. dateTime1: obj1.dateTime,
  481. dateTime7: obj7.dateTime,
  482. BASEIMGURL: app.globalData.BASEIMGURL,
  483. userInfo: app.globalData.userInfo
  484. });
  485. },
  486. clickImg() {
  487. wx.previewImage({
  488. urls: [this.data.BASEIMGURL + this.data.bigImgList[0]], //需要预览的图片http链接列表,注意是数组
  489. current: '', // 当前显示图片的http链接,默认是第一个
  490. success: function (res) {},
  491. fail: function (res) {},
  492. complete: function (res) {},
  493. })
  494. },
  495. /**
  496. * 生命周期函数--监听页面初次渲染完成
  497. */
  498. onReady: function () {
  499. },
  500. /**
  501. * 生命周期函数--监听页面显示
  502. */
  503. onShow: function () {},
  504. /**
  505. * 生命周期函数--监听页面隐藏
  506. */
  507. onHide: function () {
  508. },
  509. /**
  510. * 生命周期函数--监听页面卸载
  511. */
  512. onUnload: function () {
  513. this.setData({
  514. actid: ''
  515. })
  516. },
  517. /**
  518. * 页面相关事件处理函数--监听用户下拉动作
  519. */
  520. onPullDownRefresh: function () {
  521. },
  522. /**
  523. * 页面上拉触底事件的处理函数
  524. */
  525. onReachBottom: function () {
  526. },
  527. /**
  528. * 用户点击右上角分享
  529. */
  530. onShareAppMessage: function () {
  531. return app.onShareAppMessage();
  532. }
  533. })