index.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. import React, {Component} from 'react'
  2. import './index.css'
  3. import {NavBar, Icon} from 'antd-mobile'
  4. import {withRouter} from 'react-router-dom'
  5. import {ActivityIndicator, List, ImagePicker, InputItem, Button} from 'antd-mobile'
  6. import axios from 'axios'
  7. import {Query, Mutation} from "react-apollo"
  8. import gql from "graphql-tag"
  9. import {shop_by_props, create_shop, update_shop} from "../../../../utils/gql"
  10. import {storeFile} from "../../../../configs/url"
  11. import moment from 'moment'
  12. const Item = List.Item
  13. class Shop extends Component {
  14. constructor(props) {
  15. super(props)
  16. this.state = {}
  17. }
  18. render() {
  19. return (
  20. <div className='shop-wrap'>
  21. <NavBar
  22. className='navbar'
  23. mode="light"
  24. icon={<Icon type="left"/>}
  25. onLeftClick={() => {
  26. this.props.history.go(-2)
  27. }}
  28. >店铺管理</NavBar>
  29. <Query query={gql(shop_by_props)} variables={{limit: 1}}>
  30. {
  31. ({loading, error, data}) => {
  32. if (loading) {
  33. return (
  34. <div className="loading">
  35. <div className="align">
  36. <ActivityIndicator text="Loading..." size="large"/>
  37. </div>
  38. </div>
  39. )
  40. }
  41. if (error) {
  42. return 'error!'
  43. }
  44. let shop, newShop
  45. let shopLength = data.shopbyprops.length
  46. if (shopLength === 0) {
  47. // console.log('尚未个性化 shop');
  48. shop = {}
  49. newShop = true
  50. } else if (shopLength === 1) {
  51. // console.log('存在 shop, update');
  52. shop = data.shopbyprops[0]
  53. newShop = false
  54. } else {
  55. console.log('store 数据库出现错误')
  56. }
  57. let {name, description, address, intro, notice, slideshow, id} = shop
  58. let shopID = newShop ? 'default' : id
  59. return (
  60. <ShopRender
  61. name={name}
  62. description={description}
  63. address={address}
  64. intro={intro}
  65. slideshow={slideshow}
  66. notice={notice}
  67. newShop={newShop}
  68. shopID={shopID}
  69. />
  70. )
  71. }
  72. }
  73. </Query>
  74. </div>
  75. )
  76. }
  77. }
  78. class ShopRender extends Component {
  79. constructor(props) {
  80. super(props)
  81. this.state = {
  82. files: [],
  83. imgDatas: [],
  84. name: props.name,
  85. intro: props.intro,
  86. description: props.description,
  87. address: props.address,
  88. notice: props.notice,
  89. slideshow: props.slideshow,
  90. shopID: props.shopID
  91. }
  92. }
  93. onChange = (files, operationType) => {
  94. console.log("files", files, "operationType", operationType)
  95. let imgDatas = []
  96. let {shopID} = this.state
  97. files.forEach((file, index) => {
  98. let base64Cont = files[index].url.split(',')[1]
  99. let imgType = files[index].file.type.split('/')[1]
  100. let imgNewName = `slideshow_${index}_shopID_${shopID}.${imgType}`
  101. const imgData = {
  102. 'file-name': `e-commerce/images/${imgNewName}`,
  103. 'bucket': 'case',
  104. 'cont': base64Cont,
  105. 'public': true,
  106. 'format': 'base64'
  107. }
  108. imgDatas.push(imgData)
  109. })
  110. this.setState({
  111. imgDatas,
  112. files
  113. })
  114. console.log(imgDatas, 'imgDatas')
  115. }
  116. onReset = () => {
  117. this.setState({
  118. files: [],
  119. imgDatas: [],
  120. name: '',
  121. intro: '',
  122. description: '',
  123. address: '',
  124. notice: '',
  125. slideshow: []
  126. })
  127. }
  128. render() {
  129. let {files, name, intro, description, address, notice, slideshow, imgDatas} = this.state
  130. let {newShop, shopID} = this.props
  131. return (
  132. <div className='shop-wrapper content-wrap'>
  133. <List renderHeader={() => '店铺个性化管理'} className="my-list">
  134. <InputItem onChange={(e) => {
  135. this.setState({name: e})
  136. }} value={name} placeholder="请输入名称">名称</InputItem>
  137. <InputItem onChange={(e) => {
  138. this.setState({intro: e})
  139. }} value={intro} placeholder="请输入简介">简介</InputItem>
  140. <InputItem onChange={(e) => {
  141. this.setState({description: e})
  142. }} value={description} placeholder="请输入描述">描述</InputItem>
  143. <InputItem onChange={(e) => {
  144. this.setState({address: e})
  145. }} value={address} placeholder="请输入地址">地址</InputItem>
  146. <InputItem onChange={(e) => {
  147. this.setState({notice: e})
  148. }} value={notice} placeholder="不输入或留空则不显示">通告</InputItem>
  149. <div className='my-list-subtitle'>首页轮播图</div>
  150. <ImagePicker
  151. files={files}
  152. onChange={this.onChange}
  153. onImageClick={(index, fs) => console.log(index, fs)}
  154. selectable={true}
  155. multiple={true}
  156. />
  157. <Item>
  158. {
  159. newShop ?
  160. <CreateShopButton
  161. shopID={shopID}
  162. imgDatas={imgDatas}
  163. name={name}
  164. description={description}
  165. address={address}
  166. alert={alert}
  167. slideshow={slideshow}
  168. notice={notice}
  169. intro={intro}
  170. />
  171. :
  172. <UpdateShopButton
  173. shopID={shopID}
  174. imgDatas={imgDatas}
  175. name={name}
  176. description={description}
  177. address={address}
  178. alert={alert}
  179. slideshow={slideshow}
  180. notice={notice}
  181. intro={intro}
  182. />
  183. }
  184. <Button size="small" inline style={{marginLeft: '2.5px'}} onClick={this.onReset}>重置</Button>
  185. </Item>
  186. </List>
  187. </div>
  188. )
  189. }
  190. }
  191. class UpdateShopButton extends Component {
  192. constructor(props) {
  193. super(props)
  194. this.state = {}
  195. }
  196. uploadImg = () => {
  197. let {imgDatas} = this.props
  198. return imgDatas.map((imgData) => (
  199. axios({
  200. url: storeFile,
  201. method: 'post',
  202. data: imgData
  203. })
  204. ))
  205. }
  206. render() {
  207. let {name, description, address, intro, notice, imgDatas, shopID} = this.props
  208. return (
  209. <Mutation
  210. mutation={gql(update_shop)}
  211. refetchQueries={[{query: gql(shop_by_props), variables: {}}]}
  212. >
  213. {(updatestore, {loading, error}) => {
  214. if (loading)
  215. return (
  216. <div className="loading">
  217. <div className="align">
  218. <ActivityIndicator text="Loading..." size="large"/>
  219. </div>
  220. </div>
  221. )
  222. if (error)
  223. return 'error'
  224. let varObj = {
  225. id: shopID,
  226. name,
  227. description,
  228. address,
  229. intro,
  230. notice,
  231. updatedAt: moment().format('YYYY-MM-DD HH:mm:ss'),
  232. }
  233. return (
  234. <Button type="primary" size="small" inline onClick={() => {
  235. if (imgDatas.length !== 0) {
  236. Promise.all(this.uploadImg()).then(res => {
  237. let prefix = 'https://case-1254337200.cos.ap-beijing.myqcloud.com/'
  238. let slideshow = imgDatas.length === 1 ? prefix + imgDatas[0]['file-name'] : imgDatas.map((imgData, index) => (
  239. prefix + imgDatas[index]['file-name']
  240. ))
  241. updatestore({variables: {...varObj, slideshow}})
  242. })
  243. } else {
  244. updatestore({variables: varObj})
  245. }
  246. }}>更新</Button>
  247. )
  248. }}
  249. </Mutation>
  250. )
  251. }
  252. }
  253. class CreateShopButton extends Component {
  254. constructor(props) {
  255. super(props)
  256. this.state = {}
  257. }
  258. uploadImg = () => {
  259. let {imgDatas} = this.props
  260. return imgDatas.map((imgData) => (
  261. axios({
  262. url: storeFile,
  263. method: 'post',
  264. data: imgData
  265. })
  266. ))
  267. }
  268. render() {
  269. let {name, description, address, intro, notice, imgDatas, shopID} = this.props
  270. return (
  271. <Mutation
  272. mutation={gql(create_shop)}
  273. refetchQueries={[{query: gql(shop_by_props), variables: {}}]}
  274. >
  275. {(createstore, {loading, error}) => {
  276. if (loading)
  277. return (
  278. <div className="loading">
  279. <div className="align">
  280. <ActivityIndicator text="Loading..." size="large"/>
  281. </div>
  282. </div>
  283. )
  284. if (error)
  285. return 'error'
  286. let varObj = {
  287. id: shopID,
  288. name: name ? name : '',
  289. description: description ? description : '',
  290. address: address ? address : '',
  291. intro: intro ? intro : '',
  292. notice: notice ? notice : '',
  293. createdAt: moment().format('YYYY-MM-DD HH:mm:ss'),
  294. updatedAt: ''
  295. }
  296. return (
  297. <Button type="primary" size="small" inline onClick={() => {
  298. Promise.all(this.uploadImg()).then(res => {
  299. let prefix = 'https://case-1254337200.cos.ap-beijing.myqcloud.com/'
  300. let slideshow = imgDatas.length === 1 ? prefix + imgDatas[0]['file-name'] : imgDatas.map((imgData, index) => (
  301. prefix + imgDatas[index]['file-name']
  302. ))
  303. createstore({variables: {...varObj, slideshow}})
  304. })
  305. }}>创建</Button>
  306. )
  307. }}
  308. </Mutation>
  309. )
  310. }
  311. }
  312. export default withRouter(Shop)