choose_actions.tsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import { View, Image } from '@tarojs/components'
  2. import './choose_actions.scss'
  3. import { IconClose } from '@/components/basic/Icons'
  4. import { rpxToPx } from '@/utils/tools'
  5. import NewButton, { NewButtonType } from '@/_health/base/new_button'
  6. import Taro from '@tarojs/taro'
  7. import { BASE_IMG_URL, baseUrl } from '@/services/http/api'
  8. import { useTranslation } from 'react-i18next'
  9. import { jumpPage } from '@/features/trackTimeDuration/hooks/Common'
  10. import { MainColorType } from '@/context/themes/color'
  11. import { useEffect } from 'node_modules/@types/react'
  12. export default function ChooseActions(props: { close: any, quick: any, chooseText: any, chooseImg: any }) {
  13. const systemInfo: any = Taro.getWindowInfo ? Taro.getWindowInfo() : Taro.getSystemInfoSync();
  14. const navigationBarHeight = systemInfo.statusBarHeight + 44;
  15. const { t } = useTranslation()
  16. function camera() {
  17. addImage(true)
  18. }
  19. function album() {
  20. addImage(false)
  21. }
  22. function chat() {
  23. Taro.chooseMessageFile({
  24. count: 9,
  25. type: 'image',
  26. success: async function (res) {
  27. const results = await Promise.all(res.tempFiles.map(getImageInfo));
  28. chooseSuccess(results, true)
  29. },
  30. fail(res) {
  31. },
  32. })
  33. }
  34. function text() {
  35. props.close()
  36. props.chooseText()
  37. // jumpPage(`/_record/pages/log_record?scenario=${props.scenario}&only_text=1`)
  38. }
  39. function checkin() {
  40. props.close()
  41. props.quick()
  42. // jumpPage(`/_record/pages/log_record?scenario=${props.scenario}&check_in=1`)
  43. }
  44. function addImage(isCamera) {
  45. var source: any = isCamera ? ['camera'] : ['album']
  46. console.log(source)
  47. Taro.chooseMedia({
  48. count: 9,
  49. sizeType: ['compressed'],
  50. mediaType: ['image'],
  51. sourceType: source,
  52. success: async function (res) {
  53. const results = await Promise.all(res.tempFiles.map(getImageInfo));
  54. chooseSuccess(results, true)
  55. },
  56. fail: function (res) {
  57. }
  58. })
  59. }
  60. async function chooseSuccess(res, isAlbum) {
  61. console.log('选择图片', res)
  62. // const filePaths = res.map(file => file.path
  63. // // process.env.TARO_ENV === 'rn' || isFilePath ? file.path : file.tempFilePath
  64. // )
  65. Taro.showLoading({
  66. title: t('health.uploading')
  67. })
  68. try {
  69. const uploadedUrls = await Promise.all(res.map(path => uploadFile2(path, isAlbum ? 'album' : 'camera')))
  70. // setPics([...pics, ...uploadedUrls])
  71. // jumpPage(`/_record/pages/log_record?imgs=${JSON.stringify(uploadedUrls)}&scenario=${props.scenario}`)
  72. props.chooseImg(uploadedUrls)
  73. props.close()
  74. Taro.hideLoading()
  75. } catch (error) {
  76. console.error('Error uploading files:', error)
  77. Taro.hideLoading()
  78. }
  79. }
  80. function uploadFile2(obj: any, source: string): Promise<string> {
  81. return new Promise((resolve, reject) => {
  82. var path = obj.path
  83. const dot = path.lastIndexOf('.')
  84. const fileExt = dot > 0 ? path.substring(dot) : ''
  85. Taro.request({
  86. method: 'GET',
  87. url: `${baseUrl}/api/thirdparty/aliyun/oss-form-upload`,
  88. header: {
  89. 'Authorization': 'bearer ' + global.token
  90. },
  91. data: {
  92. type: 'FOOD_JOURNAL',
  93. file_ext: fileExt
  94. },
  95. success: (rsp) => {
  96. if (rsp.statusCode !== 200) {
  97. reject(new Error(t('health.networkError')))
  98. return
  99. }
  100. Taro.uploadFile({
  101. url: rsp.data.upload_url,
  102. filePath: path,
  103. name: 'file',
  104. formData: rsp.data.fields,
  105. success: () => {
  106. var temp = JSON.parse(JSON.stringify(obj))
  107. temp.url = rsp.data.view_url
  108. // resolve(rsp.data.view_url)
  109. resolve(temp)
  110. },
  111. fail: (error) => {
  112. reject(error)
  113. }
  114. })
  115. },
  116. fail: reject
  117. })
  118. })
  119. }
  120. const getImageInfo = (src) => {
  121. const { tempFilePath, path } = src
  122. return new Promise((resolve) => {
  123. Taro.getImageInfo({
  124. src: tempFilePath ? tempFilePath : path,
  125. success: (result) => resolve({
  126. height: result.height,
  127. width: result.width,
  128. orientation: result.orientation,
  129. path: result.path,
  130. type: result.type
  131. }),
  132. fail: (error) => resolve({
  133. height: 1024,
  134. width: 1024,
  135. orientation: 'up',
  136. path: tempFilePath,
  137. type: 'unknown'
  138. }),
  139. });
  140. });
  141. };
  142. return <View className="actions_bg" catchMove onClick={props.close}>
  143. <View className="navi_bar" style={{
  144. position: 'fixed',
  145. left: 0,
  146. right: 0,
  147. top: 0, height: navigationBarHeight, zIndex: 1000
  148. }}>
  149. <View style={{
  150. position: 'absolute',
  151. left: 0,
  152. right: 0,
  153. bottom: 0,
  154. height: 44,
  155. display: 'flex',
  156. alignItems: 'center',
  157. justifyContent: 'center'
  158. }}>
  159. <View style={{
  160. position: 'absolute',
  161. width: rpxToPx(92),
  162. height: rpxToPx(64),
  163. left: 22,
  164. top: 22 - rpxToPx(32)
  165. }}
  166. onClick={() => {
  167. Taro.navigateBack()
  168. }}>
  169. <IconClose color="#fff" width={rpxToPx(64)} height={rpxToPx(64)} />
  170. </View>
  171. </View>
  172. </View>
  173. <NewButton type={NewButtonType.img} onClick={album}>
  174. <View className='action_btn h30 bold cardShowAni'>{t('health.add_photos')}
  175. <Image className='action_btn_img' src={BASE_IMG_URL + 'image.svg'} />
  176. </View>
  177. </NewButton>
  178. <NewButton type={NewButtonType.img} onClick={text}>
  179. <View className='action_btn h30 bold cardShowAni'>{t('health.add_text')}
  180. <Image className='action_btn_img' src={BASE_IMG_URL + 'text.svg'} />
  181. </View>
  182. </NewButton>
  183. <NewButton type={NewButtonType.img} onClick={camera}>
  184. <View className='action_btn h30 bold cardShowAni'>{t('health.camera2')}
  185. <Image className='action_btn_img' src={BASE_IMG_URL + 'camera.svg'} />
  186. </View>
  187. </NewButton>
  188. <NewButton type={NewButtonType.img} onClick={chat}>
  189. <View className='action_btn h30 bold cardShowAni'>{t('health.import_chat')}
  190. <Image className='action_btn_img' src={BASE_IMG_URL + 'wechat.svg'} />
  191. </View>
  192. </NewButton>
  193. <View style={{height:rpxToPx(120)}}/>
  194. {/* <View className='cardShowAni' style={{ display: 'flex', flexDirection: 'row', alignItems: 'center', height: rpxToPx(64), marginTop: rpxToPx(26) }}>
  195. <View className='or_line'/>
  196. <View className='white h30 bold' style={{marginLeft:rpxToPx(24),marginRight:rpxToPx(24)}}>OR</View>
  197. <View className='or_line'/>
  198. </View>
  199. <NewButton type={NewButtonType.img} onClick={checkin}>
  200. <View className='action_btn h30 bold cardShowAni'>Quick Check in</View>
  201. </NewButton> */}
  202. {/* <View className='btnAni' style={{ marginBottom: rpxToPx(84), marginTop: rpxToPx(60) }} onClick={props.close}>
  203. <IconClose color="#fff" width={rpxToPx(64)} height={rpxToPx(64)} />
  204. </View> */}
  205. </View>
  206. }