index.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // nova-werun/pages/send-circle/index.js
  2. const Parse = getApp().Parse;
  3. const company = getApp().globalData.company;
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. //屏幕高度
  10. statusBarHeight: 0, // 状态栏高度
  11. screenHeight: 0, // 屏幕高度
  12. customHeight: 0, // 自定义导航栏高度(如小程序右上角胶囊按钮)
  13. bottomNavHeight: 0, // 底部导航栏高度
  14. contentHeight: 0, // 可用内容高度
  15. contentHeight2: 0,
  16. contentpadding: 0, //顶部padding高度
  17. //输入框
  18. inputValue: '', // 用于存储输入的内容
  19. textareaHeight: 52, // 初始高度,单位为 rpx
  20. // 图片
  21. fileList: [],
  22. fileLists:[],
  23. uptokenURL: '',
  24. domain: '',
  25. uploadURL: '',
  26. saveimage:false,
  27. checked:false,
  28. address:'',
  29. },
  30. /**
  31. * 生命周期函数--监听页面加载
  32. */
  33. onLoad: function (options) {
  34. // 计算
  35. const systemInfo = wx.getSystemInfoSync();
  36. const statusBarHeight = systemInfo.statusBarHeight || 0;
  37. const screenHeight = systemInfo.screenHeight || 0;
  38. const custom = wx.getMenuButtonBoundingClientRect();
  39. const customHeight = custom.height + 10 + 2 || 0;
  40. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  41. const contentpadding = (statusBarHeight + customHeight) * 750 / systemInfo.windowWidth;
  42. const contentHeight = (screenHeight - bottomNavHeight - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth;
  43. this.setData({
  44. statusBarHeight,
  45. screenHeight,
  46. customHeight,
  47. bottomNavHeight,
  48. contentpadding,
  49. contentHeight
  50. });
  51. const images = []
  52. images.push({
  53. url:options.image
  54. })
  55. console.log('options.image',options.image);
  56. if(options.image){
  57. this.setData({
  58. fileList:images,
  59. saveimage:true,
  60. })
  61. }
  62. this.getUptoken()
  63. },
  64. /**
  65. * 生命周期函数--监听页面初次渲染完成
  66. */
  67. onReady: function () {
  68. },
  69. /**
  70. * 生命周期函数--监听页面显示
  71. */
  72. onShow: function () {
  73. },
  74. /**
  75. * 生命周期函数--监听页面隐藏
  76. */
  77. onHide: function () {
  78. },
  79. /**
  80. * 生命周期函数--监听页面卸载
  81. */
  82. onUnload: function () {
  83. },
  84. /**
  85. * 页面相关事件处理函数--监听用户下拉动作
  86. */
  87. onPullDownRefresh: function () {
  88. },
  89. /**
  90. * 页面上拉触底事件的处理函数
  91. */
  92. onReachBottom: function () {
  93. },
  94. /**
  95. * 用户点击右上角分享
  96. */
  97. onShareAppMessage: function () {
  98. },
  99. //输入框高度随字体增多而变大
  100. onInput: function (event) {
  101. const value = event.detail.value; // 获取当前输入的值
  102. this.setData({
  103. inputValue: value,
  104. });
  105. console.log(this.data.inputValue);
  106. },
  107. async getUptoken() {
  108. let res = await Parse.Cloud.run('qiniu_uptoken', {
  109. company: company
  110. })
  111. this.setData({
  112. uptokenURL: res.uptoken,
  113. domain: res.domain,
  114. uploadURL: res.zoneUrl
  115. })
  116. console.log(this.data.uptokenURL, this.data.domain, this.data.uploadURL);
  117. },
  118. //获取图片数组
  119. picture(event) {
  120. console.log('event', event);
  121. let FileList = event.detail
  122. this.setData({
  123. fileList: FileList,
  124. })
  125. console.log('图片', this.data.fileLists);
  126. },
  127. //上传函数
  128. async send() {
  129. const currentUser = Parse.User.current();
  130. let Profilequery = new Parse.Query('Profile');
  131. Profilequery.equalTo('company', company);
  132. Profilequery.notEqualTo('isDeleted', true);
  133. Profilequery.equalTo('user', currentUser.id);
  134. let p = await Profilequery.first()
  135. let companyPointer = Parse.Object.extend('Company').createWithoutData(company);
  136. let Comment = new Parse.Object('AIMoment');
  137. Comment.set('profile', p.toPointer())
  138. Comment.set('company', companyPointer);
  139. Comment.set('isVisible', true);
  140. if(this.data.fileList.length>0){
  141. let url = []
  142. for (let i = 0; i < this.data.fileList.length; i++) {
  143. url.push(this.data.fileList[i].url)
  144. }
  145. this.setData({
  146. fileLists:url,
  147. })
  148. Comment.set('images', this.data.fileLists);
  149. }
  150. Comment.set('content', this.data.inputValue);
  151. if(this.data.checked){
  152. Comment.set('location', this.data.address);
  153. }
  154. try {
  155. if(this.data.fileList.length==0&& !this.data.inputValue){
  156. console.log('新数据保存失败');
  157. }else{
  158. let saveDate2 = await Comment.save();
  159. console.log(saveDate2);
  160. console.log("新数据保存成功");
  161. wx.showToast({
  162. title: '发布成功',
  163. icon: 'success',
  164. duration: 1000
  165. });
  166. setTimeout(()=>{
  167. this.goback()
  168. },1000)
  169. }
  170. } catch (error) {
  171. console.error("保存数据时出现错误:", error);
  172. }
  173. },
  174. async goback(){
  175. if(!this.data.saveimage){
  176. let pages = getCurrentPages(); //页面对象
  177. console.log(pages);
  178. let prevpage = pages[pages.length - 2]; //上一个页面对象
  179. console.log(prevpage);
  180. await prevpage.updateCom()
  181. }
  182. wx.navigateBack({
  183. delta: 1
  184. });
  185. },
  186. //选择展示当前位置
  187. onChange(event) {
  188. this.setData({
  189. checked: event.detail,
  190. });
  191. console.log(this.data.checked);
  192. this.Getlocation()
  193. },
  194. //获取当前位置
  195. Getlocation() {
  196. // 获取当前位置信息
  197. wx.getLocation({
  198. type: 'wgs84',
  199. success: (res) => {
  200. const {
  201. latitude,
  202. longitude
  203. } = res;
  204. //调用api解析地址
  205. wx.request({
  206. url: 'https://api.map.baidu.com/reverse_geocoding/v3/?ak=sHZTomd7grslfP7sPKB8tRgT49FK9TEu&output=json&coordtype=gcj02&location=' + latitude + ',' + longitude,
  207. data: {},
  208. header: {
  209. 'Content-Type': 'application/json'
  210. },
  211. success: (ops) => { // 使用箭头函数
  212. console.log(ops);
  213. // const address = ops.data.result.formatted_address;//详细地址
  214. const address = ops.data.result.formatted_address;
  215. this.setData({
  216. address: address,
  217. });
  218. console.log(this.data.address);
  219. },
  220. fail: function (resq) {
  221. wx.showModal({
  222. title: '信息提示',
  223. content: '请求失败',
  224. showCancel: false,
  225. confirmColor: '#f37938'
  226. });
  227. },
  228. complete: function () {}
  229. })
  230. },
  231. fail: (err) => {
  232. console.error(err);
  233. wx.showToast({
  234. title: '获取位置失败',
  235. icon: 'none'
  236. });
  237. }
  238. });
  239. },
  240. })