input_content.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // pages/Input_content/input_content.js
  2. const app = getApp();
  3. const url = require('../../utils/const.js');
  4. const APIClient = require('../../utils/APIClient.js');
  5. const login = require('../../utils/loginSchedule.js');
  6. const utils = require('../../utils/util.js');
  7. const HOST = url.apiUrl;
  8. Page({
  9. /**
  10. * 页面的初始数据
  11. */
  12. data: {
  13. tempFilePath: [],
  14. imgId: [],
  15. textValue: '',
  16. upload: true
  17. //type: '',
  18. //columnType: ''
  19. },
  20. /**
  21. * 获取输入框内容
  22. */
  23. bindKeyInput: function(e) {
  24. this.setData({
  25. textValue: e.detail.value
  26. })
  27. },
  28. /**
  29. * 图片上传
  30. */
  31. uploading: function () {
  32. const that = this;
  33. const length = that.data.tempFilePath.length;
  34. if(length < 2) {
  35. wx.chooseImage({
  36. count: 2, //最多可以选择的图片总数
  37. sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有
  38. sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
  39. success: function (res) {
  40. // 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片
  41. var tempFilePaths = res.tempFilePaths;
  42. //启动上传等待中...
  43. wx.showToast({
  44. title: '压缩中...',
  45. icon: 'loading',
  46. mask: true,
  47. duration: 1000
  48. })
  49. for(let item of tempFilePaths){
  50. that.data.tempFilePath.push(item);
  51. // wx.compressImage({
  52. // src: item, // 图片路径
  53. // quality: 80, // 压缩质量
  54. // success: (res)=> {
  55. // console.log('=======', res.tempFilePath)
  56. // },
  57. // fail: (err)=> {
  58. // console.log('err=======', err)
  59. // }
  60. // })
  61. }
  62. that.setData({
  63. tempFilePath: that.data.tempFilePath
  64. })
  65. var uploadImgCount = 0;
  66. for (var i = 0, h = tempFilePaths.length; i < h; i++) {
  67. //上传文件
  68. wx.uploadFile({
  69. url: HOST + '/cms/file/upload',
  70. filePath: tempFilePaths[i],
  71. name: 'uploadfile_ant',
  72. header: {
  73. "Content-Type": "multipart/form-data"
  74. },
  75. success: function (res) {
  76. uploadImgCount++;
  77. let data = JSON.parse(res.data);
  78. that.data.imgId.push(data.data)
  79. that.setData({
  80. imgId: that.data.imgId,
  81. })
  82. console.log(data)
  83. //如果是最后一张,则隐藏等待中
  84. if (uploadImgCount == tempFilePaths.length) {
  85. wx.hideToast();
  86. }
  87. },
  88. fail: function (res) {
  89. wx.hideToast();
  90. wx.showModal({
  91. title: '错误提示',
  92. content: '上传图片失败',
  93. showCancel: false,
  94. success: function (res) { }
  95. })
  96. }
  97. });
  98. }
  99. }
  100. });
  101. }else {
  102. this.setData({
  103. upload: false
  104. })
  105. }
  106. },
  107. //点击发送
  108. send: function() {
  109. const type = utils.getUrl().type;
  110. const columnType = utils.getUrl().columnType;
  111. const columnId = utils.column(columnType).columnId;
  112. const columnName = utils.column(columnType).columnName;
  113. if(this.data.imgId.length == 0 && type == 2){
  114. wx.showModal({
  115. title: '提示',
  116. content: '请上传分享的作品',
  117. success: function(res) {
  118. if (res.confirm) {
  119. console.log('用户点击确定')
  120. } else if (res.cancel) {
  121. console.log('用户点击取消')
  122. }
  123. }
  124. })
  125. return false;
  126. }
  127. let data = {
  128. "title": this.data.textValue,
  129. "type": type,
  130. "category": columnId
  131. };
  132. if(type == 2){
  133. data.imagesStrList = this.data.imgId
  134. }
  135. login.getOpenidSessionKey(function(res) {
  136. //console.log(res.data.data.uid);
  137. APIClient.getSendSchedule({
  138. uid: res.data.data.uid
  139. }, data).success(res => {
  140. console.log(res)
  141. if(res.data.success) {
  142. wx.redirectTo({
  143. url: utils.url(columnType)
  144. })
  145. }
  146. })
  147. }, function() {
  148. wx.showModal({
  149. title: '提示',
  150. content: '需要获取您的公开信息(昵称、头像等),请从小程序列表删除小学王者班后再次扫码进入,允许授权后可正常使用',
  151. showCancel: false,
  152. success: function (res) {
  153. if (res.confirm) {
  154. console.log('用户点击确定')
  155. } else if (res.cancel) {
  156. console.log('用户点击取消')
  157. }
  158. }
  159. })
  160. });
  161. },
  162. cancel: function() {
  163. wx.navigateBack({ changed: true });
  164. },
  165. /**
  166. * 图片预览
  167. */
  168. listenerButtonPreviewImage: function(e) {
  169. let index = e.target.dataset.index;
  170. let that = this;
  171. wx.previewImage({
  172. current: that.data.tempFilePath[index],
  173. urls: that.data.tempFilePath,
  174. //这根本就不走
  175. success: function(res) {
  176. //console.log(res);
  177. },
  178. //也根本不走
  179. fail: function() {
  180. //console.log('fail')
  181. }
  182. })
  183. },
  184. /**
  185. * 生命周期函数--监听页面加载
  186. */
  187. onLoad: function (options) {
  188. if(options.type == 2) {
  189. wx.setNavigationBarTitle({
  190. title: '作品展示'
  191. })
  192. }
  193. if(options.type == 1) {
  194. wx.setNavigationBarTitle({
  195. title: '答疑讨论'
  196. })
  197. }
  198. this.setData({
  199. type: options.type
  200. })
  201. },
  202. /**
  203. * 生命周期函数--监听页面初次渲染完成
  204. */
  205. onReady: function () {
  206. },
  207. /**
  208. * 生命周期函数--监听页面显示
  209. */
  210. onShow: function () {
  211. },
  212. /**
  213. * 生命周期函数--监听页面隐藏
  214. */
  215. onHide: function () {
  216. },
  217. /**
  218. * 生命周期函数--监听页面卸载
  219. */
  220. onUnload: function () {
  221. },
  222. /**
  223. * 页面相关事件处理函数--监听用户下拉动作
  224. */
  225. onPullDownRefresh: function () {
  226. },
  227. /**
  228. * 页面上拉触底事件的处理函数
  229. */
  230. onReachBottom: function () {
  231. },
  232. /**
  233. * 用户点击右上角分享
  234. */
  235. onShareAppMessage: function () {
  236. }
  237. })