upload.js 9.9 KB

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