upload.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. // pages/upload/upload.js
  2. const app = getApp()
  3. const $api = require('../../utils/api.js').API;
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. open:true,
  10. stock_date:'',
  11. stock:[{name:'',fund:''}],
  12. stockList:[],
  13. today_stock_img:[],
  14. value:[-1],
  15. is_markt:0,
  16. today_fund:'',
  17. change:0,
  18. experience:'',
  19. cur:0,
  20. freshen:true,
  21. disable:false,
  22. id:0,
  23. images:[],
  24. },
  25. /**
  26. * 生命周期函数--监听页面加载
  27. */
  28. onShow(){
  29. if (!this.data.freshen) {
  30. return;
  31. }
  32. this.setData({
  33. disable: false
  34. })
  35. $api.initUser()
  36. wx.getStorage({
  37. key: 'user',
  38. complete: res => {
  39. this.setData({
  40. role: res.data.role,
  41. })
  42. }
  43. })
  44. $api.getCurrecord().then(res=>{
  45. if (!res.data.data.id){return}
  46. let stock = res.data.data.today_stock
  47. this.setData({
  48. stock: stock.length > 0 ? stock : [{ name: '', fund: '' }],
  49. today_stock_img: res.data.data.today_stock_img,
  50. today_fund: res.data.data.today_fund,
  51. is_markt: res.data.data.is_markt,
  52. change: 1,
  53. experience: res.data.data.experience,
  54. id:res.data.data.id
  55. })
  56. })
  57. },
  58. onLoad: function (options) {
  59. var y=new Date().getFullYear()
  60. var m=Number(new Date().getMonth() + 1)
  61. var d = new Date().getDate()
  62. m=m>9?m:'0'+m;
  63. d=d>9?d:'0'+d
  64. this.setData({
  65. stock_date:y+'-'+m+'-'+d
  66. })
  67. },
  68. /**添加持股 */
  69. add(){
  70. var stock = this.data.stock;
  71. var value=this.data.value;
  72. stock.push({ name: '', fund: '' })
  73. value.push(-1)
  74. this.setData({
  75. stock: stock,
  76. value: value
  77. })
  78. },
  79. del(e){
  80. var i = e.target.dataset.id;
  81. var stock = this.data.stock;
  82. var value = this.data.value;
  83. stock.splice(i,1)
  84. value.splice(i,1)
  85. this.setData({
  86. stock: stock,
  87. value: value
  88. })
  89. },
  90. delToday(e){
  91. this.setData({
  92. today_fund: '',
  93. })
  94. },
  95. bindPickerChange(e){
  96. var i=this.data.cur;
  97. var v = e.target.dataset.name
  98. var stock=this.data.stock;
  99. stock[i].name = v
  100. stock[i].code = e.target.dataset.id
  101. this.setData({
  102. stock: stock
  103. })
  104. setTimeout(()=>{
  105. this.setData({
  106. stockList: []
  107. },50)
  108. })
  109. return;
  110. },
  111. stockSearch(e){
  112. if (e.detail.value == ''){
  113. this.setData({
  114. stockList: []
  115. })
  116. return
  117. }
  118. var i = e.target.dataset.id;
  119. var v = e.detail.value
  120. var stock = this.data.stock;
  121. stock[i].name = v;
  122. this.setData({
  123. stock: stock
  124. })
  125. $api.searchStock({ name: e.detail.value}).then(res=>{
  126. this.setData({
  127. stockList:res.data.data,
  128. cur:e.target.dataset.id
  129. })
  130. })
  131. },
  132. blur(){
  133. // console.log(233)
  134. this.setData({
  135. stockList: []
  136. })
  137. },
  138. nameChange(e){
  139. console.log(e)
  140. var i = e.target.dataset.id;
  141. var v = e.detail.value
  142. var stock = this.data.stock;
  143. stock[i].name = v;
  144. this.setData({
  145. stock: stock
  146. })
  147. },
  148. moneyChange(e){
  149. var i = e.target.dataset.id;
  150. var v = e.detail.value
  151. var stock = this.data.stock;
  152. if (v.split('.').length > 1 && v.split('.')[1].length > 4) {
  153. v = Math.floor(Number(v) * 10000) / 10000
  154. }
  155. stock[i].money = v;
  156. this.setData({
  157. stock: stock
  158. })
  159. },
  160. fundChange(e){
  161. var i = e.target.dataset.id;
  162. var v = e.detail.value
  163. var stock = this.data.stock;
  164. if (v.split('.').length > 1 && v.split('.')[1].length > 4) {
  165. v = Math.floor(Number(v) * 10000) / 10000
  166. }
  167. stock[i].fund = v;
  168. this.setData({
  169. stock: stock
  170. })
  171. },
  172. fundChange1(e) {
  173. var v = e.detail.value
  174. if (v.split('.').length > 1 && v.split('.')[1].length > 4){
  175. v = Math.floor(Number(v)*10000)/10000
  176. }
  177. this.setData({
  178. today_fund: v
  179. })
  180. },
  181. expChange(e){
  182. var v = e.detail.value
  183. this.setData({
  184. experience: v
  185. })
  186. },
  187. switch1Change(e){
  188. this.setData({
  189. is_markt: Number(e.detail.value)
  190. })
  191. },
  192. uploadImg() {
  193. var today_stock_img = this.data.today_stock_img;
  194. wx.chooseImage({
  195. success: res => {
  196. wx.showLoading({
  197. title: '正在上传',
  198. })
  199. const tempFilePaths = res.tempFiles
  200. for (let i = 0; i < tempFilePaths.length; i++) {
  201. $api.upload(tempFilePaths[i].path, 'file').then(res => {
  202. wx.hideLoading()
  203. if (res.code == 0) {
  204. console.log(res.data)
  205. today_stock_img.push(res.data.url)
  206. this.setData({
  207. today_stock_img: today_stock_img
  208. })
  209. console.log(this.data.today_stock_img,22222222222)
  210. if (i==0 && today_stock_img.length == 1){
  211. this.loadOcr(tempFilePaths[i].path,res.data.url)
  212. }
  213. } else {
  214. wx.showToast({
  215. title: '上传失败',
  216. })
  217. }
  218. })
  219. .catch(err => {
  220. wx.hideLoading()
  221. })
  222. }
  223. }
  224. })
  225. },
  226. loadOcr(path,url){
  227. var _this = this
  228. wx.getImageInfo({
  229. src:path,
  230. success:function(ires){
  231. _this.loadImageOcr(url,ires.width < ires.height ? 'phone':'pc')
  232. }
  233. })
  234. },
  235. loadImageOcr(img_url,source){
  236. var _this = this
  237. var param = {
  238. img_url:img_url.split('?')[0],
  239. source:source,
  240. }
  241. console.log('img_url',param['img_url'])
  242. console.log('source',param['source'])
  243. $api.imageOcr(param).then(res => {
  244. console.log('ocr',res)
  245. if(res.data.code == 0){
  246. _this.setData({
  247. today_fund:res.data.data.today,
  248. stock:res.data.data.list
  249. })
  250. }
  251. })
  252. },
  253. startOcr(e){
  254. var index = e.target.dataset.index;
  255. var img_url = this.data.today_stock_img[index]
  256. console.log('startocr',img_url)
  257. this.loadImageOcr(img_url,this.data.images[index].width < this.data.images[index].height ? 'phone':'pc')
  258. },
  259. imageLoad: function(e) {
  260. var width=e.detail.width; //获取图片真实宽度
  261. var height=e.detail.height;
  262. var image=this.data.images;
  263. //将图片的datadata-index作为image对象的key,然后存储图片的宽高值
  264. image[e.target.dataset.index]={
  265. width:width,
  266. height:height,
  267. }
  268. this.setData({
  269. images:image
  270. })
  271. },
  272. del1(e) {
  273. var i = e.target.dataset.id;
  274. var today_stock_img = this.data.today_stock_img;
  275. today_stock_img.splice(i, 1)
  276. this.setData({
  277. today_stock_img: today_stock_img
  278. })
  279. },
  280. prview(e){
  281. var src = e.target.dataset.src;
  282. wx.previewImage({
  283. current: src, // 当前显示图片的http链接
  284. urls: [src] // 需要预览的图片http链接列表
  285. })
  286. },
  287. post(){
  288. var stocks = []
  289. var slist = this.data.stock
  290. for(let i=0;i<slist.length;i++){
  291. if(slist[i].name.length <= 0){
  292. continue;
  293. }
  294. if(slist[i].fund > 5000){
  295. wx.showToast({
  296. icon: 'none',
  297. title: '持仓金额单位为万'
  298. })
  299. return;
  300. }
  301. stocks.push(slist[i])
  302. }
  303. var parm = {
  304. stock_date: this.data.stock_date,
  305. today_stock: stocks,
  306. is_markt: this.data.is_markt,
  307. today_stock_img: this.data.today_stock_img,
  308. today_fund: Number(this.data.today_fund),
  309. id:this.data.id,
  310. experience: this.data.experience
  311. }
  312. if (!parm.today_fund) {
  313. wx.showToast({
  314. icon: 'none',
  315. title: '请输入今日净资产'
  316. })
  317. return
  318. }
  319. if (parm.today_fund >= 10000) {
  320. wx.showToast({
  321. icon: 'none',
  322. title: '今日净资产单位为万'
  323. })
  324. return
  325. }
  326. // if (!parm.is_markt) {
  327. // // for (let i = 0; i < parm.today_stock.length;i++){
  328. // // if (!parm.today_stock[i].name || !parm.today_stock[i].fund) {
  329. // // wx.showToast({
  330. // // icon: 'none',
  331. // // title: '请选择股票并输入资金额'
  332. // // })
  333. // // return;
  334. // // break;
  335. // // }
  336. // // }
  337. // } else {
  338. // parm.today_stock = []
  339. // }
  340. console.log(parm)
  341. if (parm.today_stock_img.length <= 0) {
  342. wx.showToast({
  343. icon: 'none',
  344. title: '请上传截图'
  345. })
  346. return
  347. }
  348. wx.showLoading({
  349. title: '正在提交',
  350. })
  351. this.setData({
  352. disable: true
  353. })
  354. $api.updateRecord(parm).then(res => {
  355. wx.hideLoading()
  356. wx.showToast({
  357. title: '提交成功',
  358. })
  359. this.setData({
  360. freshen: true,
  361. disable: false
  362. })
  363. setTimeout(() => {
  364. this.setData({
  365. stock: [{ label: '', name: '', code: '', fund: '' }],
  366. stockList: [],
  367. today_stock_img: [],
  368. value: [-1],
  369. is_markt: 0,
  370. today_fund: ''
  371. })
  372. wx.switchTab({
  373. url: '../user/user',
  374. })
  375. }, 200)
  376. })
  377. .catch(
  378. this.setData({
  379. disable:false
  380. })
  381. )
  382. },
  383. /**
  384. * 生命周期函数--监听页面初次渲染完成
  385. */
  386. onReady: function () {
  387. },
  388. /**
  389. * 生命周期函数--监听页面隐藏
  390. */
  391. onHide: function () {
  392. },
  393. /**
  394. * 生命周期函数--监听页面卸载
  395. */
  396. onUnload: function () {
  397. },
  398. /**
  399. * 页面相关事件处理函数--监听用户下拉动作
  400. */
  401. onPullDownRefresh: function () {
  402. },
  403. /**
  404. * 页面上拉触底事件的处理函数
  405. */
  406. onReachBottom: function () {
  407. },
  408. /**
  409. * 用户点击右上角分享
  410. */
  411. onShareAppMessage: function () {
  412. }
  413. })