input_content.js 6.1 KB

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