index.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. // pages/application/index.js
  2. import {
  3. ajax,
  4. ele_height
  5. } from "../../utils/util";
  6. import {
  7. base
  8. } from "../../config/index"
  9. const {
  10. globalData: {
  11. systemInfo
  12. }
  13. } = getApp();
  14. Page({
  15. /**
  16. * 页面的初始数据
  17. */
  18. data: {
  19. multiIndex: [0, 0, 0],
  20. multiArray: [],
  21. uploadheight: 0,
  22. local_image_url: '',
  23. group_class: '幼儿组',
  24. works_class: '中国画',
  25. author: "",
  26. phone: "",
  27. schoolClass: "",
  28. teacher: "",
  29. teacherPhone: "",
  30. area: "",
  31. article: "",
  32. brief: ""
  33. },
  34. id: undefined,
  35. area: [],
  36. changeSelectArea: [0, 0, 0],
  37. change(e) {
  38. const key = e.target.dataset.key || '';
  39. const value = e.detail.value;
  40. const p = {};
  41. p[key] = value;
  42. this.setData(p);
  43. },
  44. bindMultiPickerChange: function (e) {
  45. this.setData({
  46. multiIndex: e.detail.value
  47. })
  48. },
  49. uploadimg(e) {
  50. this.setData({
  51. uploadheight: ele_height(e, systemInfo.screenWidth * 0.8)
  52. })
  53. },
  54. upload() {
  55. wx.chooseMedia({
  56. count: 1,
  57. mediaType: ['image'],
  58. success: e => {
  59. this.setData({
  60. local_image_url: e.tempFiles[0].tempFilePath
  61. })
  62. },
  63. })
  64. },
  65. delimg() {
  66. wx.showModal({
  67. title: "确定删除该图片?",
  68. content: "删除后需重新选择图片",
  69. success: (res) => {
  70. if (res.confirm) {
  71. this.setData({
  72. local_image_url: ''
  73. })
  74. }
  75. }
  76. })
  77. },
  78. bindMultiPickerColumnChange(e) {
  79. const {
  80. detail: {
  81. column,
  82. value
  83. }
  84. } = e;
  85. this.changeSelectArea[column] = value;
  86. const a0 = this.area[this.changeSelectArea[0]] || {
  87. child: []
  88. }
  89. const multiArray1 = a0.child.map(v => v.name);
  90. let multiArray2 = ((a0.child[this.changeSelectArea[1]] || {
  91. child: []
  92. }).child || []).map(v => v.name);
  93. this.setData({
  94. multiArray: [this.data.multiArray[0], multiArray1, multiArray2]
  95. })
  96. },
  97. getArea(call) {
  98. ajax({
  99. urlType: "fileurl",
  100. api: "/topic/tool/img/%E5%B0%91%E5%84%BF%E4%B9%A6%E7%94%BB%E5%A4%A7%E8%B5%9B/narea.json?data=" +
  101. Date.now()
  102. }).then(res => {
  103. this.area = res || [];
  104. call && call();
  105. }).catch(() => {
  106. call && call();
  107. })
  108. },
  109. updata() {
  110. const {
  111. group_class,
  112. works_class,
  113. author,
  114. phone,
  115. schoolClass,
  116. teacher,
  117. teacherPhone,
  118. area,
  119. local_image_url,
  120. article,
  121. brief
  122. } = this.data;
  123. let baseArea = this.data.multiArray[0][this.data.multiIndex[0]] + this.data.multiArray[1][this.data.multiIndex[1]];
  124. if (this.data.multiArray[2] && this.data.multiArray[2][this.data.multiIndex[2]]) baseArea += this.data.multiArray[2][this.data.multiIndex[2]]
  125. if (isNaN(phone)) return wx.showToast({
  126. title: '手机号需填入数字',
  127. icon: "none"
  128. })
  129. if (!group_class || !works_class || !author || !schoolClass || !area || !local_image_url || !article || !brief) return wx.showToast({
  130. title: '请输入完整信息',
  131. icon: "none"
  132. })
  133. wx.showLoading({
  134. title: '加载中',
  135. })
  136. wx.getImageInfo({
  137. src: local_image_url
  138. }).then((res) => {
  139. wx.uploadFile({
  140. filePath: res.path,
  141. name: 'file',
  142. header: {
  143. Authorization: wx.getStorageSync('token') || undefined,
  144. },
  145. formData: {
  146. name: author,
  147. teacherPhone,
  148. school: schoolClass,
  149. teacher,
  150. area: baseArea + area,
  151. articleGroup: works_class,
  152. birthday: group_class,
  153. articleName: article,
  154. articleDescribe: brief,
  155. phone,
  156. id: this.id
  157. },
  158. url: base.apiurl + '/article/save',
  159. complete: () => {
  160. wx.hideLoading()
  161. },
  162. success: res => {
  163. if (res.statusCode !== 200) {
  164. const data = JSON.parse(res.data || "{}")
  165. wx.showToast({
  166. title: data.message || "保存失败",
  167. icon: "error"
  168. })
  169. return
  170. }
  171. wx.navigateBack()
  172. },
  173. fail(err) {
  174. wx.showToast({
  175. title: err.message || "上传失败",
  176. icon: "error"
  177. });
  178. }
  179. })
  180. });
  181. },
  182. getIndex(text, indexs, arr) {
  183. let add = "";
  184. for (let i = 0; i < arr.length; i++) {
  185. const rex = new RegExp(arr[i].name);
  186. if (rex.test(text)) {
  187. indexs.push({
  188. index: i,
  189. name: arr[i].name,
  190. areaList: arr.map(item => item.name)
  191. });
  192. add = text.replace(arr[i].name, "")
  193. if (arr[i].child) return this.getIndex(add, indexs, arr[i].child);
  194. }
  195. }
  196. return {
  197. indexs,
  198. add
  199. }
  200. },
  201. /**
  202. * 生命周期函数--监听页面加载
  203. */
  204. onLoad(options) {
  205. this.getArea(() => {
  206. const obj = JSON.parse(decodeURIComponent(options.data));
  207. this.id = obj.id;
  208. const index = this.getIndex(obj.area, [], this.area);
  209. const multiIndex = index.indexs.map(v => v.index);
  210. this.setData({
  211. local_image_url: obj.articleUrl || '',
  212. group_class: obj.birthday || '',
  213. works_class: obj.articleGroup || '',
  214. author: obj.name || "",
  215. phone: obj.phone || "",
  216. schoolClass: obj.school || "",
  217. teacher: obj.teacher || "",
  218. teacherPhone: obj.teacherPhone || "",
  219. article: obj.articleName || "",
  220. brief: obj.articleDescribe || "",
  221. multiIndex,
  222. area: index.add || "",
  223. multiArray: index.indexs.map(v => v.areaList)
  224. })
  225. });
  226. },
  227. /**
  228. * 生命周期函数--监听页面初次渲染完成
  229. */
  230. onReady() {
  231. },
  232. /**
  233. * 生命周期函数--监听页面显示
  234. */
  235. onShow() {
  236. },
  237. /**
  238. * 生命周期函数--监听页面隐藏
  239. */
  240. onHide() {
  241. },
  242. /**
  243. * 生命周期函数--监听页面卸载
  244. */
  245. onUnload() {
  246. },
  247. /**
  248. * 页面相关事件处理函数--监听用户下拉动作
  249. */
  250. onPullDownRefresh() {
  251. },
  252. /**
  253. * 页面上拉触底事件的处理函数
  254. */
  255. onReachBottom() {
  256. },
  257. /**
  258. * 用户点击右上角分享
  259. */
  260. onShareAppMessage() {
  261. return {
  262. title: __wxConfig.accountInfo.nickname,
  263. path: "/pages/index/index"
  264. }
  265. }
  266. })