index.js 6.2 KB

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