share_material.rb 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. # encoding:utf-8
  2. require 'uuid'
  3. class ShareMaterial < ActiveRecord::Base
  4. has_paper_trail
  5. self.table_name = "share_materials"
  6. before_save :before_save
  7. belongs_to :wx_user, :foreign_key => :wx_uid
  8. validates :type_id,presence: true
  9. attr_accessor :v_image_1, :v_image_2, :v_image_3, :v_image_4, :v_image_5, :v_image_6, :v_image_7, :v_image_8, :v_image_9
  10. attr_accessor :is_delete_image_1, :is_delete_image_2, :is_delete_image_3, :is_delete_image_4, :is_delete_image_5, :is_delete_image_6, :is_delete_image_7, :is_delete_image_8, :is_delete_image_9
  11. attr_accessor :image_ratio_1, :image_ratio_2, :image_ratio_3, :image_ratio_4, :image_ratio_5, :image_ratio_6, :image_ratio_7, :image_ratio_8, :image_ratio_9
  12. MTYPE_ENUM = [["商品","product"],["项目","project"]]
  13. def type_title
  14. if m_type == "project"
  15. project = Project.where("id = ?", self.type_id).first
  16. if !project.blank?
  17. return project.title
  18. end
  19. elsif m_type == "product"
  20. product = Product.where("id = ?", self.type_id).first
  21. if !product.blank?
  22. return product.name
  23. end
  24. end
  25. return "-"
  26. end
  27. IMG_STORE_PATH = "share_materials"
  28. rails_admin do
  29. navigation_label '发圈素材管理'
  30. weight -240
  31. list do
  32. filters [:id]
  33. field :id
  34. field :wx_user
  35. field :m_type, :enum do
  36. enum do
  37. MTYPE_ENUM
  38. end
  39. end
  40. field :type_title
  41. field :type_id
  42. field :image_1st do
  43. formatted_value do
  44. bindings[:view].tag(
  45. :img,
  46. {
  47. :src => bindings[:object].get_img(1),
  48. :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
  49. :onClick => "javascript:window.open('#{bindings[:object].get_img(1)}')"
  50. }
  51. )
  52. end
  53. end
  54. field :image_2nd do
  55. formatted_value do
  56. bindings[:view].tag(
  57. :img,
  58. {
  59. :src => bindings[:object].get_img(2),
  60. :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
  61. :onClick => "javascript:window.open('#{bindings[:object].get_img(2)}')"
  62. }
  63. )
  64. end
  65. end
  66. field :is_published
  67. field :created_at
  68. end
  69. show do
  70. field :id
  71. field :wx_user
  72. field :m_type, :enum do
  73. enum do
  74. MTYPE_ENUM
  75. end
  76. end
  77. field :type_title
  78. field :type_id
  79. field :material_text
  80. field :image_1st do
  81. formatted_value do
  82. bindings[:view].tag(
  83. :img,
  84. {
  85. :src => bindings[:object].get_img(1),
  86. :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
  87. :onClick => "javascript:window.open('#{bindings[:object].get_img(1)}')"
  88. }
  89. )
  90. end
  91. end
  92. field :image_2nd do
  93. formatted_value do
  94. bindings[:view].tag(
  95. :img,
  96. {
  97. :src => bindings[:object].get_img(2),
  98. :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
  99. :onClick => "javascript:window.open('#{bindings[:object].get_img(2)}')"
  100. }
  101. )
  102. end
  103. end
  104. field :image_3rd do
  105. formatted_value do
  106. bindings[:view].tag(
  107. :img,
  108. {
  109. :src => bindings[:object].get_img(3),
  110. :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
  111. :onClick => "javascript:window.open('#{bindings[:object].get_img(3)}')"
  112. }
  113. )
  114. end
  115. end
  116. field :image_4th do
  117. formatted_value do
  118. bindings[:view].tag(
  119. :img,
  120. {
  121. :src => bindings[:object].get_img(4),
  122. :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
  123. :onClick => "javascript:window.open('#{bindings[:object].get_img(4)}')"
  124. }
  125. )
  126. end
  127. end
  128. field :image_5th do
  129. formatted_value do
  130. bindings[:view].tag(
  131. :img,
  132. {
  133. :src => bindings[:object].get_img(5),
  134. :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
  135. :onClick => "javascript:window.open('#{bindings[:object].get_img(5)}')"
  136. }
  137. )
  138. end
  139. end
  140. field :image_6th do
  141. formatted_value do
  142. bindings[:view].tag(
  143. :img,
  144. {
  145. :src => bindings[:object].get_img(6),
  146. :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
  147. :onClick => "javascript:window.open('#{bindings[:object].get_img(6)}')"
  148. }
  149. )
  150. end
  151. end
  152. field :image_7th do
  153. formatted_value do
  154. bindings[:view].tag(
  155. :img,
  156. {
  157. :src => bindings[:object].get_img(7),
  158. :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
  159. :onClick => "javascript:window.open('#{bindings[:object].get_img(7)}')"
  160. }
  161. )
  162. end
  163. end
  164. field :image_8th do
  165. formatted_value do
  166. bindings[:view].tag(
  167. :img,
  168. {
  169. :src => bindings[:object].get_img(8),
  170. :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
  171. :onClick => "javascript:window.open('#{bindings[:object].get_img(8)}')"
  172. }
  173. )
  174. end
  175. end
  176. field :image_9th do
  177. formatted_value do
  178. bindings[:view].tag(
  179. :img,
  180. {
  181. :src => bindings[:object].get_img(9),
  182. :style => 'width: 100px;height: 100px;cursor: pointer;display: block;max-width: 100px;',
  183. :onClick => "javascript:window.open('#{bindings[:object].get_img(9)}')"
  184. }
  185. )
  186. end
  187. end
  188. field :is_published
  189. field :created_at
  190. field :updated_at
  191. end
  192. edit do
  193. field :wx_uid
  194. field :m_type, :enum do
  195. enum do
  196. MTYPE_ENUM
  197. end
  198. end
  199. field :type_id
  200. field :material_text
  201. field :v_image_1, :file_upload do
  202. pretty_value do
  203. bindings[:view].tag(:img, {:src => bindings[:object].get_img(1), :class => 'preview'})
  204. end
  205. end
  206. field :image_ratio_1, :integer
  207. field :is_delete_image_1, :boolean do
  208. visible do
  209. bindings[:object].image_1st != "" && bindings[:object].image_1st != nil
  210. end
  211. end
  212. field :v_image_2, :file_upload do
  213. pretty_value do
  214. bindings[:view].tag(:img, {:src => bindings[:object].get_img(2), :class => 'preview'})
  215. end
  216. end
  217. field :image_ratio_2, :integer
  218. field :is_delete_image_2, :boolean do
  219. visible do
  220. bindings[:object].image_2nd != "" && bindings[:object].image_2nd != nil
  221. end
  222. end
  223. field :v_image_3, :file_upload do
  224. pretty_value do
  225. bindings[:view].tag(:img, {:src => bindings[:object].get_img(3), :class => 'preview'})
  226. end
  227. end
  228. field :image_ratio_3, :integer
  229. field :is_delete_image_3, :boolean do
  230. visible do
  231. bindings[:object].image_3rd != "" && bindings[:object].image_3rd != nil
  232. end
  233. end
  234. field :v_image_4, :file_upload do
  235. pretty_value do
  236. bindings[:view].tag(:img, {:src => bindings[:object].get_img(4), :class => 'preview'})
  237. end
  238. end
  239. field :image_ratio_4, :integer
  240. field :is_delete_image_4, :boolean do
  241. visible do
  242. bindings[:object].image_4th != "" && bindings[:object].image_4th != nil
  243. end
  244. end
  245. field :v_image_5, :file_upload do
  246. pretty_value do
  247. bindings[:view].tag(:img, {:src => bindings[:object].get_img(5), :class => 'preview'})
  248. end
  249. end
  250. field :image_ratio_5, :integer
  251. field :is_delete_image_5, :boolean do
  252. visible do
  253. bindings[:object].image_5th != "" && bindings[:object].image_5th != nil
  254. end
  255. end
  256. field :v_image_6, :file_upload do
  257. pretty_value do
  258. bindings[:view].tag(:img, {:src => bindings[:object].get_img(6), :class => 'preview'})
  259. end
  260. end
  261. field :image_ratio_6, :integer
  262. field :is_delete_image_6, :boolean do
  263. visible do
  264. bindings[:object].image_6th != "" && bindings[:object].image_6th != nil
  265. end
  266. end
  267. field :v_image_7, :file_upload do
  268. pretty_value do
  269. bindings[:view].tag(:img, {:src => bindings[:object].get_img(7), :class => 'preview'})
  270. end
  271. end
  272. field :image_ratio_7, :integer
  273. field :is_delete_image_7, :boolean do
  274. visible do
  275. bindings[:object].image_7th != "" && bindings[:object].image_7th != nil
  276. end
  277. end
  278. field :v_image_8, :file_upload do
  279. pretty_value do
  280. bindings[:view].tag(:img, {:src => bindings[:object].get_img(8), :class => 'preview'})
  281. end
  282. end
  283. field :image_ratio_8, :integer
  284. field :is_delete_image_8, :boolean do
  285. visible do
  286. bindings[:object].image_8th != "" && bindings[:object].image_8th != nil
  287. end
  288. end
  289. field :v_image_9, :file_upload do
  290. pretty_value do
  291. bindings[:view].tag(:img, {:src => bindings[:object].get_img(9), :class => 'preview'})
  292. end
  293. end
  294. field :image_ratio_9, :integer
  295. field :is_delete_image_9, :boolean do
  296. visible do
  297. bindings[:object].image_9th != "" && bindings[:object].image_9th != nil
  298. end
  299. end
  300. field :is_published
  301. end
  302. end
  303. after_destroy :after_destroy
  304. def v_image_1=file
  305. upload(file, 1)
  306. end
  307. def v_image_2=file
  308. upload(file, 2)
  309. end
  310. def v_image_3=file
  311. upload(file, 3)
  312. end
  313. def v_image_4=file
  314. upload(file, 4)
  315. end
  316. def v_image_5=file
  317. upload(file, 5)
  318. end
  319. def v_image_6=file
  320. upload(file, 6)
  321. end
  322. def v_image_7=file
  323. upload(file, 7)
  324. end
  325. def v_image_8=file
  326. upload(file, 8)
  327. end
  328. def v_image_9=file
  329. upload(file, 9)
  330. end
  331. # ?x-oss-process=image/quality,q_70 控制图片质量,CDN转换也可以用
  332. def upload(file, img_idx)
  333. unless file.blank?
  334. clear_img(img_idx)
  335. file_name = "#{UUID.new.generate[0...8].downcase}.jpg"
  336. file_path = "#{IMG_STORE_PATH}/#{file_name}"
  337. Ali::Oss::store(Ali::Oss::BUCKET_NAME_PUBLIC_READ, file_path, file.read)
  338. case img_idx
  339. when 1
  340. self.image_1st = file_path
  341. when 2
  342. self.image_2nd = file_path
  343. when 3
  344. self.image_3rd = file_path
  345. when 4
  346. self.image_4th = file_path
  347. when 5
  348. self.image_5th = file_path
  349. when 6
  350. self.image_6th = file_path
  351. when 7
  352. self.image_7th = file_path
  353. when 8
  354. self.image_8th = file_path
  355. when 9
  356. self.image_9th = file_path
  357. end
  358. end
  359. end
  360. def getImageFilePath(img_idx)
  361. case img_idx
  362. when 1
  363. if self.image_1st != nil && self.image_1st != ""
  364. if self.image_ratio_1 != nil && self.image_ratio_1 !=""
  365. if self.image_1st.include?"?x-oss-process=image/quality,q_"
  366. self.image_1st = self.image_1st[0, self.image_1st.rindex("_")+1] + self.image_ratio_1
  367. else
  368. self.image_1st = self.image_1st + "?x-oss-process=image/quality,q_" + self.image_ratio_1
  369. end
  370. else
  371. if !self.image_1st.include?"?x-oss-process=image/quality,q_"
  372. self.image_1st = self.image_1st + "?x-oss-process=image/quality,q_100"
  373. end
  374. end
  375. end
  376. when 2
  377. if self.image_2nd != nil && self.image_2nd != ""
  378. if self.image_ratio_2 != nil && self.image_ratio_2 != ""
  379. if self.image_2nd.include?"?x-oss-process=image/quality,q_"
  380. self.image_2nd = self.image_2nd[0, self.image_2nd.rindex("_")+1] + self.image_ratio_2
  381. else
  382. self.image_2nd = self.image_2nd + "?x-oss-process=image/quality,q_" + self.image_ratio_2
  383. end
  384. else
  385. if !self.image_2nd.include?"?x-oss-process=image/quality,q_"
  386. self.image_2nd = self.image_2nd + "?x-oss-process=image/quality,q_100"
  387. end
  388. end
  389. end
  390. when 3
  391. if self.image_3rd != nil && self.image_3rd != ""
  392. if self.image_ratio_3 != nil && self.image_ratio_3 != ""
  393. if self.image_3rd.include?"?x-oss-process=image/quality,q_"
  394. self.image_3rd = self.image_3rd[0, self.image_3rd.rindex("_")+1] + self.image_ratio_3
  395. else
  396. self.image_3rd = self.image_3rd + "?x-oss-process=image/quality,q_" + self.image_ratio_3
  397. end
  398. else
  399. if !self.image_3rd.include?"?x-oss-process=image/quality,q_"
  400. self.image_3rd = self.image_3rd + "?x-oss-process=image/quality,q_100"
  401. end
  402. end
  403. end
  404. when 4
  405. if self.image_4th != nil && self.image_4th != ""
  406. if self.image_ratio_4 != nil && self.image_ratio_4 != ""
  407. if self.image_4th.include?"?x-oss-process=image/quality,q_"
  408. self.image_4th = self.image_4th[0, self.image_4th.rindex("_")+1] + self.image_ratio_4
  409. else
  410. self.image_4th = self.image_4th + "?x-oss-process=image/quality,q_" + self.image_ratio_4
  411. end
  412. else
  413. if !self.image_4th.include?"?x-oss-process=image/quality,q_"
  414. self.image_4th = self.image_4th + "?x-oss-process=image/quality,q_100"
  415. end
  416. end
  417. end
  418. when 5
  419. if self.image_5th != nil && self.image_5th != ""
  420. if self.image_ratio_5 != nil && self.image_ratio_5 != ""
  421. if self.image_5th.include?"?x-oss-process=image/quality,q_"
  422. self.image_5th = self.image_5th[0, self.image_5th.rindex("_")+1] + self.image_ratio_5
  423. else
  424. self.image_5th = self.image_5th + "?x-oss-process=image/quality,q_" + self.image_ratio_5
  425. end
  426. else
  427. if !self.image_5th.include?"?x-oss-process=image/quality,q_"
  428. self.image_5th = self.image_5th + "?x-oss-process=image/quality,q_100"
  429. end
  430. end
  431. end
  432. when 6
  433. if self.image_6th != nil && self.image_6th != ""
  434. if self.image_ratio_6 != nil && self.image_ratio_6 != ""
  435. if self.image_6th.include?"?x-oss-process=image/quality,q_"
  436. self.image_6th = self.image_6th[0, self.image_6th.rindex("_")+1] + self.image_ratio_6
  437. else
  438. self.image_6th = self.image_6th + "?x-oss-process=image/quality,q_" + self.image_ratio_6
  439. end
  440. else
  441. if !self.image_6th.include?"?x-oss-process=image/quality,q_"
  442. self.image_6th = self.image_6th + "?x-oss-process=image/quality,q_100"
  443. end
  444. end
  445. end
  446. when 7
  447. if self.image_7th != nil && self.image_7th != ""
  448. if self.image_ratio_7 != nil && self.image_ratio_7 != ""
  449. if self.image_7th.include?"?x-oss-process=image/quality,q_"
  450. self.image_7th = self.image_7th[0, self.image_7th.rindex("_")+1] + self.image_ratio_7
  451. else
  452. self.image_7th = self.image_7th + "?x-oss-process=image/quality,q_" + self.image_ratio_7
  453. end
  454. else
  455. if !self.image_7th.include?"?x-oss-process=image/quality,q_"
  456. self.image_7th = self.image_7th + "?x-oss-process=image/quality,q_100"
  457. end
  458. end
  459. end
  460. when 8
  461. if self.image_8th != nil && self.image_8th != ""
  462. if self.image_ratio_8 != nil && self.image_ratio_8 != ""
  463. if self.image_8th.include?"?x-oss-process=image/quality,q_"
  464. self.image_8th = self.image_8th[0, self.image_8th.rindex("_")+1] + self.image_ratio_8
  465. else
  466. self.image_8th = self.image_8th + "?x-oss-process=image/quality,q_" + self.image_ratio_8
  467. end
  468. else
  469. if !self.image_8th.include?"?x-oss-process=image/quality,q_"
  470. self.image_8th = self.image_8th + "?x-oss-process=image/quality,q_100"
  471. end
  472. end
  473. end
  474. when 9
  475. if self.image_9th != nil && self.image_9th != ""
  476. if self.image_ratio_9 != nil && self.image_ratio_9 != ""
  477. if self.image_9th.include?"?x-oss-process=image/quality,q_"
  478. self.image_9th = self.image_9th[0, self.image_9th.rindex("_")+1] + self.image_ratio_9
  479. else
  480. self.image_9th = self.image_9th + "?x-oss-process=image/quality,q_" + self.image_ratio_9
  481. end
  482. else
  483. if !self.image_9th.include?"?x-oss-process=image/quality,q_"
  484. self.image_9th = self.image_9th + "?x-oss-process=image/quality,q_100"
  485. end
  486. end
  487. end
  488. end
  489. end
  490. def get_img(img_idx)
  491. store_name = ""
  492. case img_idx
  493. when 1
  494. store_name = self.image_1st
  495. when 2
  496. store_name = self.image_2nd
  497. when 3
  498. store_name = self.image_3rd
  499. when 4
  500. store_name = self.image_4th
  501. when 5
  502. store_name = self.image_5th
  503. when 6
  504. store_name = self.image_6th
  505. when 7
  506. store_name = self.image_7th
  507. when 8
  508. store_name = self.image_8th
  509. when 9
  510. store_name = self.image_9th
  511. end
  512. if store_name != "" && store_name != nil
  513. url = "http://#{Ali::Oss::CDN_URL_FOR_HOST}/#{store_name}"
  514. return url
  515. end
  516. return ""
  517. end
  518. def clear_img(img_idx)
  519. file_path = ""
  520. case img_idx
  521. when 1
  522. file_path = "#{self.image_1st}"
  523. when 2
  524. file_path = "#{self.image_2nd}"
  525. when 3
  526. file_path = "#{self.image_3rd}"
  527. when 4
  528. file_path = "#{self.image_4th}"
  529. when 5
  530. file_path = "#{self.image_5th}"
  531. when 6
  532. file_path = "#{self.image_6th}"
  533. when 7
  534. file_path = "#{self.image_7th}"
  535. when 8
  536. file_path = "#{self.image_8th}"
  537. when 9
  538. file_path = "#{self.image_9th}"
  539. end
  540. Ali::Oss.delete_object(Ali::Oss::BUCKET_NAME_PUBLIC_READ, file_path)
  541. end
  542. def after_destroy
  543. for i in 1..9
  544. clear_img(i)
  545. end
  546. end
  547. def is_delete_img(img_idx, is_delete)
  548. case img_idx
  549. when 1
  550. if is_delete == 1
  551. clear_img(1)
  552. self.image_1st = ""
  553. end
  554. when 2
  555. if is_delete == 1
  556. clear_img(2)
  557. self.image_2nd = ""
  558. end
  559. when 3
  560. if is_delete == 1
  561. clear_img(3)
  562. self.image_3rd = ""
  563. end
  564. when 4
  565. if is_delete == 1
  566. clear_img(4)
  567. self.image_4th = ""
  568. end
  569. when 5
  570. if is_delete == 1
  571. clear_img(5)
  572. self.image_5th = ""
  573. end
  574. when 6
  575. if is_delete == 1
  576. clear_img(6)
  577. self.image_6th = ""
  578. end
  579. when 7
  580. if is_delete == 1
  581. clear_img(7)
  582. self.image_7th = ""
  583. end
  584. when 8
  585. if is_delete == 1
  586. clear_img(8)
  587. self.image_8th = ""
  588. end
  589. when 9
  590. if is_delete == 1
  591. clear_img(9)
  592. self.image_9th = ""
  593. end
  594. end
  595. end
  596. def get_is_delete(img_idx)
  597. case img_idx
  598. when 1
  599. return self.is_delete_image_1.to_i
  600. when 2
  601. return self.is_delete_image_2.to_i
  602. when 3
  603. return self.is_delete_image_3.to_i
  604. when 4
  605. return self.is_delete_image_4.to_i
  606. when 5
  607. return self.is_delete_image_5.to_i
  608. when 6
  609. return self.is_delete_image_6.to_i
  610. when 7
  611. return self.is_delete_image_7.to_i
  612. when 8
  613. return self.is_delete_image_8.to_i
  614. when 9
  615. return self.is_delete_image_9.to_i
  616. end
  617. end
  618. def before_save
  619. for i in 1..9
  620. is_delete_img(i, get_is_delete(i))
  621. end
  622. for i in 1..9
  623. getImageFilePath(i)
  624. end
  625. end
  626. end