myData.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // pages/user/myData/myData.js
  2. const app = getApp(),
  3. {
  4. globalData: {
  5. util: {
  6. regeneratorRuntime,
  7. api: {
  8. myMaterial,
  9. uploadImg,
  10. addMaterial,
  11. delMaterial
  12. },
  13. request: {
  14. get_auth,
  15. post_auth,
  16. get
  17. }
  18. }
  19. }
  20. } = app;
  21. Page({
  22. /**
  23. * 页面的初始数据
  24. */
  25. data: {
  26. uploadImg: null,
  27. dataName: '',
  28. edit: false,
  29. },
  30. dataName: function (e) {
  31. this.data.dataName = e.detail.value
  32. },
  33. // 关闭遮罩层
  34. clear: function () {
  35. let that = this;
  36. that.setData({
  37. mask: false
  38. })
  39. },
  40. // 预览图片
  41. preview: function (e) {
  42. let urls = [],
  43. urlArr = e.target.dataset.urlarr,
  44. url = e.target.dataset.url;
  45. for (var i = 0; i < urlArr.length; i++) {
  46. urls.push(e.target.dataset.http + urlArr[i].url)
  47. }
  48. wx.previewImage({
  49. current: url, // 当前显示图片的http链接
  50. urls: urls // 需要预览的图片http链接列表
  51. })
  52. },
  53. // 编辑材料
  54. edit: function () {
  55. let that = this;
  56. let edit = !that.data.edit;
  57. that.setData({
  58. edit,
  59. })
  60. },
  61. // 删除材料
  62. async deleteImg(e) {
  63. console.log(e)
  64. let that = this,
  65. id = e.target.dataset.id;
  66. await get(delMaterial, {
  67. id
  68. })
  69. that.dataList();
  70. },
  71. // 资料上传
  72. async uploadFile() {
  73. let that = this;
  74. wx.chooseImage({
  75. count: 1,
  76. sizeType: ['original', 'compressed'],
  77. success(res) {
  78. wx.showLoading({
  79. title: '正在上传',
  80. })
  81. wx.uploadFile({
  82. url: uploadImg,
  83. filePath: res.tempFilePaths[0],
  84. header: { 'content-type': 'application/x-www-form-urlencoded;charset=utf-8', },
  85. name: 'filePath',
  86. formData: {
  87. 'fileType': 'image'
  88. },
  89. success(res1) {
  90. console.log(res1)
  91. let obj = {}
  92. wx.hideLoading()
  93. obj = JSON.parse(res1.data);
  94. console.log(obj)
  95. if (obj.errno == 0) {
  96. let uploadImg = obj.data;
  97. that.setData({
  98. uploadImg,
  99. mask: true,
  100. })
  101. console.log(that.data.uploadImg)
  102. }
  103. }
  104. })
  105. }
  106. })
  107. },
  108. // 上传资料提交
  109. async maskSubmit() {
  110. let that = this;
  111. let { _err } = await post_auth(addMaterial, {
  112. materialName: that.data.dataName,
  113. materialUrl: that.data.uploadImg.filePath
  114. });
  115. if (!_err) {
  116. wx.showToast({
  117. title: '上传成功',
  118. icon: "none"
  119. })
  120. that.setData({
  121. dataName: '',
  122. mask: false,
  123. })
  124. that.dataList();
  125. }
  126. },
  127. // 查询我的材料
  128. async dataList() {
  129. let that = this;
  130. let myDataList = await get_auth(myMaterial);
  131. console.log(myDataList)
  132. let items = myDataList.items,
  133. listArr = [],
  134. listArr1 = [];
  135. for (let i = 0; i < items.length; i++) {
  136. let list = {
  137. 'id': items[i].id,
  138. 'materialName': items[i].materialName,
  139. isno: 0,
  140. 'materialUrl': [{ 'url': items[i].materialUrl, 'id': items[i].id }]
  141. }
  142. listArr1.push(list)
  143. for (var j = i + 1; j < items.length; j++) {
  144. if (items[i].materialName == items[j].materialName) {
  145. var objList = {}
  146. objList.url = items[j].materialUrl
  147. objList.id = items[j].id
  148. list.materialUrl.push(objList)
  149. list.isno = items[j].isno
  150. listArr.push(list)
  151. items[j] = ''
  152. }
  153. }
  154. }
  155. for (let i = 0; i < listArr1.length; i++) {
  156. if (!listArr1[i].id) {
  157. listArr1.splice(i, 1);
  158. i--
  159. }
  160. }
  161. that.setData({
  162. image: listArr1,
  163. url: myDataList.prefix
  164. })
  165. },
  166. /**
  167. * 生命周期函数--监听页面加载
  168. */
  169. async onLoad(options) {
  170. let that = this;
  171. that.dataList();
  172. },
  173. /**
  174. * 生命周期函数--监听页面初次渲染完成
  175. */
  176. onReady: function () {
  177. },
  178. /**
  179. * 生命周期函数--监听页面显示
  180. */
  181. onShow: function () {
  182. },
  183. /**
  184. * 生命周期函数--监听页面隐藏
  185. */
  186. onHide: function () {
  187. },
  188. /**
  189. * 生命周期函数--监听页面卸载
  190. */
  191. onUnload: function () {
  192. },
  193. /**
  194. * 页面相关事件处理函数--监听用户下拉动作
  195. */
  196. onPullDownRefresh: function () {
  197. },
  198. /**
  199. * 页面上拉触底事件的处理函数
  200. */
  201. onReachBottom: function () {
  202. },
  203. /**
  204. * 用户点击右上角分享
  205. */
  206. onShareAppMessage: function () {
  207. }
  208. })