upload.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. // pages/upload/upload.js
  2. const app = getApp()
  3. const $api = require('../../utils/api.js').API;
  4. const baseURL = 'https://api.hunanwanzhu.com';
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. open:true,
  11. stock_date:'',
  12. stock:[{name:'',fund:''}],
  13. stockList:[],
  14. today_stock_img:[],
  15. value:[-1],
  16. is_markt:0,
  17. today_fund:'',
  18. change:0,
  19. experience:'',
  20. cur:0,
  21. freshen:true,
  22. disable:false,
  23. id:0,
  24. images:[],
  25. lastdata:{},
  26. },
  27. /**
  28. * 生命周期函数--监听页面加载
  29. */
  30. onShow(){
  31. if (!this.data.freshen) {
  32. return;
  33. }
  34. this.setData({
  35. disable: false
  36. })
  37. $api.initUser()
  38. wx.getStorage({
  39. key: 'user',
  40. complete: res => {
  41. this.setData({
  42. role: res.data.role,
  43. })
  44. }
  45. })
  46. $api.getCurrecord().then(res=>{
  47. if (!res.data.data.id){return}
  48. let stock = res.data.data.today_stock
  49. this.setData({
  50. stock: stock.length > 0 ? stock : [{ name: '', fund: '' }],
  51. today_stock_img: res.data.data.today_stock_img,
  52. today_fund: res.data.data.today_fund,
  53. is_markt: res.data.data.is_markt,
  54. change: 1,
  55. experience: res.data.data.experience,
  56. id:res.data.data.id
  57. })
  58. })
  59. $api.getMineLatest().then(res=>{
  60. this.setData({
  61. lastdata: res.data.data,
  62. })
  63. })
  64. },
  65. onLoad: function (options) {
  66. var y=new Date().getFullYear()
  67. var m=Number(new Date().getMonth() + 1)
  68. var d = new Date().getDate()
  69. m=m>9?m:'0'+m;
  70. d=d>9?d:'0'+d
  71. this.setData({
  72. stock_date:y+'-'+m+'-'+d
  73. })
  74. },
  75. /**添加持股 */
  76. add(){
  77. var stock = this.data.stock;
  78. var value=this.data.value;
  79. stock.push({ name: '', fund: '' })
  80. value.push(-1)
  81. this.setData({
  82. stock: stock,
  83. value: value
  84. })
  85. },
  86. del(e){
  87. var i = e.target.dataset.id;
  88. var stock = this.data.stock;
  89. var value = this.data.value;
  90. stock.splice(i,1)
  91. value.splice(i,1)
  92. this.setData({
  93. stock: stock,
  94. value: value
  95. })
  96. },
  97. delToday(e){
  98. this.setData({
  99. today_fund: '',
  100. })
  101. },
  102. bindPickerChange(e){
  103. var i=this.data.cur;
  104. var v = e.target.dataset.name
  105. var stock=this.data.stock;
  106. stock[i].name = v
  107. stock[i].code = e.target.dataset.id
  108. this.setData({
  109. stock: stock
  110. })
  111. setTimeout(()=>{
  112. this.setData({
  113. stockList: []
  114. },50)
  115. })
  116. return;
  117. },
  118. stockSearch(e){
  119. if (e.detail.value == ''){
  120. this.setData({
  121. stockList: []
  122. })
  123. return
  124. }
  125. var i = e.target.dataset.id;
  126. var v = e.detail.value
  127. var stock = this.data.stock;
  128. stock[i].name = v;
  129. this.setData({
  130. stock: stock
  131. })
  132. $api.searchStock({ name: e.detail.value}).then(res=>{
  133. this.setData({
  134. stockList:res.data.data,
  135. cur:e.target.dataset.id
  136. })
  137. })
  138. },
  139. blur(){
  140. // console.log(233)
  141. this.setData({
  142. stockList: []
  143. })
  144. },
  145. nameChange(e){
  146. console.log(e)
  147. var i = e.target.dataset.id;
  148. var v = e.detail.value
  149. var stock = this.data.stock;
  150. stock[i].name = v;
  151. this.setData({
  152. stock: stock
  153. })
  154. },
  155. moneyChange(e){
  156. var i = e.target.dataset.id;
  157. var v = e.detail.value
  158. var stock = this.data.stock;
  159. if (v.split('.').length > 1 && v.split('.')[1].length > 4) {
  160. v = Math.floor(Number(v) * 10000) / 10000
  161. }
  162. stock[i].money = v;
  163. this.setData({
  164. stock: stock
  165. })
  166. },
  167. fundChange(e){
  168. var i = e.target.dataset.id;
  169. var v = e.detail.value
  170. var stock = this.data.stock;
  171. if (v.split('.').length > 1 && v.split('.')[1].length > 4) {
  172. v = Math.floor(Number(v) * 10000) / 10000
  173. }
  174. stock[i].fund = v;
  175. this.setData({
  176. stock: stock
  177. })
  178. },
  179. fundChange1(e) {
  180. var v = e.detail.value
  181. if (v.split('.').length > 1 && v.split('.')[1].length > 4){
  182. v = Math.floor(Number(v)*10000)/10000
  183. }
  184. this.setData({
  185. today_fund: v
  186. })
  187. },
  188. expChange(e){
  189. var v = e.detail.value
  190. this.setData({
  191. experience: v
  192. })
  193. },
  194. switch1Change(e){
  195. this.setData({
  196. is_markt: Number(e.detail.value)
  197. })
  198. },
  199. // oss验签上传图片
  200. upload() {
  201. const that = this
  202. var today_stock_img = that.data.today_stock_img;
  203. wx.chooseMedia({
  204. mediaType: ['image'], // 只能上传图片 还有个属性是可以上传视频
  205. success(res) {
  206. wx.showLoading({
  207. title: '上传中',
  208. mask: true
  209. })
  210. let imgs = []
  211. let valueArr = []
  212. // 需要oss 验签 此处是调取验签的接口
  213. $api.getOssSign(res).then((r) => {
  214. wx.hideLoading()
  215. const arr = res.tempFiles
  216. // arr 是为了获取上传图片反的数组
  217. arr.forEach((e) => {
  218. return imgs.push(e.tempFilePath)
  219. })
  220. for (var i = 0; i < imgs.length; i++) {
  221. let only = imgs[i].split('.')
  222. valueArr.push(
  223. only[only.length - 1]
  224. )
  225. let filename = parseInt(Math.random() * 1000000 + new Date().getTime()) + '.' + valueArr[valueArr.length - 1]
  226. let httpData = {
  227. Key: r.data.dir + filename,
  228. OSSAccessKeyId: r.data.AccessKeyID,
  229. Policy: r.data.policy_encode,
  230. Signature: r.data.signature,
  231. }
  232. wx.uploadFile({
  233. url: baseURL+'/api/wx/uploadfile',
  234. filePath: imgs[i],
  235. formData: httpData,
  236. fileType: 'image',
  237. name: 'file',
  238. timeout: 15000,
  239. header: {
  240. 'Content-Type': 'application/x-www-form-urlencoded',
  241. },
  242. success: (res) => {
  243. if (res.statusCode == 200) {
  244. var data=JSON.parse(res.data)
  245. today_stock_img.push(data.data.url)
  246. console.log(today_stock_img)
  247. that.setData({
  248. today_stock_img: today_stock_img
  249. })
  250. that.loadOcr(imgs[0],today_stock_img[0])
  251. } else {
  252. // 返回错误信息
  253. console.log('error')
  254. }
  255. },
  256. fail: function (res) {
  257. console.log(res);
  258. }
  259. })
  260. }
  261. })
  262. }
  263. })
  264. },
  265. loadOcr(path,url){
  266. var _this = this
  267. console.log(path,url)
  268. wx.getImageInfo({
  269. src:path,
  270. success:function(ires){
  271. _this.loadImageOcr(url,ires.width < ires.height ? 'phone':'pc')
  272. }
  273. })
  274. },
  275. loadImageOcr(img_url,source){
  276. var _this = this
  277. var param = {
  278. img_url:img_url.split('?')[0],
  279. source:source,
  280. }
  281. console.log('img_url',param['img_url'])
  282. console.log('source',param['source'])
  283. $api.imageOcr(param).then(res => {
  284. console.log('ocr',res)
  285. if(res.data.code == 0){
  286. _this.setData({
  287. today_fund:res.data.data.today,
  288. stock:res.data.data.list
  289. })
  290. }
  291. })
  292. },
  293. startOcr(e){
  294. var index = e.target.dataset.index;
  295. var img_url = this.data.today_stock_img[index]
  296. console.log('startocr',img_url)
  297. this.loadImageOcr(img_url,this.data.images[index].width < this.data.images[index].height ? 'phone':'pc')
  298. },
  299. imageLoad: function(e) {
  300. var width=e.detail.width; //获取图片真实宽度
  301. var height=e.detail.height;
  302. var image=this.data.images;
  303. //将图片的datadata-index作为image对象的key,然后存储图片的宽高值
  304. image[e.target.dataset.index]={
  305. width:width,
  306. height:height,
  307. }
  308. this.setData({
  309. images:image
  310. })
  311. },
  312. del1(e) {
  313. var i = e.target.dataset.id;
  314. var today_stock_img = this.data.today_stock_img;
  315. today_stock_img.splice(i, 1)
  316. this.setData({
  317. today_stock_img: today_stock_img
  318. })
  319. },
  320. prview(e){
  321. var src = e.target.dataset.src;
  322. wx.previewImage({
  323. current: src, // 当前显示图片的http链接
  324. urls: [src] // 需要预览的图片http链接列表
  325. })
  326. },
  327. post(){
  328. var stocks = []
  329. var slist = this.data.stock
  330. for(let i=0;i<slist.length;i++){
  331. if(slist[i].name.length <= 0){
  332. continue;
  333. }
  334. if(slist[i].fund > 10000){
  335. wx.showToast({
  336. icon: 'none',
  337. title: '持仓金额超过1亿'
  338. })
  339. return;
  340. }
  341. stocks.push(slist[i])
  342. }
  343. var parm = {
  344. stock_date: this.data.stock_date,
  345. today_stock: stocks,
  346. is_markt: this.data.is_markt,
  347. today_stock_img: this.data.today_stock_img,
  348. today_fund: Number(this.data.today_fund),
  349. id:this.data.id,
  350. experience: this.data.experience
  351. }
  352. if (!parm.today_fund) {
  353. wx.showToast({
  354. icon: 'none',
  355. title: '请输入今日净资产'
  356. })
  357. return
  358. }
  359. if (parm.today_fund >= 10000) {
  360. wx.showToast({
  361. icon: 'none',
  362. title: '今日净资产超过1亿'
  363. })
  364. return
  365. }
  366. // if (!parm.is_markt) {
  367. // // for (let i = 0; i < parm.today_stock.length;i++){
  368. // // if (!parm.today_stock[i].name || !parm.today_stock[i].fund) {
  369. // // wx.showToast({
  370. // // icon: 'none',
  371. // // title: '请选择股票并输入资金额'
  372. // // })
  373. // // return;
  374. // // break;
  375. // // }
  376. // // }
  377. // } else {
  378. // parm.today_stock = []
  379. // }
  380. console.log(parm)
  381. if (parm.today_stock_img.length <= 0) {
  382. wx.showToast({
  383. icon: 'none',
  384. title: '请上传截图'
  385. })
  386. return
  387. }
  388. if (this.data.lastdata && !this.data.id){
  389. var lastfund = this.data.lastdata.today_fund;
  390. if((this.data.today_fund - lastfund)/lastfund > 0.15){
  391. const that = this
  392. wx.showActionSheet({
  393. alertText:'今日收益超过15%',
  394. itemList: ['缺席期无出入金-收益正常','缺席期有出入金-忘了申报'],
  395. success(res){
  396. if(res.tapIndex == 0){
  397. that.uploadData(parm)
  398. } else if(res.tapIndex ==1){
  399. wx.navigateTo({
  400. url: '../change/change',
  401. })
  402. }
  403. }
  404. })
  405. return
  406. }
  407. }
  408. this.uploadData(parm)
  409. },
  410. uploadData(param){
  411. wx.showLoading({
  412. title: '正在提交',
  413. })
  414. this.setData({
  415. disable: true
  416. })
  417. $api.updateRecord(param).then(res => {
  418. wx.hideLoading()
  419. wx.showToast({
  420. title: '提交成功',
  421. })
  422. this.setData({
  423. freshen: true,
  424. disable: false
  425. })
  426. setTimeout(() => {
  427. this.setData({
  428. stock: [{ label: '', name: '', code: '', fund: '' }],
  429. stockList: [],
  430. today_stock_img: [],
  431. value: [-1],
  432. is_markt: 0,
  433. today_fund: ''
  434. })
  435. wx.switchTab({
  436. url: '../user/user',
  437. })
  438. }, 200)
  439. })
  440. .catch(
  441. this.setData({
  442. disable:false
  443. })
  444. )
  445. },
  446. /**
  447. * 生命周期函数--监听页面初次渲染完成
  448. */
  449. onReady: function () {
  450. },
  451. /**
  452. * 生命周期函数--监听页面隐藏
  453. */
  454. onHide: function () {
  455. },
  456. /**
  457. * 生命周期函数--监听页面卸载
  458. */
  459. onUnload: function () {
  460. },
  461. /**
  462. * 页面相关事件处理函数--监听用户下拉动作
  463. */
  464. onPullDownRefresh: function () {
  465. },
  466. /**
  467. * 页面上拉触底事件的处理函数
  468. */
  469. onReachBottom: function () {
  470. },
  471. /**
  472. * 用户点击右上角分享
  473. */
  474. onShareAppMessage: function () {
  475. }
  476. })