input_content.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. }
  52. that.setData({
  53. tempFilePath: that.data.tempFilePath
  54. })
  55. var uploadImgCount = 0;
  56. for (var i = 0, h = tempFilePaths.length; i < h; i++) {
  57. //上传文件
  58. wx.uploadFile({
  59. url: HOST + '/cms/file/upload',
  60. filePath: tempFilePaths[i],
  61. name: 'uploadfile_ant',
  62. header: {
  63. "Content-Type": "multipart/form-data"
  64. },
  65. success: function (res) {
  66. uploadImgCount++;
  67. let data = JSON.parse(res.data);
  68. that.data.imgId.push(data.data)
  69. that.setData({
  70. imgId: that.data.imgId,
  71. })
  72. console.log(data)
  73. //如果是最后一张,则隐藏等待中
  74. if (uploadImgCount == tempFilePaths.length) {
  75. wx.hideToast();
  76. }
  77. },
  78. fail: function (res) {
  79. debugger;
  80. wx.hideToast();
  81. wx.showModal({
  82. title: '错误提示',
  83. content: '上传图片失败',
  84. showCancel: false,
  85. success: function (res) { }
  86. })
  87. }
  88. });
  89. }
  90. }
  91. });
  92. }else {
  93. this.setData({
  94. upload: false
  95. })
  96. }
  97. },
  98. //点击发送
  99. send: function() {
  100. const type = utils.getUrl().type;
  101. const columnType = utils.getUrl().columnType;
  102. const columnId = utils.column(columnType).columnId;
  103. const columnName = utils.column(columnType).columnName;
  104. if(this.data.imgId.length == 0 && type == 2){
  105. wx.showModal({
  106. title: '提示',
  107. content: '请上传分享的作品',
  108. success: function(res) {
  109. if (res.confirm) {
  110. console.log('用户点击确定')
  111. } else if (res.cancel) {
  112. console.log('用户点击取消')
  113. }
  114. }
  115. })
  116. return false;
  117. }
  118. let data = {
  119. "title": this.data.textValue,
  120. "type": type,
  121. "category": columnId
  122. };
  123. if(type == 2){
  124. data.imagesStrList = this.data.imgId
  125. }
  126. login.getOpenidSessionKey(function(res) {
  127. //console.log(res.data.data.uid);
  128. APIClient.getSendSchedule({
  129. uid: res.data.data.uid
  130. }, data).success(res => {
  131. console.log(res)
  132. if(res.data.success) {
  133. wx.redirectTo({
  134. url: utils.url(columnType)
  135. })
  136. }
  137. })
  138. }, function() {
  139. wx.showModal({
  140. title: '提示',
  141. content: '需要获取您的公开信息(昵称、头像等),请从小程序列表删除小学王者班后再次扫码进入,允许授权后可正常使用',
  142. showCancel: false,
  143. success: function (res) {
  144. if (res.confirm) {
  145. console.log('用户点击确定')
  146. } else if (res.cancel) {
  147. console.log('用户点击取消')
  148. }
  149. }
  150. })
  151. });
  152. },
  153. cancel: function() {
  154. wx.navigateBack({ changed: true });
  155. },
  156. /**
  157. * 图片预览
  158. */
  159. listenerButtonPreviewImage: function(e) {
  160. let index = e.target.dataset.index;
  161. let that = this;
  162. wx.previewImage({
  163. current: that.data.tempFilePath[index],
  164. urls: that.data.tempFilePath,
  165. //这根本就不走
  166. success: function(res) {
  167. //console.log(res);
  168. },
  169. //也根本不走
  170. fail: function() {
  171. //console.log('fail')
  172. }
  173. })
  174. },
  175. /**
  176. * 生命周期函数--监听页面加载
  177. */
  178. onLoad: function (options) {
  179. if(options.type == 2) {
  180. wx.setNavigationBarTitle({
  181. title: '作品展示'
  182. })
  183. }
  184. if(options.type == 1) {
  185. wx.setNavigationBarTitle({
  186. title: '答疑讨论'
  187. })
  188. }
  189. this.setData({
  190. type: options.type
  191. })
  192. },
  193. /**
  194. * 生命周期函数--监听页面初次渲染完成
  195. */
  196. onReady: function () {
  197. },
  198. /**
  199. * 生命周期函数--监听页面显示
  200. */
  201. onShow: function () {
  202. },
  203. /**
  204. * 生命周期函数--监听页面隐藏
  205. */
  206. onHide: function () {
  207. },
  208. /**
  209. * 生命周期函数--监听页面卸载
  210. */
  211. onUnload: function () {
  212. },
  213. /**
  214. * 页面相关事件处理函数--监听用户下拉动作
  215. */
  216. onPullDownRefresh: function () {
  217. },
  218. /**
  219. * 页面上拉触底事件的处理函数
  220. */
  221. onReachBottom: function () {
  222. },
  223. /**
  224. * 用户点击右上角分享
  225. */
  226. onShareAppMessage: function () {
  227. }
  228. })