vote.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <template>
  2. <view>
  3. <view class="con">
  4. <view class="title"><input v-model="voteTitle" maxlength="20" placeholder="请输入投票标题 (最多20个字)" placeholder-class="placeholder" /></view>
  5. <view class="time ddflex">
  6. <view class="label">截止时间</view>
  7. <picker mode="date" :value="endTime" @change="bindDateChange" class="fflex">
  8. <view class="picker" v-if="endTime">
  9. {{endTime}}
  10. </view>
  11. <view class="picker placeholder" v-else>
  12. 请选择投票截止时间
  13. </view>
  14. </picker>
  15. </view>
  16. <view class="tits">*投票必须超过2个选项哦~</view>
  17. <view class="list">
  18. <view class="li ddflex" v-for="(item, index) in optionList" :key="index">
  19. <view class="fflex"><input v-model="item.name" maxlength="15" placeholder="请输入选项描述 (15字内)" placeholder-class="placeholder" class="ipt" /></view>
  20. <view class="del" @click="removeObj(index)"><image src="../static/images/tp_del.png"></image></view>
  21. </view>
  22. </view>
  23. <view class="add-btn" @click="addObj()">添加选项</view>
  24. </view>
  25. <view class="bot">
  26. <view class="no" @click="noVote()" v-if="pollId">不设置投票</view>
  27. <view class="complete" @click="completeVote()">完成</view>
  28. </view>
  29. </view>
  30. </template>
  31. <script>
  32. const req = require('../../utils/request.js');
  33. const QQMapWX = require("../../utils/qqmap.js");
  34. export default {
  35. data() {
  36. return {
  37. voteTitle: '',
  38. endTime: '',
  39. pollId: '',
  40. optionList: [], //投票项
  41. optionListResut: [], //最终确认有值的投票项
  42. location: {},
  43. poiIndex: -1,
  44. poiList: [],
  45. };
  46. },
  47. onLoad(opt) {
  48. this.pollId = opt.pollId;
  49. if (!this.pollId) {
  50. for (var i = 0; i < 2; i++) {
  51. var obj = {
  52. name: ''
  53. };
  54. this.optionList.push(obj);
  55. }
  56. } else {
  57. let pages = getCurrentPages(); //获取所有页面栈实例列表
  58. let prevPage = pages[pages.length - 2]; //上一页页面实例
  59. var pollObj = prevPage.$vm.pollObj;
  60. if (pollObj) {
  61. this.voteTitle = pollObj.title;
  62. this.optionList = JSON.parse(pollObj.optionJson);
  63. }
  64. }
  65. QQMapWX.initMap();
  66. let location = {
  67. latitude: req.getStorage('loctionAddressMap').location.lat,
  68. longitude: req.getStorage('loctionAddressMap').location.lng
  69. }
  70. QQMapWX.reverseGeocoder(location, data => {
  71. this.poiList = data.pois;
  72. this.checkLocation(this.poiList[0],0)
  73. });
  74. },
  75. methods: {
  76. chooseLocation(){
  77. let that = this;
  78. uni.chooseLocation({//choosePoi不返回经纬度
  79. success: function (res) {
  80. console.log('位置名称:' , res);
  81. that.location = {
  82. poiName: res.name,
  83. poiAddress: res.address,
  84. poiLongitude: res.longitude,
  85. poiLatitude: res.latitude
  86. }
  87. that.reverseGeocoder(res);
  88. }
  89. });
  90. },
  91. reverseGeocoder(location) {
  92. QQMapWX.initMap();
  93. QQMapWX.reverseGeocoder(location, data => {
  94. this.location.poiRegionalism = data.ad_info.adcode.substring(0, 2) + "0000," + data.ad_info.adcode.substring(0, 4) +"00," + data.ad_info.adcode
  95. console.log('解析后的地址地址数据:', data,this.location.poiRegionalism);
  96. });
  97. },
  98. checkLocation(item,index){
  99. if(this.poiIndex == index) return false;
  100. this.poiIndex = index;
  101. this.location = {
  102. poiName: item.title,
  103. poiAddress: item.address,
  104. poiLongitude: item.location.lng,
  105. poiLatitude: item.location.lat,
  106. poiRegionalism: item.ad_info.adcode.substring(0, 2) + "0000," + item.ad_info.adcode.substring(0, 4) +"00," + item.ad_info.adcode
  107. }
  108. },
  109. //不设置投票
  110. noVote() {
  111. this.selectedBack('');
  112. },
  113. //完成投票
  114. completeVote() {
  115. this.getSaveVote();
  116. },
  117. addObj() {
  118. var obj = {
  119. name: ''
  120. };
  121. this.optionList.push(obj);
  122. },
  123. removeObj(indx) {
  124. if (this.optionList.length <= 2) {
  125. req.msg('须保留两个选项');
  126. return;
  127. }
  128. this.optionList.splice(indx, 1);
  129. },
  130. checkOptionListResut() {
  131. this.optionListResut = [];
  132. var list = this.optionList.filter(it => it.name);
  133. if (list && list.length > 0) {
  134. for (var i = 0; i < list.length; i++) {
  135. var obj = {
  136. code: i,
  137. name: list[i].name
  138. };
  139. this.optionListResut.push(obj);
  140. }
  141. }
  142. console.log('数据>>>>>>', this.optionListResut);
  143. return this.optionListResut.length;
  144. },
  145. bindDateChange: function(e) {
  146. this.endTime= e.detail.value
  147. },
  148. getSaveVote() {
  149. var tha = this;
  150. var dataP = {};
  151. if (this.pollId) {
  152. dataP.id = this.pollId;
  153. }
  154. if (!this.voteTitle) {
  155. req.msg('请输入投票标题');
  156. return;
  157. } else {
  158. dataP.title = this.voteTitle;
  159. }
  160. if(!this.endTime) return req.msg('请选择投票截止时间');
  161. dataP.endTime = this.endTime;
  162. if (this.checkOptionListResut() >= 2) {
  163. dataP.optionJson = JSON.stringify(this.optionListResut);
  164. } else {
  165. req.msg('投票必须超过2个选项');
  166. return;
  167. }
  168. // 位置
  169. if(this.location&&this.location.poiName){
  170. dataP.poiName = this.location.poiName;
  171. dataP.poiAddress = this.location.poiAddress;
  172. dataP.poiLongitude = this.location.poiLongitude;
  173. dataP.poiLatitude = this.location.poiLatitude;
  174. dataP.poiRegionalism = this.location.poiRegionalism
  175. }
  176. req.postRequest('/api/v3/poll/save', dataP, data => {
  177. req.msg('投票保存成功');
  178. setTimeout(() => {
  179. tha.selectedBack(data);
  180. }, 1500);
  181. });
  182. },
  183. selectedBack(obj) {
  184. let pages = getCurrentPages(); //获取所有页面栈实例列表
  185. let prevPage = pages[pages.length - 2]; //上一页页面实例
  186. prevPage.$vm.pollObj = obj; //修改上一页data里面的searchVal参数值为1211
  187. uni.navigateBack({
  188. //uni.navigateTo跳转的返回,默认1为返回上一级
  189. delta: 1
  190. });
  191. }
  192. }
  193. };
  194. </script>
  195. <style>
  196. @import './vote.css';
  197. </style>