index.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. import { TickSystem } from 'XrFrame/systems'
  2. import DiyApi from '../../services/diy'
  3. import GoodApi from '../../services/good'
  4. import { share } from '../../utils/http'
  5. import Navi from '../../utils/navi'
  6. import { backPage, getUser } from '../../utils/util'
  7. const bag: any = []
  8. for (var i = 0; i < 20; i++) {
  9. bag.push({
  10. label: '',
  11. value: i,
  12. url: i % 2 === 0 ? '/assets/navbar/p1.png' : '/assets/navbar/p2.png',
  13. })
  14. }
  15. interface itemdto extends Record<string, any> {
  16. id: number
  17. imageUrl: string
  18. checked: boolean
  19. }
  20. // pages/diy/index.ts
  21. Page({
  22. /**
  23. * 页面的初始数据
  24. */
  25. data: {
  26. ani: '',
  27. bag,
  28. wait: false,
  29. loading: false,
  30. loadingDiy: false,
  31. isManage: false,
  32. status: 0,
  33. userInfo: undefined,
  34. totalPage: 0,
  35. items: [] as itemdto[],
  36. pageparams: {
  37. mode: 'fangan',
  38. skipCount: 0,
  39. maxResultCount: 12,
  40. },
  41. showIndex: true,
  42. visible: false,
  43. closeBtn: true,
  44. deleteBtn: true,
  45. initIndex: 0,
  46. images: [] as string[],
  47. showSingle: false,
  48. deleteIds: [] as number[],
  49. show: false,
  50. authing: true,
  51. },
  52. /**
  53. * 生命周期函数--监听页面加载
  54. */
  55. onLoad() {},
  56. /**
  57. * 生命周期函数--监听页面初次渲染完成
  58. */
  59. onReady() {},
  60. /**
  61. * 生命周期函数--监听页面显示
  62. */
  63. onShow() {
  64. if (!this.data.authing) {
  65. console.log(this.data.pageparams.mode, this.data)
  66. this.load()
  67. this.setData({ ani: this.data.pageparams.mode === 'fangan' ? ' ani' : '' })
  68. } else {
  69. this.getTabBar().init(() => {
  70. this.setData({
  71. authing: true,
  72. })
  73. })
  74. }
  75. },
  76. /**
  77. * 生命周期函数--监听页面隐藏
  78. */
  79. onHide() {
  80. this.setData({
  81. ani: '',
  82. authing: false,
  83. })
  84. },
  85. /**
  86. * 生命周期函数--监听页面卸载
  87. */
  88. onUnload() {},
  89. /**
  90. * 页面相关事件处理函数--监听用户下拉动作
  91. */
  92. onPullDownRefresh() {},
  93. /**
  94. * 页面上拉触底事件的处理函数
  95. */
  96. onReachBottom() {},
  97. /**
  98. * 用户点击右上角分享
  99. */
  100. onShareAppMessage() {
  101. let pages = getCurrentPages() //获取所有页面栈实例列表
  102. let nowPage = pages[pages.length - 1] //当前页页面实例
  103. return {
  104. title: share.title,
  105. path: `/${nowPage.route}`,
  106. imageUrl: share.imageUrl,
  107. success(res: any) {
  108. console.log('success(res)==', res)
  109. },
  110. fail(res: any) {
  111. console.log('fail(res)==', res)
  112. },
  113. }
  114. },
  115. handleAuthDisagree() {
  116. backPage()
  117. },
  118. handleAuthOK() {
  119. this.setData({
  120. authing: false,
  121. })
  122. this.init()
  123. },
  124. init() {
  125. this.getUserInfo()
  126. if (this.data.pageparams.skipCount > 0) {
  127. return
  128. }
  129. this.load()
  130. },
  131. getUserInfo() {
  132. const info = getUser()
  133. const self = this
  134. if (info != null) {
  135. self.setData({
  136. userInfo: info as any,
  137. ani: ' ani',
  138. })
  139. } else {
  140. self.gotoLogin()
  141. }
  142. },
  143. gotoLogin() {
  144. Navi.navigateTo({
  145. url: '/pages/login/index',
  146. })
  147. },
  148. load() {
  149. this.setData({
  150. status: 1,
  151. loading: true,
  152. })
  153. DiyApi.GetDiyList({
  154. ...this.data.pageparams,
  155. skipCount: this.data.pageparams.skipCount * this.data.pageparams.maxResultCount,
  156. })
  157. .then((rsp) => {
  158. const { result } = rsp
  159. const total = Math.ceil(result.totalCount / this.data.pageparams.maxResultCount)
  160. const newItems = this.data.isManage
  161. ? result.items.map((o: itemdto) => {
  162. o.checked = false
  163. return o
  164. })
  165. : result.items
  166. this.setData({
  167. status: total > this.data.pageparams.skipCount + 1 ? 0 : 2,
  168. totalPage: Math.ceil(result.totalCount / this.data.pageparams.maxResultCount),
  169. items:
  170. this.data.pageparams.skipCount === 0
  171. ? newItems
  172. : (this.data.items || []).concat(newItems),
  173. })
  174. })
  175. .catch(() => {
  176. this.setData({
  177. loading: false,
  178. })
  179. })
  180. .finally(() => {
  181. this.setData({
  182. loading: false,
  183. })
  184. })
  185. },
  186. remove() {
  187. const ids = this.data.deleteIds as number[]
  188. return new Promise<void>((resolve, reject) => {
  189. this.setData({
  190. loading: true,
  191. })
  192. DiyApi.RemoveDiy({
  193. mode: this.data.pageparams.mode,
  194. ids,
  195. })
  196. .then((rsp) => {
  197. if (rsp.result === 'ok') {
  198. this.setData({
  199. loading: false,
  200. deleteIds: [],
  201. items: this.data.items.filter((o: itemdto) => ids.indexOf(o.id) < 0),
  202. })
  203. resolve()
  204. } else {
  205. this.setData({
  206. loading: false,
  207. })
  208. reject()
  209. }
  210. })
  211. .catch(() => {
  212. this.setData({
  213. loading: false,
  214. })
  215. reject()
  216. })
  217. })
  218. },
  219. handleManage() {
  220. this.setData({
  221. isManage: true,
  222. deleteIds: [],
  223. items: this.data.items.map((o) => {
  224. o.checked = false
  225. return o
  226. }),
  227. })
  228. },
  229. handleManageBack() {
  230. this.setData({
  231. isManage: false,
  232. })
  233. },
  234. changeTab(e: WechatMiniprogram.CustomEvent<{}, {}, { tab: string }>) {
  235. this.setData(
  236. {
  237. status: 0,
  238. isManage: false,
  239. items: [],
  240. pageparams: {
  241. ...this.data.pageparams,
  242. mode: e.currentTarget.dataset.tab,
  243. skipCount: 0,
  244. },
  245. },
  246. () => {
  247. this.load()
  248. },
  249. )
  250. },
  251. pagetolower() {
  252. if (!this.data.loading) {
  253. if (this.data.totalPage > this.data.pageparams.skipCount + 1) {
  254. this.setData(
  255. {
  256. pageparams: {
  257. ...this.data.pageparams,
  258. skipCount: this.data.pageparams.skipCount + 1,
  259. },
  260. },
  261. this.load,
  262. )
  263. } else {
  264. this.setData({
  265. status: 2,
  266. })
  267. }
  268. console.log('lower')
  269. }
  270. },
  271. showPreview(e: WechatMiniprogram.CustomEvent<{}, {}, { idx: number }>) {
  272. const idx = e.currentTarget.dataset.idx
  273. if (this.data.isManage) {
  274. const newitems = this.data.items
  275. newitems[idx].checked = !newitems[idx].checked
  276. this.setData({
  277. items: newitems,
  278. deleteIds: newitems
  279. .filter((o) => o.checked)
  280. .map((o) => o.id)
  281. .filter((o) => o != null),
  282. })
  283. return
  284. }
  285. if (this.data.pageparams.mode === 'shoucang') {
  286. const item = this.data.items[idx]
  287. Navi.navigateTo({
  288. url: '/pages/goods/detail/index?id=' + item.id + '&cateid=' + item.cateId,
  289. })
  290. return
  291. }
  292. this.setData(
  293. {
  294. initIndex: idx,
  295. images: this.data.items.map((o) => o.imageUrl),
  296. ani: '',
  297. },
  298. () =>
  299. setTimeout(() => {
  300. this.setData({
  301. visible: true,
  302. })
  303. }, 100),
  304. )
  305. },
  306. handleUncheck() {
  307. this.setData({
  308. items: this.data.items.map((o) => {
  309. o.checked = false
  310. return o
  311. }),
  312. })
  313. },
  314. onClose() {
  315. this.setData({
  316. visible: false,
  317. images: [],
  318. initIndex: 0,
  319. ani: this.data.pageparams.mode === 'fangan' ? ' ani' : '',
  320. })
  321. },
  322. handleDelete() {
  323. if (this.data.deleteIds.length > 0) {
  324. this.setData({
  325. showSingle: true,
  326. })
  327. }
  328. },
  329. onDelete() {
  330. const item = this.data.items[this.data.initIndex] as any
  331. this.setData({
  332. showSingle: true,
  333. deleteIds: [item.id],
  334. })
  335. },
  336. showGood(e: WechatMiniprogram.CustomEvent<{}, {}, { good: string; cate: string }>) {
  337. const { good, cate } = e.currentTarget.dataset
  338. Navi.navigateTo({
  339. url: '/pages/goods/detail/index?id=' + good + '&cateid=' + cate,
  340. })
  341. },
  342. closeSingleConfirm() {
  343. if (this.data.loading) {
  344. return
  345. }
  346. this.remove().then(() => {
  347. this.closeSignleCancel()
  348. this.onClose()
  349. })
  350. },
  351. closeSignleCancel() {
  352. this.setData({
  353. showSingle: false,
  354. })
  355. },
  356. showdiy() {
  357. this.setData({
  358. show: true,
  359. })
  360. },
  361. goflatdiy() {
  362. this.setData({
  363. loadingDiy: true,
  364. show: false,
  365. })
  366. DiyApi.GetDiy({
  367. type: 'flat',
  368. })
  369. .then((rsp) => {
  370. console.log(rsp, rsp.result)
  371. if (rsp.result && rsp.result.length > 0) {
  372. this.setData(
  373. {
  374. loadingDiy: false,
  375. },
  376. () => {
  377. Navi.navigateTo({
  378. url: '/pages/outlink/index?url=' + encodeURIComponent(rsp.result),
  379. })
  380. },
  381. )
  382. return
  383. }
  384. this.setData({
  385. loadingDiy: false,
  386. })
  387. })
  388. .catch(() => {
  389. this.setData({
  390. loadingDiy: false,
  391. })
  392. })
  393. },
  394. gospacediy() {
  395. this.setData({
  396. loadingDiy: true,
  397. show: false,
  398. })
  399. DiyApi.GetDiy({
  400. type: 'space',
  401. })
  402. .then((rsp) => {
  403. console.log(rsp, rsp.result)
  404. if (rsp.result && rsp.result.length > 0) {
  405. this.setData(
  406. {
  407. loadingDiy: false,
  408. },
  409. () => {
  410. Navi.navigateTo({
  411. url: '/pages/outlink/index?url=' + encodeURIComponent(rsp.result),
  412. })
  413. },
  414. )
  415. return
  416. }
  417. this.setData({
  418. loadingDiy: false,
  419. })
  420. })
  421. .catch(() => {
  422. this.setData({
  423. loadingDiy: false,
  424. })
  425. })
  426. },
  427. getLink(e: WechatMiniprogram.CustomEvent<{}, {}, { id: string; type: string }>) {
  428. const id = e.currentTarget.dataset.id
  429. const type = e.currentTarget.dataset.type
  430. if (Number(id) > 0) {
  431. this.setData({
  432. wait: true,
  433. })
  434. GoodApi.GetInspireLink({
  435. id: Number(id),
  436. type: type,
  437. })
  438. .then((rsp) => {
  439. if (rsp.result && rsp.result.length > 0) {
  440. console.log(rsp.result)
  441. Navi.navigateTo({
  442. url: '/pages/outlink/index?url=' + encodeURIComponent(rsp.result),
  443. })
  444. }
  445. })
  446. .finally(() => {
  447. this.setData({
  448. wait: false,
  449. })
  450. })
  451. }
  452. },
  453. handlePopupHide() {
  454. this.setData({
  455. show: false,
  456. })
  457. },
  458. onUploadBackground() {
  459. const self = this
  460. wx.chooseMedia({
  461. count: 9,
  462. mediaType: ['image'],
  463. sourceType: ['album', 'camera'],
  464. maxDuration: 30,
  465. camera: 'back',
  466. success(res) {
  467. const filePath = res.tempFiles[0].tempFilePath
  468. wx.getFileSystemManager().readFile({
  469. filePath,
  470. encoding: 'base64',
  471. success(res) {
  472. self.setData({
  473. wait: true,
  474. })
  475. DiyApi.Transbase({
  476. base64: res.data,
  477. mode: 'Background',
  478. })
  479. .then(() => {
  480. self.setData(
  481. {
  482. wait: false,
  483. pageparams: {
  484. ...self.data.pageparams,
  485. skipCount: 0,
  486. },
  487. },
  488. () => {
  489. self.load()
  490. },
  491. )
  492. })
  493. .catch(() => {
  494. self.setData({
  495. wait: false,
  496. })
  497. })
  498. },
  499. })
  500. },
  501. })
  502. },
  503. })